xref: /petsc/src/ts/interface/ts.c (revision f6a906c0d6af3dfdfb0ee75341b51da0ba344bfc)
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 
144a2ae208SSatish Balay #undef __FUNCT__
15e55864a3SBarry Smith #define __FUNCT__ "TSSetTypeFromOptions_Private"
16bdad233fSMatthew Knepley /*
17bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
18bdad233fSMatthew Knepley 
19bdad233fSMatthew Knepley   Collective on TS
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley   Input Parameter:
22bdad233fSMatthew Knepley . ts - The ts
23bdad233fSMatthew Knepley 
24bdad233fSMatthew Knepley   Level: intermediate
25bdad233fSMatthew Knepley 
26bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
27bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
28bdad233fSMatthew Knepley */
298c34d3f5SBarry Smith static PetscErrorCode TSSetTypeFromOptions_Private(PetscOptions *PetscOptionsObject,TS ts)
30bdad233fSMatthew Knepley {
31ace3abfcSBarry Smith   PetscBool      opt;
322fc52814SBarry Smith   const char     *defaultType;
33bdad233fSMatthew Knepley   char           typeName[256];
34dfbe8321SBarry Smith   PetscErrorCode ierr;
35bdad233fSMatthew Knepley 
36bdad233fSMatthew Knepley   PetscFunctionBegin;
37bbd56ea5SKarl Rupp   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
38bbd56ea5SKarl Rupp   else defaultType = TSEULER;
39bdad233fSMatthew Knepley 
40607a6623SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll();CHKERRQ(ierr);}
41a264d7a6SBarry Smith   ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
42a7cc72afSBarry Smith   if (opt) {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   } else {
45bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
46bdad233fSMatthew Knepley   }
47bdad233fSMatthew Knepley   PetscFunctionReturn(0);
48bdad233fSMatthew Knepley }
49bdad233fSMatthew Knepley 
502d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
512d5ee99bSBarry Smith   PetscViewer   viewer;
524b363babSBarry Smith   PetscDrawAxis axis;
532d5ee99bSBarry Smith   Vec           initialsolution;
542d5ee99bSBarry Smith   PetscBool     showinitial;
552d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
562d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
572d5ee99bSBarry Smith   int           color;
582d5ee99bSBarry Smith };
592d5ee99bSBarry Smith 
60bdad233fSMatthew Knepley #undef __FUNCT__
61bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
62bdad233fSMatthew Knepley /*@
63bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
64bdad233fSMatthew Knepley 
65bdad233fSMatthew Knepley    Collective on TS
66bdad233fSMatthew Knepley 
67bdad233fSMatthew Knepley    Input Parameter:
68bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
69bdad233fSMatthew Knepley 
70bdad233fSMatthew Knepley    Options Database Keys:
714d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
72d2daff3dSHong Zhang .  -ts_checkpoint - checkpoint for adjoint sensitivity analysis
73bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
743bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
75bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
76a3cdaa26SBarry 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
77a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
78a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
79a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
80a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
81a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
82bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
83de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
84de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
85de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
86de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
87de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
88de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
89de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
902d5ee99bSBarry Smith .  -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram
91de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
9291b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
9391b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
9491b97e58SBarry Smith 
9591b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
96bdad233fSMatthew Knepley 
97bdad233fSMatthew Knepley    Level: beginner
98bdad233fSMatthew Knepley 
99bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
100bdad233fSMatthew Knepley 
101a313700dSBarry Smith .seealso: TSGetType()
102bdad233fSMatthew Knepley @*/
1037087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
104bdad233fSMatthew Knepley {
105ace3abfcSBarry Smith   PetscBool              opt,flg;
106dfbe8321SBarry Smith   PetscErrorCode         ierr;
107649052a6SBarry Smith   PetscViewer            monviewer;
108eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
109089b2837SJed Brown   SNES                   snes;
1101c3436cfSJed Brown   TSAdapt                adapt;
11131748224SBarry Smith   PetscReal              time_step;
11249354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
113d1212d36SBarry Smith   char                   dir[16];
114bdad233fSMatthew Knepley 
115bdad233fSMatthew Knepley   PetscFunctionBegin;
1160700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1173194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
118a43b19c4SJed Brown   /* Handle TS type options */
119e55864a3SBarry Smith   ierr = TSSetTypeFromOptions_Private(PetscOptionsObject,ts);CHKERRQ(ierr);
120bdad233fSMatthew Knepley 
121bdad233fSMatthew Knepley   /* Handle generic TS options */
122d2daff3dSHong Zhang   ierr = PetscOptionsBool("-ts_checkpoint","Checkpoint for adjoint sensitivity analysis","TSSetCheckpoint",ts->checkpoint,&ts->checkpoint,NULL);CHKERRQ(ierr);
1230298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1240298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1250298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
12631748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
12731748224SBarry Smith   if (flg) {
12831748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
12931748224SBarry Smith   }
13049354f04SShri 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);
13149354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1320298fd71SBarry 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);
1330298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1340298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1350298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1360298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
137bdad233fSMatthew Knepley 
13856f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
13956f85f32SBarry Smith   {
14056f85f32SBarry Smith   PetscBool set;
14156f85f32SBarry Smith   flg  = PETSC_FALSE;
14256f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
14356f85f32SBarry Smith   if (set) {
14456f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
14556f85f32SBarry Smith   }
14656f85f32SBarry Smith   }
14756f85f32SBarry Smith #endif
14856f85f32SBarry Smith 
149bdad233fSMatthew Knepley   /* Monitor options */
150a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
151eabae89aSBarry Smith   if (flg) {
152ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
153649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
154bdad233fSMatthew Knepley   }
1555180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1565180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1575180491cSLisandro Dalcin 
1584f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
159a7cc72afSBarry Smith   if (opt) {
1600b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1613923b477SBarry Smith     PetscInt       howoften = 1;
162a80ad3e0SBarry Smith 
1630298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
164ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1654f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
166b3603a34SBarry Smith   }
1674f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
168b3603a34SBarry Smith   if (opt) {
1690b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1703923b477SBarry Smith     PetscInt       howoften = 1;
171b3603a34SBarry Smith 
1720298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
17322d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1744f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
175bdad233fSMatthew Knepley   }
1764f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
177ef20d060SBarry Smith   if (opt) {
1780b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1793923b477SBarry Smith     PetscInt       howoften = 1;
180ef20d060SBarry Smith 
1810298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
18222d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1834f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
184ef20d060SBarry Smith   }
185201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
186201da799SBarry Smith   if (opt) {
187201da799SBarry Smith     TSMonitorLGCtx ctx;
188201da799SBarry Smith     PetscInt       howoften = 1;
189201da799SBarry Smith 
1900298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
19122d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
192201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
193201da799SBarry Smith   }
194201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
195201da799SBarry Smith   if (opt) {
196201da799SBarry Smith     TSMonitorLGCtx ctx;
197201da799SBarry Smith     PetscInt       howoften = 1;
198201da799SBarry Smith 
1990298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
20022d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
201201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
202201da799SBarry Smith   }
2038189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2048189c53fSBarry Smith   if (opt) {
2058189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2068189c53fSBarry Smith     PetscInt          howoften = 1;
2078189c53fSBarry Smith 
2080298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
20922d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
2108189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2118189c53fSBarry Smith   }
212ef20d060SBarry Smith   opt  = PETSC_FALSE;
2130dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
214a7cc72afSBarry Smith   if (opt) {
21583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
21683a4ac43SBarry Smith     PetscInt         howoften = 1;
217a80ad3e0SBarry Smith 
2180298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
219ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
22083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
221bdad233fSMatthew Knepley   }
222fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2232d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2242d5ee99bSBarry Smith   if (opt) {
2252d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2262d5ee99bSBarry Smith     PetscReal        bounds[4];
2272d5ee99bSBarry Smith     PetscInt         n = 4;
2282d5ee99bSBarry Smith     PetscDraw        draw;
2292d5ee99bSBarry Smith 
2302d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2312d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2322d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2332d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2342d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
2354b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
2364b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
237f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2384b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
23907995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
2402d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2412d5ee99bSBarry Smith   }
2422d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2430dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2443a471f94SBarry Smith   if (opt) {
24583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
24683a4ac43SBarry Smith     PetscInt         howoften = 1;
2473a471f94SBarry Smith 
2480298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
249ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
25083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2513a471f94SBarry Smith   }
2523a471f94SBarry Smith   opt  = PETSC_FALSE;
25391b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
254fb1732b5SBarry Smith   if (flg) {
255fb1732b5SBarry Smith     PetscViewer ctx;
256fb1732b5SBarry Smith     if (monfilename[0]) {
257ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
258c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
259fb1732b5SBarry Smith     } else {
260ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2610298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
262fb1732b5SBarry Smith     }
263fb1732b5SBarry Smith   }
264ed81e22dSJed Brown   opt  = PETSC_FALSE;
26591b97e58SBarry 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);
266ed81e22dSJed Brown   if (flg) {
267ed81e22dSJed Brown     const char *ptr,*ptr2;
268ed81e22dSJed Brown     char       *filetemplate;
269ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
270ed81e22dSJed Brown     /* Do some cursory validation of the input. */
271ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
272ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
273ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
274ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
275ce94432eSBarry 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");
276ed81e22dSJed Brown       if (ptr2) break;
277ed81e22dSJed Brown     }
278ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
279ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
280ed81e22dSJed Brown   }
281bdad233fSMatthew Knepley 
282d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
283d1212d36SBarry Smith   if (flg) {
284d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
285d1212d36SBarry Smith     int                  ray = 0;
286d1212d36SBarry Smith     DMDADirection        ddir;
287d1212d36SBarry Smith     DM                   da;
288d1212d36SBarry Smith     PetscMPIInt          rank;
289d1212d36SBarry Smith 
290ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
291d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
292d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
293ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
294d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
295d1212d36SBarry Smith 
296d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
297b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
298d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
299d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
300ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
301d1212d36SBarry Smith     if (!rank) {
302d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
303d1212d36SBarry Smith     }
30451b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
305d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
306d1212d36SBarry Smith   }
30751b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
30851b4a12fSMatthew G. Knepley   if (flg) {
30951b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
31051b4a12fSMatthew G. Knepley     int                 ray = 0;
31151b4a12fSMatthew G. Knepley     DMDADirection       ddir;
31251b4a12fSMatthew G. Knepley     DM                  da;
31351b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
314d1212d36SBarry Smith 
31551b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
31651b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
31751b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
31851b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
31951b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
32051b4a12fSMatthew G. Knepley 
32151b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
322b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
32351b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
32451b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
32551b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
32651b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
32751b4a12fSMatthew G. Knepley   }
328a7a1495cSBarry Smith 
3297781c08eSBarry Smith   /*
3307781c08eSBarry Smith      This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui
3317781c08eSBarry Smith      will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin()
3327781c08eSBarry Smith   */
333552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
334e55864a3SBarry Smith   ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr);
335d763cef2SBarry Smith 
336d763cef2SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
337d763cef2SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
338d763cef2SBarry Smith 
339d763cef2SBarry Smith   /* Handle specific TS options */
340d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
341e55864a3SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
342d763cef2SBarry Smith   }
343d763cef2SBarry Smith 
344d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
345d763cef2SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
346d763cef2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
347d763cef2SBarry Smith   PetscFunctionReturn(0);
348d763cef2SBarry Smith }
349d763cef2SBarry Smith 
350d763cef2SBarry Smith #undef __FUNCT__
351d2daff3dSHong Zhang #define __FUNCT__ "TSSetCheckpoint"
352d2daff3dSHong Zhang /*@
353d2daff3dSHong Zhang    TSSetCheckpoint - Allows one to checkpoint the forward run,
354d2daff3dSHong Zhang    useful for adjoint sensitivity analysis.
355d2daff3dSHong Zhang 
356d2daff3dSHong Zhang    Collective on TS
357d2daff3dSHong Zhang 
358d2daff3dSHong Zhang    Input Parameters:
359d2daff3dSHong Zhang +  ts - the TS context obtained from TSCreate()
360d2daff3dSHong Zhang -  checkpoint - flag that indicates if the forward solution will be checkpointed
361d2daff3dSHong Zhang 
362d2daff3dSHong Zhang    Level: intermediate
363d2daff3dSHong Zhang 
364d2daff3dSHong Zhang .seealso:
365d2daff3dSHong Zhang 
366d2daff3dSHong Zhang .keywords: TS, set, checkpoint
367d2daff3dSHong Zhang @*/
368d2daff3dSHong Zhang PetscErrorCode  TSSetCheckpoint(TS ts,PetscBool checkpoint)
369d2daff3dSHong Zhang {
370d2daff3dSHong Zhang   PetscFunctionBegin;
371d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
372d2daff3dSHong Zhang   ts->checkpoint = checkpoint;
373d2daff3dSHong Zhang   PetscFunctionReturn(0);
374d2daff3dSHong Zhang }
375d2daff3dSHong Zhang 
376d2daff3dSHong Zhang #undef __FUNCT__
377d2daff3dSHong Zhang #define __FUNCT__ "TSSetReverseMode"
378d2daff3dSHong Zhang /*@
379e6ad8bfbSHong Zhang    TSSetReverseMode - Allows one to reset the run mode,
380d2daff3dSHong Zhang    useful for adjoint sensitivity analysis.
381d2daff3dSHong Zhang 
382d2daff3dSHong Zhang    Collective on TS
383d2daff3dSHong Zhang 
384d2daff3dSHong Zhang    Input Parameters:
385d2daff3dSHong Zhang +  ts - the TS context obtained from TSCreate()
386d2daff3dSHong Zhang -  reverse_mode - run in forward (default) or reverse mode
387d2daff3dSHong Zhang 
388d2daff3dSHong Zhang    Level: intermediate
389d2daff3dSHong Zhang 
390d2daff3dSHong Zhang .seealso:
391d2daff3dSHong Zhang 
392d2daff3dSHong Zhang .keywords: TS, set, reverse
393d2daff3dSHong Zhang @*/
394d2daff3dSHong Zhang PetscErrorCode  TSSetReverseMode(TS ts,PetscBool reverse_mode)
395d2daff3dSHong Zhang {
396f8fee759SHong Zhang   PetscErrorCode ierr;
397d2daff3dSHong Zhang   PetscFunctionBegin;
398d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
399d2daff3dSHong Zhang   ts->reverse_mode = reverse_mode;
400*f6a906c0SBarry Smith   //ts->setupcalled  = PETSC_FALSE;
401f8fee759SHong Zhang   ierr = TSSetUp(ts);CHKERRQ(ierr);
402d2daff3dSHong Zhang   PetscFunctionReturn(0);
403d2daff3dSHong Zhang }
404d2daff3dSHong Zhang 
405a7a1495cSBarry Smith #undef __FUNCT__
406a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
407a7a1495cSBarry Smith /*@
408a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
409a7a1495cSBarry Smith       set with TSSetRHSJacobian().
410a7a1495cSBarry Smith 
411a7a1495cSBarry Smith    Collective on TS and Vec
412a7a1495cSBarry Smith 
413a7a1495cSBarry Smith    Input Parameters:
414316643e7SJed Brown +  ts - the TS context
415a7a1495cSBarry Smith .  t - current timestep
4160910c330SBarry Smith -  U - input vector
417a7a1495cSBarry Smith 
418a7a1495cSBarry Smith    Output Parameters:
419a7a1495cSBarry Smith +  A - Jacobian matrix
420a7a1495cSBarry Smith .  B - optional preconditioning matrix
421a7a1495cSBarry Smith -  flag - flag indicating matrix structure
422a7a1495cSBarry Smith 
423a7a1495cSBarry Smith    Notes:
424a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
425a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
426a7a1495cSBarry Smith 
42794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
428a7a1495cSBarry Smith    flag parameter.
429a7a1495cSBarry Smith 
430a7a1495cSBarry Smith    Level: developer
431a7a1495cSBarry Smith 
432a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
433a7a1495cSBarry Smith 
43494b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
435a7a1495cSBarry Smith @*/
436d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
437a7a1495cSBarry Smith {
438dfbe8321SBarry Smith   PetscErrorCode ierr;
439270bf2e7SJed Brown   PetscObjectState Ustate;
44024989b8cSPeter Brune   DM             dm;
441942e3340SBarry Smith   DMTS           tsdm;
44224989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
44324989b8cSPeter Brune   void           *ctx;
44424989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
445a7a1495cSBarry Smith 
446a7a1495cSBarry Smith   PetscFunctionBegin;
4470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4480910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4490910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
45024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
451942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
45224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
4530298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
45459e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
4550910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
4560e4ef248SJed Brown     PetscFunctionReturn(0);
457f8ede8e7SJed Brown   }
458d90be118SSean Farley 
459ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
460d90be118SSean Farley 
461e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
46294ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
46394ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
46494ab13aaSBarry Smith     if (A != B) {
46594ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
46694ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
467e1244c69SJed Brown     }
468e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
469e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
470e1244c69SJed Brown   }
471e1244c69SJed Brown 
47224989b8cSPeter Brune   if (rhsjacobianfunc) {
47394ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
474a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
475d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
476a7a1495cSBarry Smith     PetscStackPop;
47794ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
478a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
47994ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
48094ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
481ef66eb69SBarry Smith   } else {
48294ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
48394ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
484ef66eb69SBarry Smith   }
4850e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4860910c330SBarry Smith   ts->rhsjacobian.X          = U;
48759e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
488a7a1495cSBarry Smith   PetscFunctionReturn(0);
489a7a1495cSBarry Smith }
490a7a1495cSBarry Smith 
4914a2ae208SSatish Balay #undef __FUNCT__
4924a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
493316643e7SJed Brown /*@
494d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
495d763cef2SBarry Smith 
496316643e7SJed Brown    Collective on TS and Vec
497316643e7SJed Brown 
498316643e7SJed Brown    Input Parameters:
499316643e7SJed Brown +  ts - the TS context
500316643e7SJed Brown .  t - current time
5010910c330SBarry Smith -  U - state vector
502316643e7SJed Brown 
503316643e7SJed Brown    Output Parameter:
504316643e7SJed Brown .  y - right hand side
505316643e7SJed Brown 
506316643e7SJed Brown    Note:
507316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
508316643e7SJed Brown    is used internally within the nonlinear solvers.
509316643e7SJed Brown 
510316643e7SJed Brown    Level: developer
511316643e7SJed Brown 
512316643e7SJed Brown .keywords: TS, compute
513316643e7SJed Brown 
514316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
515316643e7SJed Brown @*/
5160910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
517d763cef2SBarry Smith {
518dfbe8321SBarry Smith   PetscErrorCode ierr;
51924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
52024989b8cSPeter Brune   TSIFunction    ifunction;
52124989b8cSPeter Brune   void           *ctx;
52224989b8cSPeter Brune   DM             dm;
52324989b8cSPeter Brune 
524d763cef2SBarry Smith   PetscFunctionBegin;
5250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5260910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5270700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
52824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
52924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
5300298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
531d763cef2SBarry Smith 
532ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
533d763cef2SBarry Smith 
5340910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
53524989b8cSPeter Brune   if (rhsfunction) {
536d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
5370910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
538d763cef2SBarry Smith     PetscStackPop;
539214bc6a2SJed Brown   } else {
540214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
541b2cd27e8SJed Brown   }
54244a41b28SSean Farley 
5430910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
544d763cef2SBarry Smith   PetscFunctionReturn(0);
545d763cef2SBarry Smith }
546d763cef2SBarry Smith 
5474a2ae208SSatish Balay #undef __FUNCT__
548ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
549ef20d060SBarry Smith /*@
550ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
551ef20d060SBarry Smith 
552ef20d060SBarry Smith    Collective on TS and Vec
553ef20d060SBarry Smith 
554ef20d060SBarry Smith    Input Parameters:
555ef20d060SBarry Smith +  ts - the TS context
556ef20d060SBarry Smith -  t - current time
557ef20d060SBarry Smith 
558ef20d060SBarry Smith    Output Parameter:
5590910c330SBarry Smith .  U - the solution
560ef20d060SBarry Smith 
561ef20d060SBarry Smith    Note:
562ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
563ef20d060SBarry Smith    is used internally within the nonlinear solvers.
564ef20d060SBarry Smith 
565ef20d060SBarry Smith    Level: developer
566ef20d060SBarry Smith 
567ef20d060SBarry Smith .keywords: TS, compute
568ef20d060SBarry Smith 
569abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
570ef20d060SBarry Smith @*/
5710910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
572ef20d060SBarry Smith {
573ef20d060SBarry Smith   PetscErrorCode     ierr;
574ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
575ef20d060SBarry Smith   void               *ctx;
576ef20d060SBarry Smith   DM                 dm;
577ef20d060SBarry Smith 
578ef20d060SBarry Smith   PetscFunctionBegin;
579ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5800910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
581ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
582ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
583ef20d060SBarry Smith 
584ef20d060SBarry Smith   if (solutionfunction) {
5859b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5860910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
587ef20d060SBarry Smith     PetscStackPop;
588ef20d060SBarry Smith   }
589ef20d060SBarry Smith   PetscFunctionReturn(0);
590ef20d060SBarry Smith }
5919b7cd975SBarry Smith #undef __FUNCT__
5929b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
5939b7cd975SBarry Smith /*@
5949b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5959b7cd975SBarry Smith 
5969b7cd975SBarry Smith    Collective on TS and Vec
5979b7cd975SBarry Smith 
5989b7cd975SBarry Smith    Input Parameters:
5999b7cd975SBarry Smith +  ts - the TS context
6009b7cd975SBarry Smith -  t - current time
6019b7cd975SBarry Smith 
6029b7cd975SBarry Smith    Output Parameter:
6039b7cd975SBarry Smith .  U - the function value
6049b7cd975SBarry Smith 
6059b7cd975SBarry Smith    Note:
6069b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6079b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6089b7cd975SBarry Smith 
6099b7cd975SBarry Smith    Level: developer
6109b7cd975SBarry Smith 
6119b7cd975SBarry Smith .keywords: TS, compute
6129b7cd975SBarry Smith 
6139b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
6149b7cd975SBarry Smith @*/
6159b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
6169b7cd975SBarry Smith {
6179b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
6189b7cd975SBarry Smith   void               *ctx;
6199b7cd975SBarry Smith   DM                 dm;
6209b7cd975SBarry Smith 
6219b7cd975SBarry Smith   PetscFunctionBegin;
6229b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6239b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6249b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
6259b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
6269b7cd975SBarry Smith 
6279b7cd975SBarry Smith   if (forcing) {
6289b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
6299b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
6309b7cd975SBarry Smith     PetscStackPop;
6319b7cd975SBarry Smith   }
6329b7cd975SBarry Smith   PetscFunctionReturn(0);
6339b7cd975SBarry Smith }
634ef20d060SBarry Smith 
635ef20d060SBarry Smith #undef __FUNCT__
636214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
637214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
638214bc6a2SJed Brown {
6392dd45cf8SJed Brown   Vec            F;
640214bc6a2SJed Brown   PetscErrorCode ierr;
641214bc6a2SJed Brown 
642214bc6a2SJed Brown   PetscFunctionBegin;
6430298fd71SBarry Smith   *Frhs = NULL;
6440298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
645214bc6a2SJed Brown   if (!ts->Frhs) {
6462dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
647214bc6a2SJed Brown   }
648214bc6a2SJed Brown   *Frhs = ts->Frhs;
649214bc6a2SJed Brown   PetscFunctionReturn(0);
650214bc6a2SJed Brown }
651214bc6a2SJed Brown 
652214bc6a2SJed Brown #undef __FUNCT__
653214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
654214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
655214bc6a2SJed Brown {
656214bc6a2SJed Brown   Mat            A,B;
6572dd45cf8SJed Brown   PetscErrorCode ierr;
658214bc6a2SJed Brown 
659214bc6a2SJed Brown   PetscFunctionBegin;
660c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
661c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
6620298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
663214bc6a2SJed Brown   if (Arhs) {
664214bc6a2SJed Brown     if (!ts->Arhs) {
665214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
666214bc6a2SJed Brown     }
667214bc6a2SJed Brown     *Arhs = ts->Arhs;
668214bc6a2SJed Brown   }
669214bc6a2SJed Brown   if (Brhs) {
670214bc6a2SJed Brown     if (!ts->Brhs) {
671bdb70873SJed Brown       if (A != B) {
672214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
673bdb70873SJed Brown       } else {
674bdb70873SJed Brown         ts->Brhs = ts->Arhs;
675bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
676bdb70873SJed Brown       }
677214bc6a2SJed Brown     }
678214bc6a2SJed Brown     *Brhs = ts->Brhs;
679214bc6a2SJed Brown   }
680214bc6a2SJed Brown   PetscFunctionReturn(0);
681214bc6a2SJed Brown }
682214bc6a2SJed Brown 
683214bc6a2SJed Brown #undef __FUNCT__
684316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
685316643e7SJed Brown /*@
6860910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
687316643e7SJed Brown 
688316643e7SJed Brown    Collective on TS and Vec
689316643e7SJed Brown 
690316643e7SJed Brown    Input Parameters:
691316643e7SJed Brown +  ts - the TS context
692316643e7SJed Brown .  t - current time
6930910c330SBarry Smith .  U - state vector
6940910c330SBarry Smith .  Udot - time derivative of state vector
695214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
696316643e7SJed Brown 
697316643e7SJed Brown    Output Parameter:
698316643e7SJed Brown .  Y - right hand side
699316643e7SJed Brown 
700316643e7SJed Brown    Note:
701316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
702316643e7SJed Brown    is used internally within the nonlinear solvers.
703316643e7SJed Brown 
704316643e7SJed Brown    If the user did did not write their equations in implicit form, this
705316643e7SJed Brown    function recasts them in implicit form.
706316643e7SJed Brown 
707316643e7SJed Brown    Level: developer
708316643e7SJed Brown 
709316643e7SJed Brown .keywords: TS, compute
710316643e7SJed Brown 
711316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
712316643e7SJed Brown @*/
7130910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
714316643e7SJed Brown {
715316643e7SJed Brown   PetscErrorCode ierr;
71624989b8cSPeter Brune   TSIFunction    ifunction;
71724989b8cSPeter Brune   TSRHSFunction  rhsfunction;
71824989b8cSPeter Brune   void           *ctx;
71924989b8cSPeter Brune   DM             dm;
720316643e7SJed Brown 
721316643e7SJed Brown   PetscFunctionBegin;
7220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7230910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7240910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
7250700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
726316643e7SJed Brown 
72724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
72824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
7290298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
73024989b8cSPeter Brune 
731ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
732d90be118SSean Farley 
7330910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
73424989b8cSPeter Brune   if (ifunction) {
735316643e7SJed Brown     PetscStackPush("TS user implicit function");
7360910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
737316643e7SJed Brown     PetscStackPop;
738214bc6a2SJed Brown   }
739214bc6a2SJed Brown   if (imex) {
74024989b8cSPeter Brune     if (!ifunction) {
7410910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
7422dd45cf8SJed Brown     }
74324989b8cSPeter Brune   } else if (rhsfunction) {
74424989b8cSPeter Brune     if (ifunction) {
745214bc6a2SJed Brown       Vec Frhs;
746214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
7470910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
748214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
7492dd45cf8SJed Brown     } else {
7500910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
7510910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
752316643e7SJed Brown     }
7534a6899ffSJed Brown   }
7540910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
755316643e7SJed Brown   PetscFunctionReturn(0);
756316643e7SJed Brown }
757316643e7SJed Brown 
758316643e7SJed Brown #undef __FUNCT__
759316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
760316643e7SJed Brown /*@
761316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
762316643e7SJed Brown 
763316643e7SJed Brown    Collective on TS and Vec
764316643e7SJed Brown 
765316643e7SJed Brown    Input
766316643e7SJed Brown       Input Parameters:
767316643e7SJed Brown +  ts - the TS context
768316643e7SJed Brown .  t - current timestep
7690910c330SBarry Smith .  U - state vector
7700910c330SBarry Smith .  Udot - time derivative of state vector
771214bc6a2SJed Brown .  shift - shift to apply, see note below
772214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
773316643e7SJed Brown 
774316643e7SJed Brown    Output Parameters:
775316643e7SJed Brown +  A - Jacobian matrix
776316643e7SJed Brown .  B - optional preconditioning matrix
777316643e7SJed Brown -  flag - flag indicating matrix structure
778316643e7SJed Brown 
779316643e7SJed Brown    Notes:
7800910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
781316643e7SJed Brown 
7820910c330SBarry Smith    dF/dU + shift*dF/dUdot
783316643e7SJed Brown 
784316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
785316643e7SJed Brown    is used internally within the nonlinear solvers.
786316643e7SJed Brown 
787316643e7SJed Brown    Level: developer
788316643e7SJed Brown 
789316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
790316643e7SJed Brown 
791316643e7SJed Brown .seealso:  TSSetIJacobian()
79263495f91SJed Brown @*/
793d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
794316643e7SJed Brown {
795316643e7SJed Brown   PetscErrorCode ierr;
79624989b8cSPeter Brune   TSIJacobian    ijacobian;
79724989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
79824989b8cSPeter Brune   DM             dm;
79924989b8cSPeter Brune   void           *ctx;
800316643e7SJed Brown 
801316643e7SJed Brown   PetscFunctionBegin;
8020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8030910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8040910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
805316643e7SJed Brown   PetscValidPointer(A,6);
80694ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
807316643e7SJed Brown   PetscValidPointer(B,7);
80894ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
80924989b8cSPeter Brune 
81024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
81124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
8120298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
81324989b8cSPeter Brune 
814ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
815316643e7SJed Brown 
81694ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
81724989b8cSPeter Brune   if (ijacobian) {
818316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
819d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
820316643e7SJed Brown     PetscStackPop;
821214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
82294ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
82394ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
8244a6899ffSJed Brown   }
825214bc6a2SJed Brown   if (imex) {
826b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
82794ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
82894ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
82994ab13aaSBarry Smith       if (A != B) {
83094ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
83194ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
832214bc6a2SJed Brown       }
833214bc6a2SJed Brown     }
834214bc6a2SJed Brown   } else {
835e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
836e1244c69SJed Brown     if (rhsjacobian) {
837bd373395SBarry Smith       if (ijacobian) {
838e1244c69SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
839bd373395SBarry Smith       } else {
840bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
841e1244c69SJed Brown       }
842d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
843e1244c69SJed Brown     }
84494ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
845e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
846e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
84794ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
84894ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
84994ab13aaSBarry Smith       if (A != B) {
85094ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
85194ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
852316643e7SJed Brown       }
853e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
854e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
855e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
85694ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
85794ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
85894ab13aaSBarry Smith         if (A != B) {
85994ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
86094ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
861e1244c69SJed Brown         }
862e1244c69SJed Brown       }
86394ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
86494ab13aaSBarry Smith       if (A != B) {
86594ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
866214bc6a2SJed Brown       }
867316643e7SJed Brown     }
868316643e7SJed Brown   }
86994ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
870316643e7SJed Brown   PetscFunctionReturn(0);
871316643e7SJed Brown }
872316643e7SJed Brown 
873316643e7SJed Brown #undef __FUNCT__
8744a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
875d763cef2SBarry Smith /*@C
876d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
877b5abc632SBarry Smith     where U_t = G(t,u).
878d763cef2SBarry Smith 
8793f9fe445SBarry Smith     Logically Collective on TS
880d763cef2SBarry Smith 
881d763cef2SBarry Smith     Input Parameters:
882d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8830298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
884d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
885d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8860298fd71SBarry Smith           function evaluation routine (may be NULL)
887d763cef2SBarry Smith 
888d763cef2SBarry Smith     Calling sequence of func:
88987828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
890d763cef2SBarry Smith 
891d763cef2SBarry Smith +   t - current timestep
892d763cef2SBarry Smith .   u - input vector
893d763cef2SBarry Smith .   F - function vector
894d763cef2SBarry Smith -   ctx - [optional] user-defined function context
895d763cef2SBarry Smith 
896d763cef2SBarry Smith     Level: beginner
897d763cef2SBarry Smith 
898d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
899d763cef2SBarry Smith 
900ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
901d763cef2SBarry Smith @*/
902089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
903d763cef2SBarry Smith {
904089b2837SJed Brown   PetscErrorCode ierr;
905089b2837SJed Brown   SNES           snes;
9060298fd71SBarry Smith   Vec            ralloc = NULL;
90724989b8cSPeter Brune   DM             dm;
908d763cef2SBarry Smith 
909089b2837SJed Brown   PetscFunctionBegin;
9100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
911ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
91224989b8cSPeter Brune 
91324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
91424989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
915089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
916e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
917e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
918e856ceecSJed Brown     r    = ralloc;
919e856ceecSJed Brown   }
920089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
921e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
922d763cef2SBarry Smith   PetscFunctionReturn(0);
923d763cef2SBarry Smith }
924d763cef2SBarry Smith 
9254a2ae208SSatish Balay #undef __FUNCT__
926ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
927ef20d060SBarry Smith /*@C
928abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
929ef20d060SBarry Smith 
930ef20d060SBarry Smith     Logically Collective on TS
931ef20d060SBarry Smith 
932ef20d060SBarry Smith     Input Parameters:
933ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
934ef20d060SBarry Smith .   f - routine for evaluating the solution
935ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
9360298fd71SBarry Smith           function evaluation routine (may be NULL)
937ef20d060SBarry Smith 
938ef20d060SBarry Smith     Calling sequence of func:
939ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
940ef20d060SBarry Smith 
941ef20d060SBarry Smith +   t - current timestep
942ef20d060SBarry Smith .   u - output vector
943ef20d060SBarry Smith -   ctx - [optional] user-defined function context
944ef20d060SBarry Smith 
945abd5a294SJed Brown     Notes:
946abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
947abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
948abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
949abd5a294SJed Brown 
9504f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
951abd5a294SJed Brown 
952ef20d060SBarry Smith     Level: beginner
953ef20d060SBarry Smith 
954ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
955ef20d060SBarry Smith 
9569b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
957ef20d060SBarry Smith @*/
958ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
959ef20d060SBarry Smith {
960ef20d060SBarry Smith   PetscErrorCode ierr;
961ef20d060SBarry Smith   DM             dm;
962ef20d060SBarry Smith 
963ef20d060SBarry Smith   PetscFunctionBegin;
964ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
965ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
966ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
967ef20d060SBarry Smith   PetscFunctionReturn(0);
968ef20d060SBarry Smith }
969ef20d060SBarry Smith 
970ef20d060SBarry Smith #undef __FUNCT__
9719b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
9729b7cd975SBarry Smith /*@C
9739b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
9749b7cd975SBarry Smith 
9759b7cd975SBarry Smith     Logically Collective on TS
9769b7cd975SBarry Smith 
9779b7cd975SBarry Smith     Input Parameters:
9789b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9799b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9809b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9810298fd71SBarry Smith           function evaluation routine (may be NULL)
9829b7cd975SBarry Smith 
9839b7cd975SBarry Smith     Calling sequence of func:
9849b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9859b7cd975SBarry Smith 
9869b7cd975SBarry Smith +   t - current timestep
9879b7cd975SBarry Smith .   u - output vector
9889b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9899b7cd975SBarry Smith 
9909b7cd975SBarry Smith     Notes:
9919b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9929b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9939b7cd975SBarry Smith 
9949b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9959b7cd975SBarry Smith 
9969b7cd975SBarry Smith     Level: beginner
9979b7cd975SBarry Smith 
9989b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9999b7cd975SBarry Smith 
10009b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
10019b7cd975SBarry Smith @*/
10029b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
10039b7cd975SBarry Smith {
10049b7cd975SBarry Smith   PetscErrorCode ierr;
10059b7cd975SBarry Smith   DM             dm;
10069b7cd975SBarry Smith 
10079b7cd975SBarry Smith   PetscFunctionBegin;
10089b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10099b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
10109b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
10119b7cd975SBarry Smith   PetscFunctionReturn(0);
10129b7cd975SBarry Smith }
10139b7cd975SBarry Smith 
10149b7cd975SBarry Smith #undef __FUNCT__
10154a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
1016d763cef2SBarry Smith /*@C
1017d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
1018b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1019d763cef2SBarry Smith 
10203f9fe445SBarry Smith    Logically Collective on TS
1021d763cef2SBarry Smith 
1022d763cef2SBarry Smith    Input Parameters:
1023d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1024e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1025e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1026d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1027d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
10280298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1029d763cef2SBarry Smith 
1030d763cef2SBarry Smith    Calling sequence of func:
1031ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1032d763cef2SBarry Smith 
1033d763cef2SBarry Smith +  t - current timestep
1034d763cef2SBarry Smith .  u - input vector
1035e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1036e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1037d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1038d763cef2SBarry Smith 
1039d763cef2SBarry Smith 
1040d763cef2SBarry Smith    Level: beginner
1041d763cef2SBarry Smith 
1042d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1043d763cef2SBarry Smith 
1044ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1045d763cef2SBarry Smith 
1046d763cef2SBarry Smith @*/
1047e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1048d763cef2SBarry Smith {
1049277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1050089b2837SJed Brown   SNES           snes;
105124989b8cSPeter Brune   DM             dm;
105224989b8cSPeter Brune   TSIJacobian    ijacobian;
1053277b19d0SLisandro Dalcin 
1054d763cef2SBarry Smith   PetscFunctionBegin;
10550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1056e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1057e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1058e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1059e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1060d763cef2SBarry Smith 
106124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
106224989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1063e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1064e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1065e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1066e1244c69SJed Brown   }
10670298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1068089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10695f659677SPeter Brune   if (!ijacobian) {
1070e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10710e4ef248SJed Brown   }
1072e5d3d808SBarry Smith   if (Amat) {
1073e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10740e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1075bbd56ea5SKarl Rupp 
1076e5d3d808SBarry Smith     ts->Arhs = Amat;
10770e4ef248SJed Brown   }
1078e5d3d808SBarry Smith   if (Pmat) {
1079e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10800e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1081bbd56ea5SKarl Rupp 
1082e5d3d808SBarry Smith     ts->Brhs = Pmat;
10830e4ef248SJed Brown   }
1084d763cef2SBarry Smith   PetscFunctionReturn(0);
1085d763cef2SBarry Smith }
1086d763cef2SBarry Smith 
1087316643e7SJed Brown 
1088316643e7SJed Brown #undef __FUNCT__
1089316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1090316643e7SJed Brown /*@C
1091b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1092316643e7SJed Brown 
10933f9fe445SBarry Smith    Logically Collective on TS
1094316643e7SJed Brown 
1095316643e7SJed Brown    Input Parameters:
1096316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10970298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1098316643e7SJed Brown .  f   - the function evaluation routine
10990298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1100316643e7SJed Brown 
1101316643e7SJed Brown    Calling sequence of f:
1102316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1103316643e7SJed Brown 
1104316643e7SJed Brown +  t   - time at step/stage being solved
1105316643e7SJed Brown .  u   - state vector
1106316643e7SJed Brown .  u_t - time derivative of state vector
1107316643e7SJed Brown .  F   - function vector
1108316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1109316643e7SJed Brown 
1110316643e7SJed Brown    Important:
1111d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
1112316643e7SJed Brown 
1113316643e7SJed Brown    Level: beginner
1114316643e7SJed Brown 
1115316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1116316643e7SJed Brown 
1117d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1118316643e7SJed Brown @*/
1119089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1120316643e7SJed Brown {
1121089b2837SJed Brown   PetscErrorCode ierr;
1122089b2837SJed Brown   SNES           snes;
11230298fd71SBarry Smith   Vec            resalloc = NULL;
112424989b8cSPeter Brune   DM             dm;
1125316643e7SJed Brown 
1126316643e7SJed Brown   PetscFunctionBegin;
11270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1128ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
112924989b8cSPeter Brune 
113024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
113124989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
113224989b8cSPeter Brune 
1133089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1134e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1135e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1136e856ceecSJed Brown     res  = resalloc;
1137e856ceecSJed Brown   }
1138089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1139e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1140089b2837SJed Brown   PetscFunctionReturn(0);
1141089b2837SJed Brown }
1142089b2837SJed Brown 
1143089b2837SJed Brown #undef __FUNCT__
1144089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1145089b2837SJed Brown /*@C
1146089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1147089b2837SJed Brown 
1148089b2837SJed Brown    Not Collective
1149089b2837SJed Brown 
1150089b2837SJed Brown    Input Parameter:
1151089b2837SJed Brown .  ts - the TS context
1152089b2837SJed Brown 
1153089b2837SJed Brown    Output Parameter:
11540298fd71SBarry Smith +  r - vector to hold residual (or NULL)
11550298fd71SBarry Smith .  func - the function to compute residual (or NULL)
11560298fd71SBarry Smith -  ctx - the function context (or NULL)
1157089b2837SJed Brown 
1158089b2837SJed Brown    Level: advanced
1159089b2837SJed Brown 
1160089b2837SJed Brown .keywords: TS, nonlinear, get, function
1161089b2837SJed Brown 
1162089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1163089b2837SJed Brown @*/
1164089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1165089b2837SJed Brown {
1166089b2837SJed Brown   PetscErrorCode ierr;
1167089b2837SJed Brown   SNES           snes;
116824989b8cSPeter Brune   DM             dm;
1169089b2837SJed Brown 
1170089b2837SJed Brown   PetscFunctionBegin;
1171089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1172089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11730298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
117424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
117524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1176089b2837SJed Brown   PetscFunctionReturn(0);
1177089b2837SJed Brown }
1178089b2837SJed Brown 
1179089b2837SJed Brown #undef __FUNCT__
1180089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1181089b2837SJed Brown /*@C
1182089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1183089b2837SJed Brown 
1184089b2837SJed Brown    Not Collective
1185089b2837SJed Brown 
1186089b2837SJed Brown    Input Parameter:
1187089b2837SJed Brown .  ts - the TS context
1188089b2837SJed Brown 
1189089b2837SJed Brown    Output Parameter:
11900298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11910298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11920298fd71SBarry Smith -  ctx - the function context (or NULL)
1193089b2837SJed Brown 
1194089b2837SJed Brown    Level: advanced
1195089b2837SJed Brown 
1196089b2837SJed Brown .keywords: TS, nonlinear, get, function
1197089b2837SJed Brown 
1198089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1199089b2837SJed Brown @*/
1200089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1201089b2837SJed Brown {
1202089b2837SJed Brown   PetscErrorCode ierr;
1203089b2837SJed Brown   SNES           snes;
120424989b8cSPeter Brune   DM             dm;
1205089b2837SJed Brown 
1206089b2837SJed Brown   PetscFunctionBegin;
1207089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1208089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12090298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
121024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
121124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1212316643e7SJed Brown   PetscFunctionReturn(0);
1213316643e7SJed Brown }
1214316643e7SJed Brown 
1215316643e7SJed Brown #undef __FUNCT__
1216316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1217316643e7SJed Brown /*@C
1218a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1219ae8867d6SBarry Smith         provided with TSSetIFunction().
1220316643e7SJed Brown 
12213f9fe445SBarry Smith    Logically Collective on TS
1222316643e7SJed Brown 
1223316643e7SJed Brown    Input Parameters:
1224316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1225e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1226e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1227316643e7SJed Brown .  f   - the Jacobian evaluation routine
12280298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1229316643e7SJed Brown 
1230316643e7SJed Brown    Calling sequence of f:
1231ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1232316643e7SJed Brown 
1233316643e7SJed Brown +  t    - time at step/stage being solved
12341b4a444bSJed Brown .  U    - state vector
12351b4a444bSJed Brown .  U_t  - time derivative of state vector
1236316643e7SJed Brown .  a    - shift
1237e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1238e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1239316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1240316643e7SJed Brown 
1241316643e7SJed Brown    Notes:
1242e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1243316643e7SJed Brown 
1244a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1245b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1246a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1247a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1248a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1249a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1250a4f0a591SBarry Smith 
1251316643e7SJed Brown    Level: beginner
1252316643e7SJed Brown 
1253316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1254316643e7SJed Brown 
1255ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1256316643e7SJed Brown 
1257316643e7SJed Brown @*/
1258e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1259316643e7SJed Brown {
1260316643e7SJed Brown   PetscErrorCode ierr;
1261089b2837SJed Brown   SNES           snes;
126224989b8cSPeter Brune   DM             dm;
1263316643e7SJed Brown 
1264316643e7SJed Brown   PetscFunctionBegin;
12650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1266e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1267e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1268e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1269e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
127024989b8cSPeter Brune 
127124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
127224989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
127324989b8cSPeter Brune 
1274089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1275e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1276316643e7SJed Brown   PetscFunctionReturn(0);
1277316643e7SJed Brown }
1278316643e7SJed Brown 
12794a2ae208SSatish Balay #undef __FUNCT__
1280e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1281e1244c69SJed Brown /*@
1282e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1283e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1284e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1285e1244c69SJed Brown    not been changed by the TS.
1286e1244c69SJed Brown 
1287e1244c69SJed Brown    Logically Collective
1288e1244c69SJed Brown 
1289e1244c69SJed Brown    Input Arguments:
1290e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1291e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1292e1244c69SJed Brown 
1293e1244c69SJed Brown    Level: intermediate
1294e1244c69SJed Brown 
1295e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1296e1244c69SJed Brown @*/
1297e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1298e1244c69SJed Brown {
1299e1244c69SJed Brown   PetscFunctionBegin;
1300e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1301e1244c69SJed Brown   PetscFunctionReturn(0);
1302e1244c69SJed Brown }
1303e1244c69SJed Brown 
1304e1244c69SJed Brown #undef __FUNCT__
130555849f57SBarry Smith #define __FUNCT__ "TSLoad"
130655849f57SBarry Smith /*@C
130755849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
130855849f57SBarry Smith 
130955849f57SBarry Smith   Collective on PetscViewer
131055849f57SBarry Smith 
131155849f57SBarry Smith   Input Parameters:
131255849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
131355849f57SBarry Smith            some related function before a call to TSLoad().
131455849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
131555849f57SBarry Smith 
131655849f57SBarry Smith    Level: intermediate
131755849f57SBarry Smith 
131855849f57SBarry Smith   Notes:
131955849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
132055849f57SBarry Smith 
132155849f57SBarry Smith   Notes for advanced users:
132255849f57SBarry Smith   Most users should not need to know the details of the binary storage
132355849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
132455849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
132555849f57SBarry Smith   format is
132655849f57SBarry Smith .vb
132755849f57SBarry Smith      has not yet been determined
132855849f57SBarry Smith .ve
132955849f57SBarry Smith 
133055849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
133155849f57SBarry Smith @*/
1332f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
133355849f57SBarry Smith {
133455849f57SBarry Smith   PetscErrorCode ierr;
133555849f57SBarry Smith   PetscBool      isbinary;
133655849f57SBarry Smith   PetscInt       classid;
133755849f57SBarry Smith   char           type[256];
13382d53ad75SBarry Smith   DMTS           sdm;
1339ad6bc421SBarry Smith   DM             dm;
134055849f57SBarry Smith 
134155849f57SBarry Smith   PetscFunctionBegin;
1342f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
134355849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
134455849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
134555849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
134655849f57SBarry Smith 
134755849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1348ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
134955849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1350f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1351f2c2a1b9SBarry Smith   if (ts->ops->load) {
1352f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1353f2c2a1b9SBarry Smith   }
1354ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1355ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1356ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1357f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1358f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
13592d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13602d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
136155849f57SBarry Smith   PetscFunctionReturn(0);
136255849f57SBarry Smith }
136355849f57SBarry Smith 
13649804daf3SBarry Smith #include <petscdraw.h>
1365e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1366e04113cfSBarry Smith #include <petscviewersaws.h>
1367f05ece33SBarry Smith #endif
136855849f57SBarry Smith #undef __FUNCT__
13694a2ae208SSatish Balay #define __FUNCT__ "TSView"
13707e2c5f70SBarry Smith /*@C
1371d763cef2SBarry Smith     TSView - Prints the TS data structure.
1372d763cef2SBarry Smith 
13734c49b128SBarry Smith     Collective on TS
1374d763cef2SBarry Smith 
1375d763cef2SBarry Smith     Input Parameters:
1376d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1377d763cef2SBarry Smith -   viewer - visualization context
1378d763cef2SBarry Smith 
1379d763cef2SBarry Smith     Options Database Key:
1380d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1381d763cef2SBarry Smith 
1382d763cef2SBarry Smith     Notes:
1383d763cef2SBarry Smith     The available visualization contexts include
1384b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1385b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1386d763cef2SBarry Smith          output where only the first processor opens
1387d763cef2SBarry Smith          the file.  All other processors send their
1388d763cef2SBarry Smith          data to the first processor to print.
1389d763cef2SBarry Smith 
1390d763cef2SBarry Smith     The user can open an alternative visualization context with
1391b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1392d763cef2SBarry Smith 
1393d763cef2SBarry Smith     Level: beginner
1394d763cef2SBarry Smith 
1395d763cef2SBarry Smith .keywords: TS, timestep, view
1396d763cef2SBarry Smith 
1397b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1398d763cef2SBarry Smith @*/
13997087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1400d763cef2SBarry Smith {
1401dfbe8321SBarry Smith   PetscErrorCode ierr;
140219fd82e9SBarry Smith   TSType         type;
14032b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
14042d53ad75SBarry Smith   DMTS           sdm;
1405e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1406f05ece33SBarry Smith   PetscBool      isams;
1407f05ece33SBarry Smith #endif
1408d763cef2SBarry Smith 
1409d763cef2SBarry Smith   PetscFunctionBegin;
14100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14113050cee2SBarry Smith   if (!viewer) {
1412ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
14133050cee2SBarry Smith   }
14140700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1415c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1416fd16b177SBarry Smith 
1417251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1418251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
141955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
14202b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1421e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1422e04113cfSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&isams);CHKERRQ(ierr);
1423f05ece33SBarry Smith #endif
142432077d6dSBarry Smith   if (iascii) {
1425dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
142677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
14277c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1428d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
14295ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1430c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1431d763cef2SBarry Smith     }
14325ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1433193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
14342d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14352d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1436d52bd9f3SBarry Smith     if (ts->ops->view) {
1437d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1438d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1439d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1440d52bd9f3SBarry Smith     }
14410f5bd95cSBarry Smith   } else if (isstring) {
1442a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1443b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
144455849f57SBarry Smith   } else if (isbinary) {
144555849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
144655849f57SBarry Smith     MPI_Comm    comm;
144755849f57SBarry Smith     PetscMPIInt rank;
144855849f57SBarry Smith     char        type[256];
144955849f57SBarry Smith 
145055849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
145155849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
145255849f57SBarry Smith     if (!rank) {
145355849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
145455849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
145555849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
145655849f57SBarry Smith     }
145755849f57SBarry Smith     if (ts->ops->view) {
145855849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
145955849f57SBarry Smith     }
1460f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1461f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
14622d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14632d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
14642b0a91c0SBarry Smith   } else if (isdraw) {
14652b0a91c0SBarry Smith     PetscDraw draw;
14662b0a91c0SBarry Smith     char      str[36];
146789fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14682b0a91c0SBarry Smith 
14692b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14702b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14712b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14722b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
14730298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
147489fd9fafSBarry Smith     bottom = y - h;
14752b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14762b0a91c0SBarry Smith     if (ts->ops->view) {
14772b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14782b0a91c0SBarry Smith     }
14792b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1480e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1481f05ece33SBarry Smith   } else if (isams) {
1482d45a07a7SBarry Smith     PetscMPIInt rank;
14832657e9d9SBarry Smith     const char  *name;
14842657e9d9SBarry Smith 
14852657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1486d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1487d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1488d45a07a7SBarry Smith       char       dir[1024];
1489d45a07a7SBarry Smith 
1490e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1491a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
14922657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
14932657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
14942657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1495d763cef2SBarry Smith     }
14960acecf5bSBarry Smith     if (ts->ops->view) {
14970acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14980acecf5bSBarry Smith     }
1499f05ece33SBarry Smith #endif
1500f05ece33SBarry Smith   }
1501f05ece33SBarry Smith 
1502b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1503251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1504b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1505d763cef2SBarry Smith   PetscFunctionReturn(0);
1506d763cef2SBarry Smith }
1507d763cef2SBarry Smith 
1508d763cef2SBarry Smith 
15094a2ae208SSatish Balay #undef __FUNCT__
15104a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1511b07ff414SBarry Smith /*@
1512d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1513d763cef2SBarry Smith    the timesteppers.
1514d763cef2SBarry Smith 
15153f9fe445SBarry Smith    Logically Collective on TS
1516d763cef2SBarry Smith 
1517d763cef2SBarry Smith    Input Parameters:
1518d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1519d763cef2SBarry Smith -  usrP - optional user context
1520d763cef2SBarry Smith 
1521d763cef2SBarry Smith    Level: intermediate
1522d763cef2SBarry Smith 
1523d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1524d763cef2SBarry Smith 
1525d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1526d763cef2SBarry Smith @*/
15277087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1528d763cef2SBarry Smith {
1529d763cef2SBarry Smith   PetscFunctionBegin;
15300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1531d763cef2SBarry Smith   ts->user = usrP;
1532d763cef2SBarry Smith   PetscFunctionReturn(0);
1533d763cef2SBarry Smith }
1534d763cef2SBarry Smith 
15354a2ae208SSatish Balay #undef __FUNCT__
15364a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1537b07ff414SBarry Smith /*@
1538d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1539d763cef2SBarry Smith     timestepper.
1540d763cef2SBarry Smith 
1541d763cef2SBarry Smith     Not Collective
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith     Input Parameter:
1544d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1545d763cef2SBarry Smith 
1546d763cef2SBarry Smith     Output Parameter:
1547d763cef2SBarry Smith .   usrP - user context
1548d763cef2SBarry Smith 
1549d763cef2SBarry Smith     Level: intermediate
1550d763cef2SBarry Smith 
1551d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1552d763cef2SBarry Smith 
1553d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1554d763cef2SBarry Smith @*/
1555e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1556d763cef2SBarry Smith {
1557d763cef2SBarry Smith   PetscFunctionBegin;
15580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1559e71120c6SJed Brown   *(void**)usrP = ts->user;
1560d763cef2SBarry Smith   PetscFunctionReturn(0);
1561d763cef2SBarry Smith }
1562d763cef2SBarry Smith 
15634a2ae208SSatish Balay #undef __FUNCT__
15644a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1565d763cef2SBarry Smith /*@
1566b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1567d763cef2SBarry Smith 
1568d763cef2SBarry Smith    Not Collective
1569d763cef2SBarry Smith 
1570d763cef2SBarry Smith    Input Parameter:
1571d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1572d763cef2SBarry Smith 
1573d763cef2SBarry Smith    Output Parameter:
1574b8123daeSJed Brown .  iter - number of steps completed so far
1575d763cef2SBarry Smith 
1576d763cef2SBarry Smith    Level: intermediate
1577d763cef2SBarry Smith 
1578d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
15799be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1580d763cef2SBarry Smith @*/
15817087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1582d763cef2SBarry Smith {
1583d763cef2SBarry Smith   PetscFunctionBegin;
15840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15854482741eSBarry Smith   PetscValidIntPointer(iter,2);
1586d763cef2SBarry Smith   *iter = ts->steps;
1587d763cef2SBarry Smith   PetscFunctionReturn(0);
1588d763cef2SBarry Smith }
1589d763cef2SBarry Smith 
15904a2ae208SSatish Balay #undef __FUNCT__
15914a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1592d763cef2SBarry Smith /*@
1593d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1594d763cef2SBarry Smith    as well as the initial time.
1595d763cef2SBarry Smith 
15963f9fe445SBarry Smith    Logically Collective on TS
1597d763cef2SBarry Smith 
1598d763cef2SBarry Smith    Input Parameters:
1599d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1600d763cef2SBarry Smith .  initial_time - the initial time
1601d763cef2SBarry Smith -  time_step - the size of the timestep
1602d763cef2SBarry Smith 
1603d763cef2SBarry Smith    Level: intermediate
1604d763cef2SBarry Smith 
1605d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1606d763cef2SBarry Smith 
1607d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1608d763cef2SBarry Smith @*/
16097087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1610d763cef2SBarry Smith {
1611e144a568SJed Brown   PetscErrorCode ierr;
1612e144a568SJed Brown 
1613d763cef2SBarry Smith   PetscFunctionBegin;
16140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1615e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1616d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1617d763cef2SBarry Smith   PetscFunctionReturn(0);
1618d763cef2SBarry Smith }
1619d763cef2SBarry Smith 
16204a2ae208SSatish Balay #undef __FUNCT__
16214a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1622d763cef2SBarry Smith /*@
1623d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1624d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1625d763cef2SBarry Smith 
16263f9fe445SBarry Smith    Logically Collective on TS
1627d763cef2SBarry Smith 
1628d763cef2SBarry Smith    Input Parameters:
1629d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1630d763cef2SBarry Smith -  time_step - the size of the timestep
1631d763cef2SBarry Smith 
1632d763cef2SBarry Smith    Level: intermediate
1633d763cef2SBarry Smith 
1634d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1635d763cef2SBarry Smith 
1636d763cef2SBarry Smith .keywords: TS, set, timestep
1637d763cef2SBarry Smith @*/
16387087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1639d763cef2SBarry Smith {
1640d763cef2SBarry Smith   PetscFunctionBegin;
16410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1642c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1643d763cef2SBarry Smith   ts->time_step      = time_step;
164431748224SBarry Smith   ts->time_step_orig = time_step;
1645d763cef2SBarry Smith   PetscFunctionReturn(0);
1646d763cef2SBarry Smith }
1647d763cef2SBarry Smith 
16484a2ae208SSatish Balay #undef __FUNCT__
1649a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1650a43b19c4SJed Brown /*@
165149354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
165249354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
165349354f04SShri Abhyankar      or just return at the final time TS computed.
1654a43b19c4SJed Brown 
1655a43b19c4SJed Brown   Logically Collective on TS
1656a43b19c4SJed Brown 
1657a43b19c4SJed Brown    Input Parameter:
1658a43b19c4SJed Brown +   ts - the time-step context
165949354f04SShri Abhyankar -   eftopt - exact final time option
1660a43b19c4SJed Brown 
1661a43b19c4SJed Brown    Level: beginner
1662a43b19c4SJed Brown 
1663a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1664a43b19c4SJed Brown @*/
166549354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1666a43b19c4SJed Brown {
1667a43b19c4SJed Brown   PetscFunctionBegin;
1668a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
166949354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
167049354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1671a43b19c4SJed Brown   PetscFunctionReturn(0);
1672a43b19c4SJed Brown }
1673a43b19c4SJed Brown 
1674a43b19c4SJed Brown #undef __FUNCT__
16754a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1676d763cef2SBarry Smith /*@
1677d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1678d763cef2SBarry Smith 
1679d763cef2SBarry Smith    Not Collective
1680d763cef2SBarry Smith 
1681d763cef2SBarry Smith    Input Parameter:
1682d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1683d763cef2SBarry Smith 
1684d763cef2SBarry Smith    Output Parameter:
1685d763cef2SBarry Smith .  dt - the current timestep size
1686d763cef2SBarry Smith 
1687d763cef2SBarry Smith    Level: intermediate
1688d763cef2SBarry Smith 
1689d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1690d763cef2SBarry Smith 
1691d763cef2SBarry Smith .keywords: TS, get, timestep
1692d763cef2SBarry Smith @*/
16937087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1694d763cef2SBarry Smith {
1695d763cef2SBarry Smith   PetscFunctionBegin;
16960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1697f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1698d763cef2SBarry Smith   *dt = ts->time_step;
1699d763cef2SBarry Smith   PetscFunctionReturn(0);
1700d763cef2SBarry Smith }
1701d763cef2SBarry Smith 
17024a2ae208SSatish Balay #undef __FUNCT__
17034a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1704d8e5e3e6SSatish Balay /*@
1705d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1706d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1707d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1708d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1709d763cef2SBarry Smith 
1710d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1711d763cef2SBarry Smith 
1712d763cef2SBarry Smith    Input Parameter:
1713d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1714d763cef2SBarry Smith 
1715d763cef2SBarry Smith    Output Parameter:
1716d763cef2SBarry Smith .  v - the vector containing the solution
1717d763cef2SBarry Smith 
1718d763cef2SBarry Smith    Level: intermediate
1719d763cef2SBarry Smith 
1720d763cef2SBarry Smith .seealso: TSGetTimeStep()
1721d763cef2SBarry Smith 
1722d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1723d763cef2SBarry Smith @*/
17247087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1725d763cef2SBarry Smith {
1726d763cef2SBarry Smith   PetscFunctionBegin;
17270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17284482741eSBarry Smith   PetscValidPointer(v,2);
17298737fe31SLisandro Dalcin   *v = ts->vec_sol;
1730d763cef2SBarry Smith   PetscFunctionReturn(0);
1731d763cef2SBarry Smith }
1732d763cef2SBarry Smith 
1733c235aa8dSHong Zhang #undef __FUNCT__
1734d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointGetSensitivity"
1735c235aa8dSHong Zhang /*@
1736d4aa0a58SBarry Smith    TSAdjointGetSensitivity - Returns the sensitivity in the reverse mode.
1737c235aa8dSHong Zhang 
1738c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1739c235aa8dSHong Zhang 
1740c235aa8dSHong Zhang    Input Parameter:
1741c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1742c235aa8dSHong Zhang 
1743c235aa8dSHong Zhang    Output Parameter:
1744c235aa8dSHong Zhang .  v - the vector containing the solution
1745c235aa8dSHong Zhang 
1746c235aa8dSHong Zhang    Level: intermediate
1747c235aa8dSHong Zhang 
1748c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1749c235aa8dSHong Zhang 
1750c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1751c235aa8dSHong Zhang @*/
1752d4aa0a58SBarry Smith PetscErrorCode  TSAdjointGetSensitivity(TS ts,Vec **v,PetscInt *numberadjs)
1753c235aa8dSHong Zhang {
1754c235aa8dSHong Zhang   PetscFunctionBegin;
1755c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1756c235aa8dSHong Zhang   PetscValidPointer(v,2);
1757b18ea86cSHong Zhang   *v          = ts->vecs_sensi;
1758b18ea86cSHong Zhang   if(numberadjs) *numberadjs = ts->numberadjs;
1759c235aa8dSHong Zhang   PetscFunctionReturn(0);
1760c235aa8dSHong Zhang }
1761c235aa8dSHong Zhang 
1762bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
17634a2ae208SSatish Balay #undef __FUNCT__
1764bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1765d8e5e3e6SSatish Balay /*@
1766bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1767d763cef2SBarry Smith 
1768bdad233fSMatthew Knepley   Not collective
1769d763cef2SBarry Smith 
1770bdad233fSMatthew Knepley   Input Parameters:
1771bdad233fSMatthew Knepley + ts   - The TS
1772bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1773d763cef2SBarry Smith .vb
17740910c330SBarry Smith          U_t - A U = 0      (linear)
17750910c330SBarry Smith          U_t - A(t) U = 0   (linear)
17760910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1777d763cef2SBarry Smith .ve
1778d763cef2SBarry Smith 
1779d763cef2SBarry Smith    Level: beginner
1780d763cef2SBarry Smith 
1781bdad233fSMatthew Knepley .keywords: TS, problem type
1782bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1783d763cef2SBarry Smith @*/
17847087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1785a7cc72afSBarry Smith {
17869e2a6581SJed Brown   PetscErrorCode ierr;
17879e2a6581SJed Brown 
1788d763cef2SBarry Smith   PetscFunctionBegin;
17890700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1790bdad233fSMatthew Knepley   ts->problem_type = type;
17919e2a6581SJed Brown   if (type == TS_LINEAR) {
17929e2a6581SJed Brown     SNES snes;
17939e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
17949e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
17959e2a6581SJed Brown   }
1796d763cef2SBarry Smith   PetscFunctionReturn(0);
1797d763cef2SBarry Smith }
1798d763cef2SBarry Smith 
1799bdad233fSMatthew Knepley #undef __FUNCT__
1800bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1801bdad233fSMatthew Knepley /*@C
1802bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1803bdad233fSMatthew Knepley 
1804bdad233fSMatthew Knepley   Not collective
1805bdad233fSMatthew Knepley 
1806bdad233fSMatthew Knepley   Input Parameter:
1807bdad233fSMatthew Knepley . ts   - The TS
1808bdad233fSMatthew Knepley 
1809bdad233fSMatthew Knepley   Output Parameter:
1810bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1811bdad233fSMatthew Knepley .vb
1812089b2837SJed Brown          M U_t = A U
1813089b2837SJed Brown          M(t) U_t = A(t) U
1814b5abc632SBarry Smith          F(t,U,U_t)
1815bdad233fSMatthew Knepley .ve
1816bdad233fSMatthew Knepley 
1817bdad233fSMatthew Knepley    Level: beginner
1818bdad233fSMatthew Knepley 
1819bdad233fSMatthew Knepley .keywords: TS, problem type
1820bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1821bdad233fSMatthew Knepley @*/
18227087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1823a7cc72afSBarry Smith {
1824bdad233fSMatthew Knepley   PetscFunctionBegin;
18250700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
18264482741eSBarry Smith   PetscValidIntPointer(type,2);
1827bdad233fSMatthew Knepley   *type = ts->problem_type;
1828bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1829bdad233fSMatthew Knepley }
1830d763cef2SBarry Smith 
18314a2ae208SSatish Balay #undef __FUNCT__
18324a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1833d763cef2SBarry Smith /*@
1834d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1835d763cef2SBarry Smith    of a timestepper.
1836d763cef2SBarry Smith 
1837d763cef2SBarry Smith    Collective on TS
1838d763cef2SBarry Smith 
1839d763cef2SBarry Smith    Input Parameter:
1840d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1841d763cef2SBarry Smith 
1842d763cef2SBarry Smith    Notes:
1843d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1844d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1845d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1846d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1847d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1848d763cef2SBarry Smith 
1849d763cef2SBarry Smith    Level: advanced
1850d763cef2SBarry Smith 
1851d763cef2SBarry Smith .keywords: TS, timestep, setup
1852d763cef2SBarry Smith 
1853d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1854d763cef2SBarry Smith @*/
18557087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1856d763cef2SBarry Smith {
1857dfbe8321SBarry Smith   PetscErrorCode ierr;
18586c6b9e74SPeter Brune   DM             dm;
18596c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1860d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
18616c6b9e74SPeter Brune   TSIJacobian    ijac;
18626c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1863d763cef2SBarry Smith 
1864d763cef2SBarry Smith   PetscFunctionBegin;
18650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1866277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1867277b19d0SLisandro Dalcin 
18687adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
18699596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1870d763cef2SBarry Smith   }
1871277b19d0SLisandro Dalcin 
1872277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1873277b19d0SLisandro Dalcin 
1874d4aa0a58SBarry Smith   if (ts->reverse_mode && !ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetSensitivity() first");
1875c235aa8dSHong Zhang 
1876552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
18771c3436cfSJed Brown 
1878e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1879e1244c69SJed Brown     Mat Amat,Pmat;
1880e1244c69SJed Brown     SNES snes;
1881e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1882e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1883e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1884e1244c69SJed Brown      * have displaced the RHS matrix */
1885e1244c69SJed Brown     if (Amat == ts->Arhs) {
1886e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1887e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1888e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1889e1244c69SJed Brown     }
1890e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1891e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1892e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1893e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1894e1244c69SJed Brown     }
1895e1244c69SJed Brown   }
1896277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1897000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1898277b19d0SLisandro Dalcin   }
1899277b19d0SLisandro Dalcin 
19006c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
19016c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
19026c6b9e74SPeter Brune    */
19036c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
19040298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
19056c6b9e74SPeter Brune   if (!func) {
19066c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
19076c6b9e74SPeter Brune   }
19086c6b9e74SPeter 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.
19096c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
19106c6b9e74SPeter Brune    */
19110298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
19120298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
19130298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
19146c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
19156c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
19166c6b9e74SPeter Brune   }
1917277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1918277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1919277b19d0SLisandro Dalcin }
1920277b19d0SLisandro Dalcin 
1921277b19d0SLisandro Dalcin #undef __FUNCT__
1922*f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
1923*f6a906c0SBarry Smith /*@
1924*f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
1925*f6a906c0SBarry Smith    of an adjoint solver
1926*f6a906c0SBarry Smith 
1927*f6a906c0SBarry Smith    Collective on TS
1928*f6a906c0SBarry Smith 
1929*f6a906c0SBarry Smith    Input Parameter:
1930*f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
1931*f6a906c0SBarry Smith 
1932*f6a906c0SBarry Smith    Notes:
1933*f6a906c0SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1934*f6a906c0SBarry Smith    TSSetUp(), since these actions will automatically occur during
1935*f6a906c0SBarry Smith    the call to TSStep().  However, if one wishes to control this
1936*f6a906c0SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1937*f6a906c0SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1938*f6a906c0SBarry Smith 
1939*f6a906c0SBarry Smith    Level: advanced
1940*f6a906c0SBarry Smith 
1941*f6a906c0SBarry Smith .keywords: TS, timestep, setup
1942*f6a906c0SBarry Smith 
1943*f6a906c0SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1944*f6a906c0SBarry Smith @*/
1945*f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
1946*f6a906c0SBarry Smith {
1947*f6a906c0SBarry Smith   PetscErrorCode ierr;
1948*f6a906c0SBarry Smith 
1949*f6a906c0SBarry Smith   PetscFunctionBegin;
1950*f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1951*f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
1952*f6a906c0SBarry Smith   if (ts->ops->setupadj) {
1953*f6a906c0SBarry Smith     ierr = (*ts->ops->setupadj)(ts);CHKERRQ(ierr);
1954*f6a906c0SBarry Smith   }
1955*f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
1956*f6a906c0SBarry Smith   PetscFunctionReturn(0);
1957*f6a906c0SBarry Smith }
1958*f6a906c0SBarry Smith 
1959*f6a906c0SBarry Smith #undef __FUNCT__
1960277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1961277b19d0SLisandro Dalcin /*@
1962277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1963277b19d0SLisandro Dalcin 
1964277b19d0SLisandro Dalcin    Collective on TS
1965277b19d0SLisandro Dalcin 
1966277b19d0SLisandro Dalcin    Input Parameter:
1967277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1968277b19d0SLisandro Dalcin 
1969277b19d0SLisandro Dalcin    Level: beginner
1970277b19d0SLisandro Dalcin 
1971277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1972277b19d0SLisandro Dalcin 
1973277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1974277b19d0SLisandro Dalcin @*/
1975277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1976277b19d0SLisandro Dalcin {
1977277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1978277b19d0SLisandro Dalcin 
1979277b19d0SLisandro Dalcin   PetscFunctionBegin;
1980277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1981b18ea86cSHong Zhang 
1982277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1983277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1984277b19d0SLisandro Dalcin   }
1985277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1986bbd56ea5SKarl Rupp 
19874e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
19884e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1989214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
19906bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1991e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1992e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
199338637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1994f8fee759SHong Zhang   ts->vecs_sensi = 0;
1995f8fee759SHong Zhang   ts->vecs_sensip = 0;
1996ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
199705755b9cSHong Zhang   ierr = VecDestroy(&ts->vec_costquad);CHKERRQ(ierr);
199836eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
1999277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2000d763cef2SBarry Smith   PetscFunctionReturn(0);
2001d763cef2SBarry Smith }
2002d763cef2SBarry Smith 
20034a2ae208SSatish Balay #undef __FUNCT__
20044a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
2005d8e5e3e6SSatish Balay /*@
2006d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2007d763cef2SBarry Smith    with TSCreate().
2008d763cef2SBarry Smith 
2009d763cef2SBarry Smith    Collective on TS
2010d763cef2SBarry Smith 
2011d763cef2SBarry Smith    Input Parameter:
2012d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2013d763cef2SBarry Smith 
2014d763cef2SBarry Smith    Level: beginner
2015d763cef2SBarry Smith 
2016d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2017d763cef2SBarry Smith 
2018d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2019d763cef2SBarry Smith @*/
20206bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2021d763cef2SBarry Smith {
20226849ba73SBarry Smith   PetscErrorCode ierr;
2023d763cef2SBarry Smith 
2024d763cef2SBarry Smith   PetscFunctionBegin;
20256bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
20266bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
20276bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2028d763cef2SBarry Smith 
20296bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2030277b19d0SLisandro Dalcin 
2031e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2032e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
20336bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
20346d4c513bSLisandro Dalcin 
203584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
2036aeb4809dSShri Abhyankar   if ((*ts)->event) {
2037aeb4809dSShri Abhyankar     ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr);
2038aeb4809dSShri Abhyankar   }
20396bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
20406bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
20416bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
20426d4c513bSLisandro Dalcin 
2043a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2044d763cef2SBarry Smith   PetscFunctionReturn(0);
2045d763cef2SBarry Smith }
2046d763cef2SBarry Smith 
20474a2ae208SSatish Balay #undef __FUNCT__
20484a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2049d8e5e3e6SSatish Balay /*@
2050d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2051d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2052d763cef2SBarry Smith 
2053d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2054d763cef2SBarry Smith 
2055d763cef2SBarry Smith    Input Parameter:
2056d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2057d763cef2SBarry Smith 
2058d763cef2SBarry Smith    Output Parameter:
2059d763cef2SBarry Smith .  snes - the nonlinear solver context
2060d763cef2SBarry Smith 
2061d763cef2SBarry Smith    Notes:
2062d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2063d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
206494b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2065d763cef2SBarry Smith 
2066d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
20670298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2068d763cef2SBarry Smith 
2069d763cef2SBarry Smith    Level: beginner
2070d763cef2SBarry Smith 
2071d763cef2SBarry Smith .keywords: timestep, get, SNES
2072d763cef2SBarry Smith @*/
20737087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2074d763cef2SBarry Smith {
2075d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2076d372ba47SLisandro Dalcin 
2077d763cef2SBarry Smith   PetscFunctionBegin;
20780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20794482741eSBarry Smith   PetscValidPointer(snes,2);
2080d372ba47SLisandro Dalcin   if (!ts->snes) {
2081ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
20820298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20833bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2084d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2085496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
20869e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
20879e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
20889e2a6581SJed Brown     }
2089d372ba47SLisandro Dalcin   }
2090d763cef2SBarry Smith   *snes = ts->snes;
2091d763cef2SBarry Smith   PetscFunctionReturn(0);
2092d763cef2SBarry Smith }
2093d763cef2SBarry Smith 
20944a2ae208SSatish Balay #undef __FUNCT__
2095deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2096deb2cd25SJed Brown /*@
2097deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2098deb2cd25SJed Brown 
2099deb2cd25SJed Brown    Collective
2100deb2cd25SJed Brown 
2101deb2cd25SJed Brown    Input Parameter:
2102deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2103deb2cd25SJed Brown -  snes - the nonlinear solver context
2104deb2cd25SJed Brown 
2105deb2cd25SJed Brown    Notes:
2106deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2107deb2cd25SJed Brown 
2108deb2cd25SJed Brown    Level: developer
2109deb2cd25SJed Brown 
2110deb2cd25SJed Brown .keywords: timestep, set, SNES
2111deb2cd25SJed Brown @*/
2112deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2113deb2cd25SJed Brown {
2114deb2cd25SJed Brown   PetscErrorCode ierr;
2115d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2116deb2cd25SJed Brown 
2117deb2cd25SJed Brown   PetscFunctionBegin;
2118deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2119deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2120deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2121deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2122bbd56ea5SKarl Rupp 
2123deb2cd25SJed Brown   ts->snes = snes;
2124bbd56ea5SKarl Rupp 
21250298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
21260298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2127740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
21280298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2129740132f1SEmil Constantinescu   }
2130deb2cd25SJed Brown   PetscFunctionReturn(0);
2131deb2cd25SJed Brown }
2132deb2cd25SJed Brown 
2133deb2cd25SJed Brown #undef __FUNCT__
213494b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2135d8e5e3e6SSatish Balay /*@
213694b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2137d763cef2SBarry Smith    a TS (timestepper) context.
2138d763cef2SBarry Smith 
213994b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2140d763cef2SBarry Smith 
2141d763cef2SBarry Smith    Input Parameter:
2142d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2143d763cef2SBarry Smith 
2144d763cef2SBarry Smith    Output Parameter:
214594b7f48cSBarry Smith .  ksp - the nonlinear solver context
2146d763cef2SBarry Smith 
2147d763cef2SBarry Smith    Notes:
214894b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2149d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2150d763cef2SBarry Smith    KSP and PC contexts as well.
2151d763cef2SBarry Smith 
215294b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
21530298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2154d763cef2SBarry Smith 
2155d763cef2SBarry Smith    Level: beginner
2156d763cef2SBarry Smith 
215794b7f48cSBarry Smith .keywords: timestep, get, KSP
2158d763cef2SBarry Smith @*/
21597087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2160d763cef2SBarry Smith {
2161d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2162089b2837SJed Brown   SNES           snes;
2163d372ba47SLisandro Dalcin 
2164d763cef2SBarry Smith   PetscFunctionBegin;
21650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21664482741eSBarry Smith   PetscValidPointer(ksp,2);
216717186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2168e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2169089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2170089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2171d763cef2SBarry Smith   PetscFunctionReturn(0);
2172d763cef2SBarry Smith }
2173d763cef2SBarry Smith 
2174d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2175d763cef2SBarry Smith 
21764a2ae208SSatish Balay #undef __FUNCT__
2177adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2178adb62b0dSMatthew Knepley /*@
2179adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2180adb62b0dSMatthew Knepley    maximum time for iteration.
2181adb62b0dSMatthew Knepley 
21823f9fe445SBarry Smith    Not Collective
2183adb62b0dSMatthew Knepley 
2184adb62b0dSMatthew Knepley    Input Parameters:
2185adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
21860298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
21870298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2188adb62b0dSMatthew Knepley 
2189adb62b0dSMatthew Knepley    Level: intermediate
2190adb62b0dSMatthew Knepley 
2191adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2192adb62b0dSMatthew Knepley @*/
21937087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2194adb62b0dSMatthew Knepley {
2195adb62b0dSMatthew Knepley   PetscFunctionBegin;
21960700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2197abc0a331SBarry Smith   if (maxsteps) {
21984482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2199adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2200adb62b0dSMatthew Knepley   }
2201abc0a331SBarry Smith   if (maxtime) {
22024482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2203adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2204adb62b0dSMatthew Knepley   }
2205adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2206adb62b0dSMatthew Knepley }
2207adb62b0dSMatthew Knepley 
2208adb62b0dSMatthew Knepley #undef __FUNCT__
22094a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2210d763cef2SBarry Smith /*@
2211d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2212d763cef2SBarry Smith    maximum time for iteration.
2213d763cef2SBarry Smith 
22143f9fe445SBarry Smith    Logically Collective on TS
2215d763cef2SBarry Smith 
2216d763cef2SBarry Smith    Input Parameters:
2217d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2218d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2219d763cef2SBarry Smith -  maxtime - final time to iterate to
2220d763cef2SBarry Smith 
2221d763cef2SBarry Smith    Options Database Keys:
2222d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
22233bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2224d763cef2SBarry Smith 
2225d763cef2SBarry Smith    Notes:
2226d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2227d763cef2SBarry Smith 
2228d763cef2SBarry Smith    Level: intermediate
2229d763cef2SBarry Smith 
2230d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2231a43b19c4SJed Brown 
2232a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2233d763cef2SBarry Smith @*/
22347087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2235d763cef2SBarry Smith {
2236d763cef2SBarry Smith   PetscFunctionBegin;
22370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2238c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2239c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
224039b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
224139b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2242d763cef2SBarry Smith   PetscFunctionReturn(0);
2243d763cef2SBarry Smith }
2244d763cef2SBarry Smith 
22454a2ae208SSatish Balay #undef __FUNCT__
22464a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2247d763cef2SBarry Smith /*@
2248d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2249d763cef2SBarry Smith    for use by the TS routines.
2250d763cef2SBarry Smith 
22513f9fe445SBarry Smith    Logically Collective on TS and Vec
2252d763cef2SBarry Smith 
2253d763cef2SBarry Smith    Input Parameters:
2254d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
22550910c330SBarry Smith -  u - the solution vector
2256d763cef2SBarry Smith 
2257d763cef2SBarry Smith    Level: beginner
2258d763cef2SBarry Smith 
2259d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2260d763cef2SBarry Smith @*/
22610910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2262d763cef2SBarry Smith {
22638737fe31SLisandro Dalcin   PetscErrorCode ierr;
2264496e6a7aSJed Brown   DM             dm;
22658737fe31SLisandro Dalcin 
2266d763cef2SBarry Smith   PetscFunctionBegin;
22670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22680910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
22690910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
22706bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2271bbd56ea5SKarl Rupp 
22720910c330SBarry Smith   ts->vec_sol = u;
2273bbd56ea5SKarl Rupp 
2274496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
22750910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2276d763cef2SBarry Smith   PetscFunctionReturn(0);
2277d763cef2SBarry Smith }
2278d763cef2SBarry Smith 
2279e74ef692SMatthew Knepley #undef __FUNCT__
2280d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetSensitivity"
2281c235aa8dSHong Zhang /*@
2282d4aa0a58SBarry Smith    TSAdjointSetSensitivity - Sets the initial value of sensitivity (w.r.t. initial conditions)
2283c235aa8dSHong Zhang    for use by the TS routines.
2284c235aa8dSHong Zhang 
2285c235aa8dSHong Zhang    Logically Collective on TS and Vec
2286c235aa8dSHong Zhang 
2287c235aa8dSHong Zhang    Input Parameters:
2288c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2289c235aa8dSHong Zhang -  u - the solution vector
2290c235aa8dSHong Zhang 
2291c235aa8dSHong Zhang    Level: beginner
2292c235aa8dSHong Zhang 
2293c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2294c235aa8dSHong Zhang @*/
2295d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetSensitivity(TS ts,Vec *u,PetscInt numberadjs)
2296c235aa8dSHong Zhang {
2297c235aa8dSHong Zhang   PetscFunctionBegin;
2298c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2299b18ea86cSHong Zhang   PetscValidPointer(u,2);
2300b18ea86cSHong Zhang   ts->vecs_sensi = u;
2301d4aa0a58SBarry Smith   if(ts->numberadjs && ts->numberadjs!=numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of adjoint variables (3rd parameter) is inconsistent with the one set by TSAdjointSetSensitivityP()");
2302b18ea86cSHong Zhang   ts->numberadjs = numberadjs;
2303c235aa8dSHong Zhang 
2304ad8e2604SHong Zhang   PetscFunctionReturn(0);
2305ad8e2604SHong Zhang }
2306ad8e2604SHong Zhang 
2307ad8e2604SHong Zhang #undef __FUNCT__
2308d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetSensitivityP"
2309ad8e2604SHong Zhang /*@
2310d4aa0a58SBarry Smith    TSAdjointSetSensitivityP - Sets the initial value of sensitivity (w.r.t. parameters)
2311ad8e2604SHong Zhang    for use by the TS routines.
2312ad8e2604SHong Zhang 
2313ad8e2604SHong Zhang    Logically Collective on TS and Vec
2314ad8e2604SHong Zhang 
2315ad8e2604SHong Zhang    Input Parameters:
2316ad8e2604SHong Zhang +  ts - the TS context obtained from TSCreate()
2317ad8e2604SHong Zhang -  u - the solution vector
2318ad8e2604SHong Zhang 
2319ad8e2604SHong Zhang    Level: beginner
2320ad8e2604SHong Zhang 
2321ad8e2604SHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2322ad8e2604SHong Zhang @*/
2323d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetSensitivityP(TS ts,Vec *u,PetscInt numberadjs)
2324ad8e2604SHong Zhang {
2325ad8e2604SHong Zhang   PetscFunctionBegin;
2326ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2327ad8e2604SHong Zhang   PetscValidPointer(u,2);
2328ad8e2604SHong Zhang   ts->vecs_sensip = u;
2329d4aa0a58SBarry Smith   if(ts->numberadjs && ts->numberadjs!=numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of adjoint variables (3rd parameter) is inconsistent with the one set by TSAdjointSetSensitivity()");
2330ad8e2604SHong Zhang   ts->numberadjs = numberadjs;
2331ad8e2604SHong Zhang 
2332ad8e2604SHong Zhang   PetscFunctionReturn(0);
2333ad8e2604SHong Zhang }
2334ad8e2604SHong Zhang 
2335ad8e2604SHong Zhang #undef __FUNCT__
2336d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobianP"
2337ad8e2604SHong Zhang /*@C
2338d4aa0a58SBarry Smith   TSAdjointSetRHSJacobianP - Sets the function that computes the Jacobian w.r.t. parameters.
2339ad8e2604SHong Zhang 
2340ad8e2604SHong Zhang   Logically Collective on TS
2341ad8e2604SHong Zhang 
2342ad8e2604SHong Zhang   Input Parameters:
2343ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2344ad8e2604SHong Zhang - func - The function
2345ad8e2604SHong Zhang 
2346ad8e2604SHong Zhang   Calling sequence of func:
234705755b9cSHong Zhang $ func (TS ts,PetscReal t,Vec u,Mat A,void *ctx);
234805755b9cSHong Zhang +   t - current timestep
234905755b9cSHong Zhang .   u - input vector
235005755b9cSHong Zhang .   A - output matrix
235105755b9cSHong Zhang -   ctx - [optional] user-defined function context
2352ad8e2604SHong Zhang 
2353ad8e2604SHong Zhang   Level: intermediate
2354ad8e2604SHong Zhang 
2355ad8e2604SHong Zhang .keywords: TS, sensitivity
2356ad8e2604SHong Zhang .seealso:
2357ad8e2604SHong Zhang @*/
2358d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobianP(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2359ad8e2604SHong Zhang {
2360ad8e2604SHong Zhang   PetscErrorCode ierr;
2361ad8e2604SHong Zhang 
2362ad8e2604SHong Zhang   PetscFunctionBegin;
2363ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2364ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2365ad8e2604SHong Zhang 
2366ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2367ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2368ad8e2604SHong Zhang   if(Amat) {
2369ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2370ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2371ad8e2604SHong Zhang 
2372ad8e2604SHong Zhang     ts->Jacp = Amat;
2373ad8e2604SHong Zhang   }
2374ad8e2604SHong Zhang   PetscFunctionReturn(0);
2375ad8e2604SHong Zhang }
2376ad8e2604SHong Zhang 
2377ad8e2604SHong Zhang #undef __FUNCT__
2378d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobianP"
2379ad8e2604SHong Zhang /*@
2380d4aa0a58SBarry Smith   TSAdjointComputeRHSJacobianP - Runs the user-defined JacobianP function.
2381ad8e2604SHong Zhang 
2382ad8e2604SHong Zhang   Collective on TS
2383ad8e2604SHong Zhang 
2384ad8e2604SHong Zhang   Input Parameters:
2385ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2386ad8e2604SHong Zhang 
2387ad8e2604SHong Zhang   Level: developer
2388ad8e2604SHong Zhang 
2389ad8e2604SHong Zhang .keywords: TS, sensitivity
2390d4aa0a58SBarry Smith .seealso: TSAdjointSetRHSJacobianP()
2391ad8e2604SHong Zhang @*/
2392d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobianP(TS ts,PetscReal t,Vec X,Mat Amat)
2393ad8e2604SHong Zhang {
2394ad8e2604SHong Zhang   PetscErrorCode ierr;
2395ad8e2604SHong Zhang 
2396ad8e2604SHong Zhang   PetscFunctionBegin;
2397ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2398ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2399ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2400ad8e2604SHong Zhang 
2401ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2402ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2403ad8e2604SHong Zhang   PetscStackPop;
2404ad8e2604SHong Zhang 
2405c235aa8dSHong Zhang   PetscFunctionReturn(0);
2406c235aa8dSHong Zhang }
2407c235aa8dSHong Zhang 
2408c235aa8dSHong Zhang #undef __FUNCT__
2409d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetCostIntegrand"
24106fd0887fSHong Zhang /*@C
2411d4aa0a58SBarry Smith     TSAdjointSetCostIntegrand - Sets the routine for evaluating the quadrature (or integral) term in a cost function,
24126fd0887fSHong Zhang     where Q_t = r(t,u).
24136fd0887fSHong Zhang 
24146fd0887fSHong Zhang     Logically Collective on TS
24156fd0887fSHong Zhang 
24166fd0887fSHong Zhang     Input Parameters:
24176fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
241836eaed60SHong Zhang .   q -  vector to put the computed quadrature term in the cost function (or NULL to have it created)
24196fd0887fSHong Zhang .   fq - routine for evaluating the right-hand-side function
24206fd0887fSHong Zhang -   ctx - [optional] user-defined context for private data for the
24216fd0887fSHong Zhang           function evaluation routine (may be NULL)
24226fd0887fSHong Zhang 
24236fd0887fSHong Zhang     Calling sequence of func:
2424a2ae3dd2SHong Zhang $     TSCostIntegrand(TS ts,PetscReal t,Vec u,PetscReal *f,void *ctx);
24256fd0887fSHong Zhang 
24266fd0887fSHong Zhang +   t - current timestep
24276fd0887fSHong Zhang .   u - input vector
242836eaed60SHong Zhang .   f - function vector
24296fd0887fSHong Zhang -   ctx - [optional] user-defined function context
24306fd0887fSHong Zhang 
24316fd0887fSHong Zhang     Level: beginner
24326fd0887fSHong Zhang 
24336fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
24346fd0887fSHong Zhang 
2435d4aa0a58SBarry Smith .seealso: TSAdjointSetRHSJacobianP(),TSAdjointSetSensitivity(),TSAdjointSetSensitivityP()
24366fd0887fSHong Zhang @*/
2437d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetCostIntegrand(TS ts,PetscInt numberadjs,Vec q,PetscErrorCode (*fq)(TS,PetscReal,Vec,Vec,void*),void *ctx)
24386fd0887fSHong Zhang {
24396fd0887fSHong Zhang   PetscErrorCode ierr;
244036eaed60SHong Zhang   PetscInt size;
24416fd0887fSHong Zhang 
24426fd0887fSHong Zhang   PetscFunctionBegin;
24436fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
244436eaed60SHong Zhang   if (q) {
244536eaed60SHong Zhang     PetscValidHeaderSpecific(q,VEC_CLASSID,2);
244636eaed60SHong Zhang   } else {
2447d4aa0a58SBarry Smith     SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"TSAdjointSetCostIntegrand() requires a vector of size numberajds to hold the value of integrals as 3rd input parameter");
24486fd0887fSHong Zhang   }
2449d4aa0a58SBarry Smith   if (!ts->numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Call TSAdjointSetSensitivity() or TSAdjointSetSensitivityP() first so that the number of cost functions can be determined.");
2450d4aa0a58SBarry 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 TSAdjointSetSensitivity() or TSAdjointSetSensitivityP()");
245136eaed60SHong Zhang   ierr = VecGetSize(q,&size);CHKERRQ(ierr);
2452a2ae3dd2SHong Zhang   ierr = VecZeroEntries(q);CHKERRQ(ierr);
2453d4aa0a58SBarry Smith   if (size!=numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions is inconsistent with the number of integrals (size of the 3rd input vector of TSAdjointSetCostIntegrand()).");
24546fd0887fSHong Zhang 
245536eaed60SHong Zhang   ierr = PetscObjectReference((PetscObject)q);CHKERRQ(ierr);
245636eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costquad);CHKERRQ(ierr);
245736eaed60SHong Zhang   ts->vec_costquad = q;
245836eaed60SHong Zhang 
2459a2ae3dd2SHong Zhang   ierr                  = VecDuplicate(ts->vec_costquad,&ts->vec_costintegrand);CHKERRQ(ierr);
2460e4680132SHong Zhang   ts->costintegrand     = fq;
246105755b9cSHong Zhang   ts->costintegrandctx  = ctx;
24626fd0887fSHong Zhang 
24636fd0887fSHong Zhang   PetscFunctionReturn(0);
24646fd0887fSHong Zhang }
24656fd0887fSHong Zhang 
24666fd0887fSHong Zhang #undef __FUNCT__
2467d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointGetCostQuadrature"
246836eaed60SHong Zhang /*@
2469d4aa0a58SBarry Smith    TSAdjointGetCostQuadrature - Returns the values of the quadrature (or integral) terms in a cost function.
247036eaed60SHong Zhang    It is valid to call the routine after a backward run.
247136eaed60SHong Zhang 
247236eaed60SHong Zhang    Not Collective
247336eaed60SHong Zhang 
247436eaed60SHong Zhang    Input Parameter:
247536eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
247636eaed60SHong Zhang 
247736eaed60SHong Zhang    Output Parameter:
247836eaed60SHong Zhang .  v - the vector containing the solution
247936eaed60SHong Zhang 
248036eaed60SHong Zhang    Level: intermediate
248136eaed60SHong Zhang 
2482d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
248336eaed60SHong Zhang 
248436eaed60SHong Zhang .keywords: TS, sensitivity analysis
248536eaed60SHong Zhang @*/
2486d4aa0a58SBarry Smith PetscErrorCode  TSAdjointGetCostQuadrature(TS ts,Vec *v)
248736eaed60SHong Zhang {
248836eaed60SHong Zhang   PetscFunctionBegin;
248936eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
249036eaed60SHong Zhang   PetscValidPointer(v,2);
249136eaed60SHong Zhang   *v = ts->vec_costquad;
249236eaed60SHong Zhang   PetscFunctionReturn(0);
249336eaed60SHong Zhang }
249436eaed60SHong Zhang 
249536eaed60SHong Zhang #undef __FUNCT__
2496d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
24976fd0887fSHong Zhang /*@
2498d4aa0a58SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the quadrature function in the cost functions.
24996fd0887fSHong Zhang 
25006fd0887fSHong Zhang    Input Parameters:
25016fd0887fSHong Zhang +  ts - the TS context
25026fd0887fSHong Zhang .  t - current time
250305755b9cSHong Zhang -  U - state vector
25046fd0887fSHong Zhang 
25056fd0887fSHong Zhang    Output Parameter:
25066fd0887fSHong Zhang .  q - vector of size numberadjs to hold the outputs
25076fd0887fSHong Zhang 
25086fd0887fSHong Zhang    Note:
25096fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
25106fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
25116fd0887fSHong Zhang 
25126fd0887fSHong Zhang    Level: developer
25136fd0887fSHong Zhang 
25146fd0887fSHong Zhang .keywords: TS, compute
25156fd0887fSHong Zhang 
2516d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
25176fd0887fSHong Zhang @*/
2518d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec U,Vec q)
25196fd0887fSHong Zhang {
25206fd0887fSHong Zhang   PetscErrorCode ierr;
25216fd0887fSHong Zhang 
25226fd0887fSHong Zhang   PetscFunctionBegin;
25236fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25246fd0887fSHong Zhang   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
25256fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
25266fd0887fSHong Zhang 
25276fd0887fSHong Zhang   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr);
2528e4680132SHong Zhang   if (ts->costintegrand) {
2529e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
253005755b9cSHong Zhang     ierr = (*ts->costintegrand)(ts,t,U,q,ts->costintegrandctx);CHKERRQ(ierr);
25316fd0887fSHong Zhang     PetscStackPop;
25326fd0887fSHong Zhang   } else {
25336fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
25346fd0887fSHong Zhang   }
25356fd0887fSHong Zhang 
25366fd0887fSHong Zhang   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr);
25376fd0887fSHong Zhang   PetscFunctionReturn(0);
25386fd0887fSHong Zhang }
25396fd0887fSHong Zhang 
25406fd0887fSHong Zhang #undef __FUNCT__
2541d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetDRDYFunction"
254205755b9cSHong Zhang /*@C
2543d4aa0a58SBarry Smith   TSAdjointSetDRDYFunction - Sets the function that computes the gradient of the CostIntegrand function r w.r.t. states y.
254405755b9cSHong Zhang 
254505755b9cSHong Zhang   Logically Collective on TS
254605755b9cSHong Zhang 
254705755b9cSHong Zhang   Input Parameters:
254805755b9cSHong Zhang + ts   - The TS context obtained from TSCreate()
254905755b9cSHong Zhang - func - The function
255005755b9cSHong Zhang 
255105755b9cSHong Zhang   Calling sequence of func:
255207658559SBarry Smith . PetscErroCode func(TS ts,PetscReal t,Vec U,Vec *drdy,void *ctx);
255305755b9cSHong Zhang 
255405755b9cSHong Zhang   Level: intermediate
255505755b9cSHong Zhang 
255605755b9cSHong Zhang .keywords: TS, sensitivity
255705755b9cSHong Zhang .seealso:
255805755b9cSHong Zhang @*/
2559d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetDRDYFunction(TS ts,Vec *drdy,PetscErrorCode (*func)(TS,PetscReal,Vec,Vec*,void*),void *ctx)
256005755b9cSHong Zhang {
256105755b9cSHong Zhang   PetscFunctionBegin;
256205755b9cSHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
256305755b9cSHong Zhang 
256405755b9cSHong Zhang   ts->drdyfunction    = func;
256505755b9cSHong Zhang   ts->drdyfunctionctx = ctx;
256605755b9cSHong Zhang   ts->vecs_drdy       = drdy;
256705755b9cSHong Zhang   PetscFunctionReturn(0);
256805755b9cSHong Zhang }
256905755b9cSHong Zhang 
257005755b9cSHong Zhang #undef __FUNCT__
2571d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
257205755b9cSHong Zhang /*@
2573d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
257405755b9cSHong Zhang 
257505755b9cSHong Zhang   Collective on TS
257605755b9cSHong Zhang 
257705755b9cSHong Zhang   Input Parameters:
257805755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
257905755b9cSHong Zhang 
258005755b9cSHong Zhang   Notes:
2581d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
258205755b9cSHong Zhang   so most users would not generally call this routine themselves.
258305755b9cSHong Zhang 
258405755b9cSHong Zhang   Level: developer
258505755b9cSHong Zhang 
258605755b9cSHong Zhang .keywords: TS, sensitivity
2587d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
258805755b9cSHong Zhang @*/
2589d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec X,Vec *drdy)
259005755b9cSHong Zhang {
259105755b9cSHong Zhang   PetscErrorCode ierr;
259205755b9cSHong Zhang 
259305755b9cSHong Zhang   PetscFunctionBegin;
259405755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
259505755b9cSHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
259605755b9cSHong Zhang 
259705755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
259805755b9cSHong Zhang   ierr = (*ts->drdyfunction)(ts,t,X,drdy,ts->drdyfunctionctx); CHKERRQ(ierr);
259905755b9cSHong Zhang   PetscStackPop;
260005755b9cSHong Zhang   PetscFunctionReturn(0);
260105755b9cSHong Zhang }
260205755b9cSHong Zhang 
260305755b9cSHong Zhang #undef __FUNCT__
2604d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetDRDPFunction"
260505755b9cSHong Zhang /*@C
2606d4aa0a58SBarry Smith   TSAdjointSetDRDPFunction - Sets the function that computes the gradient of the CostIntegrand function w.r.t. parameters.
260705755b9cSHong Zhang 
260805755b9cSHong Zhang   Logically Collective on TS
260905755b9cSHong Zhang 
261005755b9cSHong Zhang   Input Parameters:
261105755b9cSHong Zhang + ts   - The TS context obtained from TSCreate()
261205755b9cSHong Zhang - func - The function
261305755b9cSHong Zhang 
261405755b9cSHong Zhang   Calling sequence of func:
261507658559SBarry Smith . func(TS ts,PetscReal t,Vec U,Vec *drdy,void *ctx);
261605755b9cSHong Zhang 
261705755b9cSHong Zhang   Level: intermediate
261805755b9cSHong Zhang 
261905755b9cSHong Zhang .keywords: TS, sensitivity
262005755b9cSHong Zhang .seealso:
262105755b9cSHong Zhang @*/
2622d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetDRDPFunction(TS ts,Vec *drdp,PetscErrorCode (*func)(TS,PetscReal,Vec,Vec*,void*),void *ctx)
262305755b9cSHong Zhang {
262405755b9cSHong Zhang   PetscFunctionBegin;
262505755b9cSHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
262605755b9cSHong Zhang 
262705755b9cSHong Zhang   ts->drdpfunction    = func;
262805755b9cSHong Zhang   ts->drdpfunctionctx = ctx;
262905755b9cSHong Zhang   ts->vecs_drdp       = drdp;
263005755b9cSHong Zhang 
263105755b9cSHong Zhang   PetscFunctionReturn(0);
263205755b9cSHong Zhang }
263305755b9cSHong Zhang 
263405755b9cSHong Zhang #undef __FUNCT__
2635d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
263605755b9cSHong Zhang /*@
2637d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
263805755b9cSHong Zhang 
263905755b9cSHong Zhang   Collective on TS
264005755b9cSHong Zhang 
264105755b9cSHong Zhang   Input Parameters:
264205755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
264305755b9cSHong Zhang 
264405755b9cSHong Zhang   Notes:
264505755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
264605755b9cSHong Zhang   so most users would not generally call this routine themselves.
264705755b9cSHong Zhang 
264805755b9cSHong Zhang   Level: developer
264905755b9cSHong Zhang 
265005755b9cSHong Zhang .keywords: TS, sensitivity
2651d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
265205755b9cSHong Zhang @*/
2653d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec X,Vec *drdp)
265405755b9cSHong Zhang {
265505755b9cSHong Zhang   PetscErrorCode ierr;
265605755b9cSHong Zhang 
265705755b9cSHong Zhang   PetscFunctionBegin;
265805755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
265905755b9cSHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
266005755b9cSHong Zhang 
266105755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
266205755b9cSHong Zhang   ierr = (*ts->drdpfunction)(ts,t,X,drdp,ts->drdpfunctionctx); CHKERRQ(ierr);
266305755b9cSHong Zhang   PetscStackPop;
266405755b9cSHong Zhang 
266505755b9cSHong Zhang   PetscFunctionReturn(0);
266605755b9cSHong Zhang }
266705755b9cSHong Zhang 
266805755b9cSHong Zhang #undef __FUNCT__
2669e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2670ac226902SBarry Smith /*@C
2671000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
26723f2090d5SJed Brown   called once at the beginning of each time step.
2673000e7ae3SMatthew Knepley 
26743f9fe445SBarry Smith   Logically Collective on TS
2675000e7ae3SMatthew Knepley 
2676000e7ae3SMatthew Knepley   Input Parameters:
2677000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2678000e7ae3SMatthew Knepley - func - The function
2679000e7ae3SMatthew Knepley 
2680000e7ae3SMatthew Knepley   Calling sequence of func:
2681000e7ae3SMatthew Knepley . func (TS ts);
2682000e7ae3SMatthew Knepley 
2683000e7ae3SMatthew Knepley   Level: intermediate
2684000e7ae3SMatthew Knepley 
2685b8123daeSJed Brown   Note:
2686b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2687b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2688b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2689b8123daeSJed Brown 
2690000e7ae3SMatthew Knepley .keywords: TS, timestep
26919be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2692000e7ae3SMatthew Knepley @*/
26937087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2694000e7ae3SMatthew Knepley {
2695000e7ae3SMatthew Knepley   PetscFunctionBegin;
26960700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2697ae60f76fSBarry Smith   ts->prestep = func;
2698000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2699000e7ae3SMatthew Knepley }
2700000e7ae3SMatthew Knepley 
2701e74ef692SMatthew Knepley #undef __FUNCT__
27023f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
270309ee8438SJed Brown /*@
27043f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
27053f2090d5SJed Brown 
27063f2090d5SJed Brown   Collective on TS
27073f2090d5SJed Brown 
27083f2090d5SJed Brown   Input Parameters:
27093f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
27103f2090d5SJed Brown 
27113f2090d5SJed Brown   Notes:
27123f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
27133f2090d5SJed Brown   so most users would not generally call this routine themselves.
27143f2090d5SJed Brown 
27153f2090d5SJed Brown   Level: developer
27163f2090d5SJed Brown 
27173f2090d5SJed Brown .keywords: TS, timestep
27189be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
27193f2090d5SJed Brown @*/
27207087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
27213f2090d5SJed Brown {
27223f2090d5SJed Brown   PetscErrorCode ierr;
27233f2090d5SJed Brown 
27243f2090d5SJed Brown   PetscFunctionBegin;
27250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2726ae60f76fSBarry Smith   if (ts->prestep) {
2727ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2728312ce896SJed Brown   }
27293f2090d5SJed Brown   PetscFunctionReturn(0);
27303f2090d5SJed Brown }
27313f2090d5SJed Brown 
27323f2090d5SJed Brown #undef __FUNCT__
2733b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2734b8123daeSJed Brown /*@C
2735b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2736b8123daeSJed Brown   called once at the beginning of each stage.
2737b8123daeSJed Brown 
2738b8123daeSJed Brown   Logically Collective on TS
2739b8123daeSJed Brown 
2740b8123daeSJed Brown   Input Parameters:
2741b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2742b8123daeSJed Brown - func - The function
2743b8123daeSJed Brown 
2744b8123daeSJed Brown   Calling sequence of func:
2745b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2746b8123daeSJed Brown 
2747b8123daeSJed Brown   Level: intermediate
2748b8123daeSJed Brown 
2749b8123daeSJed Brown   Note:
2750b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2751b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2752b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2753b8123daeSJed Brown 
2754b8123daeSJed Brown .keywords: TS, timestep
27559be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2756b8123daeSJed Brown @*/
2757b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2758b8123daeSJed Brown {
2759b8123daeSJed Brown   PetscFunctionBegin;
2760b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2761ae60f76fSBarry Smith   ts->prestage = func;
2762b8123daeSJed Brown   PetscFunctionReturn(0);
2763b8123daeSJed Brown }
2764b8123daeSJed Brown 
2765b8123daeSJed Brown #undef __FUNCT__
27669be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
27679be3e283SDebojyoti Ghosh /*@C
27689be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
27699be3e283SDebojyoti Ghosh   called once at the end of each stage.
27709be3e283SDebojyoti Ghosh 
27719be3e283SDebojyoti Ghosh   Logically Collective on TS
27729be3e283SDebojyoti Ghosh 
27739be3e283SDebojyoti Ghosh   Input Parameters:
27749be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
27759be3e283SDebojyoti Ghosh - func - The function
27769be3e283SDebojyoti Ghosh 
27779be3e283SDebojyoti Ghosh   Calling sequence of func:
27789be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
27799be3e283SDebojyoti Ghosh 
27809be3e283SDebojyoti Ghosh   Level: intermediate
27819be3e283SDebojyoti Ghosh 
27829be3e283SDebojyoti Ghosh   Note:
27839be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
27849be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
27859be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
27869be3e283SDebojyoti Ghosh 
27879be3e283SDebojyoti Ghosh .keywords: TS, timestep
27889be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
27899be3e283SDebojyoti Ghosh @*/
27909be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
27919be3e283SDebojyoti Ghosh {
27929be3e283SDebojyoti Ghosh   PetscFunctionBegin;
27939be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
27949be3e283SDebojyoti Ghosh   ts->poststage = func;
27959be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
27969be3e283SDebojyoti Ghosh }
27979be3e283SDebojyoti Ghosh 
27989be3e283SDebojyoti Ghosh #undef __FUNCT__
2799b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2800b8123daeSJed Brown /*@
2801b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2802b8123daeSJed Brown 
2803b8123daeSJed Brown   Collective on TS
2804b8123daeSJed Brown 
2805b8123daeSJed Brown   Input Parameters:
2806b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
28079be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2808b8123daeSJed Brown 
2809b8123daeSJed Brown   Notes:
2810b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2811b8123daeSJed Brown   most users would not generally call this routine themselves.
2812b8123daeSJed Brown 
2813b8123daeSJed Brown   Level: developer
2814b8123daeSJed Brown 
2815b8123daeSJed Brown .keywords: TS, timestep
28169be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2817b8123daeSJed Brown @*/
2818b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2819b8123daeSJed Brown {
2820b8123daeSJed Brown   PetscErrorCode ierr;
2821b8123daeSJed Brown 
2822b8123daeSJed Brown   PetscFunctionBegin;
2823b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2824ae60f76fSBarry Smith   if (ts->prestage) {
2825ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2826b8123daeSJed Brown   }
2827b8123daeSJed Brown   PetscFunctionReturn(0);
2828b8123daeSJed Brown }
2829b8123daeSJed Brown 
2830b8123daeSJed Brown #undef __FUNCT__
28319be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
28329be3e283SDebojyoti Ghosh /*@
28339be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
28349be3e283SDebojyoti Ghosh 
28359be3e283SDebojyoti Ghosh   Collective on TS
28369be3e283SDebojyoti Ghosh 
28379be3e283SDebojyoti Ghosh   Input Parameters:
28389be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
28399be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
28409be3e283SDebojyoti Ghosh   stageindex  - Stage number
28419be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
28429be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
28439be3e283SDebojyoti Ghosh 
28449be3e283SDebojyoti Ghosh   Notes:
28459be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
28469be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
28479be3e283SDebojyoti Ghosh 
28489be3e283SDebojyoti Ghosh   Level: developer
28499be3e283SDebojyoti Ghosh 
28509be3e283SDebojyoti Ghosh .keywords: TS, timestep
28519be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
28529be3e283SDebojyoti Ghosh @*/
28539be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
28549be3e283SDebojyoti Ghosh {
28559be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
28569be3e283SDebojyoti Ghosh 
28579be3e283SDebojyoti Ghosh   PetscFunctionBegin;
28589be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28594beae5d8SLisandro Dalcin   if (ts->poststage) {
28609be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
28619be3e283SDebojyoti Ghosh   }
28629be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
28639be3e283SDebojyoti Ghosh }
28649be3e283SDebojyoti Ghosh 
28659be3e283SDebojyoti Ghosh #undef __FUNCT__
2866e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2867ac226902SBarry Smith /*@C
2868000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
28693f2090d5SJed Brown   called once at the end of each time step.
2870000e7ae3SMatthew Knepley 
28713f9fe445SBarry Smith   Logically Collective on TS
2872000e7ae3SMatthew Knepley 
2873000e7ae3SMatthew Knepley   Input Parameters:
2874000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2875000e7ae3SMatthew Knepley - func - The function
2876000e7ae3SMatthew Knepley 
2877000e7ae3SMatthew Knepley   Calling sequence of func:
2878b8123daeSJed Brown $ func (TS ts);
2879000e7ae3SMatthew Knepley 
2880000e7ae3SMatthew Knepley   Level: intermediate
2881000e7ae3SMatthew Knepley 
2882000e7ae3SMatthew Knepley .keywords: TS, timestep
2883b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2884000e7ae3SMatthew Knepley @*/
28857087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2886000e7ae3SMatthew Knepley {
2887000e7ae3SMatthew Knepley   PetscFunctionBegin;
28880700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2889ae60f76fSBarry Smith   ts->poststep = func;
2890000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2891000e7ae3SMatthew Knepley }
2892000e7ae3SMatthew Knepley 
2893e74ef692SMatthew Knepley #undef __FUNCT__
28943f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
289509ee8438SJed Brown /*@
28963f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
28973f2090d5SJed Brown 
28983f2090d5SJed Brown   Collective on TS
28993f2090d5SJed Brown 
29003f2090d5SJed Brown   Input Parameters:
29013f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
29023f2090d5SJed Brown 
29033f2090d5SJed Brown   Notes:
29043f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
29053f2090d5SJed Brown   so most users would not generally call this routine themselves.
29063f2090d5SJed Brown 
29073f2090d5SJed Brown   Level: developer
29083f2090d5SJed Brown 
29093f2090d5SJed Brown .keywords: TS, timestep
29103f2090d5SJed Brown @*/
29117087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
29123f2090d5SJed Brown {
29133f2090d5SJed Brown   PetscErrorCode ierr;
29143f2090d5SJed Brown 
29153f2090d5SJed Brown   PetscFunctionBegin;
29160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2917ae60f76fSBarry Smith   if (ts->poststep) {
2918ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
291972ac3e02SJed Brown   }
29203f2090d5SJed Brown   PetscFunctionReturn(0);
29213f2090d5SJed Brown }
29223f2090d5SJed Brown 
2923d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2924d763cef2SBarry Smith 
29254a2ae208SSatish Balay #undef __FUNCT__
2926a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2927d763cef2SBarry Smith /*@C
2928a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2929d763cef2SBarry Smith    timestep to display the iteration's  progress.
2930d763cef2SBarry Smith 
29313f9fe445SBarry Smith    Logically Collective on TS
2932d763cef2SBarry Smith 
2933d763cef2SBarry Smith    Input Parameters:
2934d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2935e213d8f1SJed Brown .  monitor - monitoring routine
2936329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
29370298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2938b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
29390298fd71SBarry Smith           (may be NULL)
2940d763cef2SBarry Smith 
2941e213d8f1SJed Brown    Calling sequence of monitor:
29420910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2943d763cef2SBarry Smith 
2944d763cef2SBarry Smith +    ts - the TS context
294588c05cc5SBarry 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
294688c05cc5SBarry Smith                                been interpolated to)
29471f06c33eSBarry Smith .    time - current time
29480910c330SBarry Smith .    u - current iterate
2949d763cef2SBarry Smith -    mctx - [optional] monitoring context
2950d763cef2SBarry Smith 
2951d763cef2SBarry Smith    Notes:
2952d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2953d763cef2SBarry Smith    already has been loaded.
2954d763cef2SBarry Smith 
2955025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2956025f1a04SBarry Smith 
2957d763cef2SBarry Smith    Level: intermediate
2958d763cef2SBarry Smith 
2959d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2960d763cef2SBarry Smith 
2961a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2962d763cef2SBarry Smith @*/
2963c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2964d763cef2SBarry Smith {
2965d763cef2SBarry Smith   PetscFunctionBegin;
29660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
296717186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2968d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
29698704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2970d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2971d763cef2SBarry Smith   PetscFunctionReturn(0);
2972d763cef2SBarry Smith }
2973d763cef2SBarry Smith 
29744a2ae208SSatish Balay #undef __FUNCT__
2975a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2976d763cef2SBarry Smith /*@C
2977a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2978d763cef2SBarry Smith 
29793f9fe445SBarry Smith    Logically Collective on TS
2980d763cef2SBarry Smith 
2981d763cef2SBarry Smith    Input Parameters:
2982d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2983d763cef2SBarry Smith 
2984d763cef2SBarry Smith    Notes:
2985d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2986d763cef2SBarry Smith 
2987d763cef2SBarry Smith    Level: intermediate
2988d763cef2SBarry Smith 
2989d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2990d763cef2SBarry Smith 
2991a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2992d763cef2SBarry Smith @*/
29937087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2994d763cef2SBarry Smith {
2995d952e501SBarry Smith   PetscErrorCode ierr;
2996d952e501SBarry Smith   PetscInt       i;
2997d952e501SBarry Smith 
2998d763cef2SBarry Smith   PetscFunctionBegin;
29990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3000d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
30018704b422SBarry Smith     if (ts->monitordestroy[i]) {
30028704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3003d952e501SBarry Smith     }
3004d952e501SBarry Smith   }
3005d763cef2SBarry Smith   ts->numbermonitors = 0;
3006d763cef2SBarry Smith   PetscFunctionReturn(0);
3007d763cef2SBarry Smith }
3008d763cef2SBarry Smith 
30094a2ae208SSatish Balay #undef __FUNCT__
3010a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
3011d8e5e3e6SSatish Balay /*@
3012a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
30135516499fSSatish Balay 
30145516499fSSatish Balay    Level: intermediate
301541251cbbSSatish Balay 
30165516499fSSatish Balay .keywords: TS, set, monitor
30175516499fSSatish Balay 
301841251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
301941251cbbSSatish Balay @*/
3020649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
3021d763cef2SBarry Smith {
3022dfbe8321SBarry Smith   PetscErrorCode ierr;
3023ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
3024d132466eSBarry Smith 
3025d763cef2SBarry Smith   PetscFunctionBegin;
3026649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3027971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
3028649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3029d763cef2SBarry Smith   PetscFunctionReturn(0);
3030d763cef2SBarry Smith }
3031d763cef2SBarry Smith 
30324a2ae208SSatish Balay #undef __FUNCT__
3033cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
3034cd652676SJed Brown /*@
3035cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
3036cd652676SJed Brown 
3037cd652676SJed Brown    Logically Collective on TS
3038cd652676SJed Brown 
3039cd652676SJed Brown    Input Argument:
3040cd652676SJed Brown .  ts - time stepping context
3041cd652676SJed Brown 
3042cd652676SJed Brown    Output Argument:
3043cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
3044cd652676SJed Brown 
3045cd652676SJed Brown    Level: intermediate
3046cd652676SJed Brown 
3047cd652676SJed Brown .keywords: TS, set
3048cd652676SJed Brown 
3049cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
3050cd652676SJed Brown @*/
3051cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
3052cd652676SJed Brown {
3053cd652676SJed Brown   PetscFunctionBegin;
3054cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3055cd652676SJed Brown   ts->retain_stages = flg;
3056cd652676SJed Brown   PetscFunctionReturn(0);
3057cd652676SJed Brown }
3058cd652676SJed Brown 
3059cd652676SJed Brown #undef __FUNCT__
3060cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
3061cd652676SJed Brown /*@
3062cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3063cd652676SJed Brown 
3064cd652676SJed Brown    Collective on TS
3065cd652676SJed Brown 
3066cd652676SJed Brown    Input Argument:
3067cd652676SJed Brown +  ts - time stepping context
3068cd652676SJed Brown -  t - time to interpolate to
3069cd652676SJed Brown 
3070cd652676SJed Brown    Output Argument:
30710910c330SBarry Smith .  U - state at given time
3072cd652676SJed Brown 
3073cd652676SJed Brown    Notes:
3074cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
3075cd652676SJed Brown 
3076cd652676SJed Brown    Level: intermediate
3077cd652676SJed Brown 
3078cd652676SJed Brown    Developer Notes:
3079cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3080cd652676SJed Brown 
3081cd652676SJed Brown .keywords: TS, set
3082cd652676SJed Brown 
3083cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
3084cd652676SJed Brown @*/
30850910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3086cd652676SJed Brown {
3087cd652676SJed Brown   PetscErrorCode ierr;
3088cd652676SJed Brown 
3089cd652676SJed Brown   PetscFunctionBegin;
3090cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3091b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
30926712e2f1SBarry 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);
3093ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
30940910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3095cd652676SJed Brown   PetscFunctionReturn(0);
3096cd652676SJed Brown }
3097cd652676SJed Brown 
3098cd652676SJed Brown #undef __FUNCT__
30994a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3100d763cef2SBarry Smith /*@
31016d9e5789SSean Farley    TSStep - Steps one time step
3102d763cef2SBarry Smith 
3103d763cef2SBarry Smith    Collective on TS
3104d763cef2SBarry Smith 
3105d763cef2SBarry Smith    Input Parameter:
3106d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3107d763cef2SBarry Smith 
31086d9e5789SSean Farley    Level: intermediate
3109d763cef2SBarry Smith 
3110b8123daeSJed Brown    Notes:
3111b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3112b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3113b8123daeSJed Brown 
311425cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
311525cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
311625cb2221SBarry Smith 
3117d763cef2SBarry Smith .keywords: TS, timestep, solve
3118d763cef2SBarry Smith 
31199be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3120d763cef2SBarry Smith @*/
3121193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3122d763cef2SBarry Smith {
31232fb8446cSMatthew G. Knepley   DM               dm;
3124dfbe8321SBarry Smith   PetscErrorCode   ierr;
3125fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3126d763cef2SBarry Smith 
3127d763cef2SBarry Smith   PetscFunctionBegin;
31280700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3129fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3130fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3131fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3132fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3133fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3134fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3135fffbeea8SBarry Smith                                 "  year        = {2014}\n}\n",&cite);
3136fffbeea8SBarry Smith 
31372fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3138d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
3139*f6a906c0SBarry Smith   if (ts->reverse_mode) {
3140*f6a906c0SBarry Smith     ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3141*f6a906c0SBarry Smith   }
3142d405a339SMatthew Knepley 
3143362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
314424655328SShri   ts->ptime_prev = ts->ptime;
3145cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
31464e9ba7eeSMatthew G. Knepley   ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
3147bbd56ea5SKarl Rupp 
3148e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3149d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3150d2daff3dSHong Zhang   if(ts->reverse_mode) {
3151d2daff3dSHong Zhang     if(!ts->ops->stepadj) {
3152d2daff3dSHong Zhang       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);
3153d2daff3dSHong Zhang     }else {
3154d2daff3dSHong Zhang       ierr = (*ts->ops->stepadj)(ts);CHKERRQ(ierr);
3155d2daff3dSHong Zhang     }
3156d2daff3dSHong Zhang   }else {
3157193ac0bcSJed Brown     ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3158d2daff3dSHong Zhang   }
3159d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3160bbd56ea5SKarl Rupp 
316124655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3162cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3163362cd11cSLisandro Dalcin 
3164362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3165cef5090cSJed Brown     if (ts->errorifstepfailed) {
3166cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
3167ce94432eSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
31682a3a10ceSLisandro Dalcin       } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) {
31692a3a10ceSLisandro 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]);
3170ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3171cef5090cSJed Brown     }
3172362cd11cSLisandro Dalcin   } else if (!ts->reason) {
3173db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3174db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3175362cd11cSLisandro Dalcin   }
3176d763cef2SBarry Smith   PetscFunctionReturn(0);
3177d763cef2SBarry Smith }
3178d763cef2SBarry Smith 
31794a2ae208SSatish Balay #undef __FUNCT__
318005175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
318105175c85SJed Brown /*@
318205175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
318305175c85SJed Brown 
31841c3436cfSJed Brown    Collective on TS
318505175c85SJed Brown 
318605175c85SJed Brown    Input Arguments:
31871c3436cfSJed Brown +  ts - time stepping context
31881c3436cfSJed Brown .  order - desired order of accuracy
31890298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
319005175c85SJed Brown 
319105175c85SJed Brown    Output Arguments:
31920910c330SBarry Smith .  U - state at the end of the current step
319305175c85SJed Brown 
319405175c85SJed Brown    Level: advanced
319505175c85SJed Brown 
3196108c343cSJed Brown    Notes:
3197108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3198108c343cSJed 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.
3199108c343cSJed Brown 
32001c3436cfSJed Brown .seealso: TSStep(), TSAdapt
320105175c85SJed Brown @*/
32020910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
320305175c85SJed Brown {
320405175c85SJed Brown   PetscErrorCode ierr;
320505175c85SJed Brown 
320605175c85SJed Brown   PetscFunctionBegin;
320705175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
320805175c85SJed Brown   PetscValidType(ts,1);
32090910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3210ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
32110910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
321205175c85SJed Brown   PetscFunctionReturn(0);
321305175c85SJed Brown }
321405175c85SJed Brown 
321505175c85SJed Brown #undef __FUNCT__
32166a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
32176a4d4014SLisandro Dalcin /*@
32186a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
32196a4d4014SLisandro Dalcin 
32206a4d4014SLisandro Dalcin    Collective on TS
32216a4d4014SLisandro Dalcin 
32226a4d4014SLisandro Dalcin    Input Parameter:
32236a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3224cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
32255a3a76d0SJed Brown 
32266a4d4014SLisandro Dalcin    Level: beginner
32276a4d4014SLisandro Dalcin 
32285a3a76d0SJed Brown    Notes:
32295a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
32305a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
32315a3a76d0SJed Brown    stepped over the final time.
32325a3a76d0SJed Brown 
32336a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
32346a4d4014SLisandro Dalcin 
32356a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
32366a4d4014SLisandro Dalcin @*/
3237cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
32386a4d4014SLisandro Dalcin {
3239b06615a5SLisandro Dalcin   Vec               solution;
32406a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3241f22f69f0SBarry Smith 
32426a4d4014SLisandro Dalcin   PetscFunctionBegin;
32430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3244f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3245d2daff3dSHong Zhang   if (!ts->reverse_mode && 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 */
3246b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
32470910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3248b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3249b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3250b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
32515a3a76d0SJed Brown     }
32520910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3253bbd56ea5SKarl Rupp   } else if (u) {
32540910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
32555a3a76d0SJed Brown   }
3256d2daff3dSHong Zhang   ierr = TSSetUp(ts);CHKERRQ(ierr); /*compute adj coefficients if the reverse mode is on*/
32576a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
3258193ac0bcSJed Brown   ts->steps             = 0;
32595ef26d82SJed Brown   ts->ksp_its           = 0;
32605ef26d82SJed Brown   ts->snes_its          = 0;
3261c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
3262c610991cSLisandro Dalcin   ts->reject            = 0;
3263193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
3264193ac0bcSJed Brown 
3265ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3266f05ece33SBarry Smith 
3267193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
3268193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
32690910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
3270cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3271193ac0bcSJed Brown   } else {
32726a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
3273db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3274d2daff3dSHong Zhang     else if (!ts->reverse_mode && ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3275e1a7a14fSJed Brown     while (!ts->reason) {
3276d2daff3dSHong Zhang       if(!ts->reverse_mode) {
3277b06615a5SLisandro Dalcin         ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3278d2daff3dSHong Zhang       }else {
3279d2daff3dSHong Zhang         ierr = TSMonitor(ts,ts->max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3280d2daff3dSHong Zhang       }
3281193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3282aeb4809dSShri Abhyankar       if (ts->event) {
3283aeb4809dSShri Abhyankar 	ierr = TSEventMonitor(ts);CHKERRQ(ierr);
32841eda64f1SShri Abhyankar 	if (ts->event->status != TSEVENT_PROCESSING) {
32851eda64f1SShri Abhyankar 	  ierr = TSPostStep(ts);CHKERRQ(ierr);
32861eda64f1SShri Abhyankar 	}
32871eda64f1SShri Abhyankar       } else {
32881eda64f1SShri Abhyankar 	ierr = TSPostStep(ts);CHKERRQ(ierr);
3289aeb4809dSShri Abhyankar       }
3290193ac0bcSJed Brown     }
3291d2daff3dSHong Zhang     if (!ts->reverse_mode && ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
32920910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3293cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3294b06615a5SLisandro Dalcin       solution = u;
32950574a7fbSJed Brown     } else {
3296ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3297cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3298b06615a5SLisandro Dalcin       solution = ts->vec_sol;
32990574a7fbSJed Brown     }
3300d2daff3dSHong Zhang     if(!ts->reverse_mode) {
3301b06615a5SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
3302d2daff3dSHong Zhang     }
33034e9ba7eeSMatthew G. Knepley     ierr = VecViewFromOptions(u, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
3304193ac0bcSJed Brown   }
3305d2daff3dSHong Zhang 
3306ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
330756f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
33086a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
33096a4d4014SLisandro Dalcin }
33106a4d4014SLisandro Dalcin 
33116a4d4014SLisandro Dalcin #undef __FUNCT__
33124a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
3313228d79bcSJed Brown /*@
3314228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3315228d79bcSJed Brown 
3316228d79bcSJed Brown    Collective on TS
3317228d79bcSJed Brown 
3318228d79bcSJed Brown    Input Parameters:
3319228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3320228d79bcSJed Brown .  step - step number that has just completed
3321228d79bcSJed Brown .  ptime - model time of the state
33220910c330SBarry Smith -  u - state at the current model time
3323228d79bcSJed Brown 
3324228d79bcSJed Brown    Notes:
3325228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
3326228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
3327228d79bcSJed Brown 
3328228d79bcSJed Brown    Level: advanced
3329228d79bcSJed Brown 
3330228d79bcSJed Brown .keywords: TS, timestep
3331228d79bcSJed Brown @*/
33320910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3333d763cef2SBarry Smith {
33346849ba73SBarry Smith   PetscErrorCode ierr;
3335a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3336d763cef2SBarry Smith 
3337d763cef2SBarry Smith   PetscFunctionBegin;
3338b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3339b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
33405edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3341d763cef2SBarry Smith   for (i=0; i<n; i++) {
33420910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3343d763cef2SBarry Smith   }
33445edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3345d763cef2SBarry Smith   PetscFunctionReturn(0);
3346d763cef2SBarry Smith }
3347d763cef2SBarry Smith 
3348d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
33494a2ae208SSatish Balay #undef __FUNCT__
3350a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
3351d763cef2SBarry Smith /*@C
3352a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
3353a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3354d763cef2SBarry Smith 
3355d763cef2SBarry Smith    Collective on TS
3356d763cef2SBarry Smith 
3357d763cef2SBarry Smith    Input Parameters:
3358d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3359d763cef2SBarry Smith .  label - the title to put in the title bar
33607c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3361a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3362a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3363d763cef2SBarry Smith 
3364d763cef2SBarry Smith    Output Parameter:
33650b039ecaSBarry Smith .  ctx - the context
3366d763cef2SBarry Smith 
3367d763cef2SBarry Smith    Options Database Key:
33684f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
33694f09c107SBarry Smith .  -ts_monitor_lg_solution -
337099fdda47SBarry Smith .  -ts_monitor_lg_error -
337199fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
337299fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
337399fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
3374d763cef2SBarry Smith 
3375d763cef2SBarry Smith    Notes:
3376a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3377d763cef2SBarry Smith 
3378d763cef2SBarry Smith    Level: intermediate
3379d763cef2SBarry Smith 
33807c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
3381d763cef2SBarry Smith 
33824f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
33837c922b88SBarry Smith 
3384d763cef2SBarry Smith @*/
3385a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3386d763cef2SBarry Smith {
3387b0a32e0cSBarry Smith   PetscDraw      win;
3388dfbe8321SBarry Smith   PetscErrorCode ierr;
3389d763cef2SBarry Smith 
3390d763cef2SBarry Smith   PetscFunctionBegin;
3391b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
3392a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
33933fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
33940b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
33953bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr);
3396287de1a7SBarry Smith   ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr);
3397287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
3398a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3399d763cef2SBarry Smith   PetscFunctionReturn(0);
3400d763cef2SBarry Smith }
3401d763cef2SBarry Smith 
34024a2ae208SSatish Balay #undef __FUNCT__
34034f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3404b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3405d763cef2SBarry Smith {
34060b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3407c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3408dfbe8321SBarry Smith   PetscErrorCode ierr;
3409d763cef2SBarry Smith 
3410d763cef2SBarry Smith   PetscFunctionBegin;
3411b06615a5SLisandro Dalcin   if (!step) {
3412a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3413a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
3414a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
3415a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3416287de1a7SBarry Smith     ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr);
3417a9f9c1f6SBarry Smith   }
3418c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
34190b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3420b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
34210b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
34223923b477SBarry Smith   }
3423d763cef2SBarry Smith   PetscFunctionReturn(0);
3424d763cef2SBarry Smith }
3425d763cef2SBarry Smith 
34264a2ae208SSatish Balay #undef __FUNCT__
3427a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3428d763cef2SBarry Smith /*@C
3429a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3430a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3431d763cef2SBarry Smith 
34320b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3433d763cef2SBarry Smith 
3434d763cef2SBarry Smith    Input Parameter:
34350b039ecaSBarry Smith .  ctx - the monitor context
3436d763cef2SBarry Smith 
3437d763cef2SBarry Smith    Level: intermediate
3438d763cef2SBarry Smith 
3439d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3440d763cef2SBarry Smith 
34414f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3442d763cef2SBarry Smith @*/
3443a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3444d763cef2SBarry Smith {
3445b0a32e0cSBarry Smith   PetscDraw      draw;
3446dfbe8321SBarry Smith   PetscErrorCode ierr;
3447d763cef2SBarry Smith 
3448d763cef2SBarry Smith   PetscFunctionBegin;
34490b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
34506bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
34510b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
34520b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3453d763cef2SBarry Smith   PetscFunctionReturn(0);
3454d763cef2SBarry Smith }
3455d763cef2SBarry Smith 
34564a2ae208SSatish Balay #undef __FUNCT__
34574a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3458d763cef2SBarry Smith /*@
3459b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3460d763cef2SBarry Smith 
3461d763cef2SBarry Smith    Not Collective
3462d763cef2SBarry Smith 
3463d763cef2SBarry Smith    Input Parameter:
3464d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3465d763cef2SBarry Smith 
3466d763cef2SBarry Smith    Output Parameter:
3467d763cef2SBarry Smith .  t  - the current time
3468d763cef2SBarry Smith 
3469d763cef2SBarry Smith    Level: beginner
3470d763cef2SBarry Smith 
3471b8123daeSJed Brown    Note:
3472b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
34739be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3474b8123daeSJed Brown 
3475d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3476d763cef2SBarry Smith 
3477d763cef2SBarry Smith .keywords: TS, get, time
3478d763cef2SBarry Smith @*/
34797087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3480d763cef2SBarry Smith {
3481d763cef2SBarry Smith   PetscFunctionBegin;
34820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3483f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3484d763cef2SBarry Smith   *t = ts->ptime;
3485d763cef2SBarry Smith   PetscFunctionReturn(0);
3486d763cef2SBarry Smith }
3487d763cef2SBarry Smith 
34884a2ae208SSatish Balay #undef __FUNCT__
3489e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3490e5e524a1SHong Zhang /*@
3491e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3492e5e524a1SHong Zhang 
3493e5e524a1SHong Zhang    Not Collective
3494e5e524a1SHong Zhang 
3495e5e524a1SHong Zhang    Input Parameter:
3496e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3497e5e524a1SHong Zhang 
3498e5e524a1SHong Zhang    Output Parameter:
3499e5e524a1SHong Zhang .  t  - the previous time
3500e5e524a1SHong Zhang 
3501e5e524a1SHong Zhang    Level: beginner
3502e5e524a1SHong Zhang 
3503e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3504e5e524a1SHong Zhang 
3505e5e524a1SHong Zhang .keywords: TS, get, time
3506e5e524a1SHong Zhang @*/
3507e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3508e5e524a1SHong Zhang {
3509e5e524a1SHong Zhang   PetscFunctionBegin;
3510e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3511e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3512e5e524a1SHong Zhang   *t = ts->ptime_prev;
3513e5e524a1SHong Zhang   PetscFunctionReturn(0);
3514e5e524a1SHong Zhang }
3515e5e524a1SHong Zhang 
3516e5e524a1SHong Zhang #undef __FUNCT__
35176a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
35186a4d4014SLisandro Dalcin /*@
35196a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
35206a4d4014SLisandro Dalcin 
35213f9fe445SBarry Smith    Logically Collective on TS
35226a4d4014SLisandro Dalcin 
35236a4d4014SLisandro Dalcin    Input Parameters:
35246a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
35256a4d4014SLisandro Dalcin -  time - the time
35266a4d4014SLisandro Dalcin 
35276a4d4014SLisandro Dalcin    Level: intermediate
35286a4d4014SLisandro Dalcin 
35296a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
35306a4d4014SLisandro Dalcin 
35316a4d4014SLisandro Dalcin .keywords: TS, set, time
35326a4d4014SLisandro Dalcin @*/
35337087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
35346a4d4014SLisandro Dalcin {
35356a4d4014SLisandro Dalcin   PetscFunctionBegin;
35360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3537c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
35386a4d4014SLisandro Dalcin   ts->ptime = t;
35396a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
35406a4d4014SLisandro Dalcin }
35416a4d4014SLisandro Dalcin 
35426a4d4014SLisandro Dalcin #undef __FUNCT__
35434a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
3544d763cef2SBarry Smith /*@C
3545d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
3546d763cef2SBarry Smith    TS options in the database.
3547d763cef2SBarry Smith 
35483f9fe445SBarry Smith    Logically Collective on TS
3549d763cef2SBarry Smith 
3550d763cef2SBarry Smith    Input Parameter:
3551d763cef2SBarry Smith +  ts     - The TS context
3552d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3553d763cef2SBarry Smith 
3554d763cef2SBarry Smith    Notes:
3555d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3556d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3557d763cef2SBarry Smith    hyphen.
3558d763cef2SBarry Smith 
3559d763cef2SBarry Smith    Level: advanced
3560d763cef2SBarry Smith 
3561d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
3562d763cef2SBarry Smith 
3563d763cef2SBarry Smith .seealso: TSSetFromOptions()
3564d763cef2SBarry Smith 
3565d763cef2SBarry Smith @*/
35667087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
3567d763cef2SBarry Smith {
3568dfbe8321SBarry Smith   PetscErrorCode ierr;
3569089b2837SJed Brown   SNES           snes;
3570d763cef2SBarry Smith 
3571d763cef2SBarry Smith   PetscFunctionBegin;
35720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3573d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3574089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3575089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3576d763cef2SBarry Smith   PetscFunctionReturn(0);
3577d763cef2SBarry Smith }
3578d763cef2SBarry Smith 
3579d763cef2SBarry Smith 
35804a2ae208SSatish Balay #undef __FUNCT__
35814a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
3582d763cef2SBarry Smith /*@C
3583d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
3584d763cef2SBarry Smith    TS options in the database.
3585d763cef2SBarry Smith 
35863f9fe445SBarry Smith    Logically Collective on TS
3587d763cef2SBarry Smith 
3588d763cef2SBarry Smith    Input Parameter:
3589d763cef2SBarry Smith +  ts     - The TS context
3590d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3591d763cef2SBarry Smith 
3592d763cef2SBarry Smith    Notes:
3593d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3594d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3595d763cef2SBarry Smith    hyphen.
3596d763cef2SBarry Smith 
3597d763cef2SBarry Smith    Level: advanced
3598d763cef2SBarry Smith 
3599d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
3600d763cef2SBarry Smith 
3601d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
3602d763cef2SBarry Smith 
3603d763cef2SBarry Smith @*/
36047087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
3605d763cef2SBarry Smith {
3606dfbe8321SBarry Smith   PetscErrorCode ierr;
3607089b2837SJed Brown   SNES           snes;
3608d763cef2SBarry Smith 
3609d763cef2SBarry Smith   PetscFunctionBegin;
36100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3611d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3612089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3613089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3614d763cef2SBarry Smith   PetscFunctionReturn(0);
3615d763cef2SBarry Smith }
3616d763cef2SBarry Smith 
36174a2ae208SSatish Balay #undef __FUNCT__
36184a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
3619d763cef2SBarry Smith /*@C
3620d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
3621d763cef2SBarry Smith    TS options in the database.
3622d763cef2SBarry Smith 
3623d763cef2SBarry Smith    Not Collective
3624d763cef2SBarry Smith 
3625d763cef2SBarry Smith    Input Parameter:
3626d763cef2SBarry Smith .  ts - The TS context
3627d763cef2SBarry Smith 
3628d763cef2SBarry Smith    Output Parameter:
3629d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
3630d763cef2SBarry Smith 
3631d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
3632d763cef2SBarry Smith    sufficient length to hold the prefix.
3633d763cef2SBarry Smith 
3634d763cef2SBarry Smith    Level: intermediate
3635d763cef2SBarry Smith 
3636d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
3637d763cef2SBarry Smith 
3638d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
3639d763cef2SBarry Smith @*/
36407087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
3641d763cef2SBarry Smith {
3642dfbe8321SBarry Smith   PetscErrorCode ierr;
3643d763cef2SBarry Smith 
3644d763cef2SBarry Smith   PetscFunctionBegin;
36450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36464482741eSBarry Smith   PetscValidPointer(prefix,2);
3647d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3648d763cef2SBarry Smith   PetscFunctionReturn(0);
3649d763cef2SBarry Smith }
3650d763cef2SBarry Smith 
36514a2ae208SSatish Balay #undef __FUNCT__
36524a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3653d763cef2SBarry Smith /*@C
3654d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3655d763cef2SBarry Smith 
3656d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3657d763cef2SBarry Smith 
3658d763cef2SBarry Smith    Input Parameter:
3659d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3660d763cef2SBarry Smith 
3661d763cef2SBarry Smith    Output Parameters:
3662e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3663e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3664e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3665e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3666d763cef2SBarry Smith 
36670298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3668d763cef2SBarry Smith 
3669d763cef2SBarry Smith    Level: intermediate
3670d763cef2SBarry Smith 
367126d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3672d763cef2SBarry Smith 
3673d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3674d763cef2SBarry Smith @*/
3675e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3676d763cef2SBarry Smith {
3677089b2837SJed Brown   PetscErrorCode ierr;
3678089b2837SJed Brown   SNES           snes;
367924989b8cSPeter Brune   DM             dm;
3680089b2837SJed Brown 
3681d763cef2SBarry Smith   PetscFunctionBegin;
3682089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3683e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
368424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
368524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3686d763cef2SBarry Smith   PetscFunctionReturn(0);
3687d763cef2SBarry Smith }
3688d763cef2SBarry Smith 
36891713a123SBarry Smith #undef __FUNCT__
36902eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
36912eca1d9cSJed Brown /*@C
36922eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
36932eca1d9cSJed Brown 
36942eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
36952eca1d9cSJed Brown 
36962eca1d9cSJed Brown    Input Parameter:
36972eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
36982eca1d9cSJed Brown 
36992eca1d9cSJed Brown    Output Parameters:
3700e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3701e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
37022eca1d9cSJed Brown .  f   - The function to compute the matrices
37032eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
37042eca1d9cSJed Brown 
37050298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
37062eca1d9cSJed Brown 
37072eca1d9cSJed Brown    Level: advanced
37082eca1d9cSJed Brown 
37092eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
37102eca1d9cSJed Brown 
37112eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
37122eca1d9cSJed Brown @*/
3713e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
37142eca1d9cSJed Brown {
3715089b2837SJed Brown   PetscErrorCode ierr;
3716089b2837SJed Brown   SNES           snes;
371724989b8cSPeter Brune   DM             dm;
37180910c330SBarry Smith 
37192eca1d9cSJed Brown   PetscFunctionBegin;
3720089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3721f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3722e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
372324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
372424989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
37252eca1d9cSJed Brown   PetscFunctionReturn(0);
37262eca1d9cSJed Brown }
37272eca1d9cSJed Brown 
37286083293cSBarry Smith 
37292eca1d9cSJed Brown #undef __FUNCT__
373083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
37311713a123SBarry Smith /*@C
373283a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
37331713a123SBarry Smith    VecView() for the solution at each timestep
37341713a123SBarry Smith 
37351713a123SBarry Smith    Collective on TS
37361713a123SBarry Smith 
37371713a123SBarry Smith    Input Parameters:
37381713a123SBarry Smith +  ts - the TS context
37391713a123SBarry Smith .  step - current time-step
3740142b95e3SSatish Balay .  ptime - current time
37410298fd71SBarry Smith -  dummy - either a viewer or NULL
37421713a123SBarry Smith 
374399fdda47SBarry Smith    Options Database:
374499fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
374599fdda47SBarry Smith 
374699fdda47SBarry 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
374799fdda47SBarry Smith        will look bad
374899fdda47SBarry Smith 
37491713a123SBarry Smith    Level: intermediate
37501713a123SBarry Smith 
37511713a123SBarry Smith .keywords: TS,  vector, monitor, view
37521713a123SBarry Smith 
3753a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
37541713a123SBarry Smith @*/
37550910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
37561713a123SBarry Smith {
3757dfbe8321SBarry Smith   PetscErrorCode   ierr;
375883a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3759473a3ab2SBarry Smith   PetscDraw        draw;
37601713a123SBarry Smith 
37611713a123SBarry Smith   PetscFunctionBegin;
37626083293cSBarry Smith   if (!step && ictx->showinitial) {
37636083293cSBarry Smith     if (!ictx->initialsolution) {
37640910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
37651713a123SBarry Smith     }
37660910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
37676083293cSBarry Smith   }
3768b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
37690dcf80beSBarry Smith 
37706083293cSBarry Smith   if (ictx->showinitial) {
37716083293cSBarry Smith     PetscReal pause;
37726083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
37736083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
37746083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
37756083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
37766083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
37776083293cSBarry Smith   }
37780910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3779473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3780473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3781473a3ab2SBarry Smith     char      time[32];
3782473a3ab2SBarry Smith     size_t    len;
3783473a3ab2SBarry Smith 
3784473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3785473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3786473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3787473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
37880298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3789473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3790473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3791473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3792473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3793473a3ab2SBarry Smith   }
3794473a3ab2SBarry Smith 
37956083293cSBarry Smith   if (ictx->showinitial) {
37966083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
37976083293cSBarry Smith   }
37981713a123SBarry Smith   PetscFunctionReturn(0);
37991713a123SBarry Smith }
38001713a123SBarry Smith 
38012d5ee99bSBarry Smith #undef __FUNCT__
38022d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
38032d5ee99bSBarry Smith /*@C
38042d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
38052d5ee99bSBarry Smith 
38062d5ee99bSBarry Smith    Collective on TS
38072d5ee99bSBarry Smith 
38082d5ee99bSBarry Smith    Input Parameters:
38092d5ee99bSBarry Smith +  ts - the TS context
38102d5ee99bSBarry Smith .  step - current time-step
38112d5ee99bSBarry Smith .  ptime - current time
38122d5ee99bSBarry Smith -  dummy - either a viewer or NULL
38132d5ee99bSBarry Smith 
38142d5ee99bSBarry Smith    Level: intermediate
38152d5ee99bSBarry Smith 
38162d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
38172d5ee99bSBarry Smith 
38182d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
38192d5ee99bSBarry Smith @*/
38202d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
38212d5ee99bSBarry Smith {
38222d5ee99bSBarry Smith   PetscErrorCode    ierr;
38232d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
38242d5ee99bSBarry Smith   PetscDraw         draw;
38252d5ee99bSBarry Smith   MPI_Comm          comm;
38262d5ee99bSBarry Smith   PetscInt          n;
38272d5ee99bSBarry Smith   PetscMPIInt       size;
38282d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
38292d5ee99bSBarry Smith   char              time[32];
38302d5ee99bSBarry Smith   size_t            len;
38312d5ee99bSBarry Smith   const PetscScalar *U;
38322d5ee99bSBarry Smith 
38332d5ee99bSBarry Smith   PetscFunctionBegin;
38342d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
38352d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
38362d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
38372d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
38382d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
38392d5ee99bSBarry Smith 
38402d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
38412d5ee99bSBarry Smith 
38422d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
38434b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3844ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3845a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3846a4494fc1SBarry Smith       PetscFunctionReturn(0);
3847a4494fc1SBarry Smith   }
38482d5ee99bSBarry Smith   if (!step) ictx->color++;
38492d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
38502d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
38512d5ee99bSBarry Smith 
38522d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
38534b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
38542d5ee99bSBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
38552d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
38562d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
38572d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
38582d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
38592d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
38602d5ee99bSBarry Smith   }
38612d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
38622d5ee99bSBarry Smith   PetscFunctionReturn(0);
38632d5ee99bSBarry Smith }
38642d5ee99bSBarry Smith 
38651713a123SBarry Smith 
38666c699258SBarry Smith #undef __FUNCT__
386783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
38686083293cSBarry Smith /*@C
386983a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
38706083293cSBarry Smith 
38716083293cSBarry Smith    Collective on TS
38726083293cSBarry Smith 
38736083293cSBarry Smith    Input Parameters:
38746083293cSBarry Smith .    ctx - the monitor context
38756083293cSBarry Smith 
38766083293cSBarry Smith    Level: intermediate
38776083293cSBarry Smith 
38786083293cSBarry Smith .keywords: TS,  vector, monitor, view
38796083293cSBarry Smith 
388083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
38816083293cSBarry Smith @*/
388283a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
38836083293cSBarry Smith {
38846083293cSBarry Smith   PetscErrorCode ierr;
38856083293cSBarry Smith 
38866083293cSBarry Smith   PetscFunctionBegin;
38874b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
388883a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
388983a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
389083a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
38916083293cSBarry Smith   PetscFunctionReturn(0);
38926083293cSBarry Smith }
38936083293cSBarry Smith 
38946083293cSBarry Smith #undef __FUNCT__
389583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
38966083293cSBarry Smith /*@C
389783a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
38986083293cSBarry Smith 
38996083293cSBarry Smith    Collective on TS
39006083293cSBarry Smith 
39016083293cSBarry Smith    Input Parameter:
39026083293cSBarry Smith .    ts - time-step context
39036083293cSBarry Smith 
39046083293cSBarry Smith    Output Patameter:
39056083293cSBarry Smith .    ctx - the monitor context
39066083293cSBarry Smith 
390799fdda47SBarry Smith    Options Database:
390899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
390999fdda47SBarry Smith 
39106083293cSBarry Smith    Level: intermediate
39116083293cSBarry Smith 
39126083293cSBarry Smith .keywords: TS,  vector, monitor, view
39136083293cSBarry Smith 
391483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
39156083293cSBarry Smith @*/
391683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
39176083293cSBarry Smith {
39186083293cSBarry Smith   PetscErrorCode   ierr;
39196083293cSBarry Smith 
39206083293cSBarry Smith   PetscFunctionBegin;
3921b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
392283a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3923e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3924bbd56ea5SKarl Rupp 
392583a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3926473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
39270298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3928473a3ab2SBarry Smith 
3929473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
39300298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
39312d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
39326083293cSBarry Smith   PetscFunctionReturn(0);
39336083293cSBarry Smith }
39346083293cSBarry Smith 
39356083293cSBarry Smith #undef __FUNCT__
393683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
39373a471f94SBarry Smith /*@C
393883a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
39393a471f94SBarry Smith    VecView() for the error at each timestep
39403a471f94SBarry Smith 
39413a471f94SBarry Smith    Collective on TS
39423a471f94SBarry Smith 
39433a471f94SBarry Smith    Input Parameters:
39443a471f94SBarry Smith +  ts - the TS context
39453a471f94SBarry Smith .  step - current time-step
39463a471f94SBarry Smith .  ptime - current time
39470298fd71SBarry Smith -  dummy - either a viewer or NULL
39483a471f94SBarry Smith 
39493a471f94SBarry Smith    Level: intermediate
39503a471f94SBarry Smith 
39513a471f94SBarry Smith .keywords: TS,  vector, monitor, view
39523a471f94SBarry Smith 
39533a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
39543a471f94SBarry Smith @*/
39550910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
39563a471f94SBarry Smith {
39573a471f94SBarry Smith   PetscErrorCode   ierr;
395883a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
395983a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
39603a471f94SBarry Smith   Vec              work;
39613a471f94SBarry Smith 
39623a471f94SBarry Smith   PetscFunctionBegin;
3963b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
39640910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
39653a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
39660910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
39673a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
39683a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
39693a471f94SBarry Smith   PetscFunctionReturn(0);
39703a471f94SBarry Smith }
39713a471f94SBarry Smith 
39722a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
39733a471f94SBarry Smith #undef __FUNCT__
39746c699258SBarry Smith #define __FUNCT__ "TSSetDM"
39756c699258SBarry Smith /*@
39766c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
39776c699258SBarry Smith 
39783f9fe445SBarry Smith    Logically Collective on TS and DM
39796c699258SBarry Smith 
39806c699258SBarry Smith    Input Parameters:
39816c699258SBarry Smith +  ts - the preconditioner context
39826c699258SBarry Smith -  dm - the dm
39836c699258SBarry Smith 
39846c699258SBarry Smith    Level: intermediate
39856c699258SBarry Smith 
39866c699258SBarry Smith 
39876c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
39886c699258SBarry Smith @*/
39897087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
39906c699258SBarry Smith {
39916c699258SBarry Smith   PetscErrorCode ierr;
3992089b2837SJed Brown   SNES           snes;
3993942e3340SBarry Smith   DMTS           tsdm;
39946c699258SBarry Smith 
39956c699258SBarry Smith   PetscFunctionBegin;
39960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
399770663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3998942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
39992a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4000942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4001942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
400224989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
400324989b8cSPeter Brune         tsdm->originaldm = dm;
400424989b8cSPeter Brune       }
400524989b8cSPeter Brune     }
40066bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
400724989b8cSPeter Brune   }
40086c699258SBarry Smith   ts->dm = dm;
4009bbd56ea5SKarl Rupp 
4010089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4011089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
40126c699258SBarry Smith   PetscFunctionReturn(0);
40136c699258SBarry Smith }
40146c699258SBarry Smith 
40156c699258SBarry Smith #undef __FUNCT__
40166c699258SBarry Smith #define __FUNCT__ "TSGetDM"
40176c699258SBarry Smith /*@
40186c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
40196c699258SBarry Smith 
40203f9fe445SBarry Smith    Not Collective
40216c699258SBarry Smith 
40226c699258SBarry Smith    Input Parameter:
40236c699258SBarry Smith . ts - the preconditioner context
40246c699258SBarry Smith 
40256c699258SBarry Smith    Output Parameter:
40266c699258SBarry Smith .  dm - the dm
40276c699258SBarry Smith 
40286c699258SBarry Smith    Level: intermediate
40296c699258SBarry Smith 
40306c699258SBarry Smith 
40316c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
40326c699258SBarry Smith @*/
40337087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
40346c699258SBarry Smith {
4035496e6a7aSJed Brown   PetscErrorCode ierr;
4036496e6a7aSJed Brown 
40376c699258SBarry Smith   PetscFunctionBegin;
40380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4039496e6a7aSJed Brown   if (!ts->dm) {
4040ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4041496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4042496e6a7aSJed Brown   }
40436c699258SBarry Smith   *dm = ts->dm;
40446c699258SBarry Smith   PetscFunctionReturn(0);
40456c699258SBarry Smith }
40461713a123SBarry Smith 
40470f5c6efeSJed Brown #undef __FUNCT__
40480f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
40490f5c6efeSJed Brown /*@
40500f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
40510f5c6efeSJed Brown 
40523f9fe445SBarry Smith    Logically Collective on SNES
40530f5c6efeSJed Brown 
40540f5c6efeSJed Brown    Input Parameter:
4055d42a1c89SJed Brown + snes - nonlinear solver
40560910c330SBarry Smith . U - the current state at which to evaluate the residual
4057d42a1c89SJed Brown - ctx - user context, must be a TS
40580f5c6efeSJed Brown 
40590f5c6efeSJed Brown    Output Parameter:
40600f5c6efeSJed Brown . F - the nonlinear residual
40610f5c6efeSJed Brown 
40620f5c6efeSJed Brown    Notes:
40630f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
40640f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
40650f5c6efeSJed Brown 
40660f5c6efeSJed Brown    Level: advanced
40670f5c6efeSJed Brown 
40680f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
40690f5c6efeSJed Brown @*/
40700910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
40710f5c6efeSJed Brown {
40720f5c6efeSJed Brown   TS             ts = (TS)ctx;
40730f5c6efeSJed Brown   PetscErrorCode ierr;
40740f5c6efeSJed Brown 
40750f5c6efeSJed Brown   PetscFunctionBegin;
40760f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
40770910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
40780f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
40790f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
40800910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
40810f5c6efeSJed Brown   PetscFunctionReturn(0);
40820f5c6efeSJed Brown }
40830f5c6efeSJed Brown 
40840f5c6efeSJed Brown #undef __FUNCT__
40850f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
40860f5c6efeSJed Brown /*@
40870f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
40880f5c6efeSJed Brown 
40890f5c6efeSJed Brown    Collective on SNES
40900f5c6efeSJed Brown 
40910f5c6efeSJed Brown    Input Parameter:
40920f5c6efeSJed Brown + snes - nonlinear solver
40930910c330SBarry Smith . U - the current state at which to evaluate the residual
40940f5c6efeSJed Brown - ctx - user context, must be a TS
40950f5c6efeSJed Brown 
40960f5c6efeSJed Brown    Output Parameter:
40970f5c6efeSJed Brown + A - the Jacobian
40980f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
40990f5c6efeSJed Brown - flag - indicates any structure change in the matrix
41000f5c6efeSJed Brown 
41010f5c6efeSJed Brown    Notes:
41020f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
41030f5c6efeSJed Brown 
41040f5c6efeSJed Brown    Level: developer
41050f5c6efeSJed Brown 
41060f5c6efeSJed Brown .seealso: SNESSetJacobian()
41070f5c6efeSJed Brown @*/
4108d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
41090f5c6efeSJed Brown {
41100f5c6efeSJed Brown   TS             ts = (TS)ctx;
41110f5c6efeSJed Brown   PetscErrorCode ierr;
41120f5c6efeSJed Brown 
41130f5c6efeSJed Brown   PetscFunctionBegin;
41140f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
41150910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
41160f5c6efeSJed Brown   PetscValidPointer(A,3);
411794ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
41180f5c6efeSJed Brown   PetscValidPointer(B,4);
411994ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
41200f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4121d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
41220f5c6efeSJed Brown   PetscFunctionReturn(0);
41230f5c6efeSJed Brown }
4124325fc9f4SBarry Smith 
41250e4ef248SJed Brown #undef __FUNCT__
41260e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
41270e4ef248SJed Brown /*@C
41280e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
41290e4ef248SJed Brown 
41300e4ef248SJed Brown    Collective on TS
41310e4ef248SJed Brown 
41320e4ef248SJed Brown    Input Arguments:
41330e4ef248SJed Brown +  ts - time stepping context
41340e4ef248SJed Brown .  t - time at which to evaluate
41350910c330SBarry Smith .  U - state at which to evaluate
41360e4ef248SJed Brown -  ctx - context
41370e4ef248SJed Brown 
41380e4ef248SJed Brown    Output Arguments:
41390e4ef248SJed Brown .  F - right hand side
41400e4ef248SJed Brown 
41410e4ef248SJed Brown    Level: intermediate
41420e4ef248SJed Brown 
41430e4ef248SJed Brown    Notes:
41440e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
41450e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
41460e4ef248SJed Brown 
41470e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
41480e4ef248SJed Brown @*/
41490910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
41500e4ef248SJed Brown {
41510e4ef248SJed Brown   PetscErrorCode ierr;
41520e4ef248SJed Brown   Mat            Arhs,Brhs;
41530e4ef248SJed Brown 
41540e4ef248SJed Brown   PetscFunctionBegin;
41550e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4156d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
41570910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
41580e4ef248SJed Brown   PetscFunctionReturn(0);
41590e4ef248SJed Brown }
41600e4ef248SJed Brown 
41610e4ef248SJed Brown #undef __FUNCT__
41620e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
41630e4ef248SJed Brown /*@C
41640e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
41650e4ef248SJed Brown 
41660e4ef248SJed Brown    Collective on TS
41670e4ef248SJed Brown 
41680e4ef248SJed Brown    Input Arguments:
41690e4ef248SJed Brown +  ts - time stepping context
41700e4ef248SJed Brown .  t - time at which to evaluate
41710910c330SBarry Smith .  U - state at which to evaluate
41720e4ef248SJed Brown -  ctx - context
41730e4ef248SJed Brown 
41740e4ef248SJed Brown    Output Arguments:
41750e4ef248SJed Brown +  A - pointer to operator
41760e4ef248SJed Brown .  B - pointer to preconditioning matrix
41770e4ef248SJed Brown -  flg - matrix structure flag
41780e4ef248SJed Brown 
41790e4ef248SJed Brown    Level: intermediate
41800e4ef248SJed Brown 
41810e4ef248SJed Brown    Notes:
41820e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
41830e4ef248SJed Brown 
41840e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
41850e4ef248SJed Brown @*/
4186d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
41870e4ef248SJed Brown {
41880e4ef248SJed Brown   PetscFunctionBegin;
41890e4ef248SJed Brown   PetscFunctionReturn(0);
41900e4ef248SJed Brown }
41910e4ef248SJed Brown 
41920026cea9SSean Farley #undef __FUNCT__
41930026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
41940026cea9SSean Farley /*@C
41950026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
41960026cea9SSean Farley 
41970026cea9SSean Farley    Collective on TS
41980026cea9SSean Farley 
41990026cea9SSean Farley    Input Arguments:
42000026cea9SSean Farley +  ts - time stepping context
42010026cea9SSean Farley .  t - time at which to evaluate
42020910c330SBarry Smith .  U - state at which to evaluate
42030910c330SBarry Smith .  Udot - time derivative of state vector
42040026cea9SSean Farley -  ctx - context
42050026cea9SSean Farley 
42060026cea9SSean Farley    Output Arguments:
42070026cea9SSean Farley .  F - left hand side
42080026cea9SSean Farley 
42090026cea9SSean Farley    Level: intermediate
42100026cea9SSean Farley 
42110026cea9SSean Farley    Notes:
42120910c330SBarry 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
42130026cea9SSean Farley    user is required to write their own TSComputeIFunction.
42140026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
42150026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
42160026cea9SSean Farley 
42170026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
42180026cea9SSean Farley @*/
42190910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
42200026cea9SSean Farley {
42210026cea9SSean Farley   PetscErrorCode ierr;
42220026cea9SSean Farley   Mat            A,B;
42230026cea9SSean Farley 
42240026cea9SSean Farley   PetscFunctionBegin;
42250298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4226d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
42270910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
42280026cea9SSean Farley   PetscFunctionReturn(0);
42290026cea9SSean Farley }
42300026cea9SSean Farley 
42310026cea9SSean Farley #undef __FUNCT__
42320026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
42330026cea9SSean Farley /*@C
4234b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
42350026cea9SSean Farley 
42360026cea9SSean Farley    Collective on TS
42370026cea9SSean Farley 
42380026cea9SSean Farley    Input Arguments:
42390026cea9SSean Farley +  ts - time stepping context
42400026cea9SSean Farley .  t - time at which to evaluate
42410910c330SBarry Smith .  U - state at which to evaluate
42420910c330SBarry Smith .  Udot - time derivative of state vector
42430026cea9SSean Farley .  shift - shift to apply
42440026cea9SSean Farley -  ctx - context
42450026cea9SSean Farley 
42460026cea9SSean Farley    Output Arguments:
42470026cea9SSean Farley +  A - pointer to operator
42480026cea9SSean Farley .  B - pointer to preconditioning matrix
42490026cea9SSean Farley -  flg - matrix structure flag
42500026cea9SSean Farley 
4251b41af12eSJed Brown    Level: advanced
42520026cea9SSean Farley 
42530026cea9SSean Farley    Notes:
42540026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
42550026cea9SSean Farley 
4256b41af12eSJed Brown    It is only appropriate for problems of the form
4257b41af12eSJed Brown 
4258b41af12eSJed Brown $     M Udot = F(U,t)
4259b41af12eSJed Brown 
4260b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4261b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4262b41af12eSJed Brown   an implicit operator of the form
4263b41af12eSJed Brown 
4264b41af12eSJed Brown $    shift*M + J
4265b41af12eSJed Brown 
4266b41af12eSJed 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
4267b41af12eSJed Brown   a copy of M or reassemble it when requested.
4268b41af12eSJed Brown 
42690026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
42700026cea9SSean Farley @*/
4271d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
42720026cea9SSean Farley {
4273b41af12eSJed Brown   PetscErrorCode ierr;
4274b41af12eSJed Brown 
42750026cea9SSean Farley   PetscFunctionBegin;
427694ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4277b41af12eSJed Brown   ts->ijacobian.shift = shift;
42780026cea9SSean Farley   PetscFunctionReturn(0);
42790026cea9SSean Farley }
4280b41af12eSJed Brown 
4281e817cc15SEmil Constantinescu #undef __FUNCT__
4282e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
4283e817cc15SEmil Constantinescu /*@
4284e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4285e817cc15SEmil Constantinescu 
4286e817cc15SEmil Constantinescu    Not Collective
4287e817cc15SEmil Constantinescu 
4288e817cc15SEmil Constantinescu    Input Parameter:
4289e817cc15SEmil Constantinescu .  ts - the TS context
4290e817cc15SEmil Constantinescu 
4291e817cc15SEmil Constantinescu    Output Parameter:
42924e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4293e817cc15SEmil Constantinescu 
4294e817cc15SEmil Constantinescu    Level: beginner
4295e817cc15SEmil Constantinescu 
4296e817cc15SEmil Constantinescu .keywords: TS, equation type
4297e817cc15SEmil Constantinescu 
4298e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4299e817cc15SEmil Constantinescu @*/
4300e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4301e817cc15SEmil Constantinescu {
4302e817cc15SEmil Constantinescu   PetscFunctionBegin;
4303e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4304e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4305e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4306e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4307e817cc15SEmil Constantinescu }
4308e817cc15SEmil Constantinescu 
4309e817cc15SEmil Constantinescu #undef __FUNCT__
4310e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
4311e817cc15SEmil Constantinescu /*@
4312e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4313e817cc15SEmil Constantinescu 
4314e817cc15SEmil Constantinescu    Not Collective
4315e817cc15SEmil Constantinescu 
4316e817cc15SEmil Constantinescu    Input Parameter:
4317e817cc15SEmil Constantinescu +  ts - the TS context
43184e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4319e817cc15SEmil Constantinescu 
4320e817cc15SEmil Constantinescu    Level: advanced
4321e817cc15SEmil Constantinescu 
4322e817cc15SEmil Constantinescu .keywords: TS, equation type
4323e817cc15SEmil Constantinescu 
4324e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4325e817cc15SEmil Constantinescu @*/
4326e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4327e817cc15SEmil Constantinescu {
4328e817cc15SEmil Constantinescu   PetscFunctionBegin;
4329e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4330e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4331e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4332e817cc15SEmil Constantinescu }
43330026cea9SSean Farley 
43344af1b03aSJed Brown #undef __FUNCT__
43354af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
43364af1b03aSJed Brown /*@
43374af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
43384af1b03aSJed Brown 
43394af1b03aSJed Brown    Not Collective
43404af1b03aSJed Brown 
43414af1b03aSJed Brown    Input Parameter:
43424af1b03aSJed Brown .  ts - the TS context
43434af1b03aSJed Brown 
43444af1b03aSJed Brown    Output Parameter:
43454af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
43464af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
43474af1b03aSJed Brown 
4348487e0bb9SJed Brown    Level: beginner
43494af1b03aSJed Brown 
4350cd652676SJed Brown    Notes:
4351cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
43524af1b03aSJed Brown 
43534af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
43544af1b03aSJed Brown 
43554af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
43564af1b03aSJed Brown @*/
43574af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
43584af1b03aSJed Brown {
43594af1b03aSJed Brown   PetscFunctionBegin;
43604af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43614af1b03aSJed Brown   PetscValidPointer(reason,2);
43624af1b03aSJed Brown   *reason = ts->reason;
43634af1b03aSJed Brown   PetscFunctionReturn(0);
43644af1b03aSJed Brown }
43654af1b03aSJed Brown 
4366fb1732b5SBarry Smith #undef __FUNCT__
4367d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
4368d6ad946cSShri Abhyankar /*@
4369d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4370d6ad946cSShri Abhyankar 
4371d6ad946cSShri Abhyankar    Not Collective
4372d6ad946cSShri Abhyankar 
4373d6ad946cSShri Abhyankar    Input Parameter:
4374d6ad946cSShri Abhyankar +  ts - the TS context
4375d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4376d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4377d6ad946cSShri Abhyankar 
4378f5abba47SShri Abhyankar    Level: advanced
4379d6ad946cSShri Abhyankar 
4380d6ad946cSShri Abhyankar    Notes:
4381d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4382d6ad946cSShri Abhyankar 
4383d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4384d6ad946cSShri Abhyankar 
4385d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4386d6ad946cSShri Abhyankar @*/
4387d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4388d6ad946cSShri Abhyankar {
4389d6ad946cSShri Abhyankar   PetscFunctionBegin;
4390d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4391d6ad946cSShri Abhyankar   ts->reason = reason;
4392d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4393d6ad946cSShri Abhyankar }
4394d6ad946cSShri Abhyankar 
4395d6ad946cSShri Abhyankar #undef __FUNCT__
4396cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
4397cc708dedSBarry Smith /*@
4398cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4399cc708dedSBarry Smith 
4400cc708dedSBarry Smith    Not Collective
4401cc708dedSBarry Smith 
4402cc708dedSBarry Smith    Input Parameter:
4403cc708dedSBarry Smith .  ts - the TS context
4404cc708dedSBarry Smith 
4405cc708dedSBarry Smith    Output Parameter:
4406cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
4407cc708dedSBarry Smith 
4408487e0bb9SJed Brown    Level: beginner
4409cc708dedSBarry Smith 
4410cc708dedSBarry Smith    Notes:
4411cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4412cc708dedSBarry Smith 
4413cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4414cc708dedSBarry Smith 
4415cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4416cc708dedSBarry Smith @*/
4417cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4418cc708dedSBarry Smith {
4419cc708dedSBarry Smith   PetscFunctionBegin;
4420cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4421cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4422cc708dedSBarry Smith   *ftime = ts->solvetime;
4423cc708dedSBarry Smith   PetscFunctionReturn(0);
4424cc708dedSBarry Smith }
4425cc708dedSBarry Smith 
4426cc708dedSBarry Smith #undef __FUNCT__
44275ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
44289f67acb7SJed Brown /*@
44295ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
44309f67acb7SJed Brown    used by the time integrator.
44319f67acb7SJed Brown 
44329f67acb7SJed Brown    Not Collective
44339f67acb7SJed Brown 
44349f67acb7SJed Brown    Input Parameter:
44359f67acb7SJed Brown .  ts - TS context
44369f67acb7SJed Brown 
44379f67acb7SJed Brown    Output Parameter:
44389f67acb7SJed Brown .  nits - number of nonlinear iterations
44399f67acb7SJed Brown 
44409f67acb7SJed Brown    Notes:
44419f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
44429f67acb7SJed Brown 
44439f67acb7SJed Brown    Level: intermediate
44449f67acb7SJed Brown 
44459f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
44469f67acb7SJed Brown 
44475ef26d82SJed Brown .seealso:  TSGetKSPIterations()
44489f67acb7SJed Brown @*/
44495ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
44509f67acb7SJed Brown {
44519f67acb7SJed Brown   PetscFunctionBegin;
44529f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44539f67acb7SJed Brown   PetscValidIntPointer(nits,2);
44545ef26d82SJed Brown   *nits = ts->snes_its;
44559f67acb7SJed Brown   PetscFunctionReturn(0);
44569f67acb7SJed Brown }
44579f67acb7SJed Brown 
44589f67acb7SJed Brown #undef __FUNCT__
44595ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
44609f67acb7SJed Brown /*@
44615ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
44629f67acb7SJed Brown    used by the time integrator.
44639f67acb7SJed Brown 
44649f67acb7SJed Brown    Not Collective
44659f67acb7SJed Brown 
44669f67acb7SJed Brown    Input Parameter:
44679f67acb7SJed Brown .  ts - TS context
44689f67acb7SJed Brown 
44699f67acb7SJed Brown    Output Parameter:
44709f67acb7SJed Brown .  lits - number of linear iterations
44719f67acb7SJed Brown 
44729f67acb7SJed Brown    Notes:
44739f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
44749f67acb7SJed Brown 
44759f67acb7SJed Brown    Level: intermediate
44769f67acb7SJed Brown 
44779f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
44789f67acb7SJed Brown 
44795ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
44809f67acb7SJed Brown @*/
44815ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
44829f67acb7SJed Brown {
44839f67acb7SJed Brown   PetscFunctionBegin;
44849f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44859f67acb7SJed Brown   PetscValidIntPointer(lits,2);
44865ef26d82SJed Brown   *lits = ts->ksp_its;
44879f67acb7SJed Brown   PetscFunctionReturn(0);
44889f67acb7SJed Brown }
44899f67acb7SJed Brown 
44909f67acb7SJed Brown #undef __FUNCT__
4491cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
4492cef5090cSJed Brown /*@
4493cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4494cef5090cSJed Brown 
4495cef5090cSJed Brown    Not Collective
4496cef5090cSJed Brown 
4497cef5090cSJed Brown    Input Parameter:
4498cef5090cSJed Brown .  ts - TS context
4499cef5090cSJed Brown 
4500cef5090cSJed Brown    Output Parameter:
4501cef5090cSJed Brown .  rejects - number of steps rejected
4502cef5090cSJed Brown 
4503cef5090cSJed Brown    Notes:
4504cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4505cef5090cSJed Brown 
4506cef5090cSJed Brown    Level: intermediate
4507cef5090cSJed Brown 
4508cef5090cSJed Brown .keywords: TS, get, number
4509cef5090cSJed Brown 
45105ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4511cef5090cSJed Brown @*/
4512cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4513cef5090cSJed Brown {
4514cef5090cSJed Brown   PetscFunctionBegin;
4515cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4516cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4517cef5090cSJed Brown   *rejects = ts->reject;
4518cef5090cSJed Brown   PetscFunctionReturn(0);
4519cef5090cSJed Brown }
4520cef5090cSJed Brown 
4521cef5090cSJed Brown #undef __FUNCT__
4522cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
4523cef5090cSJed Brown /*@
4524cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4525cef5090cSJed Brown 
4526cef5090cSJed Brown    Not Collective
4527cef5090cSJed Brown 
4528cef5090cSJed Brown    Input Parameter:
4529cef5090cSJed Brown .  ts - TS context
4530cef5090cSJed Brown 
4531cef5090cSJed Brown    Output Parameter:
4532cef5090cSJed Brown .  fails - number of failed nonlinear solves
4533cef5090cSJed Brown 
4534cef5090cSJed Brown    Notes:
4535cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4536cef5090cSJed Brown 
4537cef5090cSJed Brown    Level: intermediate
4538cef5090cSJed Brown 
4539cef5090cSJed Brown .keywords: TS, get, number
4540cef5090cSJed Brown 
45415ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4542cef5090cSJed Brown @*/
4543cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4544cef5090cSJed Brown {
4545cef5090cSJed Brown   PetscFunctionBegin;
4546cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4547cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4548cef5090cSJed Brown   *fails = ts->num_snes_failures;
4549cef5090cSJed Brown   PetscFunctionReturn(0);
4550cef5090cSJed Brown }
4551cef5090cSJed Brown 
4552cef5090cSJed Brown #undef __FUNCT__
4553cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
4554cef5090cSJed Brown /*@
4555cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4556cef5090cSJed Brown 
4557cef5090cSJed Brown    Not Collective
4558cef5090cSJed Brown 
4559cef5090cSJed Brown    Input Parameter:
4560cef5090cSJed Brown +  ts - TS context
4561cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4562cef5090cSJed Brown 
4563cef5090cSJed Brown    Notes:
4564cef5090cSJed Brown    The counter is reset to zero for each step
4565cef5090cSJed Brown 
4566cef5090cSJed Brown    Options Database Key:
4567cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4568cef5090cSJed Brown 
4569cef5090cSJed Brown    Level: intermediate
4570cef5090cSJed Brown 
4571cef5090cSJed Brown .keywords: TS, set, maximum, number
4572cef5090cSJed Brown 
45735ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4574cef5090cSJed Brown @*/
4575cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4576cef5090cSJed Brown {
4577cef5090cSJed Brown   PetscFunctionBegin;
4578cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4579cef5090cSJed Brown   ts->max_reject = rejects;
4580cef5090cSJed Brown   PetscFunctionReturn(0);
4581cef5090cSJed Brown }
4582cef5090cSJed Brown 
4583cef5090cSJed Brown #undef __FUNCT__
4584cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
4585cef5090cSJed Brown /*@
4586cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4587cef5090cSJed Brown 
4588cef5090cSJed Brown    Not Collective
4589cef5090cSJed Brown 
4590cef5090cSJed Brown    Input Parameter:
4591cef5090cSJed Brown +  ts - TS context
4592cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4593cef5090cSJed Brown 
4594cef5090cSJed Brown    Notes:
4595cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4596cef5090cSJed Brown 
4597cef5090cSJed Brown    Options Database Key:
4598cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4599cef5090cSJed Brown 
4600cef5090cSJed Brown    Level: intermediate
4601cef5090cSJed Brown 
4602cef5090cSJed Brown .keywords: TS, set, maximum, number
4603cef5090cSJed Brown 
46045ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4605cef5090cSJed Brown @*/
4606cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4607cef5090cSJed Brown {
4608cef5090cSJed Brown   PetscFunctionBegin;
4609cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4610cef5090cSJed Brown   ts->max_snes_failures = fails;
4611cef5090cSJed Brown   PetscFunctionReturn(0);
4612cef5090cSJed Brown }
4613cef5090cSJed Brown 
4614cef5090cSJed Brown #undef __FUNCT__
46154e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
4616cef5090cSJed Brown /*@
4617cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4618cef5090cSJed Brown 
4619cef5090cSJed Brown    Not Collective
4620cef5090cSJed Brown 
4621cef5090cSJed Brown    Input Parameter:
4622cef5090cSJed Brown +  ts - TS context
4623cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4624cef5090cSJed Brown 
4625cef5090cSJed Brown    Options Database Key:
4626cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4627cef5090cSJed Brown 
4628cef5090cSJed Brown    Level: intermediate
4629cef5090cSJed Brown 
4630cef5090cSJed Brown .keywords: TS, set, error
4631cef5090cSJed Brown 
46325ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4633cef5090cSJed Brown @*/
4634cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4635cef5090cSJed Brown {
4636cef5090cSJed Brown   PetscFunctionBegin;
4637cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4638cef5090cSJed Brown   ts->errorifstepfailed = err;
4639cef5090cSJed Brown   PetscFunctionReturn(0);
4640cef5090cSJed Brown }
4641cef5090cSJed Brown 
4642cef5090cSJed Brown #undef __FUNCT__
4643fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
4644fb1732b5SBarry Smith /*@C
4645fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
4646fb1732b5SBarry Smith 
4647fb1732b5SBarry Smith    Collective on TS
4648fb1732b5SBarry Smith 
4649fb1732b5SBarry Smith    Input Parameters:
4650fb1732b5SBarry Smith +  ts - the TS context
4651fb1732b5SBarry Smith .  step - current time-step
4652fb1732b5SBarry Smith .  ptime - current time
46530910c330SBarry Smith .  u - current state
4654fb1732b5SBarry Smith -  viewer - binary viewer
4655fb1732b5SBarry Smith 
4656fb1732b5SBarry Smith    Level: intermediate
4657fb1732b5SBarry Smith 
4658fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4659fb1732b5SBarry Smith 
4660fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4661fb1732b5SBarry Smith @*/
46620910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4663fb1732b5SBarry Smith {
4664fb1732b5SBarry Smith   PetscErrorCode ierr;
4665ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4666fb1732b5SBarry Smith 
4667fb1732b5SBarry Smith   PetscFunctionBegin;
46680910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4669ed81e22dSJed Brown   PetscFunctionReturn(0);
4670ed81e22dSJed Brown }
4671ed81e22dSJed Brown 
4672ed81e22dSJed Brown #undef __FUNCT__
4673ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4674ed81e22dSJed Brown /*@C
4675ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4676ed81e22dSJed Brown 
4677ed81e22dSJed Brown    Collective on TS
4678ed81e22dSJed Brown 
4679ed81e22dSJed Brown    Input Parameters:
4680ed81e22dSJed Brown +  ts - the TS context
4681ed81e22dSJed Brown .  step - current time-step
4682ed81e22dSJed Brown .  ptime - current time
46830910c330SBarry Smith .  u - current state
4684ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4685ed81e22dSJed Brown 
4686ed81e22dSJed Brown    Level: intermediate
4687ed81e22dSJed Brown 
4688ed81e22dSJed Brown    Notes:
4689ed81e22dSJed 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.
4690ed81e22dSJed Brown    These are named according to the file name template.
4691ed81e22dSJed Brown 
4692ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4693ed81e22dSJed Brown 
4694ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4695ed81e22dSJed Brown 
4696ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4697ed81e22dSJed Brown @*/
46980910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4699ed81e22dSJed Brown {
4700ed81e22dSJed Brown   PetscErrorCode ierr;
4701ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4702ed81e22dSJed Brown   PetscViewer    viewer;
4703ed81e22dSJed Brown 
4704ed81e22dSJed Brown   PetscFunctionBegin;
47058caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4706ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
47070910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4708ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4709ed81e22dSJed Brown   PetscFunctionReturn(0);
4710ed81e22dSJed Brown }
4711ed81e22dSJed Brown 
4712ed81e22dSJed Brown #undef __FUNCT__
4713ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4714ed81e22dSJed Brown /*@C
4715ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4716ed81e22dSJed Brown 
4717ed81e22dSJed Brown    Collective on TS
4718ed81e22dSJed Brown 
4719ed81e22dSJed Brown    Input Parameters:
4720ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4721ed81e22dSJed Brown 
4722ed81e22dSJed Brown    Level: intermediate
4723ed81e22dSJed Brown 
4724ed81e22dSJed Brown    Note:
4725ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4726ed81e22dSJed Brown 
4727ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4728ed81e22dSJed Brown 
4729ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4730ed81e22dSJed Brown @*/
4731ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4732ed81e22dSJed Brown {
4733ed81e22dSJed Brown   PetscErrorCode ierr;
4734ed81e22dSJed Brown 
4735ed81e22dSJed Brown   PetscFunctionBegin;
4736ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4737fb1732b5SBarry Smith   PetscFunctionReturn(0);
4738fb1732b5SBarry Smith }
4739fb1732b5SBarry Smith 
474084df9cb4SJed Brown #undef __FUNCT__
4741552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
474284df9cb4SJed Brown /*@
4743552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
474484df9cb4SJed Brown 
4745ed81e22dSJed Brown    Collective on TS if controller has not been created yet
474684df9cb4SJed Brown 
474784df9cb4SJed Brown    Input Arguments:
4748ed81e22dSJed Brown .  ts - time stepping context
474984df9cb4SJed Brown 
475084df9cb4SJed Brown    Output Arguments:
4751ed81e22dSJed Brown .  adapt - adaptive controller
475284df9cb4SJed Brown 
475384df9cb4SJed Brown    Level: intermediate
475484df9cb4SJed Brown 
4755ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
475684df9cb4SJed Brown @*/
4757552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
475884df9cb4SJed Brown {
475984df9cb4SJed Brown   PetscErrorCode ierr;
476084df9cb4SJed Brown 
476184df9cb4SJed Brown   PetscFunctionBegin;
476284df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
476384df9cb4SJed Brown   PetscValidPointer(adapt,2);
476484df9cb4SJed Brown   if (!ts->adapt) {
4765ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
47663bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
47671c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
476884df9cb4SJed Brown   }
476984df9cb4SJed Brown   *adapt = ts->adapt;
477084df9cb4SJed Brown   PetscFunctionReturn(0);
477184df9cb4SJed Brown }
4772d6ebe24aSShri Abhyankar 
4773d6ebe24aSShri Abhyankar #undef __FUNCT__
47741c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
47751c3436cfSJed Brown /*@
47761c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
47771c3436cfSJed Brown 
47781c3436cfSJed Brown    Logically Collective
47791c3436cfSJed Brown 
47801c3436cfSJed Brown    Input Arguments:
47811c3436cfSJed Brown +  ts - time integration context
47821c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
47830298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
47841c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
47850298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
47861c3436cfSJed Brown 
4787a3cdaa26SBarry Smith    Options Database keys:
4788a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
4789a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
4790a3cdaa26SBarry Smith 
47911c3436cfSJed Brown    Level: beginner
47921c3436cfSJed Brown 
4793c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
47941c3436cfSJed Brown @*/
47951c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
47961c3436cfSJed Brown {
47971c3436cfSJed Brown   PetscErrorCode ierr;
47981c3436cfSJed Brown 
47991c3436cfSJed Brown   PetscFunctionBegin;
4800c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
48011c3436cfSJed Brown   if (vatol) {
48021c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
48031c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4804bbd56ea5SKarl Rupp 
48051c3436cfSJed Brown     ts->vatol = vatol;
48061c3436cfSJed Brown   }
4807c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
48081c3436cfSJed Brown   if (vrtol) {
48091c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
48101c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4811bbd56ea5SKarl Rupp 
48121c3436cfSJed Brown     ts->vrtol = vrtol;
48131c3436cfSJed Brown   }
48141c3436cfSJed Brown   PetscFunctionReturn(0);
48151c3436cfSJed Brown }
48161c3436cfSJed Brown 
48171c3436cfSJed Brown #undef __FUNCT__
4818c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4819c5033834SJed Brown /*@
4820c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4821c5033834SJed Brown 
4822c5033834SJed Brown    Logically Collective
4823c5033834SJed Brown 
4824c5033834SJed Brown    Input Arguments:
4825c5033834SJed Brown .  ts - time integration context
4826c5033834SJed Brown 
4827c5033834SJed Brown    Output Arguments:
48280298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
48290298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
48300298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
48310298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4832c5033834SJed Brown 
4833c5033834SJed Brown    Level: beginner
4834c5033834SJed Brown 
4835c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4836c5033834SJed Brown @*/
4837c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4838c5033834SJed Brown {
4839c5033834SJed Brown   PetscFunctionBegin;
4840c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4841c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4842c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4843c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4844c5033834SJed Brown   PetscFunctionReturn(0);
4845c5033834SJed Brown }
4846c5033834SJed Brown 
4847c5033834SJed Brown #undef __FUNCT__
48481c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
48491c3436cfSJed Brown /*@
48501c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
48511c3436cfSJed Brown 
48521c3436cfSJed Brown    Collective on TS
48531c3436cfSJed Brown 
48541c3436cfSJed Brown    Input Arguments:
48551c3436cfSJed Brown +  ts - time stepping context
48561c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
48571c3436cfSJed Brown 
48581c3436cfSJed Brown    Output Arguments:
48591c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
48601c3436cfSJed Brown 
48611c3436cfSJed Brown    Level: developer
48621c3436cfSJed Brown 
48631c3436cfSJed Brown .seealso: TSSetTolerances()
48641c3436cfSJed Brown @*/
48651c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
48661c3436cfSJed Brown {
48671c3436cfSJed Brown   PetscErrorCode    ierr;
48681c3436cfSJed Brown   PetscInt          i,n,N;
48690910c330SBarry Smith   const PetscScalar *u,*y;
48700910c330SBarry Smith   Vec               U;
48711c3436cfSJed Brown   PetscReal         sum,gsum;
48721c3436cfSJed Brown 
48731c3436cfSJed Brown   PetscFunctionBegin;
48741c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48751c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
48761c3436cfSJed Brown   PetscValidPointer(norm,3);
48770910c330SBarry Smith   U = ts->vec_sol;
48780910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4879ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
48801c3436cfSJed Brown 
48810910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
48820910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
48830910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
48841c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
48851c3436cfSJed Brown   sum  = 0.;
4886ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4887ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4888ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4889ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4890ceefc113SJed Brown     for (i=0; i<n; i++) {
48910910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
48920910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4893ceefc113SJed Brown     }
4894ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4895ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4896ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4897ceefc113SJed Brown     const PetscScalar *atol;
4898ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4899ceefc113SJed Brown     for (i=0; i<n; i++) {
49000910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49010910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4902ceefc113SJed Brown     }
4903ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4904ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4905ceefc113SJed Brown     const PetscScalar *rtol;
4906ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4907ceefc113SJed Brown     for (i=0; i<n; i++) {
49080910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49090910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4910ceefc113SJed Brown     }
4911ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4912ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
49131c3436cfSJed Brown     for (i=0; i<n; i++) {
49140910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49150910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
49161c3436cfSJed Brown     }
4917ceefc113SJed Brown   }
49180910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
49191c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
49201c3436cfSJed Brown 
4921ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
49221c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4923620fc8aaSSatish Balay   if (PetscIsInfOrNanReal(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
49241c3436cfSJed Brown   PetscFunctionReturn(0);
49251c3436cfSJed Brown }
49261c3436cfSJed Brown 
49271c3436cfSJed Brown #undef __FUNCT__
49288d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
49298d59e960SJed Brown /*@
49308d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
49318d59e960SJed Brown 
49328d59e960SJed Brown    Logically Collective on TS
49338d59e960SJed Brown 
49348d59e960SJed Brown    Input Arguments:
49358d59e960SJed Brown +  ts - time stepping context
49368d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
49378d59e960SJed Brown 
49388d59e960SJed Brown    Note:
49398d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
49408d59e960SJed Brown 
49418d59e960SJed Brown    Level: intermediate
49428d59e960SJed Brown 
49438d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
49448d59e960SJed Brown @*/
49458d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
49468d59e960SJed Brown {
49478d59e960SJed Brown   PetscFunctionBegin;
49488d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49498d59e960SJed Brown   ts->cfltime_local = cfltime;
49508d59e960SJed Brown   ts->cfltime       = -1.;
49518d59e960SJed Brown   PetscFunctionReturn(0);
49528d59e960SJed Brown }
49538d59e960SJed Brown 
49548d59e960SJed Brown #undef __FUNCT__
49558d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
49568d59e960SJed Brown /*@
49578d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
49588d59e960SJed Brown 
49598d59e960SJed Brown    Collective on TS
49608d59e960SJed Brown 
49618d59e960SJed Brown    Input Arguments:
49628d59e960SJed Brown .  ts - time stepping context
49638d59e960SJed Brown 
49648d59e960SJed Brown    Output Arguments:
49658d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
49668d59e960SJed Brown 
49678d59e960SJed Brown    Level: advanced
49688d59e960SJed Brown 
49698d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
49708d59e960SJed Brown @*/
49718d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
49728d59e960SJed Brown {
49738d59e960SJed Brown   PetscErrorCode ierr;
49748d59e960SJed Brown 
49758d59e960SJed Brown   PetscFunctionBegin;
49768d59e960SJed Brown   if (ts->cfltime < 0) {
4977ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
49788d59e960SJed Brown   }
49798d59e960SJed Brown   *cfltime = ts->cfltime;
49808d59e960SJed Brown   PetscFunctionReturn(0);
49818d59e960SJed Brown }
49828d59e960SJed Brown 
49838d59e960SJed Brown #undef __FUNCT__
4984d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4985d6ebe24aSShri Abhyankar /*@
4986d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4987d6ebe24aSShri Abhyankar 
4988d6ebe24aSShri Abhyankar    Input Parameters:
4989d6ebe24aSShri Abhyankar .  ts   - the TS context.
4990d6ebe24aSShri Abhyankar .  xl   - lower bound.
4991d6ebe24aSShri Abhyankar .  xu   - upper bound.
4992d6ebe24aSShri Abhyankar 
4993d6ebe24aSShri Abhyankar    Notes:
4994d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
4995e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
4996d6ebe24aSShri Abhyankar 
49972bd2b0e6SSatish Balay    Level: advanced
49982bd2b0e6SSatish Balay 
4999d6ebe24aSShri Abhyankar @*/
5000d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5001d6ebe24aSShri Abhyankar {
5002d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5003d6ebe24aSShri Abhyankar   SNES           snes;
5004d6ebe24aSShri Abhyankar 
5005d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5006d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5007d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5008d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5009d6ebe24aSShri Abhyankar }
5010d6ebe24aSShri Abhyankar 
5011325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
5012c6db04a5SJed Brown #include <mex.h>
5013325fc9f4SBarry Smith 
5014325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
5015325fc9f4SBarry Smith 
5016325fc9f4SBarry Smith #undef __FUNCT__
5017325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
5018325fc9f4SBarry Smith /*
5019325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
5020325fc9f4SBarry Smith                          TSSetFunctionMatlab().
5021325fc9f4SBarry Smith 
5022325fc9f4SBarry Smith    Collective on TS
5023325fc9f4SBarry Smith 
5024325fc9f4SBarry Smith    Input Parameters:
5025325fc9f4SBarry Smith +  snes - the TS context
50260910c330SBarry Smith -  u - input vector
5027325fc9f4SBarry Smith 
5028325fc9f4SBarry Smith    Output Parameter:
5029325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
5030325fc9f4SBarry Smith 
5031325fc9f4SBarry Smith    Notes:
5032325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
5033325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
5034325fc9f4SBarry Smith    themselves.
5035325fc9f4SBarry Smith 
5036325fc9f4SBarry Smith    Level: developer
5037325fc9f4SBarry Smith 
5038325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5039325fc9f4SBarry Smith 
5040325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5041325fc9f4SBarry Smith */
50420910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
5043325fc9f4SBarry Smith {
5044325fc9f4SBarry Smith   PetscErrorCode  ierr;
5045325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5046325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
5047325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
5048325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
5049325fc9f4SBarry Smith 
5050325fc9f4SBarry Smith   PetscFunctionBegin;
5051325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
50520910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
50530910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
5054325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
50550910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
5056325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
5057325fc9f4SBarry Smith 
5058325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
50590910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
50600910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
50610910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
5062bbd56ea5SKarl Rupp 
5063325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5064325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
5065325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5066325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5067325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
5068325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
5069325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
5070325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
5071325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5072325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5073325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5074325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5075325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5076325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5077325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5078325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5079325fc9f4SBarry Smith   PetscFunctionReturn(0);
5080325fc9f4SBarry Smith }
5081325fc9f4SBarry Smith 
5082325fc9f4SBarry Smith 
5083325fc9f4SBarry Smith #undef __FUNCT__
5084325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
5085325fc9f4SBarry Smith /*
5086325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
5087325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
5088e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
5089325fc9f4SBarry Smith 
5090325fc9f4SBarry Smith    Logically Collective on TS
5091325fc9f4SBarry Smith 
5092325fc9f4SBarry Smith    Input Parameters:
5093325fc9f4SBarry Smith +  ts - the TS context
5094325fc9f4SBarry Smith -  func - function evaluation routine
5095325fc9f4SBarry Smith 
5096325fc9f4SBarry Smith    Calling sequence of func:
50970910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
5098325fc9f4SBarry Smith 
5099325fc9f4SBarry Smith    Level: beginner
5100325fc9f4SBarry Smith 
5101325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5102325fc9f4SBarry Smith 
5103325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5104325fc9f4SBarry Smith */
5105cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
5106325fc9f4SBarry Smith {
5107325fc9f4SBarry Smith   PetscErrorCode  ierr;
5108325fc9f4SBarry Smith   TSMatlabContext *sctx;
5109325fc9f4SBarry Smith 
5110325fc9f4SBarry Smith   PetscFunctionBegin;
5111325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5112325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5113325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5114325fc9f4SBarry Smith   /*
5115325fc9f4SBarry Smith      This should work, but it doesn't
5116325fc9f4SBarry Smith   sctx->ctx = ctx;
5117325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5118325fc9f4SBarry Smith   */
5119325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5120bbd56ea5SKarl Rupp 
51210298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
5122325fc9f4SBarry Smith   PetscFunctionReturn(0);
5123325fc9f4SBarry Smith }
5124325fc9f4SBarry Smith 
5125325fc9f4SBarry Smith #undef __FUNCT__
5126325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
5127325fc9f4SBarry Smith /*
5128325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
5129325fc9f4SBarry Smith                          TSSetJacobianMatlab().
5130325fc9f4SBarry Smith 
5131325fc9f4SBarry Smith    Collective on TS
5132325fc9f4SBarry Smith 
5133325fc9f4SBarry Smith    Input Parameters:
5134cdcf91faSSean Farley +  ts - the TS context
51350910c330SBarry Smith .  u - input vector
5136325fc9f4SBarry Smith .  A, B - the matrices
5137325fc9f4SBarry Smith -  ctx - user context
5138325fc9f4SBarry Smith 
5139325fc9f4SBarry Smith    Level: developer
5140325fc9f4SBarry Smith 
5141325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5142325fc9f4SBarry Smith 
5143325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5144325fc9f4SBarry Smith @*/
5145f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
5146325fc9f4SBarry Smith {
5147325fc9f4SBarry Smith   PetscErrorCode  ierr;
5148325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5149325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
5150325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
5151325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
5152325fc9f4SBarry Smith 
5153325fc9f4SBarry Smith   PetscFunctionBegin;
5154cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
51550910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
5156325fc9f4SBarry Smith 
51570910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
5158325fc9f4SBarry Smith 
5159cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
51600910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
51610910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
51620910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
51630910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
5164bbd56ea5SKarl Rupp 
5165325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5166325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
5167325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5168325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5169325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
5170325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
5171325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
5172325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
5173325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
5174325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
5175325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5176325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5177325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5178325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5179325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5180325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5181325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5182325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
5183325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
5184325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5185325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
5186325fc9f4SBarry Smith   PetscFunctionReturn(0);
5187325fc9f4SBarry Smith }
5188325fc9f4SBarry Smith 
5189325fc9f4SBarry Smith 
5190325fc9f4SBarry Smith #undef __FUNCT__
5191325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
5192325fc9f4SBarry Smith /*
5193325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
5194e3c5b3baSBarry 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
5195325fc9f4SBarry Smith 
5196325fc9f4SBarry Smith    Logically Collective on TS
5197325fc9f4SBarry Smith 
5198325fc9f4SBarry Smith    Input Parameters:
5199cdcf91faSSean Farley +  ts - the TS context
5200325fc9f4SBarry Smith .  A,B - Jacobian matrices
5201325fc9f4SBarry Smith .  func - function evaluation routine
5202325fc9f4SBarry Smith -  ctx - user context
5203325fc9f4SBarry Smith 
5204325fc9f4SBarry Smith    Calling sequence of func:
52050910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
5206325fc9f4SBarry Smith 
5207325fc9f4SBarry Smith 
5208325fc9f4SBarry Smith    Level: developer
5209325fc9f4SBarry Smith 
5210325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5211325fc9f4SBarry Smith 
5212325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5213325fc9f4SBarry Smith */
5214cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
5215325fc9f4SBarry Smith {
5216325fc9f4SBarry Smith   PetscErrorCode  ierr;
5217325fc9f4SBarry Smith   TSMatlabContext *sctx;
5218325fc9f4SBarry Smith 
5219325fc9f4SBarry Smith   PetscFunctionBegin;
5220325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5221325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5222325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5223325fc9f4SBarry Smith   /*
5224325fc9f4SBarry Smith      This should work, but it doesn't
5225325fc9f4SBarry Smith   sctx->ctx = ctx;
5226325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5227325fc9f4SBarry Smith   */
5228325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5229bbd56ea5SKarl Rupp 
5230cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
5231325fc9f4SBarry Smith   PetscFunctionReturn(0);
5232325fc9f4SBarry Smith }
5233325fc9f4SBarry Smith 
5234b5b1a830SBarry Smith #undef __FUNCT__
5235b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
5236b5b1a830SBarry Smith /*
5237b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
5238b5b1a830SBarry Smith 
5239b5b1a830SBarry Smith    Collective on TS
5240b5b1a830SBarry Smith 
5241b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5242b5b1a830SBarry Smith @*/
52430910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
5244b5b1a830SBarry Smith {
5245b5b1a830SBarry Smith   PetscErrorCode  ierr;
5246b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5247a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
5248b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
5249b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
5250b5b1a830SBarry Smith 
5251b5b1a830SBarry Smith   PetscFunctionBegin;
5252cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52530910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
5254b5b1a830SBarry Smith 
5255cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
52560910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
5257bbd56ea5SKarl Rupp 
5258b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5259b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
5260b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
5261b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
5262b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
5263b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
5264b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
5265b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5266b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
5267b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
5268b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
5269b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
5270b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
5271b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
5272b5b1a830SBarry Smith   PetscFunctionReturn(0);
5273b5b1a830SBarry Smith }
5274b5b1a830SBarry Smith 
5275b5b1a830SBarry Smith 
5276b5b1a830SBarry Smith #undef __FUNCT__
5277b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
5278b5b1a830SBarry Smith /*
5279b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
5280b5b1a830SBarry Smith 
5281b5b1a830SBarry Smith    Level: developer
5282b5b1a830SBarry Smith 
5283b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
5284b5b1a830SBarry Smith 
5285b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5286b5b1a830SBarry Smith */
5287cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
5288b5b1a830SBarry Smith {
5289b5b1a830SBarry Smith   PetscErrorCode  ierr;
5290b5b1a830SBarry Smith   TSMatlabContext *sctx;
5291b5b1a830SBarry Smith 
5292b5b1a830SBarry Smith   PetscFunctionBegin;
5293b5b1a830SBarry Smith   /* currently sctx is memory bleed */
5294b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5295b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5296b5b1a830SBarry Smith   /*
5297b5b1a830SBarry Smith      This should work, but it doesn't
5298b5b1a830SBarry Smith   sctx->ctx = ctx;
5299b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5300b5b1a830SBarry Smith   */
5301b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5302bbd56ea5SKarl Rupp 
53030298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
5304b5b1a830SBarry Smith   PetscFunctionReturn(0);
5305b5b1a830SBarry Smith }
5306325fc9f4SBarry Smith #endif
5307b3603a34SBarry Smith 
53080b039ecaSBarry Smith 
53090b039ecaSBarry Smith 
5310b3603a34SBarry Smith #undef __FUNCT__
53114f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
5312b3603a34SBarry Smith /*@C
53134f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
5314b3603a34SBarry Smith        in a time based line graph
5315b3603a34SBarry Smith 
5316b3603a34SBarry Smith    Collective on TS
5317b3603a34SBarry Smith 
5318b3603a34SBarry Smith    Input Parameters:
5319b3603a34SBarry Smith +  ts - the TS context
5320b3603a34SBarry Smith .  step - current time-step
5321b3603a34SBarry Smith .  ptime - current time
5322b3603a34SBarry Smith -  lg - a line graph object
5323b3603a34SBarry Smith 
5324b3603a34SBarry Smith    Level: intermediate
5325b3603a34SBarry Smith 
53260b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
5327b3603a34SBarry Smith 
5328b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
5329b3603a34SBarry Smith 
5330b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5331b3603a34SBarry Smith @*/
53320910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5333b3603a34SBarry Smith {
5334b3603a34SBarry Smith   PetscErrorCode    ierr;
53350b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5336b3603a34SBarry Smith   const PetscScalar *yy;
533758ff32f7SBarry Smith   PetscInt          dim;
5338b3603a34SBarry Smith 
5339b3603a34SBarry Smith   PetscFunctionBegin;
534058ff32f7SBarry Smith   if (!step) {
5341a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5342a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5343a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
53440910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
53450b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
53460b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
534758ff32f7SBarry Smith   }
53480910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
5349e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5350e3efe391SJed Brown   {
5351e3efe391SJed Brown     PetscReal *yreal;
5352e3efe391SJed Brown     PetscInt  i,n;
53530910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
5354785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5355e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
53560b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5357e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5358e3efe391SJed Brown   }
5359e3efe391SJed Brown #else
53600b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5361e3efe391SJed Brown #endif
53620910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
5363b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
53640b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
53653923b477SBarry Smith   }
5366b3603a34SBarry Smith   PetscFunctionReturn(0);
5367b3603a34SBarry Smith }
5368b3603a34SBarry Smith 
5369b3603a34SBarry Smith #undef __FUNCT__
53704f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
5371ef20d060SBarry Smith /*@C
53724f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
5373ef20d060SBarry Smith        in a time based line graph
5374ef20d060SBarry Smith 
5375ef20d060SBarry Smith    Collective on TS
5376ef20d060SBarry Smith 
5377ef20d060SBarry Smith    Input Parameters:
5378ef20d060SBarry Smith +  ts - the TS context
5379ef20d060SBarry Smith .  step - current time-step
5380ef20d060SBarry Smith .  ptime - current time
5381ef20d060SBarry Smith -  lg - a line graph object
5382ef20d060SBarry Smith 
5383ef20d060SBarry Smith    Level: intermediate
5384ef20d060SBarry Smith 
5385abd5a294SJed Brown    Notes:
5386abd5a294SJed Brown    Only for sequential solves.
5387abd5a294SJed Brown 
5388abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
5389abd5a294SJed Brown 
5390abd5a294SJed Brown    Options Database Keys:
53914f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
5392ef20d060SBarry Smith 
5393ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
5394ef20d060SBarry Smith 
5395abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
5396ef20d060SBarry Smith @*/
53970910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5398ef20d060SBarry Smith {
5399ef20d060SBarry Smith   PetscErrorCode    ierr;
54000b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5401ef20d060SBarry Smith   const PetscScalar *yy;
5402ef20d060SBarry Smith   Vec               y;
5403a9f9c1f6SBarry Smith   PetscInt          dim;
5404ef20d060SBarry Smith 
5405ef20d060SBarry Smith   PetscFunctionBegin;
5406a9f9c1f6SBarry Smith   if (!step) {
5407a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5408a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
54091ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
54100910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5411a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5412a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5413a9f9c1f6SBarry Smith   }
54140910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
5415ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
54160910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
5417ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
5418e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5419e3efe391SJed Brown   {
5420e3efe391SJed Brown     PetscReal *yreal;
5421e3efe391SJed Brown     PetscInt  i,n;
5422e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
5423785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5424e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
54250b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5426e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5427e3efe391SJed Brown   }
5428e3efe391SJed Brown #else
54290b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5430e3efe391SJed Brown #endif
5431ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
5432ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
5433b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
54340b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
54353923b477SBarry Smith   }
5436ef20d060SBarry Smith   PetscFunctionReturn(0);
5437ef20d060SBarry Smith }
5438ef20d060SBarry Smith 
5439201da799SBarry Smith #undef __FUNCT__
5440201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
5441201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5442201da799SBarry Smith {
5443201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5444201da799SBarry Smith   PetscReal      x   = ptime,y;
5445201da799SBarry Smith   PetscErrorCode ierr;
5446201da799SBarry Smith   PetscInt       its;
5447201da799SBarry Smith 
5448201da799SBarry Smith   PetscFunctionBegin;
5449201da799SBarry Smith   if (!n) {
5450201da799SBarry Smith     PetscDrawAxis axis;
5451bbd56ea5SKarl Rupp 
5452201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5453201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
5454201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5455bbd56ea5SKarl Rupp 
5456201da799SBarry Smith     ctx->snes_its = 0;
5457201da799SBarry Smith   }
5458201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
5459201da799SBarry Smith   y    = its - ctx->snes_its;
5460201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
54613fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5462201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5463201da799SBarry Smith   }
5464201da799SBarry Smith   ctx->snes_its = its;
5465201da799SBarry Smith   PetscFunctionReturn(0);
5466201da799SBarry Smith }
5467201da799SBarry Smith 
5468201da799SBarry Smith #undef __FUNCT__
5469201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
5470201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5471201da799SBarry Smith {
5472201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5473201da799SBarry Smith   PetscReal      x   = ptime,y;
5474201da799SBarry Smith   PetscErrorCode ierr;
5475201da799SBarry Smith   PetscInt       its;
5476201da799SBarry Smith 
5477201da799SBarry Smith   PetscFunctionBegin;
5478201da799SBarry Smith   if (!n) {
5479201da799SBarry Smith     PetscDrawAxis axis;
5480bbd56ea5SKarl Rupp 
5481201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5482201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
5483201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5484bbd56ea5SKarl Rupp 
5485201da799SBarry Smith     ctx->ksp_its = 0;
5486201da799SBarry Smith   }
5487201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
5488201da799SBarry Smith   y    = its - ctx->ksp_its;
5489201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
549099fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5491201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5492201da799SBarry Smith   }
5493201da799SBarry Smith   ctx->ksp_its = its;
5494201da799SBarry Smith   PetscFunctionReturn(0);
5495201da799SBarry Smith }
5496f9c1d6abSBarry Smith 
5497f9c1d6abSBarry Smith #undef __FUNCT__
5498f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
5499f9c1d6abSBarry Smith /*@
5500f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
5501f9c1d6abSBarry Smith 
5502f9c1d6abSBarry Smith    Collective on TS and Vec
5503f9c1d6abSBarry Smith 
5504f9c1d6abSBarry Smith    Input Parameters:
5505f9c1d6abSBarry Smith +  ts - the TS context
5506f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
5507f9c1d6abSBarry Smith 
5508f9c1d6abSBarry Smith    Output Parameters:
5509f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
5510f9c1d6abSBarry Smith 
5511f9c1d6abSBarry Smith    Level: developer
5512f9c1d6abSBarry Smith 
5513f9c1d6abSBarry Smith .keywords: TS, compute
5514f9c1d6abSBarry Smith 
5515f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
5516f9c1d6abSBarry Smith @*/
5517f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
5518f9c1d6abSBarry Smith {
5519f9c1d6abSBarry Smith   PetscErrorCode ierr;
5520f9c1d6abSBarry Smith 
5521f9c1d6abSBarry Smith   PetscFunctionBegin;
5522f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5523ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
5524f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
5525f9c1d6abSBarry Smith   PetscFunctionReturn(0);
5526f9c1d6abSBarry Smith }
552724655328SShri 
552824655328SShri #undef __FUNCT__
552924655328SShri #define __FUNCT__ "TSRollBack"
553024655328SShri /*@
553124655328SShri    TSRollBack - Rolls back one time step
553224655328SShri 
553324655328SShri    Collective on TS
553424655328SShri 
553524655328SShri    Input Parameter:
553624655328SShri .  ts - the TS context obtained from TSCreate()
553724655328SShri 
553824655328SShri    Level: advanced
553924655328SShri 
554024655328SShri .keywords: TS, timestep, rollback
554124655328SShri 
554224655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
554324655328SShri @*/
554424655328SShri PetscErrorCode  TSRollBack(TS ts)
554524655328SShri {
554624655328SShri   PetscErrorCode ierr;
554724655328SShri 
554824655328SShri   PetscFunctionBegin;
554924655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
555024655328SShri 
555124655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
555224655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
555324655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
555424655328SShri   ts->ptime = ts->ptime_prev;
555524655328SShri   PetscFunctionReturn(0);
555624655328SShri }
5557aeb4809dSShri Abhyankar 
5558ff22ae23SHong Zhang #undef __FUNCT__
5559ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
5560ff22ae23SHong Zhang /*@
5561ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
5562ff22ae23SHong Zhang 
5563ff22ae23SHong Zhang    Input Parameter:
5564ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
5565ff22ae23SHong Zhang 
5566ff22ae23SHong Zhang    Level: advanced
5567ff22ae23SHong Zhang 
5568ff22ae23SHong Zhang .keywords: TS, getstages
5569ff22ae23SHong Zhang 
5570ff22ae23SHong Zhang .seealso: TSCreate()
5571ff22ae23SHong Zhang @*/
5572ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
5573ff22ae23SHong Zhang {
5574ff22ae23SHong Zhang   PetscErrorCode ierr;
5575ff22ae23SHong Zhang 
5576ff22ae23SHong Zhang   PetscFunctionBegin;
5577ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
5578ff22ae23SHong Zhang   PetscValidPointer(ns,2);
5579ff22ae23SHong Zhang 
5580ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
5581ff22ae23SHong Zhang   else {
5582ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
5583ff22ae23SHong Zhang   }
5584ff22ae23SHong Zhang   PetscFunctionReturn(0);
5585ff22ae23SHong Zhang }
5586ff22ae23SHong Zhang 
5587ff22ae23SHong Zhang 
5588ff22ae23SHong Zhang 
5589