xref: /petsc/src/ts/interface/ts.c (revision d67e68b7b7c6faac19633dd452ade18af02e67b7)
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__
377a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
378a7a1495cSBarry Smith /*@
379a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
380a7a1495cSBarry Smith       set with TSSetRHSJacobian().
381a7a1495cSBarry Smith 
382a7a1495cSBarry Smith    Collective on TS and Vec
383a7a1495cSBarry Smith 
384a7a1495cSBarry Smith    Input Parameters:
385316643e7SJed Brown +  ts - the TS context
386a7a1495cSBarry Smith .  t - current timestep
3870910c330SBarry Smith -  U - input vector
388a7a1495cSBarry Smith 
389a7a1495cSBarry Smith    Output Parameters:
390a7a1495cSBarry Smith +  A - Jacobian matrix
391a7a1495cSBarry Smith .  B - optional preconditioning matrix
392a7a1495cSBarry Smith -  flag - flag indicating matrix structure
393a7a1495cSBarry Smith 
394a7a1495cSBarry Smith    Notes:
395a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
396a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
397a7a1495cSBarry Smith 
39894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
399a7a1495cSBarry Smith    flag parameter.
400a7a1495cSBarry Smith 
401a7a1495cSBarry Smith    Level: developer
402a7a1495cSBarry Smith 
403a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
404a7a1495cSBarry Smith 
40594b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
406a7a1495cSBarry Smith @*/
407d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
408a7a1495cSBarry Smith {
409dfbe8321SBarry Smith   PetscErrorCode ierr;
410270bf2e7SJed Brown   PetscObjectState Ustate;
41124989b8cSPeter Brune   DM             dm;
412942e3340SBarry Smith   DMTS           tsdm;
41324989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
41424989b8cSPeter Brune   void           *ctx;
41524989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
416a7a1495cSBarry Smith 
417a7a1495cSBarry Smith   PetscFunctionBegin;
4180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4190910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4200910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
42124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
422942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
42324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
4240298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
42559e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
4260910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
4270e4ef248SJed Brown     PetscFunctionReturn(0);
428f8ede8e7SJed Brown   }
429d90be118SSean Farley 
430ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
431d90be118SSean Farley 
432e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
43394ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
43494ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
43594ab13aaSBarry Smith     if (A != B) {
43694ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
43794ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
438e1244c69SJed Brown     }
439e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
440e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
441e1244c69SJed Brown   }
442e1244c69SJed Brown 
44324989b8cSPeter Brune   if (rhsjacobianfunc) {
44494ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
445a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
446d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
447a7a1495cSBarry Smith     PetscStackPop;
44894ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
449a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
45094ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
45194ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
452ef66eb69SBarry Smith   } else {
45394ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
45494ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
455ef66eb69SBarry Smith   }
4560e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4570910c330SBarry Smith   ts->rhsjacobian.X          = U;
45859e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
459a7a1495cSBarry Smith   PetscFunctionReturn(0);
460a7a1495cSBarry Smith }
461a7a1495cSBarry Smith 
4624a2ae208SSatish Balay #undef __FUNCT__
4634a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
464316643e7SJed Brown /*@
465d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
466d763cef2SBarry Smith 
467316643e7SJed Brown    Collective on TS and Vec
468316643e7SJed Brown 
469316643e7SJed Brown    Input Parameters:
470316643e7SJed Brown +  ts - the TS context
471316643e7SJed Brown .  t - current time
4720910c330SBarry Smith -  U - state vector
473316643e7SJed Brown 
474316643e7SJed Brown    Output Parameter:
475316643e7SJed Brown .  y - right hand side
476316643e7SJed Brown 
477316643e7SJed Brown    Note:
478316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
479316643e7SJed Brown    is used internally within the nonlinear solvers.
480316643e7SJed Brown 
481316643e7SJed Brown    Level: developer
482316643e7SJed Brown 
483316643e7SJed Brown .keywords: TS, compute
484316643e7SJed Brown 
485316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
486316643e7SJed Brown @*/
4870910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
488d763cef2SBarry Smith {
489dfbe8321SBarry Smith   PetscErrorCode ierr;
49024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
49124989b8cSPeter Brune   TSIFunction    ifunction;
49224989b8cSPeter Brune   void           *ctx;
49324989b8cSPeter Brune   DM             dm;
49424989b8cSPeter Brune 
495d763cef2SBarry Smith   PetscFunctionBegin;
4960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4970910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4980700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
49924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
50024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
5010298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
502d763cef2SBarry Smith 
503ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
504d763cef2SBarry Smith 
5050910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
50624989b8cSPeter Brune   if (rhsfunction) {
507d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
5080910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
509d763cef2SBarry Smith     PetscStackPop;
510214bc6a2SJed Brown   } else {
511214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
512b2cd27e8SJed Brown   }
51344a41b28SSean Farley 
5140910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
515d763cef2SBarry Smith   PetscFunctionReturn(0);
516d763cef2SBarry Smith }
517d763cef2SBarry Smith 
5184a2ae208SSatish Balay #undef __FUNCT__
519ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
520ef20d060SBarry Smith /*@
521ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
522ef20d060SBarry Smith 
523ef20d060SBarry Smith    Collective on TS and Vec
524ef20d060SBarry Smith 
525ef20d060SBarry Smith    Input Parameters:
526ef20d060SBarry Smith +  ts - the TS context
527ef20d060SBarry Smith -  t - current time
528ef20d060SBarry Smith 
529ef20d060SBarry Smith    Output Parameter:
5300910c330SBarry Smith .  U - the solution
531ef20d060SBarry Smith 
532ef20d060SBarry Smith    Note:
533ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
534ef20d060SBarry Smith    is used internally within the nonlinear solvers.
535ef20d060SBarry Smith 
536ef20d060SBarry Smith    Level: developer
537ef20d060SBarry Smith 
538ef20d060SBarry Smith .keywords: TS, compute
539ef20d060SBarry Smith 
540abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
541ef20d060SBarry Smith @*/
5420910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
543ef20d060SBarry Smith {
544ef20d060SBarry Smith   PetscErrorCode     ierr;
545ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
546ef20d060SBarry Smith   void               *ctx;
547ef20d060SBarry Smith   DM                 dm;
548ef20d060SBarry Smith 
549ef20d060SBarry Smith   PetscFunctionBegin;
550ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5510910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
552ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
553ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
554ef20d060SBarry Smith 
555ef20d060SBarry Smith   if (solutionfunction) {
5569b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5570910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
558ef20d060SBarry Smith     PetscStackPop;
559ef20d060SBarry Smith   }
560ef20d060SBarry Smith   PetscFunctionReturn(0);
561ef20d060SBarry Smith }
5629b7cd975SBarry Smith #undef __FUNCT__
5639b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
5649b7cd975SBarry Smith /*@
5659b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5669b7cd975SBarry Smith 
5679b7cd975SBarry Smith    Collective on TS and Vec
5689b7cd975SBarry Smith 
5699b7cd975SBarry Smith    Input Parameters:
5709b7cd975SBarry Smith +  ts - the TS context
5719b7cd975SBarry Smith -  t - current time
5729b7cd975SBarry Smith 
5739b7cd975SBarry Smith    Output Parameter:
5749b7cd975SBarry Smith .  U - the function value
5759b7cd975SBarry Smith 
5769b7cd975SBarry Smith    Note:
5779b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5789b7cd975SBarry Smith    is used internally within the nonlinear solvers.
5799b7cd975SBarry Smith 
5809b7cd975SBarry Smith    Level: developer
5819b7cd975SBarry Smith 
5829b7cd975SBarry Smith .keywords: TS, compute
5839b7cd975SBarry Smith 
5849b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
5859b7cd975SBarry Smith @*/
5869b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
5879b7cd975SBarry Smith {
5889b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
5899b7cd975SBarry Smith   void               *ctx;
5909b7cd975SBarry Smith   DM                 dm;
5919b7cd975SBarry Smith 
5929b7cd975SBarry Smith   PetscFunctionBegin;
5939b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5949b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5959b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
5969b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
5979b7cd975SBarry Smith 
5989b7cd975SBarry Smith   if (forcing) {
5999b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
6009b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
6019b7cd975SBarry Smith     PetscStackPop;
6029b7cd975SBarry Smith   }
6039b7cd975SBarry Smith   PetscFunctionReturn(0);
6049b7cd975SBarry Smith }
605ef20d060SBarry Smith 
606ef20d060SBarry Smith #undef __FUNCT__
607214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
608214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
609214bc6a2SJed Brown {
6102dd45cf8SJed Brown   Vec            F;
611214bc6a2SJed Brown   PetscErrorCode ierr;
612214bc6a2SJed Brown 
613214bc6a2SJed Brown   PetscFunctionBegin;
6140298fd71SBarry Smith   *Frhs = NULL;
6150298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
616214bc6a2SJed Brown   if (!ts->Frhs) {
6172dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
618214bc6a2SJed Brown   }
619214bc6a2SJed Brown   *Frhs = ts->Frhs;
620214bc6a2SJed Brown   PetscFunctionReturn(0);
621214bc6a2SJed Brown }
622214bc6a2SJed Brown 
623214bc6a2SJed Brown #undef __FUNCT__
624214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
625214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
626214bc6a2SJed Brown {
627214bc6a2SJed Brown   Mat            A,B;
6282dd45cf8SJed Brown   PetscErrorCode ierr;
629214bc6a2SJed Brown 
630214bc6a2SJed Brown   PetscFunctionBegin;
631c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
632c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
6330298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
634214bc6a2SJed Brown   if (Arhs) {
635214bc6a2SJed Brown     if (!ts->Arhs) {
636214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
637214bc6a2SJed Brown     }
638214bc6a2SJed Brown     *Arhs = ts->Arhs;
639214bc6a2SJed Brown   }
640214bc6a2SJed Brown   if (Brhs) {
641214bc6a2SJed Brown     if (!ts->Brhs) {
642bdb70873SJed Brown       if (A != B) {
643214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
644bdb70873SJed Brown       } else {
645bdb70873SJed Brown         ts->Brhs = ts->Arhs;
646bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
647bdb70873SJed Brown       }
648214bc6a2SJed Brown     }
649214bc6a2SJed Brown     *Brhs = ts->Brhs;
650214bc6a2SJed Brown   }
651214bc6a2SJed Brown   PetscFunctionReturn(0);
652214bc6a2SJed Brown }
653214bc6a2SJed Brown 
654214bc6a2SJed Brown #undef __FUNCT__
655316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
656316643e7SJed Brown /*@
6570910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
658316643e7SJed Brown 
659316643e7SJed Brown    Collective on TS and Vec
660316643e7SJed Brown 
661316643e7SJed Brown    Input Parameters:
662316643e7SJed Brown +  ts - the TS context
663316643e7SJed Brown .  t - current time
6640910c330SBarry Smith .  U - state vector
6650910c330SBarry Smith .  Udot - time derivative of state vector
666214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
667316643e7SJed Brown 
668316643e7SJed Brown    Output Parameter:
669316643e7SJed Brown .  Y - right hand side
670316643e7SJed Brown 
671316643e7SJed Brown    Note:
672316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
673316643e7SJed Brown    is used internally within the nonlinear solvers.
674316643e7SJed Brown 
675316643e7SJed Brown    If the user did did not write their equations in implicit form, this
676316643e7SJed Brown    function recasts them in implicit form.
677316643e7SJed Brown 
678316643e7SJed Brown    Level: developer
679316643e7SJed Brown 
680316643e7SJed Brown .keywords: TS, compute
681316643e7SJed Brown 
682316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
683316643e7SJed Brown @*/
6840910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
685316643e7SJed Brown {
686316643e7SJed Brown   PetscErrorCode ierr;
68724989b8cSPeter Brune   TSIFunction    ifunction;
68824989b8cSPeter Brune   TSRHSFunction  rhsfunction;
68924989b8cSPeter Brune   void           *ctx;
69024989b8cSPeter Brune   DM             dm;
691316643e7SJed Brown 
692316643e7SJed Brown   PetscFunctionBegin;
6930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6940910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6950910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
6960700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
697316643e7SJed Brown 
69824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
69924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
7000298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
70124989b8cSPeter Brune 
702ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
703d90be118SSean Farley 
7040910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
70524989b8cSPeter Brune   if (ifunction) {
706316643e7SJed Brown     PetscStackPush("TS user implicit function");
7070910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
708316643e7SJed Brown     PetscStackPop;
709214bc6a2SJed Brown   }
710214bc6a2SJed Brown   if (imex) {
71124989b8cSPeter Brune     if (!ifunction) {
7120910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
7132dd45cf8SJed Brown     }
71424989b8cSPeter Brune   } else if (rhsfunction) {
71524989b8cSPeter Brune     if (ifunction) {
716214bc6a2SJed Brown       Vec Frhs;
717214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
7180910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
719214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
7202dd45cf8SJed Brown     } else {
7210910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
7220910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
723316643e7SJed Brown     }
7244a6899ffSJed Brown   }
7250910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
726316643e7SJed Brown   PetscFunctionReturn(0);
727316643e7SJed Brown }
728316643e7SJed Brown 
729316643e7SJed Brown #undef __FUNCT__
730316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
731316643e7SJed Brown /*@
732316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
733316643e7SJed Brown 
734316643e7SJed Brown    Collective on TS and Vec
735316643e7SJed Brown 
736316643e7SJed Brown    Input
737316643e7SJed Brown       Input Parameters:
738316643e7SJed Brown +  ts - the TS context
739316643e7SJed Brown .  t - current timestep
7400910c330SBarry Smith .  U - state vector
7410910c330SBarry Smith .  Udot - time derivative of state vector
742214bc6a2SJed Brown .  shift - shift to apply, see note below
743214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
744316643e7SJed Brown 
745316643e7SJed Brown    Output Parameters:
746316643e7SJed Brown +  A - Jacobian matrix
747316643e7SJed Brown .  B - optional preconditioning matrix
748316643e7SJed Brown -  flag - flag indicating matrix structure
749316643e7SJed Brown 
750316643e7SJed Brown    Notes:
7510910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
752316643e7SJed Brown 
7530910c330SBarry Smith    dF/dU + shift*dF/dUdot
754316643e7SJed Brown 
755316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
756316643e7SJed Brown    is used internally within the nonlinear solvers.
757316643e7SJed Brown 
758316643e7SJed Brown    Level: developer
759316643e7SJed Brown 
760316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
761316643e7SJed Brown 
762316643e7SJed Brown .seealso:  TSSetIJacobian()
76363495f91SJed Brown @*/
764d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
765316643e7SJed Brown {
766316643e7SJed Brown   PetscErrorCode ierr;
76724989b8cSPeter Brune   TSIJacobian    ijacobian;
76824989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
76924989b8cSPeter Brune   DM             dm;
77024989b8cSPeter Brune   void           *ctx;
771316643e7SJed Brown 
772316643e7SJed Brown   PetscFunctionBegin;
7730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7740910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7750910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
776316643e7SJed Brown   PetscValidPointer(A,6);
77794ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
778316643e7SJed Brown   PetscValidPointer(B,7);
77994ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
78024989b8cSPeter Brune 
78124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
78224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
7830298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
78424989b8cSPeter Brune 
785ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
786316643e7SJed Brown 
78794ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
78824989b8cSPeter Brune   if (ijacobian) {
789316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
790d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
791316643e7SJed Brown     PetscStackPop;
792214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
79394ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
79494ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
7954a6899ffSJed Brown   }
796214bc6a2SJed Brown   if (imex) {
797b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
79894ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
79994ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
80094ab13aaSBarry Smith       if (A != B) {
80194ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
80294ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
803214bc6a2SJed Brown       }
804214bc6a2SJed Brown     }
805214bc6a2SJed Brown   } else {
806e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
807e1244c69SJed Brown     if (rhsjacobian) {
808bd373395SBarry Smith       if (ijacobian) {
809e1244c69SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
810bd373395SBarry Smith       } else {
811bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
812e1244c69SJed Brown       }
813d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
814e1244c69SJed Brown     }
81594ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
816e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
817e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
81894ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
81994ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
82094ab13aaSBarry Smith       if (A != B) {
82194ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
82294ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
823316643e7SJed Brown       }
824e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
825e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
826e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
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);
832e1244c69SJed Brown         }
833e1244c69SJed Brown       }
83494ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
83594ab13aaSBarry Smith       if (A != B) {
83694ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
837214bc6a2SJed Brown       }
838316643e7SJed Brown     }
839316643e7SJed Brown   }
84094ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
841316643e7SJed Brown   PetscFunctionReturn(0);
842316643e7SJed Brown }
843316643e7SJed Brown 
844316643e7SJed Brown #undef __FUNCT__
8454a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
846d763cef2SBarry Smith /*@C
847d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
848b5abc632SBarry Smith     where U_t = G(t,u).
849d763cef2SBarry Smith 
8503f9fe445SBarry Smith     Logically Collective on TS
851d763cef2SBarry Smith 
852d763cef2SBarry Smith     Input Parameters:
853d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8540298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
855d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
856d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8570298fd71SBarry Smith           function evaluation routine (may be NULL)
858d763cef2SBarry Smith 
859d763cef2SBarry Smith     Calling sequence of func:
86087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
861d763cef2SBarry Smith 
862d763cef2SBarry Smith +   t - current timestep
863d763cef2SBarry Smith .   u - input vector
864d763cef2SBarry Smith .   F - function vector
865d763cef2SBarry Smith -   ctx - [optional] user-defined function context
866d763cef2SBarry Smith 
867d763cef2SBarry Smith     Level: beginner
868d763cef2SBarry Smith 
869d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
870d763cef2SBarry Smith 
871ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
872d763cef2SBarry Smith @*/
873089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
874d763cef2SBarry Smith {
875089b2837SJed Brown   PetscErrorCode ierr;
876089b2837SJed Brown   SNES           snes;
8770298fd71SBarry Smith   Vec            ralloc = NULL;
87824989b8cSPeter Brune   DM             dm;
879d763cef2SBarry Smith 
880089b2837SJed Brown   PetscFunctionBegin;
8810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
882ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
88324989b8cSPeter Brune 
88424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88524989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
886089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
887e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
888e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
889e856ceecSJed Brown     r    = ralloc;
890e856ceecSJed Brown   }
891089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
892e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
893d763cef2SBarry Smith   PetscFunctionReturn(0);
894d763cef2SBarry Smith }
895d763cef2SBarry Smith 
8964a2ae208SSatish Balay #undef __FUNCT__
897ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
898ef20d060SBarry Smith /*@C
899abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
900ef20d060SBarry Smith 
901ef20d060SBarry Smith     Logically Collective on TS
902ef20d060SBarry Smith 
903ef20d060SBarry Smith     Input Parameters:
904ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
905ef20d060SBarry Smith .   f - routine for evaluating the solution
906ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
9070298fd71SBarry Smith           function evaluation routine (may be NULL)
908ef20d060SBarry Smith 
909ef20d060SBarry Smith     Calling sequence of func:
910ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
911ef20d060SBarry Smith 
912ef20d060SBarry Smith +   t - current timestep
913ef20d060SBarry Smith .   u - output vector
914ef20d060SBarry Smith -   ctx - [optional] user-defined function context
915ef20d060SBarry Smith 
916abd5a294SJed Brown     Notes:
917abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
918abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
919abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
920abd5a294SJed Brown 
9214f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
922abd5a294SJed Brown 
923ef20d060SBarry Smith     Level: beginner
924ef20d060SBarry Smith 
925ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
926ef20d060SBarry Smith 
9279b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
928ef20d060SBarry Smith @*/
929ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
930ef20d060SBarry Smith {
931ef20d060SBarry Smith   PetscErrorCode ierr;
932ef20d060SBarry Smith   DM             dm;
933ef20d060SBarry Smith 
934ef20d060SBarry Smith   PetscFunctionBegin;
935ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
936ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
937ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
938ef20d060SBarry Smith   PetscFunctionReturn(0);
939ef20d060SBarry Smith }
940ef20d060SBarry Smith 
941ef20d060SBarry Smith #undef __FUNCT__
9429b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
9439b7cd975SBarry Smith /*@C
9449b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
9459b7cd975SBarry Smith 
9469b7cd975SBarry Smith     Logically Collective on TS
9479b7cd975SBarry Smith 
9489b7cd975SBarry Smith     Input Parameters:
9499b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9509b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9519b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9520298fd71SBarry Smith           function evaluation routine (may be NULL)
9539b7cd975SBarry Smith 
9549b7cd975SBarry Smith     Calling sequence of func:
9559b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9569b7cd975SBarry Smith 
9579b7cd975SBarry Smith +   t - current timestep
9589b7cd975SBarry Smith .   u - output vector
9599b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9609b7cd975SBarry Smith 
9619b7cd975SBarry Smith     Notes:
9629b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9639b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9649b7cd975SBarry Smith 
9659b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9669b7cd975SBarry Smith 
9679b7cd975SBarry Smith     Level: beginner
9689b7cd975SBarry Smith 
9699b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9709b7cd975SBarry Smith 
9719b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9729b7cd975SBarry Smith @*/
9739b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9749b7cd975SBarry Smith {
9759b7cd975SBarry Smith   PetscErrorCode ierr;
9769b7cd975SBarry Smith   DM             dm;
9779b7cd975SBarry Smith 
9789b7cd975SBarry Smith   PetscFunctionBegin;
9799b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9809b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
9819b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
9829b7cd975SBarry Smith   PetscFunctionReturn(0);
9839b7cd975SBarry Smith }
9849b7cd975SBarry Smith 
9859b7cd975SBarry Smith #undef __FUNCT__
9864a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
987d763cef2SBarry Smith /*@C
988d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
989b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
990d763cef2SBarry Smith 
9913f9fe445SBarry Smith    Logically Collective on TS
992d763cef2SBarry Smith 
993d763cef2SBarry Smith    Input Parameters:
994d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
995e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
996e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
997d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
998d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
9990298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1000d763cef2SBarry Smith 
1001d763cef2SBarry Smith    Calling sequence of func:
1002ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1003d763cef2SBarry Smith 
1004d763cef2SBarry Smith +  t - current timestep
1005d763cef2SBarry Smith .  u - input vector
1006e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1007e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1008d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1009d763cef2SBarry Smith 
1010d763cef2SBarry Smith 
1011d763cef2SBarry Smith    Level: beginner
1012d763cef2SBarry Smith 
1013d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1014d763cef2SBarry Smith 
1015ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1016d763cef2SBarry Smith 
1017d763cef2SBarry Smith @*/
1018e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1019d763cef2SBarry Smith {
1020277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1021089b2837SJed Brown   SNES           snes;
102224989b8cSPeter Brune   DM             dm;
102324989b8cSPeter Brune   TSIJacobian    ijacobian;
1024277b19d0SLisandro Dalcin 
1025d763cef2SBarry Smith   PetscFunctionBegin;
10260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1027e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1028e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1029e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1030e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1031d763cef2SBarry Smith 
103224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
103324989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1034e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1035e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1036e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1037e1244c69SJed Brown   }
10380298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1039089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10405f659677SPeter Brune   if (!ijacobian) {
1041e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10420e4ef248SJed Brown   }
1043e5d3d808SBarry Smith   if (Amat) {
1044e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10450e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1046bbd56ea5SKarl Rupp 
1047e5d3d808SBarry Smith     ts->Arhs = Amat;
10480e4ef248SJed Brown   }
1049e5d3d808SBarry Smith   if (Pmat) {
1050e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10510e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1052bbd56ea5SKarl Rupp 
1053e5d3d808SBarry Smith     ts->Brhs = Pmat;
10540e4ef248SJed Brown   }
1055d763cef2SBarry Smith   PetscFunctionReturn(0);
1056d763cef2SBarry Smith }
1057d763cef2SBarry Smith 
1058316643e7SJed Brown 
1059316643e7SJed Brown #undef __FUNCT__
1060316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1061316643e7SJed Brown /*@C
1062b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1063316643e7SJed Brown 
10643f9fe445SBarry Smith    Logically Collective on TS
1065316643e7SJed Brown 
1066316643e7SJed Brown    Input Parameters:
1067316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10680298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1069316643e7SJed Brown .  f   - the function evaluation routine
10700298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1071316643e7SJed Brown 
1072316643e7SJed Brown    Calling sequence of f:
1073316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1074316643e7SJed Brown 
1075316643e7SJed Brown +  t   - time at step/stage being solved
1076316643e7SJed Brown .  u   - state vector
1077316643e7SJed Brown .  u_t - time derivative of state vector
1078316643e7SJed Brown .  F   - function vector
1079316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1080316643e7SJed Brown 
1081316643e7SJed Brown    Important:
1082d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
1083316643e7SJed Brown 
1084316643e7SJed Brown    Level: beginner
1085316643e7SJed Brown 
1086316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1087316643e7SJed Brown 
1088d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1089316643e7SJed Brown @*/
1090089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1091316643e7SJed Brown {
1092089b2837SJed Brown   PetscErrorCode ierr;
1093089b2837SJed Brown   SNES           snes;
10940298fd71SBarry Smith   Vec            resalloc = NULL;
109524989b8cSPeter Brune   DM             dm;
1096316643e7SJed Brown 
1097316643e7SJed Brown   PetscFunctionBegin;
10980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1099ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
110024989b8cSPeter Brune 
110124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
110224989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
110324989b8cSPeter Brune 
1104089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1105e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1106e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1107e856ceecSJed Brown     res  = resalloc;
1108e856ceecSJed Brown   }
1109089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1110e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1111089b2837SJed Brown   PetscFunctionReturn(0);
1112089b2837SJed Brown }
1113089b2837SJed Brown 
1114089b2837SJed Brown #undef __FUNCT__
1115089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1116089b2837SJed Brown /*@C
1117089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1118089b2837SJed Brown 
1119089b2837SJed Brown    Not Collective
1120089b2837SJed Brown 
1121089b2837SJed Brown    Input Parameter:
1122089b2837SJed Brown .  ts - the TS context
1123089b2837SJed Brown 
1124089b2837SJed Brown    Output Parameter:
11250298fd71SBarry Smith +  r - vector to hold residual (or NULL)
11260298fd71SBarry Smith .  func - the function to compute residual (or NULL)
11270298fd71SBarry Smith -  ctx - the function context (or NULL)
1128089b2837SJed Brown 
1129089b2837SJed Brown    Level: advanced
1130089b2837SJed Brown 
1131089b2837SJed Brown .keywords: TS, nonlinear, get, function
1132089b2837SJed Brown 
1133089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1134089b2837SJed Brown @*/
1135089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1136089b2837SJed Brown {
1137089b2837SJed Brown   PetscErrorCode ierr;
1138089b2837SJed Brown   SNES           snes;
113924989b8cSPeter Brune   DM             dm;
1140089b2837SJed Brown 
1141089b2837SJed Brown   PetscFunctionBegin;
1142089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1143089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11440298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
114524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
114624989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1147089b2837SJed Brown   PetscFunctionReturn(0);
1148089b2837SJed Brown }
1149089b2837SJed Brown 
1150089b2837SJed Brown #undef __FUNCT__
1151089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1152089b2837SJed Brown /*@C
1153089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1154089b2837SJed Brown 
1155089b2837SJed Brown    Not Collective
1156089b2837SJed Brown 
1157089b2837SJed Brown    Input Parameter:
1158089b2837SJed Brown .  ts - the TS context
1159089b2837SJed Brown 
1160089b2837SJed Brown    Output Parameter:
11610298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11620298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11630298fd71SBarry Smith -  ctx - the function context (or NULL)
1164089b2837SJed Brown 
1165089b2837SJed Brown    Level: advanced
1166089b2837SJed Brown 
1167089b2837SJed Brown .keywords: TS, nonlinear, get, function
1168089b2837SJed Brown 
1169089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1170089b2837SJed Brown @*/
1171089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1172089b2837SJed Brown {
1173089b2837SJed Brown   PetscErrorCode ierr;
1174089b2837SJed Brown   SNES           snes;
117524989b8cSPeter Brune   DM             dm;
1176089b2837SJed Brown 
1177089b2837SJed Brown   PetscFunctionBegin;
1178089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1179089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11800298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
118124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
118224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1183316643e7SJed Brown   PetscFunctionReturn(0);
1184316643e7SJed Brown }
1185316643e7SJed Brown 
1186316643e7SJed Brown #undef __FUNCT__
1187316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1188316643e7SJed Brown /*@C
1189a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1190ae8867d6SBarry Smith         provided with TSSetIFunction().
1191316643e7SJed Brown 
11923f9fe445SBarry Smith    Logically Collective on TS
1193316643e7SJed Brown 
1194316643e7SJed Brown    Input Parameters:
1195316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1196e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1197e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1198316643e7SJed Brown .  f   - the Jacobian evaluation routine
11990298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1200316643e7SJed Brown 
1201316643e7SJed Brown    Calling sequence of f:
1202ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1203316643e7SJed Brown 
1204316643e7SJed Brown +  t    - time at step/stage being solved
12051b4a444bSJed Brown .  U    - state vector
12061b4a444bSJed Brown .  U_t  - time derivative of state vector
1207316643e7SJed Brown .  a    - shift
1208e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1209e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1210316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1211316643e7SJed Brown 
1212316643e7SJed Brown    Notes:
1213e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1214316643e7SJed Brown 
1215a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1216b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1217a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1218a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1219a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1220a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1221a4f0a591SBarry Smith 
1222316643e7SJed Brown    Level: beginner
1223316643e7SJed Brown 
1224316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1225316643e7SJed Brown 
1226ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1227316643e7SJed Brown 
1228316643e7SJed Brown @*/
1229e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1230316643e7SJed Brown {
1231316643e7SJed Brown   PetscErrorCode ierr;
1232089b2837SJed Brown   SNES           snes;
123324989b8cSPeter Brune   DM             dm;
1234316643e7SJed Brown 
1235316643e7SJed Brown   PetscFunctionBegin;
12360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1237e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1238e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1239e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1240e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
124124989b8cSPeter Brune 
124224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
124324989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
124424989b8cSPeter Brune 
1245089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1246e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1247316643e7SJed Brown   PetscFunctionReturn(0);
1248316643e7SJed Brown }
1249316643e7SJed Brown 
12504a2ae208SSatish Balay #undef __FUNCT__
1251e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1252e1244c69SJed Brown /*@
1253e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1254e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1255e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1256e1244c69SJed Brown    not been changed by the TS.
1257e1244c69SJed Brown 
1258e1244c69SJed Brown    Logically Collective
1259e1244c69SJed Brown 
1260e1244c69SJed Brown    Input Arguments:
1261e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1262e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1263e1244c69SJed Brown 
1264e1244c69SJed Brown    Level: intermediate
1265e1244c69SJed Brown 
1266e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1267e1244c69SJed Brown @*/
1268e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1269e1244c69SJed Brown {
1270e1244c69SJed Brown   PetscFunctionBegin;
1271e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1272e1244c69SJed Brown   PetscFunctionReturn(0);
1273e1244c69SJed Brown }
1274e1244c69SJed Brown 
1275e1244c69SJed Brown #undef __FUNCT__
127655849f57SBarry Smith #define __FUNCT__ "TSLoad"
127755849f57SBarry Smith /*@C
127855849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
127955849f57SBarry Smith 
128055849f57SBarry Smith   Collective on PetscViewer
128155849f57SBarry Smith 
128255849f57SBarry Smith   Input Parameters:
128355849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
128455849f57SBarry Smith            some related function before a call to TSLoad().
128555849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
128655849f57SBarry Smith 
128755849f57SBarry Smith    Level: intermediate
128855849f57SBarry Smith 
128955849f57SBarry Smith   Notes:
129055849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
129155849f57SBarry Smith 
129255849f57SBarry Smith   Notes for advanced users:
129355849f57SBarry Smith   Most users should not need to know the details of the binary storage
129455849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
129555849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
129655849f57SBarry Smith   format is
129755849f57SBarry Smith .vb
129855849f57SBarry Smith      has not yet been determined
129955849f57SBarry Smith .ve
130055849f57SBarry Smith 
130155849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
130255849f57SBarry Smith @*/
1303f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
130455849f57SBarry Smith {
130555849f57SBarry Smith   PetscErrorCode ierr;
130655849f57SBarry Smith   PetscBool      isbinary;
130755849f57SBarry Smith   PetscInt       classid;
130855849f57SBarry Smith   char           type[256];
13092d53ad75SBarry Smith   DMTS           sdm;
1310ad6bc421SBarry Smith   DM             dm;
131155849f57SBarry Smith 
131255849f57SBarry Smith   PetscFunctionBegin;
1313f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
131455849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
131555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
131655849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
131755849f57SBarry Smith 
131855849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1319ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
132055849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1321f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1322f2c2a1b9SBarry Smith   if (ts->ops->load) {
1323f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1324f2c2a1b9SBarry Smith   }
1325ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1326ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1327ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1328f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1329f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
13302d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13312d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
133255849f57SBarry Smith   PetscFunctionReturn(0);
133355849f57SBarry Smith }
133455849f57SBarry Smith 
13359804daf3SBarry Smith #include <petscdraw.h>
1336e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1337e04113cfSBarry Smith #include <petscviewersaws.h>
1338f05ece33SBarry Smith #endif
133955849f57SBarry Smith #undef __FUNCT__
13404a2ae208SSatish Balay #define __FUNCT__ "TSView"
13417e2c5f70SBarry Smith /*@C
1342d763cef2SBarry Smith     TSView - Prints the TS data structure.
1343d763cef2SBarry Smith 
13444c49b128SBarry Smith     Collective on TS
1345d763cef2SBarry Smith 
1346d763cef2SBarry Smith     Input Parameters:
1347d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1348d763cef2SBarry Smith -   viewer - visualization context
1349d763cef2SBarry Smith 
1350d763cef2SBarry Smith     Options Database Key:
1351d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1352d763cef2SBarry Smith 
1353d763cef2SBarry Smith     Notes:
1354d763cef2SBarry Smith     The available visualization contexts include
1355b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1356b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1357d763cef2SBarry Smith          output where only the first processor opens
1358d763cef2SBarry Smith          the file.  All other processors send their
1359d763cef2SBarry Smith          data to the first processor to print.
1360d763cef2SBarry Smith 
1361d763cef2SBarry Smith     The user can open an alternative visualization context with
1362b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1363d763cef2SBarry Smith 
1364d763cef2SBarry Smith     Level: beginner
1365d763cef2SBarry Smith 
1366d763cef2SBarry Smith .keywords: TS, timestep, view
1367d763cef2SBarry Smith 
1368b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1369d763cef2SBarry Smith @*/
13707087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1371d763cef2SBarry Smith {
1372dfbe8321SBarry Smith   PetscErrorCode ierr;
137319fd82e9SBarry Smith   TSType         type;
13742b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
13752d53ad75SBarry Smith   DMTS           sdm;
1376e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1377f05ece33SBarry Smith   PetscBool      isams;
1378f05ece33SBarry Smith #endif
1379d763cef2SBarry Smith 
1380d763cef2SBarry Smith   PetscFunctionBegin;
13810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13823050cee2SBarry Smith   if (!viewer) {
1383ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
13843050cee2SBarry Smith   }
13850700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1386c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1387fd16b177SBarry Smith 
1388251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1389251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
139055849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
13912b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1392e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1393e04113cfSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&isams);CHKERRQ(ierr);
1394f05ece33SBarry Smith #endif
139532077d6dSBarry Smith   if (iascii) {
1396dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
139777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
13987c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1399d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
14005ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1401c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1402d763cef2SBarry Smith     }
14035ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1404193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
14052d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14062d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1407d52bd9f3SBarry Smith     if (ts->ops->view) {
1408d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1409d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1410d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1411d52bd9f3SBarry Smith     }
14120f5bd95cSBarry Smith   } else if (isstring) {
1413a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1414b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
141555849f57SBarry Smith   } else if (isbinary) {
141655849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
141755849f57SBarry Smith     MPI_Comm    comm;
141855849f57SBarry Smith     PetscMPIInt rank;
141955849f57SBarry Smith     char        type[256];
142055849f57SBarry Smith 
142155849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
142255849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
142355849f57SBarry Smith     if (!rank) {
142455849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
142555849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
142655849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
142755849f57SBarry Smith     }
142855849f57SBarry Smith     if (ts->ops->view) {
142955849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
143055849f57SBarry Smith     }
1431f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1432f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
14332d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14342d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
14352b0a91c0SBarry Smith   } else if (isdraw) {
14362b0a91c0SBarry Smith     PetscDraw draw;
14372b0a91c0SBarry Smith     char      str[36];
143889fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14392b0a91c0SBarry Smith 
14402b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14412b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14422b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14432b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
14440298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
144589fd9fafSBarry Smith     bottom = y - h;
14462b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14472b0a91c0SBarry Smith     if (ts->ops->view) {
14482b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14492b0a91c0SBarry Smith     }
14502b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1451e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1452f05ece33SBarry Smith   } else if (isams) {
1453d45a07a7SBarry Smith     PetscMPIInt rank;
14542657e9d9SBarry Smith     const char  *name;
14552657e9d9SBarry Smith 
14562657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1457d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1458d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1459d45a07a7SBarry Smith       char       dir[1024];
1460d45a07a7SBarry Smith 
1461e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1462a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
14632657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
14642657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
14652657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1466d763cef2SBarry Smith     }
14670acecf5bSBarry Smith     if (ts->ops->view) {
14680acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14690acecf5bSBarry Smith     }
1470f05ece33SBarry Smith #endif
1471f05ece33SBarry Smith   }
1472f05ece33SBarry Smith 
1473b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1474251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1475b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1476d763cef2SBarry Smith   PetscFunctionReturn(0);
1477d763cef2SBarry Smith }
1478d763cef2SBarry Smith 
1479d763cef2SBarry Smith 
14804a2ae208SSatish Balay #undef __FUNCT__
14814a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1482b07ff414SBarry Smith /*@
1483d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1484d763cef2SBarry Smith    the timesteppers.
1485d763cef2SBarry Smith 
14863f9fe445SBarry Smith    Logically Collective on TS
1487d763cef2SBarry Smith 
1488d763cef2SBarry Smith    Input Parameters:
1489d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1490d763cef2SBarry Smith -  usrP - optional user context
1491d763cef2SBarry Smith 
1492d763cef2SBarry Smith    Level: intermediate
1493d763cef2SBarry Smith 
1494d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1495d763cef2SBarry Smith 
1496d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1497d763cef2SBarry Smith @*/
14987087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1499d763cef2SBarry Smith {
1500d763cef2SBarry Smith   PetscFunctionBegin;
15010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1502d763cef2SBarry Smith   ts->user = usrP;
1503d763cef2SBarry Smith   PetscFunctionReturn(0);
1504d763cef2SBarry Smith }
1505d763cef2SBarry Smith 
15064a2ae208SSatish Balay #undef __FUNCT__
15074a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1508b07ff414SBarry Smith /*@
1509d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1510d763cef2SBarry Smith     timestepper.
1511d763cef2SBarry Smith 
1512d763cef2SBarry Smith     Not Collective
1513d763cef2SBarry Smith 
1514d763cef2SBarry Smith     Input Parameter:
1515d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1516d763cef2SBarry Smith 
1517d763cef2SBarry Smith     Output Parameter:
1518d763cef2SBarry Smith .   usrP - user context
1519d763cef2SBarry Smith 
1520d763cef2SBarry Smith     Level: intermediate
1521d763cef2SBarry Smith 
1522d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1523d763cef2SBarry Smith 
1524d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1525d763cef2SBarry Smith @*/
1526e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1527d763cef2SBarry Smith {
1528d763cef2SBarry Smith   PetscFunctionBegin;
15290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1530e71120c6SJed Brown   *(void**)usrP = ts->user;
1531d763cef2SBarry Smith   PetscFunctionReturn(0);
1532d763cef2SBarry Smith }
1533d763cef2SBarry Smith 
15344a2ae208SSatish Balay #undef __FUNCT__
15354a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1536d763cef2SBarry Smith /*@
1537b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1538d763cef2SBarry Smith 
1539d763cef2SBarry Smith    Not Collective
1540d763cef2SBarry Smith 
1541d763cef2SBarry Smith    Input Parameter:
1542d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1543d763cef2SBarry Smith 
1544d763cef2SBarry Smith    Output Parameter:
1545b8123daeSJed Brown .  iter - number of steps completed so far
1546d763cef2SBarry Smith 
1547d763cef2SBarry Smith    Level: intermediate
1548d763cef2SBarry Smith 
1549d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
15509be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1551d763cef2SBarry Smith @*/
15527087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1553d763cef2SBarry Smith {
1554d763cef2SBarry Smith   PetscFunctionBegin;
15550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15564482741eSBarry Smith   PetscValidIntPointer(iter,2);
1557d763cef2SBarry Smith   *iter = ts->steps;
1558d763cef2SBarry Smith   PetscFunctionReturn(0);
1559d763cef2SBarry Smith }
1560d763cef2SBarry Smith 
15614a2ae208SSatish Balay #undef __FUNCT__
15624a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1563d763cef2SBarry Smith /*@
1564d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1565d763cef2SBarry Smith    as well as the initial time.
1566d763cef2SBarry Smith 
15673f9fe445SBarry Smith    Logically Collective on TS
1568d763cef2SBarry Smith 
1569d763cef2SBarry Smith    Input Parameters:
1570d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1571d763cef2SBarry Smith .  initial_time - the initial time
1572d763cef2SBarry Smith -  time_step - the size of the timestep
1573d763cef2SBarry Smith 
1574d763cef2SBarry Smith    Level: intermediate
1575d763cef2SBarry Smith 
1576d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1577d763cef2SBarry Smith 
1578d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1579d763cef2SBarry Smith @*/
15807087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1581d763cef2SBarry Smith {
1582e144a568SJed Brown   PetscErrorCode ierr;
1583e144a568SJed Brown 
1584d763cef2SBarry Smith   PetscFunctionBegin;
15850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1586e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1587d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1588d763cef2SBarry Smith   PetscFunctionReturn(0);
1589d763cef2SBarry Smith }
1590d763cef2SBarry Smith 
15914a2ae208SSatish Balay #undef __FUNCT__
15924a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1593d763cef2SBarry Smith /*@
1594d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1595d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1596d763cef2SBarry Smith 
15973f9fe445SBarry Smith    Logically Collective on TS
1598d763cef2SBarry Smith 
1599d763cef2SBarry Smith    Input Parameters:
1600d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1601d763cef2SBarry Smith -  time_step - the size of the timestep
1602d763cef2SBarry Smith 
1603d763cef2SBarry Smith    Level: intermediate
1604d763cef2SBarry Smith 
1605d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1606d763cef2SBarry Smith 
1607d763cef2SBarry Smith .keywords: TS, set, timestep
1608d763cef2SBarry Smith @*/
16097087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1610d763cef2SBarry Smith {
1611d763cef2SBarry Smith   PetscFunctionBegin;
16120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1613c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1614d763cef2SBarry Smith   ts->time_step      = time_step;
161531748224SBarry Smith   ts->time_step_orig = time_step;
1616d763cef2SBarry Smith   PetscFunctionReturn(0);
1617d763cef2SBarry Smith }
1618d763cef2SBarry Smith 
16194a2ae208SSatish Balay #undef __FUNCT__
1620a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1621a43b19c4SJed Brown /*@
162249354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
162349354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
162449354f04SShri Abhyankar      or just return at the final time TS computed.
1625a43b19c4SJed Brown 
1626a43b19c4SJed Brown   Logically Collective on TS
1627a43b19c4SJed Brown 
1628a43b19c4SJed Brown    Input Parameter:
1629a43b19c4SJed Brown +   ts - the time-step context
163049354f04SShri Abhyankar -   eftopt - exact final time option
1631a43b19c4SJed Brown 
1632a43b19c4SJed Brown    Level: beginner
1633a43b19c4SJed Brown 
1634a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1635a43b19c4SJed Brown @*/
163649354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1637a43b19c4SJed Brown {
1638a43b19c4SJed Brown   PetscFunctionBegin;
1639a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
164049354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
164149354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1642a43b19c4SJed Brown   PetscFunctionReturn(0);
1643a43b19c4SJed Brown }
1644a43b19c4SJed Brown 
1645a43b19c4SJed Brown #undef __FUNCT__
16464a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1647d763cef2SBarry Smith /*@
1648d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1649d763cef2SBarry Smith 
1650d763cef2SBarry Smith    Not Collective
1651d763cef2SBarry Smith 
1652d763cef2SBarry Smith    Input Parameter:
1653d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1654d763cef2SBarry Smith 
1655d763cef2SBarry Smith    Output Parameter:
1656d763cef2SBarry Smith .  dt - the current timestep size
1657d763cef2SBarry Smith 
1658d763cef2SBarry Smith    Level: intermediate
1659d763cef2SBarry Smith 
1660d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1661d763cef2SBarry Smith 
1662d763cef2SBarry Smith .keywords: TS, get, timestep
1663d763cef2SBarry Smith @*/
16647087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1665d763cef2SBarry Smith {
1666d763cef2SBarry Smith   PetscFunctionBegin;
16670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1668f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1669d763cef2SBarry Smith   *dt = ts->time_step;
1670d763cef2SBarry Smith   PetscFunctionReturn(0);
1671d763cef2SBarry Smith }
1672d763cef2SBarry Smith 
16734a2ae208SSatish Balay #undef __FUNCT__
16744a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1675d8e5e3e6SSatish Balay /*@
1676d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1677d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1678d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1679d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1680d763cef2SBarry Smith 
1681d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1682d763cef2SBarry Smith 
1683d763cef2SBarry Smith    Input Parameter:
1684d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1685d763cef2SBarry Smith 
1686d763cef2SBarry Smith    Output Parameter:
1687d763cef2SBarry Smith .  v - the vector containing the solution
1688d763cef2SBarry Smith 
1689d763cef2SBarry Smith    Level: intermediate
1690d763cef2SBarry Smith 
1691d763cef2SBarry Smith .seealso: TSGetTimeStep()
1692d763cef2SBarry Smith 
1693d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1694d763cef2SBarry Smith @*/
16957087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1696d763cef2SBarry Smith {
1697d763cef2SBarry Smith   PetscFunctionBegin;
16980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16994482741eSBarry Smith   PetscValidPointer(v,2);
17008737fe31SLisandro Dalcin   *v = ts->vec_sol;
1701d763cef2SBarry Smith   PetscFunctionReturn(0);
1702d763cef2SBarry Smith }
1703d763cef2SBarry Smith 
1704c235aa8dSHong Zhang #undef __FUNCT__
1705d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointGetSensitivity"
1706c235aa8dSHong Zhang /*@
1707d4aa0a58SBarry Smith    TSAdjointGetSensitivity - Returns the sensitivity in the reverse mode.
1708c235aa8dSHong Zhang 
1709c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1710c235aa8dSHong Zhang 
1711c235aa8dSHong Zhang    Input Parameter:
1712c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1713c235aa8dSHong Zhang 
1714c235aa8dSHong Zhang    Output Parameter:
1715c235aa8dSHong Zhang .  v - the vector containing the solution
1716c235aa8dSHong Zhang 
1717c235aa8dSHong Zhang    Level: intermediate
1718c235aa8dSHong Zhang 
1719c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1720c235aa8dSHong Zhang 
1721c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1722c235aa8dSHong Zhang @*/
1723d4aa0a58SBarry Smith PetscErrorCode  TSAdjointGetSensitivity(TS ts,Vec **v,PetscInt *numberadjs)
1724c235aa8dSHong Zhang {
1725c235aa8dSHong Zhang   PetscFunctionBegin;
1726c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1727c235aa8dSHong Zhang   PetscValidPointer(v,2);
1728b18ea86cSHong Zhang   *v          = ts->vecs_sensi;
1729b18ea86cSHong Zhang   if(numberadjs) *numberadjs = ts->numberadjs;
1730c235aa8dSHong Zhang   PetscFunctionReturn(0);
1731c235aa8dSHong Zhang }
1732c235aa8dSHong Zhang 
1733bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
17344a2ae208SSatish Balay #undef __FUNCT__
1735bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1736d8e5e3e6SSatish Balay /*@
1737bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1738d763cef2SBarry Smith 
1739bdad233fSMatthew Knepley   Not collective
1740d763cef2SBarry Smith 
1741bdad233fSMatthew Knepley   Input Parameters:
1742bdad233fSMatthew Knepley + ts   - The TS
1743bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1744d763cef2SBarry Smith .vb
17450910c330SBarry Smith          U_t - A U = 0      (linear)
17460910c330SBarry Smith          U_t - A(t) U = 0   (linear)
17470910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1748d763cef2SBarry Smith .ve
1749d763cef2SBarry Smith 
1750d763cef2SBarry Smith    Level: beginner
1751d763cef2SBarry Smith 
1752bdad233fSMatthew Knepley .keywords: TS, problem type
1753bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1754d763cef2SBarry Smith @*/
17557087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1756a7cc72afSBarry Smith {
17579e2a6581SJed Brown   PetscErrorCode ierr;
17589e2a6581SJed Brown 
1759d763cef2SBarry Smith   PetscFunctionBegin;
17600700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1761bdad233fSMatthew Knepley   ts->problem_type = type;
17629e2a6581SJed Brown   if (type == TS_LINEAR) {
17639e2a6581SJed Brown     SNES snes;
17649e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
17659e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
17669e2a6581SJed Brown   }
1767d763cef2SBarry Smith   PetscFunctionReturn(0);
1768d763cef2SBarry Smith }
1769d763cef2SBarry Smith 
1770bdad233fSMatthew Knepley #undef __FUNCT__
1771bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1772bdad233fSMatthew Knepley /*@C
1773bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1774bdad233fSMatthew Knepley 
1775bdad233fSMatthew Knepley   Not collective
1776bdad233fSMatthew Knepley 
1777bdad233fSMatthew Knepley   Input Parameter:
1778bdad233fSMatthew Knepley . ts   - The TS
1779bdad233fSMatthew Knepley 
1780bdad233fSMatthew Knepley   Output Parameter:
1781bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1782bdad233fSMatthew Knepley .vb
1783089b2837SJed Brown          M U_t = A U
1784089b2837SJed Brown          M(t) U_t = A(t) U
1785b5abc632SBarry Smith          F(t,U,U_t)
1786bdad233fSMatthew Knepley .ve
1787bdad233fSMatthew Knepley 
1788bdad233fSMatthew Knepley    Level: beginner
1789bdad233fSMatthew Knepley 
1790bdad233fSMatthew Knepley .keywords: TS, problem type
1791bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1792bdad233fSMatthew Knepley @*/
17937087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1794a7cc72afSBarry Smith {
1795bdad233fSMatthew Knepley   PetscFunctionBegin;
17960700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
17974482741eSBarry Smith   PetscValidIntPointer(type,2);
1798bdad233fSMatthew Knepley   *type = ts->problem_type;
1799bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1800bdad233fSMatthew Knepley }
1801d763cef2SBarry Smith 
18024a2ae208SSatish Balay #undef __FUNCT__
18034a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1804d763cef2SBarry Smith /*@
1805d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1806d763cef2SBarry Smith    of a timestepper.
1807d763cef2SBarry Smith 
1808d763cef2SBarry Smith    Collective on TS
1809d763cef2SBarry Smith 
1810d763cef2SBarry Smith    Input Parameter:
1811d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1812d763cef2SBarry Smith 
1813d763cef2SBarry Smith    Notes:
1814d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1815d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1816d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1817d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1818d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1819d763cef2SBarry Smith 
1820d763cef2SBarry Smith    Level: advanced
1821d763cef2SBarry Smith 
1822d763cef2SBarry Smith .keywords: TS, timestep, setup
1823d763cef2SBarry Smith 
1824d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1825d763cef2SBarry Smith @*/
18267087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1827d763cef2SBarry Smith {
1828dfbe8321SBarry Smith   PetscErrorCode ierr;
18296c6b9e74SPeter Brune   DM             dm;
18306c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1831d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
18326c6b9e74SPeter Brune   TSIJacobian    ijac;
18336c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1834d763cef2SBarry Smith 
1835d763cef2SBarry Smith   PetscFunctionBegin;
18360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1837277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1838277b19d0SLisandro Dalcin 
18392c18e0fdSBarry Smith   ts->total_steps = 0;
18407adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
18419596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1842d763cef2SBarry Smith   }
1843277b19d0SLisandro Dalcin 
1844277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1845277b19d0SLisandro Dalcin 
1846c235aa8dSHong Zhang 
1847552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
18481c3436cfSJed Brown 
1849e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1850e1244c69SJed Brown     Mat Amat,Pmat;
1851e1244c69SJed Brown     SNES snes;
1852e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1853e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1854e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1855e1244c69SJed Brown      * have displaced the RHS matrix */
1856e1244c69SJed Brown     if (Amat == ts->Arhs) {
1857e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1858e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1859e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1860e1244c69SJed Brown     }
1861e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1862e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1863e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1864e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1865e1244c69SJed Brown     }
1866e1244c69SJed Brown   }
1867277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1868000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1869277b19d0SLisandro Dalcin   }
1870277b19d0SLisandro Dalcin 
18716c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
18726c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
18736c6b9e74SPeter Brune    */
18746c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
18750298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
18766c6b9e74SPeter Brune   if (!func) {
18776c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
18786c6b9e74SPeter Brune   }
18796c6b9e74SPeter 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.
18806c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
18816c6b9e74SPeter Brune    */
18820298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
18830298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
18840298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
18856c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
18866c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
18876c6b9e74SPeter Brune   }
1888277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1889277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1890277b19d0SLisandro Dalcin }
1891277b19d0SLisandro Dalcin 
1892277b19d0SLisandro Dalcin #undef __FUNCT__
1893f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
1894f6a906c0SBarry Smith /*@
1895f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
1896f6a906c0SBarry Smith    of an adjoint solver
1897f6a906c0SBarry Smith 
1898f6a906c0SBarry Smith    Collective on TS
1899f6a906c0SBarry Smith 
1900f6a906c0SBarry Smith    Input Parameter:
1901f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
1902f6a906c0SBarry Smith 
1903f6a906c0SBarry Smith    Notes:
1904f6a906c0SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1905f6a906c0SBarry Smith    TSSetUp(), since these actions will automatically occur during
1906f6a906c0SBarry Smith    the call to TSStep().  However, if one wishes to control this
1907f6a906c0SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1908f6a906c0SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1909f6a906c0SBarry Smith 
1910f6a906c0SBarry Smith    Level: advanced
1911f6a906c0SBarry Smith 
1912f6a906c0SBarry Smith .keywords: TS, timestep, setup
1913f6a906c0SBarry Smith 
1914f6a906c0SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1915f6a906c0SBarry Smith @*/
1916f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
1917f6a906c0SBarry Smith {
1918f6a906c0SBarry Smith   PetscErrorCode ierr;
1919f6a906c0SBarry Smith 
1920f6a906c0SBarry Smith   PetscFunctionBegin;
1921f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1922f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
192308c7845fSBarry Smith   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetSensitivity() first");
1924f6a906c0SBarry Smith   if (ts->ops->setupadj) {
1925f6a906c0SBarry Smith     ierr = (*ts->ops->setupadj)(ts);CHKERRQ(ierr);
1926f6a906c0SBarry Smith   }
1927f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
1928f6a906c0SBarry Smith   PetscFunctionReturn(0);
1929f6a906c0SBarry Smith }
1930f6a906c0SBarry Smith 
1931f6a906c0SBarry Smith #undef __FUNCT__
1932277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1933277b19d0SLisandro Dalcin /*@
1934277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1935277b19d0SLisandro Dalcin 
1936277b19d0SLisandro Dalcin    Collective on TS
1937277b19d0SLisandro Dalcin 
1938277b19d0SLisandro Dalcin    Input Parameter:
1939277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1940277b19d0SLisandro Dalcin 
1941277b19d0SLisandro Dalcin    Level: beginner
1942277b19d0SLisandro Dalcin 
1943277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1944277b19d0SLisandro Dalcin 
1945277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1946277b19d0SLisandro Dalcin @*/
1947277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1948277b19d0SLisandro Dalcin {
1949277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1950277b19d0SLisandro Dalcin 
1951277b19d0SLisandro Dalcin   PetscFunctionBegin;
1952277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1953b18ea86cSHong Zhang 
1954277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1955277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1956277b19d0SLisandro Dalcin   }
1957277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1958bbd56ea5SKarl Rupp 
19594e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
19604e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1961214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
19626bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1963e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1964e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
196538637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1966f8fee759SHong Zhang   ts->vecs_sensi = 0;
1967f8fee759SHong Zhang   ts->vecs_sensip = 0;
1968ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
196905755b9cSHong Zhang   ierr = VecDestroy(&ts->vec_costquad);CHKERRQ(ierr);
197036eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
1971277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1972d763cef2SBarry Smith   PetscFunctionReturn(0);
1973d763cef2SBarry Smith }
1974d763cef2SBarry Smith 
19754a2ae208SSatish Balay #undef __FUNCT__
19764a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1977d8e5e3e6SSatish Balay /*@
1978d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1979d763cef2SBarry Smith    with TSCreate().
1980d763cef2SBarry Smith 
1981d763cef2SBarry Smith    Collective on TS
1982d763cef2SBarry Smith 
1983d763cef2SBarry Smith    Input Parameter:
1984d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1985d763cef2SBarry Smith 
1986d763cef2SBarry Smith    Level: beginner
1987d763cef2SBarry Smith 
1988d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1989d763cef2SBarry Smith 
1990d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1991d763cef2SBarry Smith @*/
19926bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1993d763cef2SBarry Smith {
19946849ba73SBarry Smith   PetscErrorCode ierr;
1995d763cef2SBarry Smith 
1996d763cef2SBarry Smith   PetscFunctionBegin;
19976bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
19986bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
19996bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2000d763cef2SBarry Smith 
20016bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2002277b19d0SLisandro Dalcin 
2003e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2004e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
20056bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
20066d4c513bSLisandro Dalcin 
200784df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
2008aeb4809dSShri Abhyankar   if ((*ts)->event) {
2009aeb4809dSShri Abhyankar     ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr);
2010aeb4809dSShri Abhyankar   }
20116bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
20126bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
20136bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
20146d4c513bSLisandro Dalcin 
2015a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2016d763cef2SBarry Smith   PetscFunctionReturn(0);
2017d763cef2SBarry Smith }
2018d763cef2SBarry Smith 
20194a2ae208SSatish Balay #undef __FUNCT__
20204a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2021d8e5e3e6SSatish Balay /*@
2022d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2023d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2024d763cef2SBarry Smith 
2025d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2026d763cef2SBarry Smith 
2027d763cef2SBarry Smith    Input Parameter:
2028d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2029d763cef2SBarry Smith 
2030d763cef2SBarry Smith    Output Parameter:
2031d763cef2SBarry Smith .  snes - the nonlinear solver context
2032d763cef2SBarry Smith 
2033d763cef2SBarry Smith    Notes:
2034d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2035d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
203694b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2037d763cef2SBarry Smith 
2038d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
20390298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2040d763cef2SBarry Smith 
2041d763cef2SBarry Smith    Level: beginner
2042d763cef2SBarry Smith 
2043d763cef2SBarry Smith .keywords: timestep, get, SNES
2044d763cef2SBarry Smith @*/
20457087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2046d763cef2SBarry Smith {
2047d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2048d372ba47SLisandro Dalcin 
2049d763cef2SBarry Smith   PetscFunctionBegin;
20500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20514482741eSBarry Smith   PetscValidPointer(snes,2);
2052d372ba47SLisandro Dalcin   if (!ts->snes) {
2053ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
20540298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20553bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2056d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2057496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
20589e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
20599e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
20609e2a6581SJed Brown     }
2061d372ba47SLisandro Dalcin   }
2062d763cef2SBarry Smith   *snes = ts->snes;
2063d763cef2SBarry Smith   PetscFunctionReturn(0);
2064d763cef2SBarry Smith }
2065d763cef2SBarry Smith 
20664a2ae208SSatish Balay #undef __FUNCT__
2067deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2068deb2cd25SJed Brown /*@
2069deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2070deb2cd25SJed Brown 
2071deb2cd25SJed Brown    Collective
2072deb2cd25SJed Brown 
2073deb2cd25SJed Brown    Input Parameter:
2074deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2075deb2cd25SJed Brown -  snes - the nonlinear solver context
2076deb2cd25SJed Brown 
2077deb2cd25SJed Brown    Notes:
2078deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2079deb2cd25SJed Brown 
2080deb2cd25SJed Brown    Level: developer
2081deb2cd25SJed Brown 
2082deb2cd25SJed Brown .keywords: timestep, set, SNES
2083deb2cd25SJed Brown @*/
2084deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2085deb2cd25SJed Brown {
2086deb2cd25SJed Brown   PetscErrorCode ierr;
2087d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2088deb2cd25SJed Brown 
2089deb2cd25SJed Brown   PetscFunctionBegin;
2090deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2091deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2092deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2093deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2094bbd56ea5SKarl Rupp 
2095deb2cd25SJed Brown   ts->snes = snes;
2096bbd56ea5SKarl Rupp 
20970298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20980298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2099740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
21000298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2101740132f1SEmil Constantinescu   }
2102deb2cd25SJed Brown   PetscFunctionReturn(0);
2103deb2cd25SJed Brown }
2104deb2cd25SJed Brown 
2105deb2cd25SJed Brown #undef __FUNCT__
210694b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2107d8e5e3e6SSatish Balay /*@
210894b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2109d763cef2SBarry Smith    a TS (timestepper) context.
2110d763cef2SBarry Smith 
211194b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2112d763cef2SBarry Smith 
2113d763cef2SBarry Smith    Input Parameter:
2114d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2115d763cef2SBarry Smith 
2116d763cef2SBarry Smith    Output Parameter:
211794b7f48cSBarry Smith .  ksp - the nonlinear solver context
2118d763cef2SBarry Smith 
2119d763cef2SBarry Smith    Notes:
212094b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2121d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2122d763cef2SBarry Smith    KSP and PC contexts as well.
2123d763cef2SBarry Smith 
212494b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
21250298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2126d763cef2SBarry Smith 
2127d763cef2SBarry Smith    Level: beginner
2128d763cef2SBarry Smith 
212994b7f48cSBarry Smith .keywords: timestep, get, KSP
2130d763cef2SBarry Smith @*/
21317087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2132d763cef2SBarry Smith {
2133d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2134089b2837SJed Brown   SNES           snes;
2135d372ba47SLisandro Dalcin 
2136d763cef2SBarry Smith   PetscFunctionBegin;
21370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21384482741eSBarry Smith   PetscValidPointer(ksp,2);
213917186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2140e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2141089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2142089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2143d763cef2SBarry Smith   PetscFunctionReturn(0);
2144d763cef2SBarry Smith }
2145d763cef2SBarry Smith 
2146d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2147d763cef2SBarry Smith 
21484a2ae208SSatish Balay #undef __FUNCT__
2149adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2150adb62b0dSMatthew Knepley /*@
2151adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2152adb62b0dSMatthew Knepley    maximum time for iteration.
2153adb62b0dSMatthew Knepley 
21543f9fe445SBarry Smith    Not Collective
2155adb62b0dSMatthew Knepley 
2156adb62b0dSMatthew Knepley    Input Parameters:
2157adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
21580298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
21590298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2160adb62b0dSMatthew Knepley 
2161adb62b0dSMatthew Knepley    Level: intermediate
2162adb62b0dSMatthew Knepley 
2163adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2164adb62b0dSMatthew Knepley @*/
21657087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2166adb62b0dSMatthew Knepley {
2167adb62b0dSMatthew Knepley   PetscFunctionBegin;
21680700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2169abc0a331SBarry Smith   if (maxsteps) {
21704482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2171adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2172adb62b0dSMatthew Knepley   }
2173abc0a331SBarry Smith   if (maxtime) {
21744482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2175adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2176adb62b0dSMatthew Knepley   }
2177adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2178adb62b0dSMatthew Knepley }
2179adb62b0dSMatthew Knepley 
2180adb62b0dSMatthew Knepley #undef __FUNCT__
21814a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2182d763cef2SBarry Smith /*@
2183d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2184d763cef2SBarry Smith    maximum time for iteration.
2185d763cef2SBarry Smith 
21863f9fe445SBarry Smith    Logically Collective on TS
2187d763cef2SBarry Smith 
2188d763cef2SBarry Smith    Input Parameters:
2189d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2190d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2191d763cef2SBarry Smith -  maxtime - final time to iterate to
2192d763cef2SBarry Smith 
2193d763cef2SBarry Smith    Options Database Keys:
2194d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
21953bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2196d763cef2SBarry Smith 
2197d763cef2SBarry Smith    Notes:
2198d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2199d763cef2SBarry Smith 
2200d763cef2SBarry Smith    Level: intermediate
2201d763cef2SBarry Smith 
2202d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2203a43b19c4SJed Brown 
2204a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2205d763cef2SBarry Smith @*/
22067087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2207d763cef2SBarry Smith {
2208d763cef2SBarry Smith   PetscFunctionBegin;
22090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2210c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2211c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
221239b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
221339b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2214d763cef2SBarry Smith   PetscFunctionReturn(0);
2215d763cef2SBarry Smith }
2216d763cef2SBarry Smith 
22174a2ae208SSatish Balay #undef __FUNCT__
22184a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2219d763cef2SBarry Smith /*@
2220d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2221d763cef2SBarry Smith    for use by the TS routines.
2222d763cef2SBarry Smith 
22233f9fe445SBarry Smith    Logically Collective on TS and Vec
2224d763cef2SBarry Smith 
2225d763cef2SBarry Smith    Input Parameters:
2226d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
22270910c330SBarry Smith -  u - the solution vector
2228d763cef2SBarry Smith 
2229d763cef2SBarry Smith    Level: beginner
2230d763cef2SBarry Smith 
2231d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2232d763cef2SBarry Smith @*/
22330910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2234d763cef2SBarry Smith {
22358737fe31SLisandro Dalcin   PetscErrorCode ierr;
2236496e6a7aSJed Brown   DM             dm;
22378737fe31SLisandro Dalcin 
2238d763cef2SBarry Smith   PetscFunctionBegin;
22390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22400910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
22410910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
22426bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2243bbd56ea5SKarl Rupp 
22440910c330SBarry Smith   ts->vec_sol = u;
2245bbd56ea5SKarl Rupp 
2246496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
22470910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2248d763cef2SBarry Smith   PetscFunctionReturn(0);
2249d763cef2SBarry Smith }
2250d763cef2SBarry Smith 
2251e74ef692SMatthew Knepley #undef __FUNCT__
2252d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetSensitivity"
2253c235aa8dSHong Zhang /*@
2254d4aa0a58SBarry Smith    TSAdjointSetSensitivity - Sets the initial value of sensitivity (w.r.t. initial conditions)
2255c235aa8dSHong Zhang    for use by the TS routines.
2256c235aa8dSHong Zhang 
2257c235aa8dSHong Zhang    Logically Collective on TS and Vec
2258c235aa8dSHong Zhang 
2259c235aa8dSHong Zhang    Input Parameters:
2260c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2261c235aa8dSHong Zhang -  u - the solution vector
2262c235aa8dSHong Zhang 
2263c235aa8dSHong Zhang    Level: beginner
2264c235aa8dSHong Zhang 
2265c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2266c235aa8dSHong Zhang @*/
2267d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetSensitivity(TS ts,Vec *u,PetscInt numberadjs)
2268c235aa8dSHong Zhang {
2269c235aa8dSHong Zhang   PetscFunctionBegin;
2270c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2271b18ea86cSHong Zhang   PetscValidPointer(u,2);
2272b18ea86cSHong Zhang   ts->vecs_sensi = u;
2273d4aa0a58SBarry 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()");
2274b18ea86cSHong Zhang   ts->numberadjs = numberadjs;
2275c235aa8dSHong Zhang 
2276ad8e2604SHong Zhang   PetscFunctionReturn(0);
2277ad8e2604SHong Zhang }
2278ad8e2604SHong Zhang 
2279ad8e2604SHong Zhang #undef __FUNCT__
2280d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetSensitivityP"
2281ad8e2604SHong Zhang /*@
2282d4aa0a58SBarry Smith    TSAdjointSetSensitivityP - Sets the initial value of sensitivity (w.r.t. parameters)
2283ad8e2604SHong Zhang    for use by the TS routines.
2284ad8e2604SHong Zhang 
2285ad8e2604SHong Zhang    Logically Collective on TS and Vec
2286ad8e2604SHong Zhang 
2287ad8e2604SHong Zhang    Input Parameters:
2288ad8e2604SHong Zhang +  ts - the TS context obtained from TSCreate()
2289ad8e2604SHong Zhang -  u - the solution vector
2290ad8e2604SHong Zhang 
2291ad8e2604SHong Zhang    Level: beginner
2292ad8e2604SHong Zhang 
2293ad8e2604SHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2294ad8e2604SHong Zhang @*/
2295d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetSensitivityP(TS ts,Vec *u,PetscInt numberadjs)
2296ad8e2604SHong Zhang {
2297ad8e2604SHong Zhang   PetscFunctionBegin;
2298ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2299ad8e2604SHong Zhang   PetscValidPointer(u,2);
2300ad8e2604SHong Zhang   ts->vecs_sensip = 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 TSAdjointSetSensitivity()");
2302ad8e2604SHong Zhang   ts->numberadjs = numberadjs;
2303ad8e2604SHong Zhang 
2304ad8e2604SHong Zhang   PetscFunctionReturn(0);
2305ad8e2604SHong Zhang }
2306ad8e2604SHong Zhang 
2307ad8e2604SHong Zhang #undef __FUNCT__
2308d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobianP"
2309ad8e2604SHong Zhang /*@C
2310d4aa0a58SBarry Smith   TSAdjointSetRHSJacobianP - Sets the function that computes the Jacobian w.r.t. parameters.
2311ad8e2604SHong Zhang 
2312ad8e2604SHong Zhang   Logically Collective on TS
2313ad8e2604SHong Zhang 
2314ad8e2604SHong Zhang   Input Parameters:
2315ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2316ad8e2604SHong Zhang - func - The function
2317ad8e2604SHong Zhang 
2318ad8e2604SHong Zhang   Calling sequence of func:
231905755b9cSHong Zhang $ func (TS ts,PetscReal t,Vec u,Mat A,void *ctx);
232005755b9cSHong Zhang +   t - current timestep
232105755b9cSHong Zhang .   u - input vector
232205755b9cSHong Zhang .   A - output matrix
232305755b9cSHong Zhang -   ctx - [optional] user-defined function context
2324ad8e2604SHong Zhang 
2325ad8e2604SHong Zhang   Level: intermediate
2326ad8e2604SHong Zhang 
2327ad8e2604SHong Zhang .keywords: TS, sensitivity
2328ad8e2604SHong Zhang .seealso:
2329ad8e2604SHong Zhang @*/
2330d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobianP(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2331ad8e2604SHong Zhang {
2332ad8e2604SHong Zhang   PetscErrorCode ierr;
2333ad8e2604SHong Zhang 
2334ad8e2604SHong Zhang   PetscFunctionBegin;
2335ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2336ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2337ad8e2604SHong Zhang 
2338ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2339ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2340ad8e2604SHong Zhang   if(Amat) {
2341ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2342ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2343ad8e2604SHong Zhang 
2344ad8e2604SHong Zhang     ts->Jacp = Amat;
2345ad8e2604SHong Zhang   }
2346ad8e2604SHong Zhang   PetscFunctionReturn(0);
2347ad8e2604SHong Zhang }
2348ad8e2604SHong Zhang 
2349ad8e2604SHong Zhang #undef __FUNCT__
2350d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobianP"
2351ad8e2604SHong Zhang /*@
2352d4aa0a58SBarry Smith   TSAdjointComputeRHSJacobianP - Runs the user-defined JacobianP function.
2353ad8e2604SHong Zhang 
2354ad8e2604SHong Zhang   Collective on TS
2355ad8e2604SHong Zhang 
2356ad8e2604SHong Zhang   Input Parameters:
2357ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2358ad8e2604SHong Zhang 
2359ad8e2604SHong Zhang   Level: developer
2360ad8e2604SHong Zhang 
2361ad8e2604SHong Zhang .keywords: TS, sensitivity
2362d4aa0a58SBarry Smith .seealso: TSAdjointSetRHSJacobianP()
2363ad8e2604SHong Zhang @*/
2364d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobianP(TS ts,PetscReal t,Vec X,Mat Amat)
2365ad8e2604SHong Zhang {
2366ad8e2604SHong Zhang   PetscErrorCode ierr;
2367ad8e2604SHong Zhang 
2368ad8e2604SHong Zhang   PetscFunctionBegin;
2369ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2370ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2371ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2372ad8e2604SHong Zhang 
2373ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2374ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2375ad8e2604SHong Zhang   PetscStackPop;
2376ad8e2604SHong Zhang 
2377c235aa8dSHong Zhang   PetscFunctionReturn(0);
2378c235aa8dSHong Zhang }
2379c235aa8dSHong Zhang 
2380c235aa8dSHong Zhang #undef __FUNCT__
2381d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetCostIntegrand"
23826fd0887fSHong Zhang /*@C
2383d4aa0a58SBarry Smith     TSAdjointSetCostIntegrand - Sets the routine for evaluating the quadrature (or integral) term in a cost function,
23846fd0887fSHong Zhang     where Q_t = r(t,u).
23856fd0887fSHong Zhang 
23866fd0887fSHong Zhang     Logically Collective on TS
23876fd0887fSHong Zhang 
23886fd0887fSHong Zhang     Input Parameters:
23896fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
239036eaed60SHong Zhang .   q -  vector to put the computed quadrature term in the cost function (or NULL to have it created)
23916fd0887fSHong Zhang .   fq - routine for evaluating the right-hand-side function
23926fd0887fSHong Zhang -   ctx - [optional] user-defined context for private data for the
23936fd0887fSHong Zhang           function evaluation routine (may be NULL)
23946fd0887fSHong Zhang 
23956fd0887fSHong Zhang     Calling sequence of func:
2396a2ae3dd2SHong Zhang $     TSCostIntegrand(TS ts,PetscReal t,Vec u,PetscReal *f,void *ctx);
23976fd0887fSHong Zhang 
23986fd0887fSHong Zhang +   t - current timestep
23996fd0887fSHong Zhang .   u - input vector
240036eaed60SHong Zhang .   f - function vector
24016fd0887fSHong Zhang -   ctx - [optional] user-defined function context
24026fd0887fSHong Zhang 
24036fd0887fSHong Zhang     Level: beginner
24046fd0887fSHong Zhang 
24056fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
24066fd0887fSHong Zhang 
2407d4aa0a58SBarry Smith .seealso: TSAdjointSetRHSJacobianP(),TSAdjointSetSensitivity(),TSAdjointSetSensitivityP()
24086fd0887fSHong Zhang @*/
2409d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetCostIntegrand(TS ts,PetscInt numberadjs,Vec q,PetscErrorCode (*fq)(TS,PetscReal,Vec,Vec,void*),void *ctx)
24106fd0887fSHong Zhang {
24116fd0887fSHong Zhang   PetscErrorCode ierr;
241236eaed60SHong Zhang   PetscInt size;
24136fd0887fSHong Zhang 
24146fd0887fSHong Zhang   PetscFunctionBegin;
24156fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
241636eaed60SHong Zhang   if (q) {
241736eaed60SHong Zhang     PetscValidHeaderSpecific(q,VEC_CLASSID,2);
241836eaed60SHong Zhang   } else {
2419d4aa0a58SBarry 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");
24206fd0887fSHong Zhang   }
2421d4aa0a58SBarry 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.");
2422d4aa0a58SBarry 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()");
242336eaed60SHong Zhang   ierr = VecGetSize(q,&size);CHKERRQ(ierr);
2424a2ae3dd2SHong Zhang   ierr = VecZeroEntries(q);CHKERRQ(ierr);
2425d4aa0a58SBarry 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()).");
24266fd0887fSHong Zhang 
242736eaed60SHong Zhang   ierr = PetscObjectReference((PetscObject)q);CHKERRQ(ierr);
242836eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costquad);CHKERRQ(ierr);
242936eaed60SHong Zhang   ts->vec_costquad = q;
243036eaed60SHong Zhang 
2431a2ae3dd2SHong Zhang   ierr                  = VecDuplicate(ts->vec_costquad,&ts->vec_costintegrand);CHKERRQ(ierr);
2432e4680132SHong Zhang   ts->costintegrand     = fq;
243305755b9cSHong Zhang   ts->costintegrandctx  = ctx;
24346fd0887fSHong Zhang 
24356fd0887fSHong Zhang   PetscFunctionReturn(0);
24366fd0887fSHong Zhang }
24376fd0887fSHong Zhang 
24386fd0887fSHong Zhang #undef __FUNCT__
2439d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointGetCostQuadrature"
244036eaed60SHong Zhang /*@
2441d4aa0a58SBarry Smith    TSAdjointGetCostQuadrature - Returns the values of the quadrature (or integral) terms in a cost function.
244236eaed60SHong Zhang    It is valid to call the routine after a backward run.
244336eaed60SHong Zhang 
244436eaed60SHong Zhang    Not Collective
244536eaed60SHong Zhang 
244636eaed60SHong Zhang    Input Parameter:
244736eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
244836eaed60SHong Zhang 
244936eaed60SHong Zhang    Output Parameter:
245036eaed60SHong Zhang .  v - the vector containing the solution
245136eaed60SHong Zhang 
245236eaed60SHong Zhang    Level: intermediate
245336eaed60SHong Zhang 
2454d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
245536eaed60SHong Zhang 
245636eaed60SHong Zhang .keywords: TS, sensitivity analysis
245736eaed60SHong Zhang @*/
2458d4aa0a58SBarry Smith PetscErrorCode  TSAdjointGetCostQuadrature(TS ts,Vec *v)
245936eaed60SHong Zhang {
246036eaed60SHong Zhang   PetscFunctionBegin;
246136eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
246236eaed60SHong Zhang   PetscValidPointer(v,2);
246336eaed60SHong Zhang   *v = ts->vec_costquad;
246436eaed60SHong Zhang   PetscFunctionReturn(0);
246536eaed60SHong Zhang }
246636eaed60SHong Zhang 
246736eaed60SHong Zhang #undef __FUNCT__
2468d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
24696fd0887fSHong Zhang /*@
2470d4aa0a58SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the quadrature function in the cost functions.
24716fd0887fSHong Zhang 
24726fd0887fSHong Zhang    Input Parameters:
24736fd0887fSHong Zhang +  ts - the TS context
24746fd0887fSHong Zhang .  t - current time
247505755b9cSHong Zhang -  U - state vector
24766fd0887fSHong Zhang 
24776fd0887fSHong Zhang    Output Parameter:
24786fd0887fSHong Zhang .  q - vector of size numberadjs to hold the outputs
24796fd0887fSHong Zhang 
24806fd0887fSHong Zhang    Note:
24816fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
24826fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
24836fd0887fSHong Zhang 
24846fd0887fSHong Zhang    Level: developer
24856fd0887fSHong Zhang 
24866fd0887fSHong Zhang .keywords: TS, compute
24876fd0887fSHong Zhang 
2488d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
24896fd0887fSHong Zhang @*/
2490d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec U,Vec q)
24916fd0887fSHong Zhang {
24926fd0887fSHong Zhang   PetscErrorCode ierr;
24936fd0887fSHong Zhang 
24946fd0887fSHong Zhang   PetscFunctionBegin;
24956fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24966fd0887fSHong Zhang   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
24976fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
24986fd0887fSHong Zhang 
24996fd0887fSHong Zhang   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr);
2500e4680132SHong Zhang   if (ts->costintegrand) {
2501e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
250205755b9cSHong Zhang     ierr = (*ts->costintegrand)(ts,t,U,q,ts->costintegrandctx);CHKERRQ(ierr);
25036fd0887fSHong Zhang     PetscStackPop;
25046fd0887fSHong Zhang   } else {
25056fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
25066fd0887fSHong Zhang   }
25076fd0887fSHong Zhang 
25086fd0887fSHong Zhang   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr);
25096fd0887fSHong Zhang   PetscFunctionReturn(0);
25106fd0887fSHong Zhang }
25116fd0887fSHong Zhang 
25126fd0887fSHong Zhang #undef __FUNCT__
2513d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetDRDYFunction"
251405755b9cSHong Zhang /*@C
2515d4aa0a58SBarry Smith   TSAdjointSetDRDYFunction - Sets the function that computes the gradient of the CostIntegrand function r w.r.t. states y.
251605755b9cSHong Zhang 
251705755b9cSHong Zhang   Logically Collective on TS
251805755b9cSHong Zhang 
251905755b9cSHong Zhang   Input Parameters:
252005755b9cSHong Zhang + ts   - The TS context obtained from TSCreate()
252105755b9cSHong Zhang - func - The function
252205755b9cSHong Zhang 
252305755b9cSHong Zhang   Calling sequence of func:
252407658559SBarry Smith . PetscErroCode func(TS ts,PetscReal t,Vec U,Vec *drdy,void *ctx);
252505755b9cSHong Zhang 
252605755b9cSHong Zhang   Level: intermediate
252705755b9cSHong Zhang 
252805755b9cSHong Zhang .keywords: TS, sensitivity
252905755b9cSHong Zhang .seealso:
253005755b9cSHong Zhang @*/
2531d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetDRDYFunction(TS ts,Vec *drdy,PetscErrorCode (*func)(TS,PetscReal,Vec,Vec*,void*),void *ctx)
253205755b9cSHong Zhang {
253305755b9cSHong Zhang   PetscFunctionBegin;
253405755b9cSHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
253505755b9cSHong Zhang 
253605755b9cSHong Zhang   ts->drdyfunction    = func;
253705755b9cSHong Zhang   ts->drdyfunctionctx = ctx;
253805755b9cSHong Zhang   ts->vecs_drdy       = drdy;
253905755b9cSHong Zhang   PetscFunctionReturn(0);
254005755b9cSHong Zhang }
254105755b9cSHong Zhang 
254205755b9cSHong Zhang #undef __FUNCT__
2543d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
254405755b9cSHong Zhang /*@
2545d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
254605755b9cSHong Zhang 
254705755b9cSHong Zhang   Collective on TS
254805755b9cSHong Zhang 
254905755b9cSHong Zhang   Input Parameters:
255005755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
255105755b9cSHong Zhang 
255205755b9cSHong Zhang   Notes:
2553d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
255405755b9cSHong Zhang   so most users would not generally call this routine themselves.
255505755b9cSHong Zhang 
255605755b9cSHong Zhang   Level: developer
255705755b9cSHong Zhang 
255805755b9cSHong Zhang .keywords: TS, sensitivity
2559d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
256005755b9cSHong Zhang @*/
2561d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec X,Vec *drdy)
256205755b9cSHong Zhang {
256305755b9cSHong Zhang   PetscErrorCode ierr;
256405755b9cSHong Zhang 
256505755b9cSHong Zhang   PetscFunctionBegin;
256605755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
256705755b9cSHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
256805755b9cSHong Zhang 
256905755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
257005755b9cSHong Zhang   ierr = (*ts->drdyfunction)(ts,t,X,drdy,ts->drdyfunctionctx); CHKERRQ(ierr);
257105755b9cSHong Zhang   PetscStackPop;
257205755b9cSHong Zhang   PetscFunctionReturn(0);
257305755b9cSHong Zhang }
257405755b9cSHong Zhang 
257505755b9cSHong Zhang #undef __FUNCT__
2576d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetDRDPFunction"
257705755b9cSHong Zhang /*@C
2578d4aa0a58SBarry Smith   TSAdjointSetDRDPFunction - Sets the function that computes the gradient of the CostIntegrand function w.r.t. parameters.
257905755b9cSHong Zhang 
258005755b9cSHong Zhang   Logically Collective on TS
258105755b9cSHong Zhang 
258205755b9cSHong Zhang   Input Parameters:
258305755b9cSHong Zhang + ts   - The TS context obtained from TSCreate()
258405755b9cSHong Zhang - func - The function
258505755b9cSHong Zhang 
258605755b9cSHong Zhang   Calling sequence of func:
258707658559SBarry Smith . func(TS ts,PetscReal t,Vec U,Vec *drdy,void *ctx);
258805755b9cSHong Zhang 
258905755b9cSHong Zhang   Level: intermediate
259005755b9cSHong Zhang 
259105755b9cSHong Zhang .keywords: TS, sensitivity
259205755b9cSHong Zhang .seealso:
259305755b9cSHong Zhang @*/
2594d4aa0a58SBarry Smith PetscErrorCode  TSAdjointSetDRDPFunction(TS ts,Vec *drdp,PetscErrorCode (*func)(TS,PetscReal,Vec,Vec*,void*),void *ctx)
259505755b9cSHong Zhang {
259605755b9cSHong Zhang   PetscFunctionBegin;
259705755b9cSHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
259805755b9cSHong Zhang 
259905755b9cSHong Zhang   ts->drdpfunction    = func;
260005755b9cSHong Zhang   ts->drdpfunctionctx = ctx;
260105755b9cSHong Zhang   ts->vecs_drdp       = drdp;
260205755b9cSHong Zhang 
260305755b9cSHong Zhang   PetscFunctionReturn(0);
260405755b9cSHong Zhang }
260505755b9cSHong Zhang 
260605755b9cSHong Zhang #undef __FUNCT__
2607d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
260805755b9cSHong Zhang /*@
2609d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
261005755b9cSHong Zhang 
261105755b9cSHong Zhang   Collective on TS
261205755b9cSHong Zhang 
261305755b9cSHong Zhang   Input Parameters:
261405755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
261505755b9cSHong Zhang 
261605755b9cSHong Zhang   Notes:
261705755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
261805755b9cSHong Zhang   so most users would not generally call this routine themselves.
261905755b9cSHong Zhang 
262005755b9cSHong Zhang   Level: developer
262105755b9cSHong Zhang 
262205755b9cSHong Zhang .keywords: TS, sensitivity
2623d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
262405755b9cSHong Zhang @*/
2625d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec X,Vec *drdp)
262605755b9cSHong Zhang {
262705755b9cSHong Zhang   PetscErrorCode ierr;
262805755b9cSHong Zhang 
262905755b9cSHong Zhang   PetscFunctionBegin;
263005755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
263105755b9cSHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
263205755b9cSHong Zhang 
263305755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
263405755b9cSHong Zhang   ierr = (*ts->drdpfunction)(ts,t,X,drdp,ts->drdpfunctionctx); CHKERRQ(ierr);
263505755b9cSHong Zhang   PetscStackPop;
263605755b9cSHong Zhang 
263705755b9cSHong Zhang   PetscFunctionReturn(0);
263805755b9cSHong Zhang }
263905755b9cSHong Zhang 
264005755b9cSHong Zhang #undef __FUNCT__
2641e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2642ac226902SBarry Smith /*@C
2643000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
26443f2090d5SJed Brown   called once at the beginning of each time step.
2645000e7ae3SMatthew Knepley 
26463f9fe445SBarry Smith   Logically Collective on TS
2647000e7ae3SMatthew Knepley 
2648000e7ae3SMatthew Knepley   Input Parameters:
2649000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2650000e7ae3SMatthew Knepley - func - The function
2651000e7ae3SMatthew Knepley 
2652000e7ae3SMatthew Knepley   Calling sequence of func:
2653000e7ae3SMatthew Knepley . func (TS ts);
2654000e7ae3SMatthew Knepley 
2655000e7ae3SMatthew Knepley   Level: intermediate
2656000e7ae3SMatthew Knepley 
2657b8123daeSJed Brown   Note:
2658b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2659b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2660b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2661b8123daeSJed Brown 
2662000e7ae3SMatthew Knepley .keywords: TS, timestep
26639be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2664000e7ae3SMatthew Knepley @*/
26657087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2666000e7ae3SMatthew Knepley {
2667000e7ae3SMatthew Knepley   PetscFunctionBegin;
26680700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2669ae60f76fSBarry Smith   ts->prestep = func;
2670000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2671000e7ae3SMatthew Knepley }
2672000e7ae3SMatthew Knepley 
2673e74ef692SMatthew Knepley #undef __FUNCT__
26743f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
267509ee8438SJed Brown /*@
26763f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
26773f2090d5SJed Brown 
26783f2090d5SJed Brown   Collective on TS
26793f2090d5SJed Brown 
26803f2090d5SJed Brown   Input Parameters:
26813f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
26823f2090d5SJed Brown 
26833f2090d5SJed Brown   Notes:
26843f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
26853f2090d5SJed Brown   so most users would not generally call this routine themselves.
26863f2090d5SJed Brown 
26873f2090d5SJed Brown   Level: developer
26883f2090d5SJed Brown 
26893f2090d5SJed Brown .keywords: TS, timestep
26909be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
26913f2090d5SJed Brown @*/
26927087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
26933f2090d5SJed Brown {
26943f2090d5SJed Brown   PetscErrorCode ierr;
26953f2090d5SJed Brown 
26963f2090d5SJed Brown   PetscFunctionBegin;
26970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2698ae60f76fSBarry Smith   if (ts->prestep) {
2699ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2700312ce896SJed Brown   }
27013f2090d5SJed Brown   PetscFunctionReturn(0);
27023f2090d5SJed Brown }
27033f2090d5SJed Brown 
27043f2090d5SJed Brown #undef __FUNCT__
2705b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2706b8123daeSJed Brown /*@C
2707b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2708b8123daeSJed Brown   called once at the beginning of each stage.
2709b8123daeSJed Brown 
2710b8123daeSJed Brown   Logically Collective on TS
2711b8123daeSJed Brown 
2712b8123daeSJed Brown   Input Parameters:
2713b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2714b8123daeSJed Brown - func - The function
2715b8123daeSJed Brown 
2716b8123daeSJed Brown   Calling sequence of func:
2717b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2718b8123daeSJed Brown 
2719b8123daeSJed Brown   Level: intermediate
2720b8123daeSJed Brown 
2721b8123daeSJed Brown   Note:
2722b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2723b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2724b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2725b8123daeSJed Brown 
2726b8123daeSJed Brown .keywords: TS, timestep
27279be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2728b8123daeSJed Brown @*/
2729b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2730b8123daeSJed Brown {
2731b8123daeSJed Brown   PetscFunctionBegin;
2732b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2733ae60f76fSBarry Smith   ts->prestage = func;
2734b8123daeSJed Brown   PetscFunctionReturn(0);
2735b8123daeSJed Brown }
2736b8123daeSJed Brown 
2737b8123daeSJed Brown #undef __FUNCT__
27389be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
27399be3e283SDebojyoti Ghosh /*@C
27409be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
27419be3e283SDebojyoti Ghosh   called once at the end of each stage.
27429be3e283SDebojyoti Ghosh 
27439be3e283SDebojyoti Ghosh   Logically Collective on TS
27449be3e283SDebojyoti Ghosh 
27459be3e283SDebojyoti Ghosh   Input Parameters:
27469be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
27479be3e283SDebojyoti Ghosh - func - The function
27489be3e283SDebojyoti Ghosh 
27499be3e283SDebojyoti Ghosh   Calling sequence of func:
27509be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
27519be3e283SDebojyoti Ghosh 
27529be3e283SDebojyoti Ghosh   Level: intermediate
27539be3e283SDebojyoti Ghosh 
27549be3e283SDebojyoti Ghosh   Note:
27559be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
27569be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
27579be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
27589be3e283SDebojyoti Ghosh 
27599be3e283SDebojyoti Ghosh .keywords: TS, timestep
27609be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
27619be3e283SDebojyoti Ghosh @*/
27629be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
27639be3e283SDebojyoti Ghosh {
27649be3e283SDebojyoti Ghosh   PetscFunctionBegin;
27659be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
27669be3e283SDebojyoti Ghosh   ts->poststage = func;
27679be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
27689be3e283SDebojyoti Ghosh }
27699be3e283SDebojyoti Ghosh 
27709be3e283SDebojyoti Ghosh #undef __FUNCT__
2771b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2772b8123daeSJed Brown /*@
2773b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2774b8123daeSJed Brown 
2775b8123daeSJed Brown   Collective on TS
2776b8123daeSJed Brown 
2777b8123daeSJed Brown   Input Parameters:
2778b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
27799be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2780b8123daeSJed Brown 
2781b8123daeSJed Brown   Notes:
2782b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2783b8123daeSJed Brown   most users would not generally call this routine themselves.
2784b8123daeSJed Brown 
2785b8123daeSJed Brown   Level: developer
2786b8123daeSJed Brown 
2787b8123daeSJed Brown .keywords: TS, timestep
27889be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2789b8123daeSJed Brown @*/
2790b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2791b8123daeSJed Brown {
2792b8123daeSJed Brown   PetscErrorCode ierr;
2793b8123daeSJed Brown 
2794b8123daeSJed Brown   PetscFunctionBegin;
2795b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2796ae60f76fSBarry Smith   if (ts->prestage) {
2797ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2798b8123daeSJed Brown   }
2799b8123daeSJed Brown   PetscFunctionReturn(0);
2800b8123daeSJed Brown }
2801b8123daeSJed Brown 
2802b8123daeSJed Brown #undef __FUNCT__
28039be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
28049be3e283SDebojyoti Ghosh /*@
28059be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
28069be3e283SDebojyoti Ghosh 
28079be3e283SDebojyoti Ghosh   Collective on TS
28089be3e283SDebojyoti Ghosh 
28099be3e283SDebojyoti Ghosh   Input Parameters:
28109be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
28119be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
28129be3e283SDebojyoti Ghosh   stageindex  - Stage number
28139be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
28149be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
28159be3e283SDebojyoti Ghosh 
28169be3e283SDebojyoti Ghosh   Notes:
28179be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
28189be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
28199be3e283SDebojyoti Ghosh 
28209be3e283SDebojyoti Ghosh   Level: developer
28219be3e283SDebojyoti Ghosh 
28229be3e283SDebojyoti Ghosh .keywords: TS, timestep
28239be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
28249be3e283SDebojyoti Ghosh @*/
28259be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
28269be3e283SDebojyoti Ghosh {
28279be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
28289be3e283SDebojyoti Ghosh 
28299be3e283SDebojyoti Ghosh   PetscFunctionBegin;
28309be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28314beae5d8SLisandro Dalcin   if (ts->poststage) {
28329be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
28339be3e283SDebojyoti Ghosh   }
28349be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
28359be3e283SDebojyoti Ghosh }
28369be3e283SDebojyoti Ghosh 
28379be3e283SDebojyoti Ghosh #undef __FUNCT__
2838e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2839ac226902SBarry Smith /*@C
2840000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
28413f2090d5SJed Brown   called once at the end of each time step.
2842000e7ae3SMatthew Knepley 
28433f9fe445SBarry Smith   Logically Collective on TS
2844000e7ae3SMatthew Knepley 
2845000e7ae3SMatthew Knepley   Input Parameters:
2846000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2847000e7ae3SMatthew Knepley - func - The function
2848000e7ae3SMatthew Knepley 
2849000e7ae3SMatthew Knepley   Calling sequence of func:
2850b8123daeSJed Brown $ func (TS ts);
2851000e7ae3SMatthew Knepley 
2852000e7ae3SMatthew Knepley   Level: intermediate
2853000e7ae3SMatthew Knepley 
2854000e7ae3SMatthew Knepley .keywords: TS, timestep
2855b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2856000e7ae3SMatthew Knepley @*/
28577087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2858000e7ae3SMatthew Knepley {
2859000e7ae3SMatthew Knepley   PetscFunctionBegin;
28600700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2861ae60f76fSBarry Smith   ts->poststep = func;
2862000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2863000e7ae3SMatthew Knepley }
2864000e7ae3SMatthew Knepley 
2865e74ef692SMatthew Knepley #undef __FUNCT__
28663f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
286709ee8438SJed Brown /*@
28683f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
28693f2090d5SJed Brown 
28703f2090d5SJed Brown   Collective on TS
28713f2090d5SJed Brown 
28723f2090d5SJed Brown   Input Parameters:
28733f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
28743f2090d5SJed Brown 
28753f2090d5SJed Brown   Notes:
28763f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
28773f2090d5SJed Brown   so most users would not generally call this routine themselves.
28783f2090d5SJed Brown 
28793f2090d5SJed Brown   Level: developer
28803f2090d5SJed Brown 
28813f2090d5SJed Brown .keywords: TS, timestep
28823f2090d5SJed Brown @*/
28837087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
28843f2090d5SJed Brown {
28853f2090d5SJed Brown   PetscErrorCode ierr;
28863f2090d5SJed Brown 
28873f2090d5SJed Brown   PetscFunctionBegin;
28880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2889ae60f76fSBarry Smith   if (ts->poststep) {
2890ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
289172ac3e02SJed Brown   }
28923f2090d5SJed Brown   PetscFunctionReturn(0);
28933f2090d5SJed Brown }
28943f2090d5SJed Brown 
2895d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2896d763cef2SBarry Smith 
28974a2ae208SSatish Balay #undef __FUNCT__
2898a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2899d763cef2SBarry Smith /*@C
2900a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2901d763cef2SBarry Smith    timestep to display the iteration's  progress.
2902d763cef2SBarry Smith 
29033f9fe445SBarry Smith    Logically Collective on TS
2904d763cef2SBarry Smith 
2905d763cef2SBarry Smith    Input Parameters:
2906d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2907e213d8f1SJed Brown .  monitor - monitoring routine
2908329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
29090298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2910b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
29110298fd71SBarry Smith           (may be NULL)
2912d763cef2SBarry Smith 
2913e213d8f1SJed Brown    Calling sequence of monitor:
29140910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2915d763cef2SBarry Smith 
2916d763cef2SBarry Smith +    ts - the TS context
291788c05cc5SBarry 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
291888c05cc5SBarry Smith                                been interpolated to)
29191f06c33eSBarry Smith .    time - current time
29200910c330SBarry Smith .    u - current iterate
2921d763cef2SBarry Smith -    mctx - [optional] monitoring context
2922d763cef2SBarry Smith 
2923d763cef2SBarry Smith    Notes:
2924d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2925d763cef2SBarry Smith    already has been loaded.
2926d763cef2SBarry Smith 
2927025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2928025f1a04SBarry Smith 
2929d763cef2SBarry Smith    Level: intermediate
2930d763cef2SBarry Smith 
2931d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2932d763cef2SBarry Smith 
2933a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2934d763cef2SBarry Smith @*/
2935c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2936d763cef2SBarry Smith {
2937d763cef2SBarry Smith   PetscFunctionBegin;
29380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
293917186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2940d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
29418704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2942d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2943d763cef2SBarry Smith   PetscFunctionReturn(0);
2944d763cef2SBarry Smith }
2945d763cef2SBarry Smith 
29464a2ae208SSatish Balay #undef __FUNCT__
2947a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2948d763cef2SBarry Smith /*@C
2949a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2950d763cef2SBarry Smith 
29513f9fe445SBarry Smith    Logically Collective on TS
2952d763cef2SBarry Smith 
2953d763cef2SBarry Smith    Input Parameters:
2954d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2955d763cef2SBarry Smith 
2956d763cef2SBarry Smith    Notes:
2957d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2958d763cef2SBarry Smith 
2959d763cef2SBarry Smith    Level: intermediate
2960d763cef2SBarry Smith 
2961d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2962d763cef2SBarry Smith 
2963a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2964d763cef2SBarry Smith @*/
29657087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2966d763cef2SBarry Smith {
2967d952e501SBarry Smith   PetscErrorCode ierr;
2968d952e501SBarry Smith   PetscInt       i;
2969d952e501SBarry Smith 
2970d763cef2SBarry Smith   PetscFunctionBegin;
29710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2972d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
29738704b422SBarry Smith     if (ts->monitordestroy[i]) {
29748704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2975d952e501SBarry Smith     }
2976d952e501SBarry Smith   }
2977d763cef2SBarry Smith   ts->numbermonitors = 0;
2978d763cef2SBarry Smith   PetscFunctionReturn(0);
2979d763cef2SBarry Smith }
2980d763cef2SBarry Smith 
29814a2ae208SSatish Balay #undef __FUNCT__
2982a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2983d8e5e3e6SSatish Balay /*@
2984a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
29855516499fSSatish Balay 
29865516499fSSatish Balay    Level: intermediate
298741251cbbSSatish Balay 
29885516499fSSatish Balay .keywords: TS, set, monitor
29895516499fSSatish Balay 
299041251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
299141251cbbSSatish Balay @*/
2992649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2993d763cef2SBarry Smith {
2994dfbe8321SBarry Smith   PetscErrorCode ierr;
2995ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2996d132466eSBarry Smith 
2997d763cef2SBarry Smith   PetscFunctionBegin;
2998649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2999971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
3000649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3001d763cef2SBarry Smith   PetscFunctionReturn(0);
3002d763cef2SBarry Smith }
3003d763cef2SBarry Smith 
30044a2ae208SSatish Balay #undef __FUNCT__
3005cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
3006cd652676SJed Brown /*@
3007cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
3008cd652676SJed Brown 
3009cd652676SJed Brown    Logically Collective on TS
3010cd652676SJed Brown 
3011cd652676SJed Brown    Input Argument:
3012cd652676SJed Brown .  ts - time stepping context
3013cd652676SJed Brown 
3014cd652676SJed Brown    Output Argument:
3015cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
3016cd652676SJed Brown 
3017cd652676SJed Brown    Level: intermediate
3018cd652676SJed Brown 
3019cd652676SJed Brown .keywords: TS, set
3020cd652676SJed Brown 
3021cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
3022cd652676SJed Brown @*/
3023cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
3024cd652676SJed Brown {
3025cd652676SJed Brown   PetscFunctionBegin;
3026cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3027cd652676SJed Brown   ts->retain_stages = flg;
3028cd652676SJed Brown   PetscFunctionReturn(0);
3029cd652676SJed Brown }
3030cd652676SJed Brown 
3031cd652676SJed Brown #undef __FUNCT__
3032cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
3033cd652676SJed Brown /*@
3034cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3035cd652676SJed Brown 
3036cd652676SJed Brown    Collective on TS
3037cd652676SJed Brown 
3038cd652676SJed Brown    Input Argument:
3039cd652676SJed Brown +  ts - time stepping context
3040cd652676SJed Brown -  t - time to interpolate to
3041cd652676SJed Brown 
3042cd652676SJed Brown    Output Argument:
30430910c330SBarry Smith .  U - state at given time
3044cd652676SJed Brown 
3045cd652676SJed Brown    Notes:
3046cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
3047cd652676SJed Brown 
3048cd652676SJed Brown    Level: intermediate
3049cd652676SJed Brown 
3050cd652676SJed Brown    Developer Notes:
3051cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3052cd652676SJed Brown 
3053cd652676SJed Brown .keywords: TS, set
3054cd652676SJed Brown 
3055cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
3056cd652676SJed Brown @*/
30570910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3058cd652676SJed Brown {
3059cd652676SJed Brown   PetscErrorCode ierr;
3060cd652676SJed Brown 
3061cd652676SJed Brown   PetscFunctionBegin;
3062cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3063b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
30646712e2f1SBarry 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);
3065ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
30660910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3067cd652676SJed Brown   PetscFunctionReturn(0);
3068cd652676SJed Brown }
3069cd652676SJed Brown 
3070cd652676SJed Brown #undef __FUNCT__
30714a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3072d763cef2SBarry Smith /*@
30736d9e5789SSean Farley    TSStep - Steps one time step
3074d763cef2SBarry Smith 
3075d763cef2SBarry Smith    Collective on TS
3076d763cef2SBarry Smith 
3077d763cef2SBarry Smith    Input Parameter:
3078d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3079d763cef2SBarry Smith 
30806d9e5789SSean Farley    Level: intermediate
3081d763cef2SBarry Smith 
3082b8123daeSJed Brown    Notes:
3083b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3084b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3085b8123daeSJed Brown 
308625cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
308725cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
308825cb2221SBarry Smith 
3089d763cef2SBarry Smith .keywords: TS, timestep, solve
3090d763cef2SBarry Smith 
30919be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3092d763cef2SBarry Smith @*/
3093193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3094d763cef2SBarry Smith {
30952fb8446cSMatthew G. Knepley   DM               dm;
3096dfbe8321SBarry Smith   PetscErrorCode   ierr;
3097fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3098d763cef2SBarry Smith 
3099d763cef2SBarry Smith   PetscFunctionBegin;
31000700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3101fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3102fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3103fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3104fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3105fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3106fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3107fffbeea8SBarry Smith                                 "  year        = {2014}\n}\n",&cite);
3108fffbeea8SBarry Smith 
31092fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3110d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
3111d405a339SMatthew Knepley 
3112362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
311324655328SShri   ts->ptime_prev = ts->ptime;
3114cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
31154e9ba7eeSMatthew G. Knepley   ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
3116bbd56ea5SKarl Rupp 
3117e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3118d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3119193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
312008c7845fSBarry Smith   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
312108c7845fSBarry Smith 
312208c7845fSBarry Smith   ts->time_step_prev = ts->ptime - ts->ptime_prev;
312308c7845fSBarry Smith   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
312408c7845fSBarry Smith 
312508c7845fSBarry Smith   if (ts->reason < 0) {
312608c7845fSBarry Smith     if (ts->errorifstepfailed) {
312708c7845fSBarry Smith       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
312808c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3129d2daff3dSHong Zhang     }
313008c7845fSBarry Smith   } else if (!ts->reason) {
313108c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
313208c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
313308c7845fSBarry Smith   }
31342c18e0fdSBarry Smith   ts->total_steps++;
313508c7845fSBarry Smith   PetscFunctionReturn(0);
313608c7845fSBarry Smith }
313708c7845fSBarry Smith 
313808c7845fSBarry Smith #undef __FUNCT__
313908c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
314008c7845fSBarry Smith /*@
314108c7845fSBarry Smith    TSAdjointStep - Steps one time step
314208c7845fSBarry Smith 
314308c7845fSBarry Smith    Collective on TS
314408c7845fSBarry Smith 
314508c7845fSBarry Smith    Input Parameter:
314608c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
314708c7845fSBarry Smith 
314808c7845fSBarry Smith    Level: intermediate
314908c7845fSBarry Smith 
315008c7845fSBarry Smith    Notes:
315108c7845fSBarry Smith    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
315208c7845fSBarry Smith    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
315308c7845fSBarry Smith 
315408c7845fSBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
315508c7845fSBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
315608c7845fSBarry Smith 
315708c7845fSBarry Smith .keywords: TS, timestep, solve
315808c7845fSBarry Smith 
315908c7845fSBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
316008c7845fSBarry Smith @*/
316108c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
316208c7845fSBarry Smith {
316308c7845fSBarry Smith   DM               dm;
316408c7845fSBarry Smith   PetscErrorCode   ierr;
316508c7845fSBarry Smith 
316608c7845fSBarry Smith   PetscFunctionBegin;
316708c7845fSBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
316808c7845fSBarry Smith   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
316908c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
317008c7845fSBarry Smith 
317108c7845fSBarry Smith   ts->reason = TS_CONVERGED_ITERATING;
317208c7845fSBarry Smith   ts->ptime_prev = ts->ptime;
317308c7845fSBarry Smith   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
317408c7845fSBarry Smith   ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
317508c7845fSBarry Smith 
317608c7845fSBarry Smith   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
317708c7845fSBarry Smith   if (!ts->ops->stepadj) 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);
317808c7845fSBarry Smith   ierr = (*ts->ops->stepadj)(ts);CHKERRQ(ierr);
3179d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3180bbd56ea5SKarl Rupp 
318124655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3182cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3183362cd11cSLisandro Dalcin 
3184362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3185cef5090cSJed Brown     if (ts->errorifstepfailed) {
3186cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
3187ce94432eSBarry 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]);
31882a3a10ceSLisandro Dalcin       } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) {
31892a3a10ceSLisandro 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]);
3190ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3191cef5090cSJed Brown     }
3192362cd11cSLisandro Dalcin   } else if (!ts->reason) {
3193db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3194db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3195362cd11cSLisandro Dalcin   }
31962c18e0fdSBarry Smith   ts->total_steps--;
3197d763cef2SBarry Smith   PetscFunctionReturn(0);
3198d763cef2SBarry Smith }
3199d763cef2SBarry Smith 
32004a2ae208SSatish Balay #undef __FUNCT__
320105175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
320205175c85SJed Brown /*@
320305175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
320405175c85SJed Brown 
32051c3436cfSJed Brown    Collective on TS
320605175c85SJed Brown 
320705175c85SJed Brown    Input Arguments:
32081c3436cfSJed Brown +  ts - time stepping context
32091c3436cfSJed Brown .  order - desired order of accuracy
32100298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
321105175c85SJed Brown 
321205175c85SJed Brown    Output Arguments:
32130910c330SBarry Smith .  U - state at the end of the current step
321405175c85SJed Brown 
321505175c85SJed Brown    Level: advanced
321605175c85SJed Brown 
3217108c343cSJed Brown    Notes:
3218108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3219108c343cSJed 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.
3220108c343cSJed Brown 
32211c3436cfSJed Brown .seealso: TSStep(), TSAdapt
322205175c85SJed Brown @*/
32230910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
322405175c85SJed Brown {
322505175c85SJed Brown   PetscErrorCode ierr;
322605175c85SJed Brown 
322705175c85SJed Brown   PetscFunctionBegin;
322805175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
322905175c85SJed Brown   PetscValidType(ts,1);
32300910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3231ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
32320910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
323305175c85SJed Brown   PetscFunctionReturn(0);
323405175c85SJed Brown }
323505175c85SJed Brown 
323605175c85SJed Brown #undef __FUNCT__
3237*d67e68b7SBarry Smith #define __FUNCT__ "OutputBIN"
3238*d67e68b7SBarry Smith static PetscErrorCode OutputBIN(const char *filename, PetscViewer *viewer)
3239*d67e68b7SBarry Smith {
3240*d67e68b7SBarry Smith   PetscErrorCode ierr;
3241*d67e68b7SBarry Smith 
3242*d67e68b7SBarry Smith   PetscFunctionBeginUser;
3243*d67e68b7SBarry Smith   ierr = PetscViewerCreate(PETSC_COMM_WORLD, viewer);CHKERRQ(ierr);
3244*d67e68b7SBarry Smith   ierr = PetscViewerSetType(*viewer, PETSCVIEWERBINARY);CHKERRQ(ierr);
3245*d67e68b7SBarry Smith   ierr = PetscViewerFileSetMode(*viewer,FILE_MODE_WRITE);CHKERRQ(ierr);
3246*d67e68b7SBarry Smith   ierr = PetscViewerFileSetName(*viewer, filename);CHKERRQ(ierr);
3247*d67e68b7SBarry Smith   PetscFunctionReturn(0);
3248*d67e68b7SBarry Smith }
3249*d67e68b7SBarry Smith 
3250*d67e68b7SBarry Smith #undef __FUNCT__
3251*d67e68b7SBarry Smith #define __FUNCT__ "TSTrajectorySet"
3252*d67e68b7SBarry Smith PetscErrorCode TSTrajectorySet(TS ts,PetscInt stepnum,PetscReal time,Vec X)
3253*d67e68b7SBarry Smith {
3254*d67e68b7SBarry Smith   PetscViewer    viewer;
3255*d67e68b7SBarry Smith   PetscInt       ns,i;
3256*d67e68b7SBarry Smith   Vec            *Y;
3257*d67e68b7SBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
3258*d67e68b7SBarry Smith   PetscReal      tprev;
3259*d67e68b7SBarry Smith   PetscErrorCode ierr;
3260*d67e68b7SBarry Smith 
3261*d67e68b7SBarry Smith   PetscFunctionBeginUser;
3262*d67e68b7SBarry Smith   if (stepnum == 0) {
3263*d67e68b7SBarry Smith #if defined(PETSC_HAVE_POPEN)
3264*d67e68b7SBarry Smith     ierr = TSGetTotalSteps(ts,&stepnum);CHKERRQ(ierr);
3265*d67e68b7SBarry Smith     if (stepnum == 0) {
3266*d67e68b7SBarry Smith       PetscMPIInt rank;
3267*d67e68b7SBarry Smith       ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
3268*d67e68b7SBarry Smith       if (!rank) {
3269*d67e68b7SBarry Smith         char command[PETSC_MAX_PATH_LEN];
3270*d67e68b7SBarry Smith         FILE *fd;
3271*d67e68b7SBarry Smith         int  err;
3272*d67e68b7SBarry Smith 
3273*d67e68b7SBarry Smith         ierr = PetscMemzero(command,sizeof(command));CHKERRQ(ierr);
3274*d67e68b7SBarry Smith         ierr = PetscSNPrintf(command,PETSC_MAX_PATH_LEN,"rm -fr %s","SA-data");CHKERRQ(ierr);
3275*d67e68b7SBarry Smith         ierr = PetscPOpen(PETSC_COMM_SELF,NULL,command,"r",&fd);CHKERRQ(ierr);
3276*d67e68b7SBarry Smith         ierr = PetscPClose(PETSC_COMM_SELF,fd,&err);CHKERRQ(ierr);
3277*d67e68b7SBarry Smith         ierr = PetscSNPrintf(command,PETSC_MAX_PATH_LEN,"mkdir %s","SA-data");CHKERRQ(ierr);
3278*d67e68b7SBarry Smith         ierr = PetscPOpen(PETSC_COMM_SELF,NULL,command,"r",&fd);CHKERRQ(ierr);
3279*d67e68b7SBarry Smith         ierr = PetscPClose(PETSC_COMM_SELF,fd,&err);CHKERRQ(ierr);
3280*d67e68b7SBarry Smith       }
3281*d67e68b7SBarry Smith     }
3282*d67e68b7SBarry Smith #endif
3283*d67e68b7SBarry Smith     PetscFunctionReturn(0);
3284*d67e68b7SBarry Smith   }
3285*d67e68b7SBarry Smith   ierr = TSGetPrevTime(ts,&tprev);CHKERRQ(ierr);
3286*d67e68b7SBarry Smith   ierr = TSGetTotalSteps(ts,&stepnum);CHKERRQ(ierr);
3287*d67e68b7SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),"SA-data/SA-%06d.bin",stepnum);CHKERRQ(ierr);
3288*d67e68b7SBarry Smith   ierr = OutputBIN(filename,&viewer);CHKERRQ(ierr);
3289*d67e68b7SBarry Smith   ierr = VecView(X,viewer);CHKERRQ(ierr);
3290*d67e68b7SBarry Smith   /* ierr = PetscRealView(1,&time,viewer);CHKERRQ(ierr); */
3291*d67e68b7SBarry Smith   ierr = PetscViewerBinaryWrite(viewer,&tprev,1,PETSC_REAL,PETSC_FALSE);CHKERRQ(ierr);
3292*d67e68b7SBarry Smith   /* ierr = PetscViewerBinaryWrite(viewer,&h ,1,PETSC_REAL,PETSC_FALSE);CHKERRQ(ierr); */
3293*d67e68b7SBarry Smith   ierr = TSGetStages(ts,&ns,&Y);CHKERRQ(ierr);
3294*d67e68b7SBarry Smith 
3295*d67e68b7SBarry Smith   for (i=0;i<ns;i++) {
3296*d67e68b7SBarry Smith     ierr = VecView(Y[i],viewer);CHKERRQ(ierr);
3297*d67e68b7SBarry Smith   }
3298*d67e68b7SBarry Smith 
3299*d67e68b7SBarry Smith   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3300*d67e68b7SBarry Smith   PetscFunctionReturn(0);
3301*d67e68b7SBarry Smith }
3302*d67e68b7SBarry Smith 
3303*d67e68b7SBarry Smith #undef __FUNCT__
3304*d67e68b7SBarry Smith #define __FUNCT__ "TSTrajectoryGet"
3305*d67e68b7SBarry Smith PetscErrorCode TSTrajectoryGet(TS ts,PetscInt step,PetscReal t,Vec X)
3306*d67e68b7SBarry Smith {
3307*d67e68b7SBarry Smith   PetscReal      ptime;
3308*d67e68b7SBarry Smith   Vec            Sol,*Y;
3309*d67e68b7SBarry Smith   PetscInt       Nr,i;
3310*d67e68b7SBarry Smith   PetscViewer    viewer;
3311*d67e68b7SBarry Smith   PetscReal      timepre;
3312*d67e68b7SBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
3313*d67e68b7SBarry Smith   PetscErrorCode ierr;
3314*d67e68b7SBarry Smith 
3315*d67e68b7SBarry Smith   PetscFunctionBeginUser;
3316*d67e68b7SBarry Smith   ierr = TSGetTotalSteps(ts,&step);CHKERRQ(ierr);
3317*d67e68b7SBarry Smith   ierr = PetscSNPrintf(filename,sizeof filename,"SA-data/SA-%06d.bin",step);CHKERRQ(ierr);
3318*d67e68b7SBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr);
3319*d67e68b7SBarry Smith 
3320*d67e68b7SBarry Smith   ierr = TSGetSolution(ts,&Sol);CHKERRQ(ierr);
3321*d67e68b7SBarry Smith   ierr = VecLoad(Sol,viewer);CHKERRQ(ierr);
3322*d67e68b7SBarry Smith 
3323*d67e68b7SBarry Smith   Nr   = 1;
3324*d67e68b7SBarry Smith   /* ierr = PetscRealLoad(Nr,&Nr,&timepre,viewer);CHKERRQ(ierr); */
3325*d67e68b7SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&timepre,1,PETSC_REAL);CHKERRQ(ierr);
3326*d67e68b7SBarry Smith 
3327*d67e68b7SBarry Smith   ierr = TSGetStages(ts,&Nr,&Y);CHKERRQ(ierr);
3328*d67e68b7SBarry Smith   for (i=0;i<Nr ;i++) {
3329*d67e68b7SBarry Smith     ierr = VecLoad(Y[i],viewer);CHKERRQ(ierr);
3330*d67e68b7SBarry Smith   }
3331*d67e68b7SBarry Smith 
3332*d67e68b7SBarry Smith   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3333*d67e68b7SBarry Smith 
3334*d67e68b7SBarry Smith   ierr = TSGetTime(ts,&ptime);CHKERRQ(ierr);
3335*d67e68b7SBarry Smith   ierr = TSSetTimeStep(ts,-ptime+timepre);CHKERRQ(ierr);
3336*d67e68b7SBarry Smith 
3337*d67e68b7SBarry Smith   PetscFunctionReturn(0);
3338*d67e68b7SBarry Smith }
3339*d67e68b7SBarry Smith 
3340*d67e68b7SBarry Smith #undef __FUNCT__
33416a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
33426a4d4014SLisandro Dalcin /*@
33436a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
33446a4d4014SLisandro Dalcin 
33456a4d4014SLisandro Dalcin    Collective on TS
33466a4d4014SLisandro Dalcin 
33476a4d4014SLisandro Dalcin    Input Parameter:
33486a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3349cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
33505a3a76d0SJed Brown 
33516a4d4014SLisandro Dalcin    Level: beginner
33526a4d4014SLisandro Dalcin 
33535a3a76d0SJed Brown    Notes:
33545a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
33555a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
33565a3a76d0SJed Brown    stepped over the final time.
33575a3a76d0SJed Brown 
33586a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
33596a4d4014SLisandro Dalcin 
33606a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
33616a4d4014SLisandro Dalcin @*/
3362cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
33636a4d4014SLisandro Dalcin {
3364b06615a5SLisandro Dalcin   Vec               solution;
33656a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3366f22f69f0SBarry Smith 
33676a4d4014SLisandro Dalcin   PetscFunctionBegin;
33680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3369f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3370c88f2d44SBarry Smith   if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
3371b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
33720910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3373b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3374b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3375b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
33765a3a76d0SJed Brown     }
33770910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3378bbd56ea5SKarl Rupp   } else if (u) {
33790910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
33805a3a76d0SJed Brown   }
3381d2daff3dSHong Zhang   ierr = TSSetUp(ts);CHKERRQ(ierr); /*compute adj coefficients if the reverse mode is on*/
33826a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
3383193ac0bcSJed Brown   ts->steps             = 0;
33845ef26d82SJed Brown   ts->ksp_its           = 0;
33855ef26d82SJed Brown   ts->snes_its          = 0;
3386c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
3387c610991cSLisandro Dalcin   ts->reject            = 0;
3388193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
3389193ac0bcSJed Brown 
3390ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3391f05ece33SBarry Smith 
3392193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
3393193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
33940910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
3395cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3396193ac0bcSJed Brown   } else {
33976a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
3398db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3399c88f2d44SBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3400e1a7a14fSJed Brown     while (!ts->reason) {
3401b06615a5SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3402*d67e68b7SBarry Smith       ierr = TSTrajectorySet(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3403193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3404aeb4809dSShri Abhyankar       if (ts->event) {
3405aeb4809dSShri Abhyankar 	ierr = TSEventMonitor(ts);CHKERRQ(ierr);
34061eda64f1SShri Abhyankar 	if (ts->event->status != TSEVENT_PROCESSING) {
34071eda64f1SShri Abhyankar 	  ierr = TSPostStep(ts);CHKERRQ(ierr);
34081eda64f1SShri Abhyankar 	}
34091eda64f1SShri Abhyankar       } else {
34101eda64f1SShri Abhyankar 	ierr = TSPostStep(ts);CHKERRQ(ierr);
3411aeb4809dSShri Abhyankar       }
3412193ac0bcSJed Brown     }
3413c88f2d44SBarry Smith     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
34140910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3415cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3416b06615a5SLisandro Dalcin       solution = u;
34170574a7fbSJed Brown     } else {
3418ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3419cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3420b06615a5SLisandro Dalcin       solution = ts->vec_sol;
34210574a7fbSJed Brown     }
3422*d67e68b7SBarry Smith     ierr = TSTrajectorySet(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
3423b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
34244e9ba7eeSMatthew G. Knepley     ierr = VecViewFromOptions(u, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
3425193ac0bcSJed Brown   }
3426d2daff3dSHong Zhang 
3427ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
342856f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
34296a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
34306a4d4014SLisandro Dalcin }
34316a4d4014SLisandro Dalcin 
34326a4d4014SLisandro Dalcin #undef __FUNCT__
3433c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
3434c88f2d44SBarry Smith /*@
3435c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
3436c88f2d44SBarry Smith 
3437c88f2d44SBarry Smith    Collective on TS
3438c88f2d44SBarry Smith 
3439c88f2d44SBarry Smith    Input Parameter:
3440c88f2d44SBarry Smith +  ts - the TS context obtained from TSCreate()
3441c88f2d44SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
3442c88f2d44SBarry Smith 
3443c88f2d44SBarry Smith    Level: intermediate
3444c88f2d44SBarry Smith 
3445c88f2d44SBarry Smith    Notes:
3446c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
3447c88f2d44SBarry Smith 
3448c88f2d44SBarry Smith .keywords: TS, timestep, solve
3449c88f2d44SBarry Smith 
3450c88f2d44SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep()
3451c88f2d44SBarry Smith @*/
3452c88f2d44SBarry Smith PetscErrorCode TSAdjointSolve(TS ts,Vec u)
3453c88f2d44SBarry Smith {
3454c88f2d44SBarry Smith   PetscErrorCode    ierr;
3455c88f2d44SBarry Smith 
3456c88f2d44SBarry Smith   PetscFunctionBegin;
3457c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3458c88f2d44SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3459c88f2d44SBarry Smith   if (u) {
3460c88f2d44SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
3461c88f2d44SBarry Smith   }
3462c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3463c88f2d44SBarry Smith   /* reset time step and iteration counters */
3464c88f2d44SBarry Smith   ts->steps             = 0;
3465c88f2d44SBarry Smith   ts->ksp_its           = 0;
3466c88f2d44SBarry Smith   ts->snes_its          = 0;
3467c88f2d44SBarry Smith   ts->num_snes_failures = 0;
3468c88f2d44SBarry Smith   ts->reject            = 0;
3469c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
3470c88f2d44SBarry Smith 
3471c88f2d44SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3472c88f2d44SBarry Smith 
3473c88f2d44SBarry Smith   if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3474c88f2d44SBarry Smith   while (!ts->reason) {
3475*d67e68b7SBarry Smith     ierr = TSTrajectoryGet(ts,ts->max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3476c88f2d44SBarry Smith     ierr = TSMonitor(ts,ts->max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
347708c7845fSBarry Smith     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
3478c88f2d44SBarry Smith     if (ts->event) {
3479c88f2d44SBarry Smith       ierr = TSEventMonitor(ts);CHKERRQ(ierr);
3480c88f2d44SBarry Smith       if (ts->event->status != TSEVENT_PROCESSING) {
3481c88f2d44SBarry Smith         ierr = TSPostStep(ts);CHKERRQ(ierr);
3482c88f2d44SBarry Smith       }
3483c88f2d44SBarry Smith     } else {
3484c88f2d44SBarry Smith       ierr = TSPostStep(ts);CHKERRQ(ierr);
3485c88f2d44SBarry Smith     }
3486c88f2d44SBarry Smith   }
3487c88f2d44SBarry Smith   if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3488c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
3489*d67e68b7SBarry Smith   ierr = TSMonitor(ts,0,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3490c88f2d44SBarry Smith   ierr = VecViewFromOptions(u, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
3491c88f2d44SBarry Smith 
3492c88f2d44SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
3493c88f2d44SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3494c88f2d44SBarry Smith   PetscFunctionReturn(0);
3495c88f2d44SBarry Smith }
3496c88f2d44SBarry Smith 
3497c88f2d44SBarry Smith #undef __FUNCT__
34984a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
3499228d79bcSJed Brown /*@
3500228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3501228d79bcSJed Brown 
3502228d79bcSJed Brown    Collective on TS
3503228d79bcSJed Brown 
3504228d79bcSJed Brown    Input Parameters:
3505228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3506228d79bcSJed Brown .  step - step number that has just completed
3507228d79bcSJed Brown .  ptime - model time of the state
35080910c330SBarry Smith -  u - state at the current model time
3509228d79bcSJed Brown 
3510228d79bcSJed Brown    Notes:
3511228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
3512228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
3513228d79bcSJed Brown 
3514228d79bcSJed Brown    Level: advanced
3515228d79bcSJed Brown 
3516228d79bcSJed Brown .keywords: TS, timestep
3517228d79bcSJed Brown @*/
35180910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3519d763cef2SBarry Smith {
35206849ba73SBarry Smith   PetscErrorCode ierr;
3521a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3522d763cef2SBarry Smith 
3523d763cef2SBarry Smith   PetscFunctionBegin;
3524b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3525b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
35265edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3527d763cef2SBarry Smith   for (i=0; i<n; i++) {
35280910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3529d763cef2SBarry Smith   }
35305edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3531d763cef2SBarry Smith   PetscFunctionReturn(0);
3532d763cef2SBarry Smith }
3533d763cef2SBarry Smith 
3534d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
35354a2ae208SSatish Balay #undef __FUNCT__
3536a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
3537d763cef2SBarry Smith /*@C
3538a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
3539a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3540d763cef2SBarry Smith 
3541d763cef2SBarry Smith    Collective on TS
3542d763cef2SBarry Smith 
3543d763cef2SBarry Smith    Input Parameters:
3544d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3545d763cef2SBarry Smith .  label - the title to put in the title bar
35467c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3547a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3548a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3549d763cef2SBarry Smith 
3550d763cef2SBarry Smith    Output Parameter:
35510b039ecaSBarry Smith .  ctx - the context
3552d763cef2SBarry Smith 
3553d763cef2SBarry Smith    Options Database Key:
35544f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
35554f09c107SBarry Smith .  -ts_monitor_lg_solution -
355699fdda47SBarry Smith .  -ts_monitor_lg_error -
355799fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
355899fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
355999fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
3560d763cef2SBarry Smith 
3561d763cef2SBarry Smith    Notes:
3562a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3563d763cef2SBarry Smith 
3564d763cef2SBarry Smith    Level: intermediate
3565d763cef2SBarry Smith 
35667c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
3567d763cef2SBarry Smith 
35684f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
35697c922b88SBarry Smith 
3570d763cef2SBarry Smith @*/
3571a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3572d763cef2SBarry Smith {
3573b0a32e0cSBarry Smith   PetscDraw      win;
3574dfbe8321SBarry Smith   PetscErrorCode ierr;
3575d763cef2SBarry Smith 
3576d763cef2SBarry Smith   PetscFunctionBegin;
3577b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
3578a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
35793fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
35800b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
35813bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr);
3582287de1a7SBarry Smith   ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr);
3583287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
3584a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3585d763cef2SBarry Smith   PetscFunctionReturn(0);
3586d763cef2SBarry Smith }
3587d763cef2SBarry Smith 
35884a2ae208SSatish Balay #undef __FUNCT__
35894f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3590b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3591d763cef2SBarry Smith {
35920b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3593c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3594dfbe8321SBarry Smith   PetscErrorCode ierr;
3595d763cef2SBarry Smith 
3596d763cef2SBarry Smith   PetscFunctionBegin;
3597b06615a5SLisandro Dalcin   if (!step) {
3598a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3599a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
3600a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
3601a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3602287de1a7SBarry Smith     ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr);
3603a9f9c1f6SBarry Smith   }
3604c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
36050b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3606b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
36070b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
36083923b477SBarry Smith   }
3609d763cef2SBarry Smith   PetscFunctionReturn(0);
3610d763cef2SBarry Smith }
3611d763cef2SBarry Smith 
36124a2ae208SSatish Balay #undef __FUNCT__
3613a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3614d763cef2SBarry Smith /*@C
3615a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3616a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3617d763cef2SBarry Smith 
36180b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3619d763cef2SBarry Smith 
3620d763cef2SBarry Smith    Input Parameter:
36210b039ecaSBarry Smith .  ctx - the monitor context
3622d763cef2SBarry Smith 
3623d763cef2SBarry Smith    Level: intermediate
3624d763cef2SBarry Smith 
3625d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3626d763cef2SBarry Smith 
36274f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3628d763cef2SBarry Smith @*/
3629a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3630d763cef2SBarry Smith {
3631b0a32e0cSBarry Smith   PetscDraw      draw;
3632dfbe8321SBarry Smith   PetscErrorCode ierr;
3633d763cef2SBarry Smith 
3634d763cef2SBarry Smith   PetscFunctionBegin;
36350b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
36366bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
36370b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
36380b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3639d763cef2SBarry Smith   PetscFunctionReturn(0);
3640d763cef2SBarry Smith }
3641d763cef2SBarry Smith 
36424a2ae208SSatish Balay #undef __FUNCT__
36434a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3644d763cef2SBarry Smith /*@
3645b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3646d763cef2SBarry Smith 
3647d763cef2SBarry Smith    Not Collective
3648d763cef2SBarry Smith 
3649d763cef2SBarry Smith    Input Parameter:
3650d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3651d763cef2SBarry Smith 
3652d763cef2SBarry Smith    Output Parameter:
3653d763cef2SBarry Smith .  t  - the current time
3654d763cef2SBarry Smith 
3655d763cef2SBarry Smith    Level: beginner
3656d763cef2SBarry Smith 
3657b8123daeSJed Brown    Note:
3658b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
36599be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3660b8123daeSJed Brown 
3661d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3662d763cef2SBarry Smith 
3663d763cef2SBarry Smith .keywords: TS, get, time
3664d763cef2SBarry Smith @*/
36657087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3666d763cef2SBarry Smith {
3667d763cef2SBarry Smith   PetscFunctionBegin;
36680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3669f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3670d763cef2SBarry Smith   *t = ts->ptime;
3671d763cef2SBarry Smith   PetscFunctionReturn(0);
3672d763cef2SBarry Smith }
3673d763cef2SBarry Smith 
36744a2ae208SSatish Balay #undef __FUNCT__
3675e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3676e5e524a1SHong Zhang /*@
3677e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3678e5e524a1SHong Zhang 
3679e5e524a1SHong Zhang    Not Collective
3680e5e524a1SHong Zhang 
3681e5e524a1SHong Zhang    Input Parameter:
3682e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3683e5e524a1SHong Zhang 
3684e5e524a1SHong Zhang    Output Parameter:
3685e5e524a1SHong Zhang .  t  - the previous time
3686e5e524a1SHong Zhang 
3687e5e524a1SHong Zhang    Level: beginner
3688e5e524a1SHong Zhang 
3689e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3690e5e524a1SHong Zhang 
3691e5e524a1SHong Zhang .keywords: TS, get, time
3692e5e524a1SHong Zhang @*/
3693e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3694e5e524a1SHong Zhang {
3695e5e524a1SHong Zhang   PetscFunctionBegin;
3696e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3697e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3698e5e524a1SHong Zhang   *t = ts->ptime_prev;
3699e5e524a1SHong Zhang   PetscFunctionReturn(0);
3700e5e524a1SHong Zhang }
3701e5e524a1SHong Zhang 
3702e5e524a1SHong Zhang #undef __FUNCT__
37036a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
37046a4d4014SLisandro Dalcin /*@
37056a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
37066a4d4014SLisandro Dalcin 
37073f9fe445SBarry Smith    Logically Collective on TS
37086a4d4014SLisandro Dalcin 
37096a4d4014SLisandro Dalcin    Input Parameters:
37106a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
37116a4d4014SLisandro Dalcin -  time - the time
37126a4d4014SLisandro Dalcin 
37136a4d4014SLisandro Dalcin    Level: intermediate
37146a4d4014SLisandro Dalcin 
37156a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
37166a4d4014SLisandro Dalcin 
37176a4d4014SLisandro Dalcin .keywords: TS, set, time
37186a4d4014SLisandro Dalcin @*/
37197087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
37206a4d4014SLisandro Dalcin {
37216a4d4014SLisandro Dalcin   PetscFunctionBegin;
37220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3723c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
37246a4d4014SLisandro Dalcin   ts->ptime = t;
37256a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
37266a4d4014SLisandro Dalcin }
37276a4d4014SLisandro Dalcin 
37286a4d4014SLisandro Dalcin #undef __FUNCT__
37294a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
3730d763cef2SBarry Smith /*@C
3731d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
3732d763cef2SBarry Smith    TS options in the database.
3733d763cef2SBarry Smith 
37343f9fe445SBarry Smith    Logically Collective on TS
3735d763cef2SBarry Smith 
3736d763cef2SBarry Smith    Input Parameter:
3737d763cef2SBarry Smith +  ts     - The TS context
3738d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3739d763cef2SBarry Smith 
3740d763cef2SBarry Smith    Notes:
3741d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3742d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3743d763cef2SBarry Smith    hyphen.
3744d763cef2SBarry Smith 
3745d763cef2SBarry Smith    Level: advanced
3746d763cef2SBarry Smith 
3747d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
3748d763cef2SBarry Smith 
3749d763cef2SBarry Smith .seealso: TSSetFromOptions()
3750d763cef2SBarry Smith 
3751d763cef2SBarry Smith @*/
37527087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
3753d763cef2SBarry Smith {
3754dfbe8321SBarry Smith   PetscErrorCode ierr;
3755089b2837SJed Brown   SNES           snes;
3756d763cef2SBarry Smith 
3757d763cef2SBarry Smith   PetscFunctionBegin;
37580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3759d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3760089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3761089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3762d763cef2SBarry Smith   PetscFunctionReturn(0);
3763d763cef2SBarry Smith }
3764d763cef2SBarry Smith 
3765d763cef2SBarry Smith 
37664a2ae208SSatish Balay #undef __FUNCT__
37674a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
3768d763cef2SBarry Smith /*@C
3769d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
3770d763cef2SBarry Smith    TS options in the database.
3771d763cef2SBarry Smith 
37723f9fe445SBarry Smith    Logically Collective on TS
3773d763cef2SBarry Smith 
3774d763cef2SBarry Smith    Input Parameter:
3775d763cef2SBarry Smith +  ts     - The TS context
3776d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3777d763cef2SBarry Smith 
3778d763cef2SBarry Smith    Notes:
3779d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3780d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3781d763cef2SBarry Smith    hyphen.
3782d763cef2SBarry Smith 
3783d763cef2SBarry Smith    Level: advanced
3784d763cef2SBarry Smith 
3785d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
3786d763cef2SBarry Smith 
3787d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
3788d763cef2SBarry Smith 
3789d763cef2SBarry Smith @*/
37907087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
3791d763cef2SBarry Smith {
3792dfbe8321SBarry Smith   PetscErrorCode ierr;
3793089b2837SJed Brown   SNES           snes;
3794d763cef2SBarry Smith 
3795d763cef2SBarry Smith   PetscFunctionBegin;
37960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3797d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3798089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3799089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3800d763cef2SBarry Smith   PetscFunctionReturn(0);
3801d763cef2SBarry Smith }
3802d763cef2SBarry Smith 
38034a2ae208SSatish Balay #undef __FUNCT__
38044a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
3805d763cef2SBarry Smith /*@C
3806d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
3807d763cef2SBarry Smith    TS options in the database.
3808d763cef2SBarry Smith 
3809d763cef2SBarry Smith    Not Collective
3810d763cef2SBarry Smith 
3811d763cef2SBarry Smith    Input Parameter:
3812d763cef2SBarry Smith .  ts - The TS context
3813d763cef2SBarry Smith 
3814d763cef2SBarry Smith    Output Parameter:
3815d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
3816d763cef2SBarry Smith 
3817d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
3818d763cef2SBarry Smith    sufficient length to hold the prefix.
3819d763cef2SBarry Smith 
3820d763cef2SBarry Smith    Level: intermediate
3821d763cef2SBarry Smith 
3822d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
3823d763cef2SBarry Smith 
3824d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
3825d763cef2SBarry Smith @*/
38267087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
3827d763cef2SBarry Smith {
3828dfbe8321SBarry Smith   PetscErrorCode ierr;
3829d763cef2SBarry Smith 
3830d763cef2SBarry Smith   PetscFunctionBegin;
38310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38324482741eSBarry Smith   PetscValidPointer(prefix,2);
3833d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3834d763cef2SBarry Smith   PetscFunctionReturn(0);
3835d763cef2SBarry Smith }
3836d763cef2SBarry Smith 
38374a2ae208SSatish Balay #undef __FUNCT__
38384a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3839d763cef2SBarry Smith /*@C
3840d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3841d763cef2SBarry Smith 
3842d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3843d763cef2SBarry Smith 
3844d763cef2SBarry Smith    Input Parameter:
3845d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3846d763cef2SBarry Smith 
3847d763cef2SBarry Smith    Output Parameters:
3848e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3849e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3850e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3851e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3852d763cef2SBarry Smith 
38530298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3854d763cef2SBarry Smith 
3855d763cef2SBarry Smith    Level: intermediate
3856d763cef2SBarry Smith 
385726d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3858d763cef2SBarry Smith 
3859d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3860d763cef2SBarry Smith @*/
3861e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3862d763cef2SBarry Smith {
3863089b2837SJed Brown   PetscErrorCode ierr;
3864089b2837SJed Brown   SNES           snes;
386524989b8cSPeter Brune   DM             dm;
3866089b2837SJed Brown 
3867d763cef2SBarry Smith   PetscFunctionBegin;
3868089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3869e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
387024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
387124989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3872d763cef2SBarry Smith   PetscFunctionReturn(0);
3873d763cef2SBarry Smith }
3874d763cef2SBarry Smith 
38751713a123SBarry Smith #undef __FUNCT__
38762eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
38772eca1d9cSJed Brown /*@C
38782eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
38792eca1d9cSJed Brown 
38802eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
38812eca1d9cSJed Brown 
38822eca1d9cSJed Brown    Input Parameter:
38832eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
38842eca1d9cSJed Brown 
38852eca1d9cSJed Brown    Output Parameters:
3886e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3887e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
38882eca1d9cSJed Brown .  f   - The function to compute the matrices
38892eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
38902eca1d9cSJed Brown 
38910298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
38922eca1d9cSJed Brown 
38932eca1d9cSJed Brown    Level: advanced
38942eca1d9cSJed Brown 
38952eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
38962eca1d9cSJed Brown 
38972eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
38982eca1d9cSJed Brown @*/
3899e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
39002eca1d9cSJed Brown {
3901089b2837SJed Brown   PetscErrorCode ierr;
3902089b2837SJed Brown   SNES           snes;
390324989b8cSPeter Brune   DM             dm;
39040910c330SBarry Smith 
39052eca1d9cSJed Brown   PetscFunctionBegin;
3906089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3907f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3908e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
390924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
391024989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
39112eca1d9cSJed Brown   PetscFunctionReturn(0);
39122eca1d9cSJed Brown }
39132eca1d9cSJed Brown 
39146083293cSBarry Smith 
39152eca1d9cSJed Brown #undef __FUNCT__
391683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
39171713a123SBarry Smith /*@C
391883a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
39191713a123SBarry Smith    VecView() for the solution at each timestep
39201713a123SBarry Smith 
39211713a123SBarry Smith    Collective on TS
39221713a123SBarry Smith 
39231713a123SBarry Smith    Input Parameters:
39241713a123SBarry Smith +  ts - the TS context
39251713a123SBarry Smith .  step - current time-step
3926142b95e3SSatish Balay .  ptime - current time
39270298fd71SBarry Smith -  dummy - either a viewer or NULL
39281713a123SBarry Smith 
392999fdda47SBarry Smith    Options Database:
393099fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
393199fdda47SBarry Smith 
393299fdda47SBarry 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
393399fdda47SBarry Smith        will look bad
393499fdda47SBarry Smith 
39351713a123SBarry Smith    Level: intermediate
39361713a123SBarry Smith 
39371713a123SBarry Smith .keywords: TS,  vector, monitor, view
39381713a123SBarry Smith 
3939a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
39401713a123SBarry Smith @*/
39410910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
39421713a123SBarry Smith {
3943dfbe8321SBarry Smith   PetscErrorCode   ierr;
394483a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3945473a3ab2SBarry Smith   PetscDraw        draw;
39461713a123SBarry Smith 
39471713a123SBarry Smith   PetscFunctionBegin;
39486083293cSBarry Smith   if (!step && ictx->showinitial) {
39496083293cSBarry Smith     if (!ictx->initialsolution) {
39500910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
39511713a123SBarry Smith     }
39520910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
39536083293cSBarry Smith   }
3954b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
39550dcf80beSBarry Smith 
39566083293cSBarry Smith   if (ictx->showinitial) {
39576083293cSBarry Smith     PetscReal pause;
39586083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
39596083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
39606083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
39616083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
39626083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
39636083293cSBarry Smith   }
39640910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3965473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3966473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3967473a3ab2SBarry Smith     char      time[32];
3968473a3ab2SBarry Smith     size_t    len;
3969473a3ab2SBarry Smith 
3970473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3971473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3972473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3973473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
39740298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3975473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3976473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3977473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3978473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3979473a3ab2SBarry Smith   }
3980473a3ab2SBarry Smith 
39816083293cSBarry Smith   if (ictx->showinitial) {
39826083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
39836083293cSBarry Smith   }
39841713a123SBarry Smith   PetscFunctionReturn(0);
39851713a123SBarry Smith }
39861713a123SBarry Smith 
39872d5ee99bSBarry Smith #undef __FUNCT__
39882d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
39892d5ee99bSBarry Smith /*@C
39902d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
39912d5ee99bSBarry Smith 
39922d5ee99bSBarry Smith    Collective on TS
39932d5ee99bSBarry Smith 
39942d5ee99bSBarry Smith    Input Parameters:
39952d5ee99bSBarry Smith +  ts - the TS context
39962d5ee99bSBarry Smith .  step - current time-step
39972d5ee99bSBarry Smith .  ptime - current time
39982d5ee99bSBarry Smith -  dummy - either a viewer or NULL
39992d5ee99bSBarry Smith 
40002d5ee99bSBarry Smith    Level: intermediate
40012d5ee99bSBarry Smith 
40022d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
40032d5ee99bSBarry Smith 
40042d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
40052d5ee99bSBarry Smith @*/
40062d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
40072d5ee99bSBarry Smith {
40082d5ee99bSBarry Smith   PetscErrorCode    ierr;
40092d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
40102d5ee99bSBarry Smith   PetscDraw         draw;
40112d5ee99bSBarry Smith   MPI_Comm          comm;
40122d5ee99bSBarry Smith   PetscInt          n;
40132d5ee99bSBarry Smith   PetscMPIInt       size;
40142d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
40152d5ee99bSBarry Smith   char              time[32];
40162d5ee99bSBarry Smith   size_t            len;
40172d5ee99bSBarry Smith   const PetscScalar *U;
40182d5ee99bSBarry Smith 
40192d5ee99bSBarry Smith   PetscFunctionBegin;
40202d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
40212d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
40222d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
40232d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
40242d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
40252d5ee99bSBarry Smith 
40262d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
40272d5ee99bSBarry Smith 
40282d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
40294b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
4030ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
4031a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
4032a4494fc1SBarry Smith       PetscFunctionReturn(0);
4033a4494fc1SBarry Smith   }
40342d5ee99bSBarry Smith   if (!step) ictx->color++;
40352d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
40362d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
40372d5ee99bSBarry Smith 
40382d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
40394b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
40402d5ee99bSBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
40412d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
40422d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
40432d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
40442d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
40452d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
40462d5ee99bSBarry Smith   }
40472d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
40482d5ee99bSBarry Smith   PetscFunctionReturn(0);
40492d5ee99bSBarry Smith }
40502d5ee99bSBarry Smith 
40511713a123SBarry Smith 
40526c699258SBarry Smith #undef __FUNCT__
405383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
40546083293cSBarry Smith /*@C
405583a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
40566083293cSBarry Smith 
40576083293cSBarry Smith    Collective on TS
40586083293cSBarry Smith 
40596083293cSBarry Smith    Input Parameters:
40606083293cSBarry Smith .    ctx - the monitor context
40616083293cSBarry Smith 
40626083293cSBarry Smith    Level: intermediate
40636083293cSBarry Smith 
40646083293cSBarry Smith .keywords: TS,  vector, monitor, view
40656083293cSBarry Smith 
406683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
40676083293cSBarry Smith @*/
406883a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
40696083293cSBarry Smith {
40706083293cSBarry Smith   PetscErrorCode ierr;
40716083293cSBarry Smith 
40726083293cSBarry Smith   PetscFunctionBegin;
40734b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
407483a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
407583a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
407683a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
40776083293cSBarry Smith   PetscFunctionReturn(0);
40786083293cSBarry Smith }
40796083293cSBarry Smith 
40806083293cSBarry Smith #undef __FUNCT__
408183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
40826083293cSBarry Smith /*@C
408383a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
40846083293cSBarry Smith 
40856083293cSBarry Smith    Collective on TS
40866083293cSBarry Smith 
40876083293cSBarry Smith    Input Parameter:
40886083293cSBarry Smith .    ts - time-step context
40896083293cSBarry Smith 
40906083293cSBarry Smith    Output Patameter:
40916083293cSBarry Smith .    ctx - the monitor context
40926083293cSBarry Smith 
409399fdda47SBarry Smith    Options Database:
409499fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
409599fdda47SBarry Smith 
40966083293cSBarry Smith    Level: intermediate
40976083293cSBarry Smith 
40986083293cSBarry Smith .keywords: TS,  vector, monitor, view
40996083293cSBarry Smith 
410083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
41016083293cSBarry Smith @*/
410283a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
41036083293cSBarry Smith {
41046083293cSBarry Smith   PetscErrorCode   ierr;
41056083293cSBarry Smith 
41066083293cSBarry Smith   PetscFunctionBegin;
4107b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
410883a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4109e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4110bbd56ea5SKarl Rupp 
411183a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4112473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
41130298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4114473a3ab2SBarry Smith 
4115473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
41160298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
41172d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
41186083293cSBarry Smith   PetscFunctionReturn(0);
41196083293cSBarry Smith }
41206083293cSBarry Smith 
41216083293cSBarry Smith #undef __FUNCT__
412283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
41233a471f94SBarry Smith /*@C
412483a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
41253a471f94SBarry Smith    VecView() for the error at each timestep
41263a471f94SBarry Smith 
41273a471f94SBarry Smith    Collective on TS
41283a471f94SBarry Smith 
41293a471f94SBarry Smith    Input Parameters:
41303a471f94SBarry Smith +  ts - the TS context
41313a471f94SBarry Smith .  step - current time-step
41323a471f94SBarry Smith .  ptime - current time
41330298fd71SBarry Smith -  dummy - either a viewer or NULL
41343a471f94SBarry Smith 
41353a471f94SBarry Smith    Level: intermediate
41363a471f94SBarry Smith 
41373a471f94SBarry Smith .keywords: TS,  vector, monitor, view
41383a471f94SBarry Smith 
41393a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
41403a471f94SBarry Smith @*/
41410910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
41423a471f94SBarry Smith {
41433a471f94SBarry Smith   PetscErrorCode   ierr;
414483a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
414583a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
41463a471f94SBarry Smith   Vec              work;
41473a471f94SBarry Smith 
41483a471f94SBarry Smith   PetscFunctionBegin;
4149b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
41500910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
41513a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
41520910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
41533a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
41543a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
41553a471f94SBarry Smith   PetscFunctionReturn(0);
41563a471f94SBarry Smith }
41573a471f94SBarry Smith 
41582a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
41593a471f94SBarry Smith #undef __FUNCT__
41606c699258SBarry Smith #define __FUNCT__ "TSSetDM"
41616c699258SBarry Smith /*@
41626c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
41636c699258SBarry Smith 
41643f9fe445SBarry Smith    Logically Collective on TS and DM
41656c699258SBarry Smith 
41666c699258SBarry Smith    Input Parameters:
41676c699258SBarry Smith +  ts - the preconditioner context
41686c699258SBarry Smith -  dm - the dm
41696c699258SBarry Smith 
41706c699258SBarry Smith    Level: intermediate
41716c699258SBarry Smith 
41726c699258SBarry Smith 
41736c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
41746c699258SBarry Smith @*/
41757087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
41766c699258SBarry Smith {
41776c699258SBarry Smith   PetscErrorCode ierr;
4178089b2837SJed Brown   SNES           snes;
4179942e3340SBarry Smith   DMTS           tsdm;
41806c699258SBarry Smith 
41816c699258SBarry Smith   PetscFunctionBegin;
41820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
418370663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4184942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
41852a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4186942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4187942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
418824989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
418924989b8cSPeter Brune         tsdm->originaldm = dm;
419024989b8cSPeter Brune       }
419124989b8cSPeter Brune     }
41926bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
419324989b8cSPeter Brune   }
41946c699258SBarry Smith   ts->dm = dm;
4195bbd56ea5SKarl Rupp 
4196089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4197089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
41986c699258SBarry Smith   PetscFunctionReturn(0);
41996c699258SBarry Smith }
42006c699258SBarry Smith 
42016c699258SBarry Smith #undef __FUNCT__
42026c699258SBarry Smith #define __FUNCT__ "TSGetDM"
42036c699258SBarry Smith /*@
42046c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
42056c699258SBarry Smith 
42063f9fe445SBarry Smith    Not Collective
42076c699258SBarry Smith 
42086c699258SBarry Smith    Input Parameter:
42096c699258SBarry Smith . ts - the preconditioner context
42106c699258SBarry Smith 
42116c699258SBarry Smith    Output Parameter:
42126c699258SBarry Smith .  dm - the dm
42136c699258SBarry Smith 
42146c699258SBarry Smith    Level: intermediate
42156c699258SBarry Smith 
42166c699258SBarry Smith 
42176c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
42186c699258SBarry Smith @*/
42197087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
42206c699258SBarry Smith {
4221496e6a7aSJed Brown   PetscErrorCode ierr;
4222496e6a7aSJed Brown 
42236c699258SBarry Smith   PetscFunctionBegin;
42240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4225496e6a7aSJed Brown   if (!ts->dm) {
4226ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4227496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4228496e6a7aSJed Brown   }
42296c699258SBarry Smith   *dm = ts->dm;
42306c699258SBarry Smith   PetscFunctionReturn(0);
42316c699258SBarry Smith }
42321713a123SBarry Smith 
42330f5c6efeSJed Brown #undef __FUNCT__
42340f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
42350f5c6efeSJed Brown /*@
42360f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
42370f5c6efeSJed Brown 
42383f9fe445SBarry Smith    Logically Collective on SNES
42390f5c6efeSJed Brown 
42400f5c6efeSJed Brown    Input Parameter:
4241d42a1c89SJed Brown + snes - nonlinear solver
42420910c330SBarry Smith . U - the current state at which to evaluate the residual
4243d42a1c89SJed Brown - ctx - user context, must be a TS
42440f5c6efeSJed Brown 
42450f5c6efeSJed Brown    Output Parameter:
42460f5c6efeSJed Brown . F - the nonlinear residual
42470f5c6efeSJed Brown 
42480f5c6efeSJed Brown    Notes:
42490f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
42500f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
42510f5c6efeSJed Brown 
42520f5c6efeSJed Brown    Level: advanced
42530f5c6efeSJed Brown 
42540f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
42550f5c6efeSJed Brown @*/
42560910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
42570f5c6efeSJed Brown {
42580f5c6efeSJed Brown   TS             ts = (TS)ctx;
42590f5c6efeSJed Brown   PetscErrorCode ierr;
42600f5c6efeSJed Brown 
42610f5c6efeSJed Brown   PetscFunctionBegin;
42620f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
42630910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
42640f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
42650f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
42660910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
42670f5c6efeSJed Brown   PetscFunctionReturn(0);
42680f5c6efeSJed Brown }
42690f5c6efeSJed Brown 
42700f5c6efeSJed Brown #undef __FUNCT__
42710f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
42720f5c6efeSJed Brown /*@
42730f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
42740f5c6efeSJed Brown 
42750f5c6efeSJed Brown    Collective on SNES
42760f5c6efeSJed Brown 
42770f5c6efeSJed Brown    Input Parameter:
42780f5c6efeSJed Brown + snes - nonlinear solver
42790910c330SBarry Smith . U - the current state at which to evaluate the residual
42800f5c6efeSJed Brown - ctx - user context, must be a TS
42810f5c6efeSJed Brown 
42820f5c6efeSJed Brown    Output Parameter:
42830f5c6efeSJed Brown + A - the Jacobian
42840f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
42850f5c6efeSJed Brown - flag - indicates any structure change in the matrix
42860f5c6efeSJed Brown 
42870f5c6efeSJed Brown    Notes:
42880f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
42890f5c6efeSJed Brown 
42900f5c6efeSJed Brown    Level: developer
42910f5c6efeSJed Brown 
42920f5c6efeSJed Brown .seealso: SNESSetJacobian()
42930f5c6efeSJed Brown @*/
4294d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
42950f5c6efeSJed Brown {
42960f5c6efeSJed Brown   TS             ts = (TS)ctx;
42970f5c6efeSJed Brown   PetscErrorCode ierr;
42980f5c6efeSJed Brown 
42990f5c6efeSJed Brown   PetscFunctionBegin;
43000f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
43010910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
43020f5c6efeSJed Brown   PetscValidPointer(A,3);
430394ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
43040f5c6efeSJed Brown   PetscValidPointer(B,4);
430594ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
43060f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4307d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
43080f5c6efeSJed Brown   PetscFunctionReturn(0);
43090f5c6efeSJed Brown }
4310325fc9f4SBarry Smith 
43110e4ef248SJed Brown #undef __FUNCT__
43120e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
43130e4ef248SJed Brown /*@C
43140e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
43150e4ef248SJed Brown 
43160e4ef248SJed Brown    Collective on TS
43170e4ef248SJed Brown 
43180e4ef248SJed Brown    Input Arguments:
43190e4ef248SJed Brown +  ts - time stepping context
43200e4ef248SJed Brown .  t - time at which to evaluate
43210910c330SBarry Smith .  U - state at which to evaluate
43220e4ef248SJed Brown -  ctx - context
43230e4ef248SJed Brown 
43240e4ef248SJed Brown    Output Arguments:
43250e4ef248SJed Brown .  F - right hand side
43260e4ef248SJed Brown 
43270e4ef248SJed Brown    Level: intermediate
43280e4ef248SJed Brown 
43290e4ef248SJed Brown    Notes:
43300e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
43310e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
43320e4ef248SJed Brown 
43330e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
43340e4ef248SJed Brown @*/
43350910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
43360e4ef248SJed Brown {
43370e4ef248SJed Brown   PetscErrorCode ierr;
43380e4ef248SJed Brown   Mat            Arhs,Brhs;
43390e4ef248SJed Brown 
43400e4ef248SJed Brown   PetscFunctionBegin;
43410e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4342d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
43430910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
43440e4ef248SJed Brown   PetscFunctionReturn(0);
43450e4ef248SJed Brown }
43460e4ef248SJed Brown 
43470e4ef248SJed Brown #undef __FUNCT__
43480e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
43490e4ef248SJed Brown /*@C
43500e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
43510e4ef248SJed Brown 
43520e4ef248SJed Brown    Collective on TS
43530e4ef248SJed Brown 
43540e4ef248SJed Brown    Input Arguments:
43550e4ef248SJed Brown +  ts - time stepping context
43560e4ef248SJed Brown .  t - time at which to evaluate
43570910c330SBarry Smith .  U - state at which to evaluate
43580e4ef248SJed Brown -  ctx - context
43590e4ef248SJed Brown 
43600e4ef248SJed Brown    Output Arguments:
43610e4ef248SJed Brown +  A - pointer to operator
43620e4ef248SJed Brown .  B - pointer to preconditioning matrix
43630e4ef248SJed Brown -  flg - matrix structure flag
43640e4ef248SJed Brown 
43650e4ef248SJed Brown    Level: intermediate
43660e4ef248SJed Brown 
43670e4ef248SJed Brown    Notes:
43680e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
43690e4ef248SJed Brown 
43700e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
43710e4ef248SJed Brown @*/
4372d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
43730e4ef248SJed Brown {
43740e4ef248SJed Brown   PetscFunctionBegin;
43750e4ef248SJed Brown   PetscFunctionReturn(0);
43760e4ef248SJed Brown }
43770e4ef248SJed Brown 
43780026cea9SSean Farley #undef __FUNCT__
43790026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
43800026cea9SSean Farley /*@C
43810026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
43820026cea9SSean Farley 
43830026cea9SSean Farley    Collective on TS
43840026cea9SSean Farley 
43850026cea9SSean Farley    Input Arguments:
43860026cea9SSean Farley +  ts - time stepping context
43870026cea9SSean Farley .  t - time at which to evaluate
43880910c330SBarry Smith .  U - state at which to evaluate
43890910c330SBarry Smith .  Udot - time derivative of state vector
43900026cea9SSean Farley -  ctx - context
43910026cea9SSean Farley 
43920026cea9SSean Farley    Output Arguments:
43930026cea9SSean Farley .  F - left hand side
43940026cea9SSean Farley 
43950026cea9SSean Farley    Level: intermediate
43960026cea9SSean Farley 
43970026cea9SSean Farley    Notes:
43980910c330SBarry 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
43990026cea9SSean Farley    user is required to write their own TSComputeIFunction.
44000026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
44010026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
44020026cea9SSean Farley 
44030026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
44040026cea9SSean Farley @*/
44050910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
44060026cea9SSean Farley {
44070026cea9SSean Farley   PetscErrorCode ierr;
44080026cea9SSean Farley   Mat            A,B;
44090026cea9SSean Farley 
44100026cea9SSean Farley   PetscFunctionBegin;
44110298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4412d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
44130910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
44140026cea9SSean Farley   PetscFunctionReturn(0);
44150026cea9SSean Farley }
44160026cea9SSean Farley 
44170026cea9SSean Farley #undef __FUNCT__
44180026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
44190026cea9SSean Farley /*@C
4420b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
44210026cea9SSean Farley 
44220026cea9SSean Farley    Collective on TS
44230026cea9SSean Farley 
44240026cea9SSean Farley    Input Arguments:
44250026cea9SSean Farley +  ts - time stepping context
44260026cea9SSean Farley .  t - time at which to evaluate
44270910c330SBarry Smith .  U - state at which to evaluate
44280910c330SBarry Smith .  Udot - time derivative of state vector
44290026cea9SSean Farley .  shift - shift to apply
44300026cea9SSean Farley -  ctx - context
44310026cea9SSean Farley 
44320026cea9SSean Farley    Output Arguments:
44330026cea9SSean Farley +  A - pointer to operator
44340026cea9SSean Farley .  B - pointer to preconditioning matrix
44350026cea9SSean Farley -  flg - matrix structure flag
44360026cea9SSean Farley 
4437b41af12eSJed Brown    Level: advanced
44380026cea9SSean Farley 
44390026cea9SSean Farley    Notes:
44400026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
44410026cea9SSean Farley 
4442b41af12eSJed Brown    It is only appropriate for problems of the form
4443b41af12eSJed Brown 
4444b41af12eSJed Brown $     M Udot = F(U,t)
4445b41af12eSJed Brown 
4446b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4447b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4448b41af12eSJed Brown   an implicit operator of the form
4449b41af12eSJed Brown 
4450b41af12eSJed Brown $    shift*M + J
4451b41af12eSJed Brown 
4452b41af12eSJed 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
4453b41af12eSJed Brown   a copy of M or reassemble it when requested.
4454b41af12eSJed Brown 
44550026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
44560026cea9SSean Farley @*/
4457d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
44580026cea9SSean Farley {
4459b41af12eSJed Brown   PetscErrorCode ierr;
4460b41af12eSJed Brown 
44610026cea9SSean Farley   PetscFunctionBegin;
446294ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4463b41af12eSJed Brown   ts->ijacobian.shift = shift;
44640026cea9SSean Farley   PetscFunctionReturn(0);
44650026cea9SSean Farley }
4466b41af12eSJed Brown 
4467e817cc15SEmil Constantinescu #undef __FUNCT__
4468e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
4469e817cc15SEmil Constantinescu /*@
4470e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4471e817cc15SEmil Constantinescu 
4472e817cc15SEmil Constantinescu    Not Collective
4473e817cc15SEmil Constantinescu 
4474e817cc15SEmil Constantinescu    Input Parameter:
4475e817cc15SEmil Constantinescu .  ts - the TS context
4476e817cc15SEmil Constantinescu 
4477e817cc15SEmil Constantinescu    Output Parameter:
44784e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4479e817cc15SEmil Constantinescu 
4480e817cc15SEmil Constantinescu    Level: beginner
4481e817cc15SEmil Constantinescu 
4482e817cc15SEmil Constantinescu .keywords: TS, equation type
4483e817cc15SEmil Constantinescu 
4484e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4485e817cc15SEmil Constantinescu @*/
4486e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4487e817cc15SEmil Constantinescu {
4488e817cc15SEmil Constantinescu   PetscFunctionBegin;
4489e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4490e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4491e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4492e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4493e817cc15SEmil Constantinescu }
4494e817cc15SEmil Constantinescu 
4495e817cc15SEmil Constantinescu #undef __FUNCT__
4496e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
4497e817cc15SEmil Constantinescu /*@
4498e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4499e817cc15SEmil Constantinescu 
4500e817cc15SEmil Constantinescu    Not Collective
4501e817cc15SEmil Constantinescu 
4502e817cc15SEmil Constantinescu    Input Parameter:
4503e817cc15SEmil Constantinescu +  ts - the TS context
45044e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4505e817cc15SEmil Constantinescu 
4506e817cc15SEmil Constantinescu    Level: advanced
4507e817cc15SEmil Constantinescu 
4508e817cc15SEmil Constantinescu .keywords: TS, equation type
4509e817cc15SEmil Constantinescu 
4510e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4511e817cc15SEmil Constantinescu @*/
4512e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4513e817cc15SEmil Constantinescu {
4514e817cc15SEmil Constantinescu   PetscFunctionBegin;
4515e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4516e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4517e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4518e817cc15SEmil Constantinescu }
45190026cea9SSean Farley 
45204af1b03aSJed Brown #undef __FUNCT__
45214af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
45224af1b03aSJed Brown /*@
45234af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
45244af1b03aSJed Brown 
45254af1b03aSJed Brown    Not Collective
45264af1b03aSJed Brown 
45274af1b03aSJed Brown    Input Parameter:
45284af1b03aSJed Brown .  ts - the TS context
45294af1b03aSJed Brown 
45304af1b03aSJed Brown    Output Parameter:
45314af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
45324af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
45334af1b03aSJed Brown 
4534487e0bb9SJed Brown    Level: beginner
45354af1b03aSJed Brown 
4536cd652676SJed Brown    Notes:
4537cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
45384af1b03aSJed Brown 
45394af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
45404af1b03aSJed Brown 
45414af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
45424af1b03aSJed Brown @*/
45434af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
45444af1b03aSJed Brown {
45454af1b03aSJed Brown   PetscFunctionBegin;
45464af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45474af1b03aSJed Brown   PetscValidPointer(reason,2);
45484af1b03aSJed Brown   *reason = ts->reason;
45494af1b03aSJed Brown   PetscFunctionReturn(0);
45504af1b03aSJed Brown }
45514af1b03aSJed Brown 
4552fb1732b5SBarry Smith #undef __FUNCT__
4553d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
4554d6ad946cSShri Abhyankar /*@
4555d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4556d6ad946cSShri Abhyankar 
4557d6ad946cSShri Abhyankar    Not Collective
4558d6ad946cSShri Abhyankar 
4559d6ad946cSShri Abhyankar    Input Parameter:
4560d6ad946cSShri Abhyankar +  ts - the TS context
4561d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4562d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4563d6ad946cSShri Abhyankar 
4564f5abba47SShri Abhyankar    Level: advanced
4565d6ad946cSShri Abhyankar 
4566d6ad946cSShri Abhyankar    Notes:
4567d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4568d6ad946cSShri Abhyankar 
4569d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4570d6ad946cSShri Abhyankar 
4571d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4572d6ad946cSShri Abhyankar @*/
4573d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4574d6ad946cSShri Abhyankar {
4575d6ad946cSShri Abhyankar   PetscFunctionBegin;
4576d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4577d6ad946cSShri Abhyankar   ts->reason = reason;
4578d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4579d6ad946cSShri Abhyankar }
4580d6ad946cSShri Abhyankar 
4581d6ad946cSShri Abhyankar #undef __FUNCT__
4582cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
4583cc708dedSBarry Smith /*@
4584cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4585cc708dedSBarry Smith 
4586cc708dedSBarry Smith    Not Collective
4587cc708dedSBarry Smith 
4588cc708dedSBarry Smith    Input Parameter:
4589cc708dedSBarry Smith .  ts - the TS context
4590cc708dedSBarry Smith 
4591cc708dedSBarry Smith    Output Parameter:
4592cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
4593cc708dedSBarry Smith 
4594487e0bb9SJed Brown    Level: beginner
4595cc708dedSBarry Smith 
4596cc708dedSBarry Smith    Notes:
4597cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4598cc708dedSBarry Smith 
4599cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4600cc708dedSBarry Smith 
4601cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4602cc708dedSBarry Smith @*/
4603cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4604cc708dedSBarry Smith {
4605cc708dedSBarry Smith   PetscFunctionBegin;
4606cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4607cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4608cc708dedSBarry Smith   *ftime = ts->solvetime;
4609cc708dedSBarry Smith   PetscFunctionReturn(0);
4610cc708dedSBarry Smith }
4611cc708dedSBarry Smith 
4612cc708dedSBarry Smith #undef __FUNCT__
46132c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
46142c18e0fdSBarry Smith /*@
46152c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
46162c18e0fdSBarry Smith 
46172c18e0fdSBarry Smith    Not Collective
46182c18e0fdSBarry Smith 
46192c18e0fdSBarry Smith    Input Parameter:
46202c18e0fdSBarry Smith .  ts - the TS context
46212c18e0fdSBarry Smith 
46222c18e0fdSBarry Smith    Output Parameter:
46232c18e0fdSBarry Smith .  steps - the number of steps
46242c18e0fdSBarry Smith 
46252c18e0fdSBarry Smith    Level: beginner
46262c18e0fdSBarry Smith 
46272c18e0fdSBarry Smith    Notes:
46282c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
46292c18e0fdSBarry Smith 
46302c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
46312c18e0fdSBarry Smith 
46322c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
46332c18e0fdSBarry Smith @*/
46342c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
46352c18e0fdSBarry Smith {
46362c18e0fdSBarry Smith   PetscFunctionBegin;
46372c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46382c18e0fdSBarry Smith   PetscValidPointer(steps,2);
46392c18e0fdSBarry Smith   *steps = ts->total_steps;
46402c18e0fdSBarry Smith   PetscFunctionReturn(0);
46412c18e0fdSBarry Smith }
46422c18e0fdSBarry Smith 
46432c18e0fdSBarry Smith #undef __FUNCT__
46445ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
46459f67acb7SJed Brown /*@
46465ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
46479f67acb7SJed Brown    used by the time integrator.
46489f67acb7SJed Brown 
46499f67acb7SJed Brown    Not Collective
46509f67acb7SJed Brown 
46519f67acb7SJed Brown    Input Parameter:
46529f67acb7SJed Brown .  ts - TS context
46539f67acb7SJed Brown 
46549f67acb7SJed Brown    Output Parameter:
46559f67acb7SJed Brown .  nits - number of nonlinear iterations
46569f67acb7SJed Brown 
46579f67acb7SJed Brown    Notes:
46589f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
46599f67acb7SJed Brown 
46609f67acb7SJed Brown    Level: intermediate
46619f67acb7SJed Brown 
46629f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
46639f67acb7SJed Brown 
46645ef26d82SJed Brown .seealso:  TSGetKSPIterations()
46659f67acb7SJed Brown @*/
46665ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
46679f67acb7SJed Brown {
46689f67acb7SJed Brown   PetscFunctionBegin;
46699f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46709f67acb7SJed Brown   PetscValidIntPointer(nits,2);
46715ef26d82SJed Brown   *nits = ts->snes_its;
46729f67acb7SJed Brown   PetscFunctionReturn(0);
46739f67acb7SJed Brown }
46749f67acb7SJed Brown 
46759f67acb7SJed Brown #undef __FUNCT__
46765ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
46779f67acb7SJed Brown /*@
46785ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
46799f67acb7SJed Brown    used by the time integrator.
46809f67acb7SJed Brown 
46819f67acb7SJed Brown    Not Collective
46829f67acb7SJed Brown 
46839f67acb7SJed Brown    Input Parameter:
46849f67acb7SJed Brown .  ts - TS context
46859f67acb7SJed Brown 
46869f67acb7SJed Brown    Output Parameter:
46879f67acb7SJed Brown .  lits - number of linear iterations
46889f67acb7SJed Brown 
46899f67acb7SJed Brown    Notes:
46909f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
46919f67acb7SJed Brown 
46929f67acb7SJed Brown    Level: intermediate
46939f67acb7SJed Brown 
46949f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
46959f67acb7SJed Brown 
46965ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
46979f67acb7SJed Brown @*/
46985ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
46999f67acb7SJed Brown {
47009f67acb7SJed Brown   PetscFunctionBegin;
47019f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47029f67acb7SJed Brown   PetscValidIntPointer(lits,2);
47035ef26d82SJed Brown   *lits = ts->ksp_its;
47049f67acb7SJed Brown   PetscFunctionReturn(0);
47059f67acb7SJed Brown }
47069f67acb7SJed Brown 
47079f67acb7SJed Brown #undef __FUNCT__
4708cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
4709cef5090cSJed Brown /*@
4710cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4711cef5090cSJed Brown 
4712cef5090cSJed Brown    Not Collective
4713cef5090cSJed Brown 
4714cef5090cSJed Brown    Input Parameter:
4715cef5090cSJed Brown .  ts - TS context
4716cef5090cSJed Brown 
4717cef5090cSJed Brown    Output Parameter:
4718cef5090cSJed Brown .  rejects - number of steps rejected
4719cef5090cSJed Brown 
4720cef5090cSJed Brown    Notes:
4721cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4722cef5090cSJed Brown 
4723cef5090cSJed Brown    Level: intermediate
4724cef5090cSJed Brown 
4725cef5090cSJed Brown .keywords: TS, get, number
4726cef5090cSJed Brown 
47275ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4728cef5090cSJed Brown @*/
4729cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4730cef5090cSJed Brown {
4731cef5090cSJed Brown   PetscFunctionBegin;
4732cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4733cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4734cef5090cSJed Brown   *rejects = ts->reject;
4735cef5090cSJed Brown   PetscFunctionReturn(0);
4736cef5090cSJed Brown }
4737cef5090cSJed Brown 
4738cef5090cSJed Brown #undef __FUNCT__
4739cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
4740cef5090cSJed Brown /*@
4741cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4742cef5090cSJed Brown 
4743cef5090cSJed Brown    Not Collective
4744cef5090cSJed Brown 
4745cef5090cSJed Brown    Input Parameter:
4746cef5090cSJed Brown .  ts - TS context
4747cef5090cSJed Brown 
4748cef5090cSJed Brown    Output Parameter:
4749cef5090cSJed Brown .  fails - number of failed nonlinear solves
4750cef5090cSJed Brown 
4751cef5090cSJed Brown    Notes:
4752cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4753cef5090cSJed Brown 
4754cef5090cSJed Brown    Level: intermediate
4755cef5090cSJed Brown 
4756cef5090cSJed Brown .keywords: TS, get, number
4757cef5090cSJed Brown 
47585ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4759cef5090cSJed Brown @*/
4760cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4761cef5090cSJed Brown {
4762cef5090cSJed Brown   PetscFunctionBegin;
4763cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4764cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4765cef5090cSJed Brown   *fails = ts->num_snes_failures;
4766cef5090cSJed Brown   PetscFunctionReturn(0);
4767cef5090cSJed Brown }
4768cef5090cSJed Brown 
4769cef5090cSJed Brown #undef __FUNCT__
4770cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
4771cef5090cSJed Brown /*@
4772cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4773cef5090cSJed Brown 
4774cef5090cSJed Brown    Not Collective
4775cef5090cSJed Brown 
4776cef5090cSJed Brown    Input Parameter:
4777cef5090cSJed Brown +  ts - TS context
4778cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4779cef5090cSJed Brown 
4780cef5090cSJed Brown    Notes:
4781cef5090cSJed Brown    The counter is reset to zero for each step
4782cef5090cSJed Brown 
4783cef5090cSJed Brown    Options Database Key:
4784cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4785cef5090cSJed Brown 
4786cef5090cSJed Brown    Level: intermediate
4787cef5090cSJed Brown 
4788cef5090cSJed Brown .keywords: TS, set, maximum, number
4789cef5090cSJed Brown 
47905ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4791cef5090cSJed Brown @*/
4792cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4793cef5090cSJed Brown {
4794cef5090cSJed Brown   PetscFunctionBegin;
4795cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4796cef5090cSJed Brown   ts->max_reject = rejects;
4797cef5090cSJed Brown   PetscFunctionReturn(0);
4798cef5090cSJed Brown }
4799cef5090cSJed Brown 
4800cef5090cSJed Brown #undef __FUNCT__
4801cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
4802cef5090cSJed Brown /*@
4803cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4804cef5090cSJed Brown 
4805cef5090cSJed Brown    Not Collective
4806cef5090cSJed Brown 
4807cef5090cSJed Brown    Input Parameter:
4808cef5090cSJed Brown +  ts - TS context
4809cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4810cef5090cSJed Brown 
4811cef5090cSJed Brown    Notes:
4812cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4813cef5090cSJed Brown 
4814cef5090cSJed Brown    Options Database Key:
4815cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4816cef5090cSJed Brown 
4817cef5090cSJed Brown    Level: intermediate
4818cef5090cSJed Brown 
4819cef5090cSJed Brown .keywords: TS, set, maximum, number
4820cef5090cSJed Brown 
48215ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4822cef5090cSJed Brown @*/
4823cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4824cef5090cSJed Brown {
4825cef5090cSJed Brown   PetscFunctionBegin;
4826cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4827cef5090cSJed Brown   ts->max_snes_failures = fails;
4828cef5090cSJed Brown   PetscFunctionReturn(0);
4829cef5090cSJed Brown }
4830cef5090cSJed Brown 
4831cef5090cSJed Brown #undef __FUNCT__
48324e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
4833cef5090cSJed Brown /*@
4834cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4835cef5090cSJed Brown 
4836cef5090cSJed Brown    Not Collective
4837cef5090cSJed Brown 
4838cef5090cSJed Brown    Input Parameter:
4839cef5090cSJed Brown +  ts - TS context
4840cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4841cef5090cSJed Brown 
4842cef5090cSJed Brown    Options Database Key:
4843cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4844cef5090cSJed Brown 
4845cef5090cSJed Brown    Level: intermediate
4846cef5090cSJed Brown 
4847cef5090cSJed Brown .keywords: TS, set, error
4848cef5090cSJed Brown 
48495ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4850cef5090cSJed Brown @*/
4851cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4852cef5090cSJed Brown {
4853cef5090cSJed Brown   PetscFunctionBegin;
4854cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4855cef5090cSJed Brown   ts->errorifstepfailed = err;
4856cef5090cSJed Brown   PetscFunctionReturn(0);
4857cef5090cSJed Brown }
4858cef5090cSJed Brown 
4859cef5090cSJed Brown #undef __FUNCT__
4860fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
4861fb1732b5SBarry Smith /*@C
4862fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
4863fb1732b5SBarry Smith 
4864fb1732b5SBarry Smith    Collective on TS
4865fb1732b5SBarry Smith 
4866fb1732b5SBarry Smith    Input Parameters:
4867fb1732b5SBarry Smith +  ts - the TS context
4868fb1732b5SBarry Smith .  step - current time-step
4869fb1732b5SBarry Smith .  ptime - current time
48700910c330SBarry Smith .  u - current state
4871fb1732b5SBarry Smith -  viewer - binary viewer
4872fb1732b5SBarry Smith 
4873fb1732b5SBarry Smith    Level: intermediate
4874fb1732b5SBarry Smith 
4875fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4876fb1732b5SBarry Smith 
4877fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4878fb1732b5SBarry Smith @*/
48790910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4880fb1732b5SBarry Smith {
4881fb1732b5SBarry Smith   PetscErrorCode ierr;
4882ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4883fb1732b5SBarry Smith 
4884fb1732b5SBarry Smith   PetscFunctionBegin;
48850910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4886ed81e22dSJed Brown   PetscFunctionReturn(0);
4887ed81e22dSJed Brown }
4888ed81e22dSJed Brown 
4889ed81e22dSJed Brown #undef __FUNCT__
4890ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4891ed81e22dSJed Brown /*@C
4892ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4893ed81e22dSJed Brown 
4894ed81e22dSJed Brown    Collective on TS
4895ed81e22dSJed Brown 
4896ed81e22dSJed Brown    Input Parameters:
4897ed81e22dSJed Brown +  ts - the TS context
4898ed81e22dSJed Brown .  step - current time-step
4899ed81e22dSJed Brown .  ptime - current time
49000910c330SBarry Smith .  u - current state
4901ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4902ed81e22dSJed Brown 
4903ed81e22dSJed Brown    Level: intermediate
4904ed81e22dSJed Brown 
4905ed81e22dSJed Brown    Notes:
4906ed81e22dSJed 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.
4907ed81e22dSJed Brown    These are named according to the file name template.
4908ed81e22dSJed Brown 
4909ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4910ed81e22dSJed Brown 
4911ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4912ed81e22dSJed Brown 
4913ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4914ed81e22dSJed Brown @*/
49150910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4916ed81e22dSJed Brown {
4917ed81e22dSJed Brown   PetscErrorCode ierr;
4918ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4919ed81e22dSJed Brown   PetscViewer    viewer;
4920ed81e22dSJed Brown 
4921ed81e22dSJed Brown   PetscFunctionBegin;
49228caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4923ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
49240910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4925ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4926ed81e22dSJed Brown   PetscFunctionReturn(0);
4927ed81e22dSJed Brown }
4928ed81e22dSJed Brown 
4929ed81e22dSJed Brown #undef __FUNCT__
4930ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4931ed81e22dSJed Brown /*@C
4932ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4933ed81e22dSJed Brown 
4934ed81e22dSJed Brown    Collective on TS
4935ed81e22dSJed Brown 
4936ed81e22dSJed Brown    Input Parameters:
4937ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4938ed81e22dSJed Brown 
4939ed81e22dSJed Brown    Level: intermediate
4940ed81e22dSJed Brown 
4941ed81e22dSJed Brown    Note:
4942ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4943ed81e22dSJed Brown 
4944ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4945ed81e22dSJed Brown 
4946ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4947ed81e22dSJed Brown @*/
4948ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4949ed81e22dSJed Brown {
4950ed81e22dSJed Brown   PetscErrorCode ierr;
4951ed81e22dSJed Brown 
4952ed81e22dSJed Brown   PetscFunctionBegin;
4953ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4954fb1732b5SBarry Smith   PetscFunctionReturn(0);
4955fb1732b5SBarry Smith }
4956fb1732b5SBarry Smith 
495784df9cb4SJed Brown #undef __FUNCT__
4958552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
495984df9cb4SJed Brown /*@
4960552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
496184df9cb4SJed Brown 
4962ed81e22dSJed Brown    Collective on TS if controller has not been created yet
496384df9cb4SJed Brown 
496484df9cb4SJed Brown    Input Arguments:
4965ed81e22dSJed Brown .  ts - time stepping context
496684df9cb4SJed Brown 
496784df9cb4SJed Brown    Output Arguments:
4968ed81e22dSJed Brown .  adapt - adaptive controller
496984df9cb4SJed Brown 
497084df9cb4SJed Brown    Level: intermediate
497184df9cb4SJed Brown 
4972ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
497384df9cb4SJed Brown @*/
4974552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
497584df9cb4SJed Brown {
497684df9cb4SJed Brown   PetscErrorCode ierr;
497784df9cb4SJed Brown 
497884df9cb4SJed Brown   PetscFunctionBegin;
497984df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
498084df9cb4SJed Brown   PetscValidPointer(adapt,2);
498184df9cb4SJed Brown   if (!ts->adapt) {
4982ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
49833bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
49841c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
498584df9cb4SJed Brown   }
498684df9cb4SJed Brown   *adapt = ts->adapt;
498784df9cb4SJed Brown   PetscFunctionReturn(0);
498884df9cb4SJed Brown }
4989d6ebe24aSShri Abhyankar 
4990d6ebe24aSShri Abhyankar #undef __FUNCT__
49911c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
49921c3436cfSJed Brown /*@
49931c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
49941c3436cfSJed Brown 
49951c3436cfSJed Brown    Logically Collective
49961c3436cfSJed Brown 
49971c3436cfSJed Brown    Input Arguments:
49981c3436cfSJed Brown +  ts - time integration context
49991c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
50000298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
50011c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
50020298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
50031c3436cfSJed Brown 
5004a3cdaa26SBarry Smith    Options Database keys:
5005a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5006a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5007a3cdaa26SBarry Smith 
50081c3436cfSJed Brown    Level: beginner
50091c3436cfSJed Brown 
5010c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
50111c3436cfSJed Brown @*/
50121c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
50131c3436cfSJed Brown {
50141c3436cfSJed Brown   PetscErrorCode ierr;
50151c3436cfSJed Brown 
50161c3436cfSJed Brown   PetscFunctionBegin;
5017c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
50181c3436cfSJed Brown   if (vatol) {
50191c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
50201c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
5021bbd56ea5SKarl Rupp 
50221c3436cfSJed Brown     ts->vatol = vatol;
50231c3436cfSJed Brown   }
5024c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
50251c3436cfSJed Brown   if (vrtol) {
50261c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
50271c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
5028bbd56ea5SKarl Rupp 
50291c3436cfSJed Brown     ts->vrtol = vrtol;
50301c3436cfSJed Brown   }
50311c3436cfSJed Brown   PetscFunctionReturn(0);
50321c3436cfSJed Brown }
50331c3436cfSJed Brown 
50341c3436cfSJed Brown #undef __FUNCT__
5035c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
5036c5033834SJed Brown /*@
5037c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5038c5033834SJed Brown 
5039c5033834SJed Brown    Logically Collective
5040c5033834SJed Brown 
5041c5033834SJed Brown    Input Arguments:
5042c5033834SJed Brown .  ts - time integration context
5043c5033834SJed Brown 
5044c5033834SJed Brown    Output Arguments:
50450298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
50460298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
50470298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
50480298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5049c5033834SJed Brown 
5050c5033834SJed Brown    Level: beginner
5051c5033834SJed Brown 
5052c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5053c5033834SJed Brown @*/
5054c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5055c5033834SJed Brown {
5056c5033834SJed Brown   PetscFunctionBegin;
5057c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5058c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5059c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5060c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5061c5033834SJed Brown   PetscFunctionReturn(0);
5062c5033834SJed Brown }
5063c5033834SJed Brown 
5064c5033834SJed Brown #undef __FUNCT__
50651c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
50661c3436cfSJed Brown /*@
50671c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
50681c3436cfSJed Brown 
50691c3436cfSJed Brown    Collective on TS
50701c3436cfSJed Brown 
50711c3436cfSJed Brown    Input Arguments:
50721c3436cfSJed Brown +  ts - time stepping context
50731c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
50741c3436cfSJed Brown 
50751c3436cfSJed Brown    Output Arguments:
50761c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
50771c3436cfSJed Brown 
50781c3436cfSJed Brown    Level: developer
50791c3436cfSJed Brown 
50801c3436cfSJed Brown .seealso: TSSetTolerances()
50811c3436cfSJed Brown @*/
50821c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
50831c3436cfSJed Brown {
50841c3436cfSJed Brown   PetscErrorCode    ierr;
50851c3436cfSJed Brown   PetscInt          i,n,N;
50860910c330SBarry Smith   const PetscScalar *u,*y;
50870910c330SBarry Smith   Vec               U;
50881c3436cfSJed Brown   PetscReal         sum,gsum;
50891c3436cfSJed Brown 
50901c3436cfSJed Brown   PetscFunctionBegin;
50911c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50921c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
50931c3436cfSJed Brown   PetscValidPointer(norm,3);
50940910c330SBarry Smith   U = ts->vec_sol;
50950910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
5096ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
50971c3436cfSJed Brown 
50980910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
50990910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
51000910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
51011c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
51021c3436cfSJed Brown   sum  = 0.;
5103ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
5104ceefc113SJed Brown     const PetscScalar *atol,*rtol;
5105ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
5106ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
5107ceefc113SJed Brown     for (i=0; i<n; i++) {
51080910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51090910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
5110ceefc113SJed Brown     }
5111ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
5112ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
5113ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
5114ceefc113SJed Brown     const PetscScalar *atol;
5115ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
5116ceefc113SJed Brown     for (i=0; i<n; i++) {
51170910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51180910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
5119ceefc113SJed Brown     }
5120ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
5121ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
5122ceefc113SJed Brown     const PetscScalar *rtol;
5123ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
5124ceefc113SJed Brown     for (i=0; i<n; i++) {
51250910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51260910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
5127ceefc113SJed Brown     }
5128ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
5129ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
51301c3436cfSJed Brown     for (i=0; i<n; i++) {
51310910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51320910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
51331c3436cfSJed Brown     }
5134ceefc113SJed Brown   }
51350910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
51361c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
51371c3436cfSJed Brown 
5138ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
51391c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
5140620fc8aaSSatish Balay   if (PetscIsInfOrNanReal(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
51411c3436cfSJed Brown   PetscFunctionReturn(0);
51421c3436cfSJed Brown }
51431c3436cfSJed Brown 
51441c3436cfSJed Brown #undef __FUNCT__
51458d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
51468d59e960SJed Brown /*@
51478d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
51488d59e960SJed Brown 
51498d59e960SJed Brown    Logically Collective on TS
51508d59e960SJed Brown 
51518d59e960SJed Brown    Input Arguments:
51528d59e960SJed Brown +  ts - time stepping context
51538d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
51548d59e960SJed Brown 
51558d59e960SJed Brown    Note:
51568d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
51578d59e960SJed Brown 
51588d59e960SJed Brown    Level: intermediate
51598d59e960SJed Brown 
51608d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
51618d59e960SJed Brown @*/
51628d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
51638d59e960SJed Brown {
51648d59e960SJed Brown   PetscFunctionBegin;
51658d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
51668d59e960SJed Brown   ts->cfltime_local = cfltime;
51678d59e960SJed Brown   ts->cfltime       = -1.;
51688d59e960SJed Brown   PetscFunctionReturn(0);
51698d59e960SJed Brown }
51708d59e960SJed Brown 
51718d59e960SJed Brown #undef __FUNCT__
51728d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
51738d59e960SJed Brown /*@
51748d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
51758d59e960SJed Brown 
51768d59e960SJed Brown    Collective on TS
51778d59e960SJed Brown 
51788d59e960SJed Brown    Input Arguments:
51798d59e960SJed Brown .  ts - time stepping context
51808d59e960SJed Brown 
51818d59e960SJed Brown    Output Arguments:
51828d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
51838d59e960SJed Brown 
51848d59e960SJed Brown    Level: advanced
51858d59e960SJed Brown 
51868d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
51878d59e960SJed Brown @*/
51888d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
51898d59e960SJed Brown {
51908d59e960SJed Brown   PetscErrorCode ierr;
51918d59e960SJed Brown 
51928d59e960SJed Brown   PetscFunctionBegin;
51938d59e960SJed Brown   if (ts->cfltime < 0) {
5194ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
51958d59e960SJed Brown   }
51968d59e960SJed Brown   *cfltime = ts->cfltime;
51978d59e960SJed Brown   PetscFunctionReturn(0);
51988d59e960SJed Brown }
51998d59e960SJed Brown 
52008d59e960SJed Brown #undef __FUNCT__
5201d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
5202d6ebe24aSShri Abhyankar /*@
5203d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5204d6ebe24aSShri Abhyankar 
5205d6ebe24aSShri Abhyankar    Input Parameters:
5206d6ebe24aSShri Abhyankar .  ts   - the TS context.
5207d6ebe24aSShri Abhyankar .  xl   - lower bound.
5208d6ebe24aSShri Abhyankar .  xu   - upper bound.
5209d6ebe24aSShri Abhyankar 
5210d6ebe24aSShri Abhyankar    Notes:
5211d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5212e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5213d6ebe24aSShri Abhyankar 
52142bd2b0e6SSatish Balay    Level: advanced
52152bd2b0e6SSatish Balay 
5216d6ebe24aSShri Abhyankar @*/
5217d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5218d6ebe24aSShri Abhyankar {
5219d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5220d6ebe24aSShri Abhyankar   SNES           snes;
5221d6ebe24aSShri Abhyankar 
5222d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5223d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5224d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5225d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5226d6ebe24aSShri Abhyankar }
5227d6ebe24aSShri Abhyankar 
5228325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
5229c6db04a5SJed Brown #include <mex.h>
5230325fc9f4SBarry Smith 
5231325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
5232325fc9f4SBarry Smith 
5233325fc9f4SBarry Smith #undef __FUNCT__
5234325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
5235325fc9f4SBarry Smith /*
5236325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
5237325fc9f4SBarry Smith                          TSSetFunctionMatlab().
5238325fc9f4SBarry Smith 
5239325fc9f4SBarry Smith    Collective on TS
5240325fc9f4SBarry Smith 
5241325fc9f4SBarry Smith    Input Parameters:
5242325fc9f4SBarry Smith +  snes - the TS context
52430910c330SBarry Smith -  u - input vector
5244325fc9f4SBarry Smith 
5245325fc9f4SBarry Smith    Output Parameter:
5246325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
5247325fc9f4SBarry Smith 
5248325fc9f4SBarry Smith    Notes:
5249325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
5250325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
5251325fc9f4SBarry Smith    themselves.
5252325fc9f4SBarry Smith 
5253325fc9f4SBarry Smith    Level: developer
5254325fc9f4SBarry Smith 
5255325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5256325fc9f4SBarry Smith 
5257325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5258325fc9f4SBarry Smith */
52590910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
5260325fc9f4SBarry Smith {
5261325fc9f4SBarry Smith   PetscErrorCode  ierr;
5262325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5263325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
5264325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
5265325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
5266325fc9f4SBarry Smith 
5267325fc9f4SBarry Smith   PetscFunctionBegin;
5268325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
52690910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
52700910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
5271325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
52720910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
5273325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
5274325fc9f4SBarry Smith 
5275325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
52760910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
52770910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
52780910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
5279bbd56ea5SKarl Rupp 
5280325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5281325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
5282325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5283325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5284325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
5285325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
5286325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
5287325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
5288325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5289325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5290325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5291325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5292325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5293325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5294325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5295325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5296325fc9f4SBarry Smith   PetscFunctionReturn(0);
5297325fc9f4SBarry Smith }
5298325fc9f4SBarry Smith 
5299325fc9f4SBarry Smith 
5300325fc9f4SBarry Smith #undef __FUNCT__
5301325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
5302325fc9f4SBarry Smith /*
5303325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
5304325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
5305e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
5306325fc9f4SBarry Smith 
5307325fc9f4SBarry Smith    Logically Collective on TS
5308325fc9f4SBarry Smith 
5309325fc9f4SBarry Smith    Input Parameters:
5310325fc9f4SBarry Smith +  ts - the TS context
5311325fc9f4SBarry Smith -  func - function evaluation routine
5312325fc9f4SBarry Smith 
5313325fc9f4SBarry Smith    Calling sequence of func:
53140910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
5315325fc9f4SBarry Smith 
5316325fc9f4SBarry Smith    Level: beginner
5317325fc9f4SBarry Smith 
5318325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5319325fc9f4SBarry Smith 
5320325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5321325fc9f4SBarry Smith */
5322cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
5323325fc9f4SBarry Smith {
5324325fc9f4SBarry Smith   PetscErrorCode  ierr;
5325325fc9f4SBarry Smith   TSMatlabContext *sctx;
5326325fc9f4SBarry Smith 
5327325fc9f4SBarry Smith   PetscFunctionBegin;
5328325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5329325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5330325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5331325fc9f4SBarry Smith   /*
5332325fc9f4SBarry Smith      This should work, but it doesn't
5333325fc9f4SBarry Smith   sctx->ctx = ctx;
5334325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5335325fc9f4SBarry Smith   */
5336325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5337bbd56ea5SKarl Rupp 
53380298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
5339325fc9f4SBarry Smith   PetscFunctionReturn(0);
5340325fc9f4SBarry Smith }
5341325fc9f4SBarry Smith 
5342325fc9f4SBarry Smith #undef __FUNCT__
5343325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
5344325fc9f4SBarry Smith /*
5345325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
5346325fc9f4SBarry Smith                          TSSetJacobianMatlab().
5347325fc9f4SBarry Smith 
5348325fc9f4SBarry Smith    Collective on TS
5349325fc9f4SBarry Smith 
5350325fc9f4SBarry Smith    Input Parameters:
5351cdcf91faSSean Farley +  ts - the TS context
53520910c330SBarry Smith .  u - input vector
5353325fc9f4SBarry Smith .  A, B - the matrices
5354325fc9f4SBarry Smith -  ctx - user context
5355325fc9f4SBarry Smith 
5356325fc9f4SBarry Smith    Level: developer
5357325fc9f4SBarry Smith 
5358325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5359325fc9f4SBarry Smith 
5360325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5361325fc9f4SBarry Smith @*/
5362f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
5363325fc9f4SBarry Smith {
5364325fc9f4SBarry Smith   PetscErrorCode  ierr;
5365325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5366325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
5367325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
5368325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
5369325fc9f4SBarry Smith 
5370325fc9f4SBarry Smith   PetscFunctionBegin;
5371cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53720910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
5373325fc9f4SBarry Smith 
53740910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
5375325fc9f4SBarry Smith 
5376cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
53770910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
53780910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
53790910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
53800910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
5381bbd56ea5SKarl Rupp 
5382325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5383325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
5384325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5385325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5386325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
5387325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
5388325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
5389325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
5390325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
5391325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
5392325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5393325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5394325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5395325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5396325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5397325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5398325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5399325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
5400325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
5401325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5402325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
5403325fc9f4SBarry Smith   PetscFunctionReturn(0);
5404325fc9f4SBarry Smith }
5405325fc9f4SBarry Smith 
5406325fc9f4SBarry Smith 
5407325fc9f4SBarry Smith #undef __FUNCT__
5408325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
5409325fc9f4SBarry Smith /*
5410325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
5411e3c5b3baSBarry 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
5412325fc9f4SBarry Smith 
5413325fc9f4SBarry Smith    Logically Collective on TS
5414325fc9f4SBarry Smith 
5415325fc9f4SBarry Smith    Input Parameters:
5416cdcf91faSSean Farley +  ts - the TS context
5417325fc9f4SBarry Smith .  A,B - Jacobian matrices
5418325fc9f4SBarry Smith .  func - function evaluation routine
5419325fc9f4SBarry Smith -  ctx - user context
5420325fc9f4SBarry Smith 
5421325fc9f4SBarry Smith    Calling sequence of func:
54220910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
5423325fc9f4SBarry Smith 
5424325fc9f4SBarry Smith 
5425325fc9f4SBarry Smith    Level: developer
5426325fc9f4SBarry Smith 
5427325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5428325fc9f4SBarry Smith 
5429325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5430325fc9f4SBarry Smith */
5431cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
5432325fc9f4SBarry Smith {
5433325fc9f4SBarry Smith   PetscErrorCode  ierr;
5434325fc9f4SBarry Smith   TSMatlabContext *sctx;
5435325fc9f4SBarry Smith 
5436325fc9f4SBarry Smith   PetscFunctionBegin;
5437325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5438325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5439325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5440325fc9f4SBarry Smith   /*
5441325fc9f4SBarry Smith      This should work, but it doesn't
5442325fc9f4SBarry Smith   sctx->ctx = ctx;
5443325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5444325fc9f4SBarry Smith   */
5445325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5446bbd56ea5SKarl Rupp 
5447cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
5448325fc9f4SBarry Smith   PetscFunctionReturn(0);
5449325fc9f4SBarry Smith }
5450325fc9f4SBarry Smith 
5451b5b1a830SBarry Smith #undef __FUNCT__
5452b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
5453b5b1a830SBarry Smith /*
5454b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
5455b5b1a830SBarry Smith 
5456b5b1a830SBarry Smith    Collective on TS
5457b5b1a830SBarry Smith 
5458b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5459b5b1a830SBarry Smith @*/
54600910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
5461b5b1a830SBarry Smith {
5462b5b1a830SBarry Smith   PetscErrorCode  ierr;
5463b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5464a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
5465b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
5466b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
5467b5b1a830SBarry Smith 
5468b5b1a830SBarry Smith   PetscFunctionBegin;
5469cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54700910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
5471b5b1a830SBarry Smith 
5472cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
54730910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
5474bbd56ea5SKarl Rupp 
5475b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5476b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
5477b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
5478b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
5479b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
5480b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
5481b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
5482b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5483b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
5484b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
5485b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
5486b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
5487b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
5488b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
5489b5b1a830SBarry Smith   PetscFunctionReturn(0);
5490b5b1a830SBarry Smith }
5491b5b1a830SBarry Smith 
5492b5b1a830SBarry Smith 
5493b5b1a830SBarry Smith #undef __FUNCT__
5494b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
5495b5b1a830SBarry Smith /*
5496b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
5497b5b1a830SBarry Smith 
5498b5b1a830SBarry Smith    Level: developer
5499b5b1a830SBarry Smith 
5500b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
5501b5b1a830SBarry Smith 
5502b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5503b5b1a830SBarry Smith */
5504cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
5505b5b1a830SBarry Smith {
5506b5b1a830SBarry Smith   PetscErrorCode  ierr;
5507b5b1a830SBarry Smith   TSMatlabContext *sctx;
5508b5b1a830SBarry Smith 
5509b5b1a830SBarry Smith   PetscFunctionBegin;
5510b5b1a830SBarry Smith   /* currently sctx is memory bleed */
5511b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5512b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5513b5b1a830SBarry Smith   /*
5514b5b1a830SBarry Smith      This should work, but it doesn't
5515b5b1a830SBarry Smith   sctx->ctx = ctx;
5516b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5517b5b1a830SBarry Smith   */
5518b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5519bbd56ea5SKarl Rupp 
55200298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
5521b5b1a830SBarry Smith   PetscFunctionReturn(0);
5522b5b1a830SBarry Smith }
5523325fc9f4SBarry Smith #endif
5524b3603a34SBarry Smith 
55250b039ecaSBarry Smith 
55260b039ecaSBarry Smith 
5527b3603a34SBarry Smith #undef __FUNCT__
55284f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
5529b3603a34SBarry Smith /*@C
55304f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
5531b3603a34SBarry Smith        in a time based line graph
5532b3603a34SBarry Smith 
5533b3603a34SBarry Smith    Collective on TS
5534b3603a34SBarry Smith 
5535b3603a34SBarry Smith    Input Parameters:
5536b3603a34SBarry Smith +  ts - the TS context
5537b3603a34SBarry Smith .  step - current time-step
5538b3603a34SBarry Smith .  ptime - current time
5539b3603a34SBarry Smith -  lg - a line graph object
5540b3603a34SBarry Smith 
5541b3603a34SBarry Smith    Level: intermediate
5542b3603a34SBarry Smith 
55430b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
5544b3603a34SBarry Smith 
5545b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
5546b3603a34SBarry Smith 
5547b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5548b3603a34SBarry Smith @*/
55490910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5550b3603a34SBarry Smith {
5551b3603a34SBarry Smith   PetscErrorCode    ierr;
55520b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5553b3603a34SBarry Smith   const PetscScalar *yy;
555458ff32f7SBarry Smith   PetscInt          dim;
5555b3603a34SBarry Smith 
5556b3603a34SBarry Smith   PetscFunctionBegin;
555758ff32f7SBarry Smith   if (!step) {
5558a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5559a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5560a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
55610910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
55620b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
55630b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
556458ff32f7SBarry Smith   }
55650910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
5566e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5567e3efe391SJed Brown   {
5568e3efe391SJed Brown     PetscReal *yreal;
5569e3efe391SJed Brown     PetscInt  i,n;
55700910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
5571785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5572e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
55730b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5574e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5575e3efe391SJed Brown   }
5576e3efe391SJed Brown #else
55770b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5578e3efe391SJed Brown #endif
55790910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
5580b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
55810b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
55823923b477SBarry Smith   }
5583b3603a34SBarry Smith   PetscFunctionReturn(0);
5584b3603a34SBarry Smith }
5585b3603a34SBarry Smith 
5586b3603a34SBarry Smith #undef __FUNCT__
55874f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
5588ef20d060SBarry Smith /*@C
55894f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
5590ef20d060SBarry Smith        in a time based line graph
5591ef20d060SBarry Smith 
5592ef20d060SBarry Smith    Collective on TS
5593ef20d060SBarry Smith 
5594ef20d060SBarry Smith    Input Parameters:
5595ef20d060SBarry Smith +  ts - the TS context
5596ef20d060SBarry Smith .  step - current time-step
5597ef20d060SBarry Smith .  ptime - current time
5598ef20d060SBarry Smith -  lg - a line graph object
5599ef20d060SBarry Smith 
5600ef20d060SBarry Smith    Level: intermediate
5601ef20d060SBarry Smith 
5602abd5a294SJed Brown    Notes:
5603abd5a294SJed Brown    Only for sequential solves.
5604abd5a294SJed Brown 
5605abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
5606abd5a294SJed Brown 
5607abd5a294SJed Brown    Options Database Keys:
56084f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
5609ef20d060SBarry Smith 
5610ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
5611ef20d060SBarry Smith 
5612abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
5613ef20d060SBarry Smith @*/
56140910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5615ef20d060SBarry Smith {
5616ef20d060SBarry Smith   PetscErrorCode    ierr;
56170b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5618ef20d060SBarry Smith   const PetscScalar *yy;
5619ef20d060SBarry Smith   Vec               y;
5620a9f9c1f6SBarry Smith   PetscInt          dim;
5621ef20d060SBarry Smith 
5622ef20d060SBarry Smith   PetscFunctionBegin;
5623a9f9c1f6SBarry Smith   if (!step) {
5624a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5625a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
56261ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
56270910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5628a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5629a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5630a9f9c1f6SBarry Smith   }
56310910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
5632ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
56330910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
5634ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
5635e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5636e3efe391SJed Brown   {
5637e3efe391SJed Brown     PetscReal *yreal;
5638e3efe391SJed Brown     PetscInt  i,n;
5639e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
5640785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5641e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
56420b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5643e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5644e3efe391SJed Brown   }
5645e3efe391SJed Brown #else
56460b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5647e3efe391SJed Brown #endif
5648ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
5649ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
5650b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
56510b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
56523923b477SBarry Smith   }
5653ef20d060SBarry Smith   PetscFunctionReturn(0);
5654ef20d060SBarry Smith }
5655ef20d060SBarry Smith 
5656201da799SBarry Smith #undef __FUNCT__
5657201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
5658201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5659201da799SBarry Smith {
5660201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5661201da799SBarry Smith   PetscReal      x   = ptime,y;
5662201da799SBarry Smith   PetscErrorCode ierr;
5663201da799SBarry Smith   PetscInt       its;
5664201da799SBarry Smith 
5665201da799SBarry Smith   PetscFunctionBegin;
5666201da799SBarry Smith   if (!n) {
5667201da799SBarry Smith     PetscDrawAxis axis;
5668bbd56ea5SKarl Rupp 
5669201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5670201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
5671201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5672bbd56ea5SKarl Rupp 
5673201da799SBarry Smith     ctx->snes_its = 0;
5674201da799SBarry Smith   }
5675201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
5676201da799SBarry Smith   y    = its - ctx->snes_its;
5677201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
56783fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5679201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5680201da799SBarry Smith   }
5681201da799SBarry Smith   ctx->snes_its = its;
5682201da799SBarry Smith   PetscFunctionReturn(0);
5683201da799SBarry Smith }
5684201da799SBarry Smith 
5685201da799SBarry Smith #undef __FUNCT__
5686201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
5687201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5688201da799SBarry Smith {
5689201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5690201da799SBarry Smith   PetscReal      x   = ptime,y;
5691201da799SBarry Smith   PetscErrorCode ierr;
5692201da799SBarry Smith   PetscInt       its;
5693201da799SBarry Smith 
5694201da799SBarry Smith   PetscFunctionBegin;
5695201da799SBarry Smith   if (!n) {
5696201da799SBarry Smith     PetscDrawAxis axis;
5697bbd56ea5SKarl Rupp 
5698201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5699201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
5700201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5701bbd56ea5SKarl Rupp 
5702201da799SBarry Smith     ctx->ksp_its = 0;
5703201da799SBarry Smith   }
5704201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
5705201da799SBarry Smith   y    = its - ctx->ksp_its;
5706201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
570799fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5708201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5709201da799SBarry Smith   }
5710201da799SBarry Smith   ctx->ksp_its = its;
5711201da799SBarry Smith   PetscFunctionReturn(0);
5712201da799SBarry Smith }
5713f9c1d6abSBarry Smith 
5714f9c1d6abSBarry Smith #undef __FUNCT__
5715f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
5716f9c1d6abSBarry Smith /*@
5717f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
5718f9c1d6abSBarry Smith 
5719f9c1d6abSBarry Smith    Collective on TS and Vec
5720f9c1d6abSBarry Smith 
5721f9c1d6abSBarry Smith    Input Parameters:
5722f9c1d6abSBarry Smith +  ts - the TS context
5723f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
5724f9c1d6abSBarry Smith 
5725f9c1d6abSBarry Smith    Output Parameters:
5726f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
5727f9c1d6abSBarry Smith 
5728f9c1d6abSBarry Smith    Level: developer
5729f9c1d6abSBarry Smith 
5730f9c1d6abSBarry Smith .keywords: TS, compute
5731f9c1d6abSBarry Smith 
5732f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
5733f9c1d6abSBarry Smith @*/
5734f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
5735f9c1d6abSBarry Smith {
5736f9c1d6abSBarry Smith   PetscErrorCode ierr;
5737f9c1d6abSBarry Smith 
5738f9c1d6abSBarry Smith   PetscFunctionBegin;
5739f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5740ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
5741f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
5742f9c1d6abSBarry Smith   PetscFunctionReturn(0);
5743f9c1d6abSBarry Smith }
574424655328SShri 
574524655328SShri #undef __FUNCT__
574624655328SShri #define __FUNCT__ "TSRollBack"
574724655328SShri /*@
574824655328SShri    TSRollBack - Rolls back one time step
574924655328SShri 
575024655328SShri    Collective on TS
575124655328SShri 
575224655328SShri    Input Parameter:
575324655328SShri .  ts - the TS context obtained from TSCreate()
575424655328SShri 
575524655328SShri    Level: advanced
575624655328SShri 
575724655328SShri .keywords: TS, timestep, rollback
575824655328SShri 
575924655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
576024655328SShri @*/
576124655328SShri PetscErrorCode  TSRollBack(TS ts)
576224655328SShri {
576324655328SShri   PetscErrorCode ierr;
576424655328SShri 
576524655328SShri   PetscFunctionBegin;
576624655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
576724655328SShri 
576824655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
576924655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
577024655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
577124655328SShri   ts->ptime = ts->ptime_prev;
577224655328SShri   PetscFunctionReturn(0);
577324655328SShri }
5774aeb4809dSShri Abhyankar 
5775ff22ae23SHong Zhang #undef __FUNCT__
5776ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
5777ff22ae23SHong Zhang /*@
5778ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
5779ff22ae23SHong Zhang 
5780ff22ae23SHong Zhang    Input Parameter:
5781ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
5782ff22ae23SHong Zhang 
5783ff22ae23SHong Zhang    Level: advanced
5784ff22ae23SHong Zhang 
5785ff22ae23SHong Zhang .keywords: TS, getstages
5786ff22ae23SHong Zhang 
5787ff22ae23SHong Zhang .seealso: TSCreate()
5788ff22ae23SHong Zhang @*/
5789ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
5790ff22ae23SHong Zhang {
5791ff22ae23SHong Zhang   PetscErrorCode ierr;
5792ff22ae23SHong Zhang 
5793ff22ae23SHong Zhang   PetscFunctionBegin;
5794ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
5795ff22ae23SHong Zhang   PetscValidPointer(ns,2);
5796ff22ae23SHong Zhang 
5797ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
5798ff22ae23SHong Zhang   else {
5799ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
5800ff22ae23SHong Zhang   }
5801ff22ae23SHong Zhang   PetscFunctionReturn(0);
5802ff22ae23SHong Zhang }
5803ff22ae23SHong Zhang 
5804