xref: /petsc/src/ts/interface/ts.c (revision b3d3934dd032f675c0f0a24a60e80a333df5cc63)
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__
15bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
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 */
296849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(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
72bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
733bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
74bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
75bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
76de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
77de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
78de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
79de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
80de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
81de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
82de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
832d5ee99bSBarry Smith .  -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram
84de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
8591b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
86*b3d3934dSBarry Smith .  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
87*b3d3934dSBarry Smith -  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
8891b97e58SBarry Smith 
8991b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
90bdad233fSMatthew Knepley 
91bdad233fSMatthew Knepley    Level: beginner
92bdad233fSMatthew Knepley 
93bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
94bdad233fSMatthew Knepley 
95a313700dSBarry Smith .seealso: TSGetType()
96bdad233fSMatthew Knepley @*/
977087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
98bdad233fSMatthew Knepley {
99ace3abfcSBarry Smith   PetscBool              opt,flg;
100dfbe8321SBarry Smith   PetscErrorCode         ierr;
101649052a6SBarry Smith   PetscViewer            monviewer;
102eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
103089b2837SJed Brown   SNES                   snes;
1041c3436cfSJed Brown   TSAdapt                adapt;
10531748224SBarry Smith   PetscReal              time_step;
10649354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
107d1212d36SBarry Smith   char                   dir[16];
108bdad233fSMatthew Knepley 
109bdad233fSMatthew Knepley   PetscFunctionBegin;
1100700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1113194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
112a43b19c4SJed Brown   /* Handle TS type options */
113a43b19c4SJed Brown   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
114bdad233fSMatthew Knepley 
115bdad233fSMatthew Knepley   /* Handle generic TS options */
1160298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1170298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1180298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
11931748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
12031748224SBarry Smith   if (flg) {
12131748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
12231748224SBarry Smith   }
12349354f04SShri 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);
12449354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1250298fd71SBarry 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);
1260298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1270298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1280298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1290298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
130bdad233fSMatthew Knepley 
13156f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
13256f85f32SBarry Smith   {
13356f85f32SBarry Smith   PetscBool set;
13456f85f32SBarry Smith   flg  = PETSC_FALSE;
13556f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
13656f85f32SBarry Smith   if (set) {
13756f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
13856f85f32SBarry Smith   }
13956f85f32SBarry Smith   }
14056f85f32SBarry Smith #endif
14156f85f32SBarry Smith 
142bdad233fSMatthew Knepley   /* Monitor options */
143a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
144eabae89aSBarry Smith   if (flg) {
145ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
146649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
147bdad233fSMatthew Knepley   }
1485180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1495180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1505180491cSLisandro Dalcin 
1514f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
152a7cc72afSBarry Smith   if (opt) {
1530b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1543923b477SBarry Smith     PetscInt       howoften = 1;
155a80ad3e0SBarry Smith 
1560298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
157ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1584f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
159b3603a34SBarry Smith   }
1604f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
161b3603a34SBarry Smith   if (opt) {
1620b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1633923b477SBarry Smith     PetscInt       howoften = 1;
164b3603a34SBarry Smith 
1650298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
16622d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1674f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
168bdad233fSMatthew Knepley   }
1694f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
170ef20d060SBarry Smith   if (opt) {
1710b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1723923b477SBarry Smith     PetscInt       howoften = 1;
173ef20d060SBarry Smith 
1740298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
17522d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1764f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
177ef20d060SBarry Smith   }
178201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
179201da799SBarry Smith   if (opt) {
180201da799SBarry Smith     TSMonitorLGCtx ctx;
181201da799SBarry Smith     PetscInt       howoften = 1;
182201da799SBarry Smith 
1830298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
18422d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
185201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
186201da799SBarry Smith   }
187201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
188201da799SBarry Smith   if (opt) {
189201da799SBarry Smith     TSMonitorLGCtx ctx;
190201da799SBarry Smith     PetscInt       howoften = 1;
191201da799SBarry Smith 
1920298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
19322d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
194201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
195201da799SBarry Smith   }
1968189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1978189c53fSBarry Smith   if (opt) {
1988189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
1998189c53fSBarry Smith     PetscInt          howoften = 1;
2008189c53fSBarry Smith 
2010298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
20222d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
2038189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2048189c53fSBarry Smith   }
205ef20d060SBarry Smith   opt  = PETSC_FALSE;
2060dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
207a7cc72afSBarry Smith   if (opt) {
20883a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
20983a4ac43SBarry Smith     PetscInt         howoften = 1;
210a80ad3e0SBarry Smith 
2110298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
212ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
21383a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
214bdad233fSMatthew Knepley   }
215fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2162d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2172d5ee99bSBarry Smith   if (opt) {
2182d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2192d5ee99bSBarry Smith     PetscReal        bounds[4];
2202d5ee99bSBarry Smith     PetscInt         n = 4;
2212d5ee99bSBarry Smith     PetscDraw        draw;
2222d5ee99bSBarry Smith 
2232d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2242d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2252d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2262d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2272d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
2284b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
2294b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
230f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2314b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
23207995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
2332d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2342d5ee99bSBarry Smith   }
2352d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2360dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2373a471f94SBarry Smith   if (opt) {
23883a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
23983a4ac43SBarry Smith     PetscInt         howoften = 1;
2403a471f94SBarry Smith 
2410298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
242ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
24383a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2443a471f94SBarry Smith   }
2453a471f94SBarry Smith   opt  = PETSC_FALSE;
24691b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
247fb1732b5SBarry Smith   if (flg) {
248fb1732b5SBarry Smith     PetscViewer ctx;
249fb1732b5SBarry Smith     if (monfilename[0]) {
250ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
251c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
252fb1732b5SBarry Smith     } else {
253ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2540298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
255fb1732b5SBarry Smith     }
256fb1732b5SBarry Smith   }
257ed81e22dSJed Brown   opt  = PETSC_FALSE;
25891b97e58SBarry 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);
259ed81e22dSJed Brown   if (flg) {
260ed81e22dSJed Brown     const char *ptr,*ptr2;
261ed81e22dSJed Brown     char       *filetemplate;
262ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
263ed81e22dSJed Brown     /* Do some cursory validation of the input. */
264ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
265ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
266ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
267ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
268ce94432eSBarry 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");
269ed81e22dSJed Brown       if (ptr2) break;
270ed81e22dSJed Brown     }
271ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
272ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
273ed81e22dSJed Brown   }
274bdad233fSMatthew Knepley 
275d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
276d1212d36SBarry Smith   if (flg) {
277d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
278d1212d36SBarry Smith     int                  ray = 0;
279d1212d36SBarry Smith     DMDADirection        ddir;
280d1212d36SBarry Smith     DM                   da;
281d1212d36SBarry Smith     PetscMPIInt          rank;
282d1212d36SBarry Smith 
283ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
284d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
285d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
286ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
287d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
288d1212d36SBarry Smith 
289d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
290d1212d36SBarry Smith     ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr);
291d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
292d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
293ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
294d1212d36SBarry Smith     if (!rank) {
295d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
296d1212d36SBarry Smith     }
29751b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
298d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
299d1212d36SBarry Smith   }
30051b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
30151b4a12fSMatthew G. Knepley   if (flg) {
30251b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
30351b4a12fSMatthew G. Knepley     int                 ray = 0;
30451b4a12fSMatthew G. Knepley     DMDADirection       ddir;
30551b4a12fSMatthew G. Knepley     DM                  da;
30651b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
30751b4a12fSMatthew G. Knepley 
30851b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
30951b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
31051b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
31151b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
31251b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
31351b4a12fSMatthew G. Knepley 
31451b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
31551b4a12fSMatthew G. Knepley     ierr = PetscNew(TSMonitorDMDARayCtx, &rayctx);CHKERRQ(ierr);
31651b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
31751b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
31851b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
31951b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
32051b4a12fSMatthew G. Knepley   }
321d1212d36SBarry Smith 
322*b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
323*b3d3934dSBarry Smith   if (opt) {
324*b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
325*b3d3934dSBarry Smith 
326*b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
327*b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
328*b3d3934dSBarry Smith   }
329*b3d3934dSBarry Smith 
3307781c08eSBarry Smith   /*
3317781c08eSBarry Smith      This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui
3327781c08eSBarry Smith      will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin()
3337781c08eSBarry Smith   */
334552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
3351c3436cfSJed Brown   ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
3361c3436cfSJed Brown 
337d52bd9f3SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
338d52bd9f3SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
339d52bd9f3SBarry Smith 
340bdad233fSMatthew Knepley   /* Handle specific TS options */
341abc0a331SBarry Smith   if (ts->ops->setfromoptions) {
342bdad233fSMatthew Knepley     ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
343bdad233fSMatthew Knepley   }
3445d973c19SBarry Smith 
3455d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
3465d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
347bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
348bdad233fSMatthew Knepley   PetscFunctionReturn(0);
349bdad233fSMatthew Knepley }
350bdad233fSMatthew Knepley 
351bdad233fSMatthew Knepley #undef __FUNCT__
352cdcf91faSSean Farley #undef __FUNCT__
3534a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
354a7a1495cSBarry Smith /*@
3558c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
356a7a1495cSBarry Smith       set with TSSetRHSJacobian().
357a7a1495cSBarry Smith 
358a7a1495cSBarry Smith    Collective on TS and Vec
359a7a1495cSBarry Smith 
360a7a1495cSBarry Smith    Input Parameters:
361316643e7SJed Brown +  ts - the TS context
362a7a1495cSBarry Smith .  t - current timestep
3630910c330SBarry Smith -  U - input vector
364a7a1495cSBarry Smith 
365a7a1495cSBarry Smith    Output Parameters:
366a7a1495cSBarry Smith +  A - Jacobian matrix
367a7a1495cSBarry Smith .  B - optional preconditioning matrix
368a7a1495cSBarry Smith -  flag - flag indicating matrix structure
369a7a1495cSBarry Smith 
370a7a1495cSBarry Smith    Notes:
371a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
372a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
373a7a1495cSBarry Smith 
37494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
375a7a1495cSBarry Smith    flag parameter.
376a7a1495cSBarry Smith 
377a7a1495cSBarry Smith    Level: developer
378a7a1495cSBarry Smith 
379a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
380a7a1495cSBarry Smith 
38194b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
382a7a1495cSBarry Smith @*/
3830910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
384a7a1495cSBarry Smith {
385dfbe8321SBarry Smith   PetscErrorCode ierr;
386270bf2e7SJed Brown   PetscObjectState Ustate;
38724989b8cSPeter Brune   DM             dm;
388942e3340SBarry Smith   DMTS           tsdm;
38924989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
39024989b8cSPeter Brune   void           *ctx;
39124989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
392a7a1495cSBarry Smith 
393a7a1495cSBarry Smith   PetscFunctionBegin;
3940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3950910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3960910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
39724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
398942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
39924989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
4000298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
40159e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
4020910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
4030e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
4040e4ef248SJed Brown     PetscFunctionReturn(0);
405f8ede8e7SJed Brown   }
406d90be118SSean Farley 
407ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
408d90be118SSean Farley 
409e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
410e1244c69SJed Brown     ierr = MatShift(*A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
411e1244c69SJed Brown     ierr = MatScale(*A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
412e1244c69SJed Brown     if (*A != *B) {
413e1244c69SJed Brown       ierr = MatShift(*B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
414e1244c69SJed Brown       ierr = MatScale(*B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
415e1244c69SJed Brown     }
416e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
417e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
418e1244c69SJed Brown   }
419e1244c69SJed Brown 
42024989b8cSPeter Brune   if (rhsjacobianfunc) {
4210910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
422a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
423a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
4240910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
425a7a1495cSBarry Smith     PetscStackPop;
4260910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
427a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
4280700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
4290700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
430ef66eb69SBarry Smith   } else {
431214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
432214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
433214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
434ef66eb69SBarry Smith   }
4350e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4360910c330SBarry Smith   ts->rhsjacobian.X          = U;
43759e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
4380e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
439a7a1495cSBarry Smith   PetscFunctionReturn(0);
440a7a1495cSBarry Smith }
441a7a1495cSBarry Smith 
4424a2ae208SSatish Balay #undef __FUNCT__
4434a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
444316643e7SJed Brown /*@
445d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
446d763cef2SBarry Smith 
447316643e7SJed Brown    Collective on TS and Vec
448316643e7SJed Brown 
449316643e7SJed Brown    Input Parameters:
450316643e7SJed Brown +  ts - the TS context
451316643e7SJed Brown .  t - current time
4520910c330SBarry Smith -  U - state vector
453316643e7SJed Brown 
454316643e7SJed Brown    Output Parameter:
455316643e7SJed Brown .  y - right hand side
456316643e7SJed Brown 
457316643e7SJed Brown    Note:
458316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
459316643e7SJed Brown    is used internally within the nonlinear solvers.
460316643e7SJed Brown 
461316643e7SJed Brown    Level: developer
462316643e7SJed Brown 
463316643e7SJed Brown .keywords: TS, compute
464316643e7SJed Brown 
465316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
466316643e7SJed Brown @*/
4670910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
468d763cef2SBarry Smith {
469dfbe8321SBarry Smith   PetscErrorCode ierr;
47024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
47124989b8cSPeter Brune   TSIFunction    ifunction;
47224989b8cSPeter Brune   void           *ctx;
47324989b8cSPeter Brune   DM             dm;
47424989b8cSPeter Brune 
475d763cef2SBarry Smith   PetscFunctionBegin;
4760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4770910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4780700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
47924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
48024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
4810298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
482d763cef2SBarry Smith 
483ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
484d763cef2SBarry Smith 
4850910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
48624989b8cSPeter Brune   if (rhsfunction) {
487d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
4880910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
489d763cef2SBarry Smith     PetscStackPop;
490214bc6a2SJed Brown   } else {
491214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
492b2cd27e8SJed Brown   }
49344a41b28SSean Farley 
4940910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
495d763cef2SBarry Smith   PetscFunctionReturn(0);
496d763cef2SBarry Smith }
497d763cef2SBarry Smith 
4984a2ae208SSatish Balay #undef __FUNCT__
499ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
500ef20d060SBarry Smith /*@
501ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
502ef20d060SBarry Smith 
503ef20d060SBarry Smith    Collective on TS and Vec
504ef20d060SBarry Smith 
505ef20d060SBarry Smith    Input Parameters:
506ef20d060SBarry Smith +  ts - the TS context
507ef20d060SBarry Smith -  t - current time
508ef20d060SBarry Smith 
509ef20d060SBarry Smith    Output Parameter:
5100910c330SBarry Smith .  U - the solution
511ef20d060SBarry Smith 
512ef20d060SBarry Smith    Note:
513ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
514ef20d060SBarry Smith    is used internally within the nonlinear solvers.
515ef20d060SBarry Smith 
516ef20d060SBarry Smith    Level: developer
517ef20d060SBarry Smith 
518ef20d060SBarry Smith .keywords: TS, compute
519ef20d060SBarry Smith 
520abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
521ef20d060SBarry Smith @*/
5220910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
523ef20d060SBarry Smith {
524ef20d060SBarry Smith   PetscErrorCode     ierr;
525ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
526ef20d060SBarry Smith   void               *ctx;
527ef20d060SBarry Smith   DM                 dm;
528ef20d060SBarry Smith 
529ef20d060SBarry Smith   PetscFunctionBegin;
530ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5310910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
532ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
533ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
534ef20d060SBarry Smith 
535ef20d060SBarry Smith   if (solutionfunction) {
5369b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5370910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
538ef20d060SBarry Smith     PetscStackPop;
539ef20d060SBarry Smith   }
540ef20d060SBarry Smith   PetscFunctionReturn(0);
541ef20d060SBarry Smith }
5429b7cd975SBarry Smith #undef __FUNCT__
5439b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
5449b7cd975SBarry Smith /*@
5459b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5469b7cd975SBarry Smith 
5479b7cd975SBarry Smith    Collective on TS and Vec
5489b7cd975SBarry Smith 
5499b7cd975SBarry Smith    Input Parameters:
5509b7cd975SBarry Smith +  ts - the TS context
5519b7cd975SBarry Smith -  t - current time
5529b7cd975SBarry Smith 
5539b7cd975SBarry Smith    Output Parameter:
5549b7cd975SBarry Smith .  U - the function value
5559b7cd975SBarry Smith 
5569b7cd975SBarry Smith    Note:
5579b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5589b7cd975SBarry Smith    is used internally within the nonlinear solvers.
5599b7cd975SBarry Smith 
5609b7cd975SBarry Smith    Level: developer
5619b7cd975SBarry Smith 
5629b7cd975SBarry Smith .keywords: TS, compute
5639b7cd975SBarry Smith 
5649b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
5659b7cd975SBarry Smith @*/
5669b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
5679b7cd975SBarry Smith {
5689b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
5699b7cd975SBarry Smith   void               *ctx;
5709b7cd975SBarry Smith   DM                 dm;
5719b7cd975SBarry Smith 
5729b7cd975SBarry Smith   PetscFunctionBegin;
5739b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5749b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5759b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
5769b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
5779b7cd975SBarry Smith 
5789b7cd975SBarry Smith   if (forcing) {
5799b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
5809b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
5819b7cd975SBarry Smith     PetscStackPop;
5829b7cd975SBarry Smith   }
5839b7cd975SBarry Smith   PetscFunctionReturn(0);
5849b7cd975SBarry Smith }
585ef20d060SBarry Smith 
586ef20d060SBarry Smith #undef __FUNCT__
587214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
588214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
589214bc6a2SJed Brown {
5902dd45cf8SJed Brown   Vec            F;
591214bc6a2SJed Brown   PetscErrorCode ierr;
592214bc6a2SJed Brown 
593214bc6a2SJed Brown   PetscFunctionBegin;
5940298fd71SBarry Smith   *Frhs = NULL;
5950298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
596214bc6a2SJed Brown   if (!ts->Frhs) {
5972dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
598214bc6a2SJed Brown   }
599214bc6a2SJed Brown   *Frhs = ts->Frhs;
600214bc6a2SJed Brown   PetscFunctionReturn(0);
601214bc6a2SJed Brown }
602214bc6a2SJed Brown 
603214bc6a2SJed Brown #undef __FUNCT__
604214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
605214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
606214bc6a2SJed Brown {
607214bc6a2SJed Brown   Mat            A,B;
6082dd45cf8SJed Brown   PetscErrorCode ierr;
609214bc6a2SJed Brown 
610214bc6a2SJed Brown   PetscFunctionBegin;
6110298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
612214bc6a2SJed Brown   if (Arhs) {
613214bc6a2SJed Brown     if (!ts->Arhs) {
614214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
615214bc6a2SJed Brown     }
616214bc6a2SJed Brown     *Arhs = ts->Arhs;
617214bc6a2SJed Brown   }
618214bc6a2SJed Brown   if (Brhs) {
619214bc6a2SJed Brown     if (!ts->Brhs) {
620bdb70873SJed Brown       if (A != B) {
621214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
622bdb70873SJed Brown       } else {
623bdb70873SJed Brown         ts->Brhs = ts->Arhs;
624bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
625bdb70873SJed Brown       }
626214bc6a2SJed Brown     }
627214bc6a2SJed Brown     *Brhs = ts->Brhs;
628214bc6a2SJed Brown   }
629214bc6a2SJed Brown   PetscFunctionReturn(0);
630214bc6a2SJed Brown }
631214bc6a2SJed Brown 
632214bc6a2SJed Brown #undef __FUNCT__
633316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
634316643e7SJed Brown /*@
6350910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
636316643e7SJed Brown 
637316643e7SJed Brown    Collective on TS and Vec
638316643e7SJed Brown 
639316643e7SJed Brown    Input Parameters:
640316643e7SJed Brown +  ts - the TS context
641316643e7SJed Brown .  t - current time
6420910c330SBarry Smith .  U - state vector
6430910c330SBarry Smith .  Udot - time derivative of state vector
644214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
645316643e7SJed Brown 
646316643e7SJed Brown    Output Parameter:
647316643e7SJed Brown .  Y - right hand side
648316643e7SJed Brown 
649316643e7SJed Brown    Note:
650316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
651316643e7SJed Brown    is used internally within the nonlinear solvers.
652316643e7SJed Brown 
653316643e7SJed Brown    If the user did did not write their equations in implicit form, this
654316643e7SJed Brown    function recasts them in implicit form.
655316643e7SJed Brown 
656316643e7SJed Brown    Level: developer
657316643e7SJed Brown 
658316643e7SJed Brown .keywords: TS, compute
659316643e7SJed Brown 
660316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
661316643e7SJed Brown @*/
6620910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
663316643e7SJed Brown {
664316643e7SJed Brown   PetscErrorCode ierr;
66524989b8cSPeter Brune   TSIFunction    ifunction;
66624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
66724989b8cSPeter Brune   void           *ctx;
66824989b8cSPeter Brune   DM             dm;
669316643e7SJed Brown 
670316643e7SJed Brown   PetscFunctionBegin;
6710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6720910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6730910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
6740700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
675316643e7SJed Brown 
67624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
67724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
6780298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
67924989b8cSPeter Brune 
680ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
681d90be118SSean Farley 
6820910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
68324989b8cSPeter Brune   if (ifunction) {
684316643e7SJed Brown     PetscStackPush("TS user implicit function");
6850910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
686316643e7SJed Brown     PetscStackPop;
687214bc6a2SJed Brown   }
688214bc6a2SJed Brown   if (imex) {
68924989b8cSPeter Brune     if (!ifunction) {
6900910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
6912dd45cf8SJed Brown     }
69224989b8cSPeter Brune   } else if (rhsfunction) {
69324989b8cSPeter Brune     if (ifunction) {
694214bc6a2SJed Brown       Vec Frhs;
695214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
6960910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
697214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
6982dd45cf8SJed Brown     } else {
6990910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
7000910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
701316643e7SJed Brown     }
7024a6899ffSJed Brown   }
7030910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
704316643e7SJed Brown   PetscFunctionReturn(0);
705316643e7SJed Brown }
706316643e7SJed Brown 
707316643e7SJed Brown #undef __FUNCT__
708316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
709316643e7SJed Brown /*@
710316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
711316643e7SJed Brown 
712316643e7SJed Brown    Collective on TS and Vec
713316643e7SJed Brown 
714316643e7SJed Brown    Input
715316643e7SJed Brown       Input Parameters:
716316643e7SJed Brown +  ts - the TS context
717316643e7SJed Brown .  t - current timestep
7180910c330SBarry Smith .  U - state vector
7190910c330SBarry Smith .  Udot - time derivative of state vector
720214bc6a2SJed Brown .  shift - shift to apply, see note below
721214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
722316643e7SJed Brown 
723316643e7SJed Brown    Output Parameters:
724316643e7SJed Brown +  A - Jacobian matrix
725316643e7SJed Brown .  B - optional preconditioning matrix
726316643e7SJed Brown -  flag - flag indicating matrix structure
727316643e7SJed Brown 
728316643e7SJed Brown    Notes:
7290910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
730316643e7SJed Brown 
7310910c330SBarry Smith    dF/dU + shift*dF/dUdot
732316643e7SJed Brown 
733316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
734316643e7SJed Brown    is used internally within the nonlinear solvers.
735316643e7SJed Brown 
736316643e7SJed Brown    Level: developer
737316643e7SJed Brown 
738316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
739316643e7SJed Brown 
740316643e7SJed Brown .seealso:  TSSetIJacobian()
74163495f91SJed Brown @*/
7420910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
743316643e7SJed Brown {
744316643e7SJed Brown   PetscErrorCode ierr;
74524989b8cSPeter Brune   TSIJacobian    ijacobian;
74624989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
74724989b8cSPeter Brune   DM             dm;
74824989b8cSPeter Brune   void           *ctx;
749316643e7SJed Brown 
750316643e7SJed Brown   PetscFunctionBegin;
7510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7520910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7530910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
754316643e7SJed Brown   PetscValidPointer(A,6);
7550700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
756316643e7SJed Brown   PetscValidPointer(B,7);
7570700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
758316643e7SJed Brown   PetscValidPointer(flg,8);
75924989b8cSPeter Brune 
76024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
76124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
7620298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
76324989b8cSPeter Brune 
764ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
765316643e7SJed Brown 
7664e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
7670910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
76824989b8cSPeter Brune   if (ijacobian) {
7692dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
770316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
7710910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
772316643e7SJed Brown     PetscStackPop;
773214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
774214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
775214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
7764a6899ffSJed Brown   }
777214bc6a2SJed Brown   if (imex) {
778b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
779214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
7802dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
781214bc6a2SJed Brown       if (*A != *B) {
782214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
783214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
784214bc6a2SJed Brown       }
785214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
786214bc6a2SJed Brown     }
787214bc6a2SJed Brown   } else {
788e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
789e1244c69SJed Brown     MatStructure flg2;
790e1244c69SJed Brown     if (rhsjacobian) {
791e1244c69SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
792e1244c69SJed Brown       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
793e1244c69SJed Brown     }
794e1244c69SJed Brown     if (Arhs == *A) {           /* No IJacobian, so we only have the RHS matrix */
795e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
796e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
797214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
798214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
799316643e7SJed Brown       if (*A != *B) {
800316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
801316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
802316643e7SJed Brown       }
803e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
804e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
805e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
806e1244c69SJed Brown         ierr = MatZeroEntries(*A);CHKERRQ(ierr);
807e1244c69SJed Brown         ierr = MatShift(*A,shift);CHKERRQ(ierr);
808e1244c69SJed Brown         if (*A != *B) {
809e1244c69SJed Brown           ierr = MatZeroEntries(*B);CHKERRQ(ierr);
810e1244c69SJed Brown           ierr = MatShift(*B,shift);CHKERRQ(ierr);
811e1244c69SJed Brown         }
812e1244c69SJed Brown       }
813214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
814214bc6a2SJed Brown       if (*A != *B) {
815214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
816214bc6a2SJed Brown       }
817214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
818214bc6a2SJed Brown     }
819316643e7SJed Brown   }
8200026cea9SSean Farley 
8210910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
822316643e7SJed Brown   PetscFunctionReturn(0);
823316643e7SJed Brown }
824316643e7SJed Brown 
825316643e7SJed Brown #undef __FUNCT__
8264a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
827d763cef2SBarry Smith /*@C
828d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
829b5abc632SBarry Smith     where U_t = G(t,u).
830d763cef2SBarry Smith 
8313f9fe445SBarry Smith     Logically Collective on TS
832d763cef2SBarry Smith 
833d763cef2SBarry Smith     Input Parameters:
834d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8350298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
836d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
837d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8380298fd71SBarry Smith           function evaluation routine (may be NULL)
839d763cef2SBarry Smith 
840d763cef2SBarry Smith     Calling sequence of func:
84187828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
842d763cef2SBarry Smith 
843d763cef2SBarry Smith +   t - current timestep
844d763cef2SBarry Smith .   u - input vector
845d763cef2SBarry Smith .   F - function vector
846d763cef2SBarry Smith -   ctx - [optional] user-defined function context
847d763cef2SBarry Smith 
848d763cef2SBarry Smith     Level: beginner
849d763cef2SBarry Smith 
850d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
851d763cef2SBarry Smith 
852d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
853d763cef2SBarry Smith @*/
854089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
855d763cef2SBarry Smith {
856089b2837SJed Brown   PetscErrorCode ierr;
857089b2837SJed Brown   SNES           snes;
8580298fd71SBarry Smith   Vec            ralloc = NULL;
85924989b8cSPeter Brune   DM             dm;
860d763cef2SBarry Smith 
861089b2837SJed Brown   PetscFunctionBegin;
8620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
863ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
86424989b8cSPeter Brune 
86524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
86624989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
867089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
868e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
869e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
870e856ceecSJed Brown     r    = ralloc;
871e856ceecSJed Brown   }
872089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
873e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
874d763cef2SBarry Smith   PetscFunctionReturn(0);
875d763cef2SBarry Smith }
876d763cef2SBarry Smith 
8774a2ae208SSatish Balay #undef __FUNCT__
878ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
879ef20d060SBarry Smith /*@C
880abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
881ef20d060SBarry Smith 
882ef20d060SBarry Smith     Logically Collective on TS
883ef20d060SBarry Smith 
884ef20d060SBarry Smith     Input Parameters:
885ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
886ef20d060SBarry Smith .   f - routine for evaluating the solution
887ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
8880298fd71SBarry Smith           function evaluation routine (may be NULL)
889ef20d060SBarry Smith 
890ef20d060SBarry Smith     Calling sequence of func:
891ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
892ef20d060SBarry Smith 
893ef20d060SBarry Smith +   t - current timestep
894ef20d060SBarry Smith .   u - output vector
895ef20d060SBarry Smith -   ctx - [optional] user-defined function context
896ef20d060SBarry Smith 
897abd5a294SJed Brown     Notes:
898abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
899abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
900abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
901abd5a294SJed Brown 
9024f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
903abd5a294SJed Brown 
904ef20d060SBarry Smith     Level: beginner
905ef20d060SBarry Smith 
906ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
907ef20d060SBarry Smith 
9089b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
909ef20d060SBarry Smith @*/
910ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
911ef20d060SBarry Smith {
912ef20d060SBarry Smith   PetscErrorCode ierr;
913ef20d060SBarry Smith   DM             dm;
914ef20d060SBarry Smith 
915ef20d060SBarry Smith   PetscFunctionBegin;
916ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
917ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
918ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
919ef20d060SBarry Smith   PetscFunctionReturn(0);
920ef20d060SBarry Smith }
921ef20d060SBarry Smith 
922ef20d060SBarry Smith #undef __FUNCT__
9239b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
9249b7cd975SBarry Smith /*@C
9259b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
9269b7cd975SBarry Smith 
9279b7cd975SBarry Smith     Logically Collective on TS
9289b7cd975SBarry Smith 
9299b7cd975SBarry Smith     Input Parameters:
9309b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9319b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9329b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9330298fd71SBarry Smith           function evaluation routine (may be NULL)
9349b7cd975SBarry Smith 
9359b7cd975SBarry Smith     Calling sequence of func:
9369b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9379b7cd975SBarry Smith 
9389b7cd975SBarry Smith +   t - current timestep
9399b7cd975SBarry Smith .   u - output vector
9409b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9419b7cd975SBarry Smith 
9429b7cd975SBarry Smith     Notes:
9439b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9449b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9459b7cd975SBarry Smith 
9469b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9479b7cd975SBarry Smith 
9489b7cd975SBarry Smith     Level: beginner
9499b7cd975SBarry Smith 
9509b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9519b7cd975SBarry Smith 
9529b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9539b7cd975SBarry Smith @*/
9549b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9559b7cd975SBarry Smith {
9569b7cd975SBarry Smith   PetscErrorCode ierr;
9579b7cd975SBarry Smith   DM             dm;
9589b7cd975SBarry Smith 
9599b7cd975SBarry Smith   PetscFunctionBegin;
9609b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9619b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
9629b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
9639b7cd975SBarry Smith   PetscFunctionReturn(0);
9649b7cd975SBarry Smith }
9659b7cd975SBarry Smith 
9669b7cd975SBarry Smith #undef __FUNCT__
9674a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
968d763cef2SBarry Smith /*@C
969d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
970b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
971d763cef2SBarry Smith 
9723f9fe445SBarry Smith    Logically Collective on TS
973d763cef2SBarry Smith 
974d763cef2SBarry Smith    Input Parameters:
975d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
976e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
977e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
978d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
979d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
9800298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
981d763cef2SBarry Smith 
982d763cef2SBarry Smith    Calling sequence of func:
98387828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
984d763cef2SBarry Smith 
985d763cef2SBarry Smith +  t - current timestep
986d763cef2SBarry Smith .  u - input vector
987e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
988e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
989d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
99094b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
991d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
992d763cef2SBarry Smith 
993d763cef2SBarry Smith    Notes:
99494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
995d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
996d763cef2SBarry Smith 
997d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
998d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
99956335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
1000d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
1001d763cef2SBarry Smith    throughout the global iterations.
1002d763cef2SBarry Smith 
1003d763cef2SBarry Smith    Level: beginner
1004d763cef2SBarry Smith 
1005d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1006d763cef2SBarry Smith 
1007e1244c69SJed Brown .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse()
1008d763cef2SBarry Smith 
1009d763cef2SBarry Smith @*/
1010e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1011d763cef2SBarry Smith {
1012277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1013089b2837SJed Brown   SNES           snes;
101424989b8cSPeter Brune   DM             dm;
101524989b8cSPeter Brune   TSIJacobian    ijacobian;
1016277b19d0SLisandro Dalcin 
1017d763cef2SBarry Smith   PetscFunctionBegin;
10180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1019e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1020e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1021e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1022e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1023d763cef2SBarry Smith 
102424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
102524989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1026e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1027e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1028e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1029e1244c69SJed Brown   }
10300298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1031089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10325f659677SPeter Brune   if (!ijacobian) {
1033e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10340e4ef248SJed Brown   }
1035e5d3d808SBarry Smith   if (Amat) {
1036e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10370e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1038bbd56ea5SKarl Rupp 
1039e5d3d808SBarry Smith     ts->Arhs = Amat;
10400e4ef248SJed Brown   }
1041e5d3d808SBarry Smith   if (Pmat) {
1042e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10430e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1044bbd56ea5SKarl Rupp 
1045e5d3d808SBarry Smith     ts->Brhs = Pmat;
10460e4ef248SJed Brown   }
1047d763cef2SBarry Smith   PetscFunctionReturn(0);
1048d763cef2SBarry Smith }
1049d763cef2SBarry Smith 
1050316643e7SJed Brown 
1051316643e7SJed Brown #undef __FUNCT__
1052316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1053316643e7SJed Brown /*@C
1054b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1055316643e7SJed Brown 
10563f9fe445SBarry Smith    Logically Collective on TS
1057316643e7SJed Brown 
1058316643e7SJed Brown    Input Parameters:
1059316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10600298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1061316643e7SJed Brown .  f   - the function evaluation routine
10620298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1063316643e7SJed Brown 
1064316643e7SJed Brown    Calling sequence of f:
1065316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1066316643e7SJed Brown 
1067316643e7SJed Brown +  t   - time at step/stage being solved
1068316643e7SJed Brown .  u   - state vector
1069316643e7SJed Brown .  u_t - time derivative of state vector
1070316643e7SJed Brown .  F   - function vector
1071316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1072316643e7SJed Brown 
1073316643e7SJed Brown    Important:
1074d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
1075316643e7SJed Brown 
1076316643e7SJed Brown    Level: beginner
1077316643e7SJed Brown 
1078316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1079316643e7SJed Brown 
1080d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1081316643e7SJed Brown @*/
1082089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1083316643e7SJed Brown {
1084089b2837SJed Brown   PetscErrorCode ierr;
1085089b2837SJed Brown   SNES           snes;
10860298fd71SBarry Smith   Vec            resalloc = NULL;
108724989b8cSPeter Brune   DM             dm;
1088316643e7SJed Brown 
1089316643e7SJed Brown   PetscFunctionBegin;
10900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1091ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
109224989b8cSPeter Brune 
109324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
109424989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
109524989b8cSPeter Brune 
1096089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1097e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1098e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1099e856ceecSJed Brown     res  = resalloc;
1100e856ceecSJed Brown   }
1101089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1102e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1103089b2837SJed Brown   PetscFunctionReturn(0);
1104089b2837SJed Brown }
1105089b2837SJed Brown 
1106089b2837SJed Brown #undef __FUNCT__
1107089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1108089b2837SJed Brown /*@C
1109089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1110089b2837SJed Brown 
1111089b2837SJed Brown    Not Collective
1112089b2837SJed Brown 
1113089b2837SJed Brown    Input Parameter:
1114089b2837SJed Brown .  ts - the TS context
1115089b2837SJed Brown 
1116089b2837SJed Brown    Output Parameter:
11170298fd71SBarry Smith +  r - vector to hold residual (or NULL)
11180298fd71SBarry Smith .  func - the function to compute residual (or NULL)
11190298fd71SBarry Smith -  ctx - the function context (or NULL)
1120089b2837SJed Brown 
1121089b2837SJed Brown    Level: advanced
1122089b2837SJed Brown 
1123089b2837SJed Brown .keywords: TS, nonlinear, get, function
1124089b2837SJed Brown 
1125089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1126089b2837SJed Brown @*/
1127089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1128089b2837SJed Brown {
1129089b2837SJed Brown   PetscErrorCode ierr;
1130089b2837SJed Brown   SNES           snes;
113124989b8cSPeter Brune   DM             dm;
1132089b2837SJed Brown 
1133089b2837SJed Brown   PetscFunctionBegin;
1134089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1135089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11360298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
113724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
113824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1139089b2837SJed Brown   PetscFunctionReturn(0);
1140089b2837SJed Brown }
1141089b2837SJed Brown 
1142089b2837SJed Brown #undef __FUNCT__
1143089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1144089b2837SJed Brown /*@C
1145089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1146089b2837SJed Brown 
1147089b2837SJed Brown    Not Collective
1148089b2837SJed Brown 
1149089b2837SJed Brown    Input Parameter:
1150089b2837SJed Brown .  ts - the TS context
1151089b2837SJed Brown 
1152089b2837SJed Brown    Output Parameter:
11530298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11540298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11550298fd71SBarry Smith -  ctx - the function context (or NULL)
1156089b2837SJed Brown 
1157089b2837SJed Brown    Level: advanced
1158089b2837SJed Brown 
1159089b2837SJed Brown .keywords: TS, nonlinear, get, function
1160089b2837SJed Brown 
1161089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1162089b2837SJed Brown @*/
1163089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1164089b2837SJed Brown {
1165089b2837SJed Brown   PetscErrorCode ierr;
1166089b2837SJed Brown   SNES           snes;
116724989b8cSPeter Brune   DM             dm;
1168089b2837SJed Brown 
1169089b2837SJed Brown   PetscFunctionBegin;
1170089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1171089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11720298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
117324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
117424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1175316643e7SJed Brown   PetscFunctionReturn(0);
1176316643e7SJed Brown }
1177316643e7SJed Brown 
1178316643e7SJed Brown #undef __FUNCT__
1179316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1180316643e7SJed Brown /*@C
1181a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1182a4f0a591SBarry Smith         you provided with TSSetIFunction().
1183316643e7SJed Brown 
11843f9fe445SBarry Smith    Logically Collective on TS
1185316643e7SJed Brown 
1186316643e7SJed Brown    Input Parameters:
1187316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1188e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1189e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1190316643e7SJed Brown .  f   - the Jacobian evaluation routine
11910298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1192316643e7SJed Brown 
1193316643e7SJed Brown    Calling sequence of f:
1194e5d3d808SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *Amat,Mat *Pmat,MatStructure *flag,void *ctx);
1195316643e7SJed Brown 
1196316643e7SJed Brown +  t    - time at step/stage being solved
11971b4a444bSJed Brown .  U    - state vector
11981b4a444bSJed Brown .  U_t  - time derivative of state vector
1199316643e7SJed Brown .  a    - shift
1200e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1201e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1202316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1203316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1204316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1205316643e7SJed Brown 
1206316643e7SJed Brown    Notes:
1207e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1208316643e7SJed Brown 
1209a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1210b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1211a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1212a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1213a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1214a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1215a4f0a591SBarry Smith 
1216316643e7SJed Brown    Level: beginner
1217316643e7SJed Brown 
1218316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1219316643e7SJed Brown 
12208d359177SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault()
1221316643e7SJed Brown 
1222316643e7SJed Brown @*/
1223e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1224316643e7SJed Brown {
1225316643e7SJed Brown   PetscErrorCode ierr;
1226089b2837SJed Brown   SNES           snes;
122724989b8cSPeter Brune   DM             dm;
1228316643e7SJed Brown 
1229316643e7SJed Brown   PetscFunctionBegin;
12300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1231e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1232e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1233e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1234e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
123524989b8cSPeter Brune 
123624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
123724989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
123824989b8cSPeter Brune 
1239089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1240e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1241316643e7SJed Brown   PetscFunctionReturn(0);
1242316643e7SJed Brown }
1243316643e7SJed Brown 
12444a2ae208SSatish Balay #undef __FUNCT__
1245e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1246e1244c69SJed Brown /*@
1247e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1248e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1249e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1250e1244c69SJed Brown    not been changed by the TS.
1251e1244c69SJed Brown 
1252e1244c69SJed Brown    Logically Collective
1253e1244c69SJed Brown 
1254e1244c69SJed Brown    Input Arguments:
1255e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1256e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1257e1244c69SJed Brown 
1258e1244c69SJed Brown    Level: intermediate
1259e1244c69SJed Brown 
1260e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1261e1244c69SJed Brown @*/
1262e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1263e1244c69SJed Brown {
1264e1244c69SJed Brown   PetscFunctionBegin;
1265e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1266e1244c69SJed Brown   PetscFunctionReturn(0);
1267e1244c69SJed Brown }
1268e1244c69SJed Brown 
1269e1244c69SJed Brown #undef __FUNCT__
127055849f57SBarry Smith #define __FUNCT__ "TSLoad"
127155849f57SBarry Smith /*@C
127255849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
127355849f57SBarry Smith 
127455849f57SBarry Smith   Collective on PetscViewer
127555849f57SBarry Smith 
127655849f57SBarry Smith   Input Parameters:
127755849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
127855849f57SBarry Smith            some related function before a call to TSLoad().
127955849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
128055849f57SBarry Smith 
128155849f57SBarry Smith    Level: intermediate
128255849f57SBarry Smith 
128355849f57SBarry Smith   Notes:
128455849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
128555849f57SBarry Smith 
128655849f57SBarry Smith   Notes for advanced users:
128755849f57SBarry Smith   Most users should not need to know the details of the binary storage
128855849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
128955849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
129055849f57SBarry Smith   format is
129155849f57SBarry Smith .vb
129255849f57SBarry Smith      has not yet been determined
129355849f57SBarry Smith .ve
129455849f57SBarry Smith 
129555849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
129655849f57SBarry Smith @*/
1297f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
129855849f57SBarry Smith {
129955849f57SBarry Smith   PetscErrorCode ierr;
130055849f57SBarry Smith   PetscBool      isbinary;
130155849f57SBarry Smith   PetscInt       classid;
130255849f57SBarry Smith   char           type[256];
13032d53ad75SBarry Smith   DMTS           sdm;
1304ad6bc421SBarry Smith   DM             dm;
130555849f57SBarry Smith 
130655849f57SBarry Smith   PetscFunctionBegin;
1307f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
130855849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
130955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
131055849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
131155849f57SBarry Smith 
131255849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1313ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
131455849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1315f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1316f2c2a1b9SBarry Smith   if (ts->ops->load) {
1317f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1318f2c2a1b9SBarry Smith   }
1319ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1320ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1321ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1322f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1323f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
13242d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13252d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
132655849f57SBarry Smith   PetscFunctionReturn(0);
132755849f57SBarry Smith }
132855849f57SBarry Smith 
13299804daf3SBarry Smith #include <petscdraw.h>
1330e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1331e04113cfSBarry Smith #include <petscviewersaws.h>
1332f05ece33SBarry Smith #endif
133355849f57SBarry Smith #undef __FUNCT__
13344a2ae208SSatish Balay #define __FUNCT__ "TSView"
13357e2c5f70SBarry Smith /*@C
1336d763cef2SBarry Smith     TSView - Prints the TS data structure.
1337d763cef2SBarry Smith 
13384c49b128SBarry Smith     Collective on TS
1339d763cef2SBarry Smith 
1340d763cef2SBarry Smith     Input Parameters:
1341d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1342d763cef2SBarry Smith -   viewer - visualization context
1343d763cef2SBarry Smith 
1344d763cef2SBarry Smith     Options Database Key:
1345d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1346d763cef2SBarry Smith 
1347d763cef2SBarry Smith     Notes:
1348d763cef2SBarry Smith     The available visualization contexts include
1349b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1350b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1351d763cef2SBarry Smith          output where only the first processor opens
1352d763cef2SBarry Smith          the file.  All other processors send their
1353d763cef2SBarry Smith          data to the first processor to print.
1354d763cef2SBarry Smith 
1355d763cef2SBarry Smith     The user can open an alternative visualization context with
1356b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1357d763cef2SBarry Smith 
1358d763cef2SBarry Smith     Level: beginner
1359d763cef2SBarry Smith 
1360d763cef2SBarry Smith .keywords: TS, timestep, view
1361d763cef2SBarry Smith 
1362b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1363d763cef2SBarry Smith @*/
13647087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1365d763cef2SBarry Smith {
1366dfbe8321SBarry Smith   PetscErrorCode ierr;
136719fd82e9SBarry Smith   TSType         type;
13682b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
13692d53ad75SBarry Smith   DMTS           sdm;
1370e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1371f05ece33SBarry Smith   PetscBool      isams;
1372f05ece33SBarry Smith #endif
1373d763cef2SBarry Smith 
1374d763cef2SBarry Smith   PetscFunctionBegin;
13750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13763050cee2SBarry Smith   if (!viewer) {
1377ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
13783050cee2SBarry Smith   }
13790700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1380c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1381fd16b177SBarry Smith 
1382251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1383251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
138455849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
13852b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1386e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1387e04113cfSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&isams);CHKERRQ(ierr);
1388f05ece33SBarry Smith #endif
138932077d6dSBarry Smith   if (iascii) {
1390dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
139177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1392a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1393d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
13945ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1395c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1396d763cef2SBarry Smith     }
13975ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1398193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
13992d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14002d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1401d52bd9f3SBarry Smith     if (ts->ops->view) {
1402d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1403d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1404d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1405d52bd9f3SBarry Smith     }
14060f5bd95cSBarry Smith   } else if (isstring) {
1407a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1408b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
140955849f57SBarry Smith   } else if (isbinary) {
141055849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
141155849f57SBarry Smith     MPI_Comm    comm;
141255849f57SBarry Smith     PetscMPIInt rank;
141355849f57SBarry Smith     char        type[256];
141455849f57SBarry Smith 
141555849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
141655849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
141755849f57SBarry Smith     if (!rank) {
141855849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
141955849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
142055849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
142155849f57SBarry Smith     }
142255849f57SBarry Smith     if (ts->ops->view) {
142355849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
142455849f57SBarry Smith     }
1425f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1426f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
14272d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14282d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
14292b0a91c0SBarry Smith   } else if (isdraw) {
14302b0a91c0SBarry Smith     PetscDraw draw;
14312b0a91c0SBarry Smith     char      str[36];
143289fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14332b0a91c0SBarry Smith 
14342b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14352b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14362b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14372b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
14380298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
143989fd9fafSBarry Smith     bottom = y - h;
14402b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14412b0a91c0SBarry Smith     if (ts->ops->view) {
14422b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14432b0a91c0SBarry Smith     }
14442b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1445e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1446f05ece33SBarry Smith   } else if (isams) {
1447d45a07a7SBarry Smith     PetscMPIInt rank;
14482657e9d9SBarry Smith     const char  *name;
14492657e9d9SBarry Smith 
14502657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1451d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1452d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1453d45a07a7SBarry Smith       char       dir[1024];
1454d45a07a7SBarry Smith 
1455e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1456a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
14572657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
14582657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
14592657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1460d763cef2SBarry Smith     }
14610acecf5bSBarry Smith     if (ts->ops->view) {
14620acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14630acecf5bSBarry Smith     }
1464f05ece33SBarry Smith #endif
1465f05ece33SBarry Smith   }
1466f05ece33SBarry Smith 
1467b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1468251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1469b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1470d763cef2SBarry Smith   PetscFunctionReturn(0);
1471d763cef2SBarry Smith }
1472d763cef2SBarry Smith 
1473d763cef2SBarry Smith 
14744a2ae208SSatish Balay #undef __FUNCT__
14754a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1476b07ff414SBarry Smith /*@
1477d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1478d763cef2SBarry Smith    the timesteppers.
1479d763cef2SBarry Smith 
14803f9fe445SBarry Smith    Logically Collective on TS
1481d763cef2SBarry Smith 
1482d763cef2SBarry Smith    Input Parameters:
1483d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1484d763cef2SBarry Smith -  usrP - optional user context
1485d763cef2SBarry Smith 
1486d763cef2SBarry Smith    Level: intermediate
1487d763cef2SBarry Smith 
1488d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1489d763cef2SBarry Smith 
1490d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1491d763cef2SBarry Smith @*/
14927087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1493d763cef2SBarry Smith {
1494d763cef2SBarry Smith   PetscFunctionBegin;
14950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1496d763cef2SBarry Smith   ts->user = usrP;
1497d763cef2SBarry Smith   PetscFunctionReturn(0);
1498d763cef2SBarry Smith }
1499d763cef2SBarry Smith 
15004a2ae208SSatish Balay #undef __FUNCT__
15014a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1502b07ff414SBarry Smith /*@
1503d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1504d763cef2SBarry Smith     timestepper.
1505d763cef2SBarry Smith 
1506d763cef2SBarry Smith     Not Collective
1507d763cef2SBarry Smith 
1508d763cef2SBarry Smith     Input Parameter:
1509d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1510d763cef2SBarry Smith 
1511d763cef2SBarry Smith     Output Parameter:
1512d763cef2SBarry Smith .   usrP - user context
1513d763cef2SBarry Smith 
1514d763cef2SBarry Smith     Level: intermediate
1515d763cef2SBarry Smith 
1516d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1517d763cef2SBarry Smith 
1518d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1519d763cef2SBarry Smith @*/
1520e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1521d763cef2SBarry Smith {
1522d763cef2SBarry Smith   PetscFunctionBegin;
15230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1524e71120c6SJed Brown   *(void**)usrP = ts->user;
1525d763cef2SBarry Smith   PetscFunctionReturn(0);
1526d763cef2SBarry Smith }
1527d763cef2SBarry Smith 
15284a2ae208SSatish Balay #undef __FUNCT__
15294a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1530d763cef2SBarry Smith /*@
1531b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1532d763cef2SBarry Smith 
1533d763cef2SBarry Smith    Not Collective
1534d763cef2SBarry Smith 
1535d763cef2SBarry Smith    Input Parameter:
1536d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1537d763cef2SBarry Smith 
1538d763cef2SBarry Smith    Output Parameter:
1539b8123daeSJed Brown .  iter - number of steps completed so far
1540d763cef2SBarry Smith 
1541d763cef2SBarry Smith    Level: intermediate
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
15449be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1545d763cef2SBarry Smith @*/
15467087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1547d763cef2SBarry Smith {
1548d763cef2SBarry Smith   PetscFunctionBegin;
15490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15504482741eSBarry Smith   PetscValidIntPointer(iter,2);
1551d763cef2SBarry Smith   *iter = ts->steps;
1552d763cef2SBarry Smith   PetscFunctionReturn(0);
1553d763cef2SBarry Smith }
1554d763cef2SBarry Smith 
15554a2ae208SSatish Balay #undef __FUNCT__
15564a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1557d763cef2SBarry Smith /*@
1558d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1559d763cef2SBarry Smith    as well as the initial time.
1560d763cef2SBarry Smith 
15613f9fe445SBarry Smith    Logically Collective on TS
1562d763cef2SBarry Smith 
1563d763cef2SBarry Smith    Input Parameters:
1564d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1565d763cef2SBarry Smith .  initial_time - the initial time
1566d763cef2SBarry Smith -  time_step - the size of the timestep
1567d763cef2SBarry Smith 
1568d763cef2SBarry Smith    Level: intermediate
1569d763cef2SBarry Smith 
1570d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1571d763cef2SBarry Smith 
1572d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1573d763cef2SBarry Smith @*/
15747087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1575d763cef2SBarry Smith {
1576e144a568SJed Brown   PetscErrorCode ierr;
1577e144a568SJed Brown 
1578d763cef2SBarry Smith   PetscFunctionBegin;
15790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1580e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1581d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1582d763cef2SBarry Smith   PetscFunctionReturn(0);
1583d763cef2SBarry Smith }
1584d763cef2SBarry Smith 
15854a2ae208SSatish Balay #undef __FUNCT__
15864a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1587d763cef2SBarry Smith /*@
1588d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1589d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1590d763cef2SBarry Smith 
15913f9fe445SBarry Smith    Logically Collective on TS
1592d763cef2SBarry Smith 
1593d763cef2SBarry Smith    Input Parameters:
1594d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1595d763cef2SBarry Smith -  time_step - the size of the timestep
1596d763cef2SBarry Smith 
1597d763cef2SBarry Smith    Level: intermediate
1598d763cef2SBarry Smith 
1599d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1600d763cef2SBarry Smith 
1601d763cef2SBarry Smith .keywords: TS, set, timestep
1602d763cef2SBarry Smith @*/
16037087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1604d763cef2SBarry Smith {
1605d763cef2SBarry Smith   PetscFunctionBegin;
16060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1607c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1608d763cef2SBarry Smith   ts->time_step      = time_step;
160931748224SBarry Smith   ts->time_step_orig = time_step;
1610d763cef2SBarry Smith   PetscFunctionReturn(0);
1611d763cef2SBarry Smith }
1612d763cef2SBarry Smith 
16134a2ae208SSatish Balay #undef __FUNCT__
1614a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1615a43b19c4SJed Brown /*@
161649354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
161749354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
161849354f04SShri Abhyankar      or just return at the final time TS computed.
1619a43b19c4SJed Brown 
1620a43b19c4SJed Brown   Logically Collective on TS
1621a43b19c4SJed Brown 
1622a43b19c4SJed Brown    Input Parameter:
1623a43b19c4SJed Brown +   ts - the time-step context
162449354f04SShri Abhyankar -   eftopt - exact final time option
1625a43b19c4SJed Brown 
1626a43b19c4SJed Brown    Level: beginner
1627a43b19c4SJed Brown 
1628a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1629a43b19c4SJed Brown @*/
163049354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1631a43b19c4SJed Brown {
1632a43b19c4SJed Brown   PetscFunctionBegin;
1633a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
163449354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
163549354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1636a43b19c4SJed Brown   PetscFunctionReturn(0);
1637a43b19c4SJed Brown }
1638a43b19c4SJed Brown 
1639a43b19c4SJed Brown #undef __FUNCT__
16404a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1641d763cef2SBarry Smith /*@
1642d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1643d763cef2SBarry Smith 
1644d763cef2SBarry Smith    Not Collective
1645d763cef2SBarry Smith 
1646d763cef2SBarry Smith    Input Parameter:
1647d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1648d763cef2SBarry Smith 
1649d763cef2SBarry Smith    Output Parameter:
1650d763cef2SBarry Smith .  dt - the current timestep size
1651d763cef2SBarry Smith 
1652d763cef2SBarry Smith    Level: intermediate
1653d763cef2SBarry Smith 
1654d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1655d763cef2SBarry Smith 
1656d763cef2SBarry Smith .keywords: TS, get, timestep
1657d763cef2SBarry Smith @*/
16587087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1659d763cef2SBarry Smith {
1660d763cef2SBarry Smith   PetscFunctionBegin;
16610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1662f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1663d763cef2SBarry Smith   *dt = ts->time_step;
1664d763cef2SBarry Smith   PetscFunctionReturn(0);
1665d763cef2SBarry Smith }
1666d763cef2SBarry Smith 
16674a2ae208SSatish Balay #undef __FUNCT__
16684a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1669d8e5e3e6SSatish Balay /*@
1670d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1671d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1672d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1673d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1674d763cef2SBarry Smith 
1675d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1676d763cef2SBarry Smith 
1677d763cef2SBarry Smith    Input Parameter:
1678d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1679d763cef2SBarry Smith 
1680d763cef2SBarry Smith    Output Parameter:
1681d763cef2SBarry Smith .  v - the vector containing the solution
1682d763cef2SBarry Smith 
1683d763cef2SBarry Smith    Level: intermediate
1684d763cef2SBarry Smith 
1685d763cef2SBarry Smith .seealso: TSGetTimeStep()
1686d763cef2SBarry Smith 
1687d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1688d763cef2SBarry Smith @*/
16897087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1690d763cef2SBarry Smith {
1691d763cef2SBarry Smith   PetscFunctionBegin;
16920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16934482741eSBarry Smith   PetscValidPointer(v,2);
16948737fe31SLisandro Dalcin   *v = ts->vec_sol;
1695d763cef2SBarry Smith   PetscFunctionReturn(0);
1696d763cef2SBarry Smith }
1697d763cef2SBarry Smith 
1698bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
16994a2ae208SSatish Balay #undef __FUNCT__
1700bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1701d8e5e3e6SSatish Balay /*@
1702bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1703d763cef2SBarry Smith 
1704bdad233fSMatthew Knepley   Not collective
1705d763cef2SBarry Smith 
1706bdad233fSMatthew Knepley   Input Parameters:
1707bdad233fSMatthew Knepley + ts   - The TS
1708bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1709d763cef2SBarry Smith .vb
17100910c330SBarry Smith          U_t - A U = 0      (linear)
17110910c330SBarry Smith          U_t - A(t) U = 0   (linear)
17120910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1713d763cef2SBarry Smith .ve
1714d763cef2SBarry Smith 
1715d763cef2SBarry Smith    Level: beginner
1716d763cef2SBarry Smith 
1717bdad233fSMatthew Knepley .keywords: TS, problem type
1718bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1719d763cef2SBarry Smith @*/
17207087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1721a7cc72afSBarry Smith {
17229e2a6581SJed Brown   PetscErrorCode ierr;
17239e2a6581SJed Brown 
1724d763cef2SBarry Smith   PetscFunctionBegin;
17250700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1726bdad233fSMatthew Knepley   ts->problem_type = type;
17279e2a6581SJed Brown   if (type == TS_LINEAR) {
17289e2a6581SJed Brown     SNES snes;
17299e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
17309e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
17319e2a6581SJed Brown   }
1732d763cef2SBarry Smith   PetscFunctionReturn(0);
1733d763cef2SBarry Smith }
1734d763cef2SBarry Smith 
1735bdad233fSMatthew Knepley #undef __FUNCT__
1736bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1737bdad233fSMatthew Knepley /*@C
1738bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1739bdad233fSMatthew Knepley 
1740bdad233fSMatthew Knepley   Not collective
1741bdad233fSMatthew Knepley 
1742bdad233fSMatthew Knepley   Input Parameter:
1743bdad233fSMatthew Knepley . ts   - The TS
1744bdad233fSMatthew Knepley 
1745bdad233fSMatthew Knepley   Output Parameter:
1746bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1747bdad233fSMatthew Knepley .vb
1748089b2837SJed Brown          M U_t = A U
1749089b2837SJed Brown          M(t) U_t = A(t) U
1750b5abc632SBarry Smith          F(t,U,U_t)
1751bdad233fSMatthew Knepley .ve
1752bdad233fSMatthew Knepley 
1753bdad233fSMatthew Knepley    Level: beginner
1754bdad233fSMatthew Knepley 
1755bdad233fSMatthew Knepley .keywords: TS, problem type
1756bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1757bdad233fSMatthew Knepley @*/
17587087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1759a7cc72afSBarry Smith {
1760bdad233fSMatthew Knepley   PetscFunctionBegin;
17610700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
17624482741eSBarry Smith   PetscValidIntPointer(type,2);
1763bdad233fSMatthew Knepley   *type = ts->problem_type;
1764bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1765bdad233fSMatthew Knepley }
1766d763cef2SBarry Smith 
17674a2ae208SSatish Balay #undef __FUNCT__
17684a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1769d763cef2SBarry Smith /*@
1770d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1771d763cef2SBarry Smith    of a timestepper.
1772d763cef2SBarry Smith 
1773d763cef2SBarry Smith    Collective on TS
1774d763cef2SBarry Smith 
1775d763cef2SBarry Smith    Input Parameter:
1776d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1777d763cef2SBarry Smith 
1778d763cef2SBarry Smith    Notes:
1779d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1780d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1781d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1782d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1783d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1784d763cef2SBarry Smith 
1785d763cef2SBarry Smith    Level: advanced
1786d763cef2SBarry Smith 
1787d763cef2SBarry Smith .keywords: TS, timestep, setup
1788d763cef2SBarry Smith 
1789d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1790d763cef2SBarry Smith @*/
17917087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1792d763cef2SBarry Smith {
1793dfbe8321SBarry Smith   PetscErrorCode ierr;
17946c6b9e74SPeter Brune   DM             dm;
17956c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
17966c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
17976c6b9e74SPeter Brune   TSIJacobian    ijac;
17986c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1799d763cef2SBarry Smith 
1800d763cef2SBarry Smith   PetscFunctionBegin;
18010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1802277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1803277b19d0SLisandro Dalcin 
18047adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
18059596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1806d763cef2SBarry Smith   }
1807277b19d0SLisandro Dalcin 
1808277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1809277b19d0SLisandro Dalcin 
1810552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
18111c3436cfSJed Brown 
1812e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1813e1244c69SJed Brown     Mat Amat,Pmat;
1814e1244c69SJed Brown     SNES snes;
1815e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1816e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1817e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1818e1244c69SJed Brown      * have displaced the RHS matrix */
1819e1244c69SJed Brown     if (Amat == ts->Arhs) {
1820e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1821e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1822e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1823e1244c69SJed Brown     }
1824e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1825e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1826e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1827e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1828e1244c69SJed Brown     }
1829e1244c69SJed Brown   }
1830e1244c69SJed Brown 
1831277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1832000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1833277b19d0SLisandro Dalcin   }
1834277b19d0SLisandro Dalcin 
18356c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
18366c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
18376c6b9e74SPeter Brune    */
18386c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
18390298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
18406c6b9e74SPeter Brune   if (!func) {
18416c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
18426c6b9e74SPeter Brune   }
18436c6b9e74SPeter 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.
18446c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
18456c6b9e74SPeter Brune    */
18460298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
18470298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
18480298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
18496c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
18506c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
18516c6b9e74SPeter Brune   }
1852277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1853277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1854277b19d0SLisandro Dalcin }
1855277b19d0SLisandro Dalcin 
1856277b19d0SLisandro Dalcin #undef __FUNCT__
1857277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1858277b19d0SLisandro Dalcin /*@
1859277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1860277b19d0SLisandro Dalcin 
1861277b19d0SLisandro Dalcin    Collective on TS
1862277b19d0SLisandro Dalcin 
1863277b19d0SLisandro Dalcin    Input Parameter:
1864277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1865277b19d0SLisandro Dalcin 
1866277b19d0SLisandro Dalcin    Level: beginner
1867277b19d0SLisandro Dalcin 
1868277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1869277b19d0SLisandro Dalcin 
1870277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1871277b19d0SLisandro Dalcin @*/
1872277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1873277b19d0SLisandro Dalcin {
1874277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1875277b19d0SLisandro Dalcin 
1876277b19d0SLisandro Dalcin   PetscFunctionBegin;
1877277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1878277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1879277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1880277b19d0SLisandro Dalcin   }
1881277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1882bbd56ea5SKarl Rupp 
18834e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
18844e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1885214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
18866bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1887e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1888e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
188938637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1890bbd56ea5SKarl Rupp 
1891277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1892d763cef2SBarry Smith   PetscFunctionReturn(0);
1893d763cef2SBarry Smith }
1894d763cef2SBarry Smith 
18954a2ae208SSatish Balay #undef __FUNCT__
18964a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1897d8e5e3e6SSatish Balay /*@
1898d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1899d763cef2SBarry Smith    with TSCreate().
1900d763cef2SBarry Smith 
1901d763cef2SBarry Smith    Collective on TS
1902d763cef2SBarry Smith 
1903d763cef2SBarry Smith    Input Parameter:
1904d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1905d763cef2SBarry Smith 
1906d763cef2SBarry Smith    Level: beginner
1907d763cef2SBarry Smith 
1908d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1909d763cef2SBarry Smith 
1910d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1911d763cef2SBarry Smith @*/
19126bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1913d763cef2SBarry Smith {
19146849ba73SBarry Smith   PetscErrorCode ierr;
1915d763cef2SBarry Smith 
1916d763cef2SBarry Smith   PetscFunctionBegin;
19176bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
19186bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
19196bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1920d763cef2SBarry Smith 
19216bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1922277b19d0SLisandro Dalcin 
1923e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
1924e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
19256bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
19266d4c513bSLisandro Dalcin 
192784df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
19286bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
19296bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
19306bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
19316d4c513bSLisandro Dalcin 
1932a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1933d763cef2SBarry Smith   PetscFunctionReturn(0);
1934d763cef2SBarry Smith }
1935d763cef2SBarry Smith 
19364a2ae208SSatish Balay #undef __FUNCT__
19374a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1938d8e5e3e6SSatish Balay /*@
1939d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1940d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1941d763cef2SBarry Smith 
1942d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1943d763cef2SBarry Smith 
1944d763cef2SBarry Smith    Input Parameter:
1945d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1946d763cef2SBarry Smith 
1947d763cef2SBarry Smith    Output Parameter:
1948d763cef2SBarry Smith .  snes - the nonlinear solver context
1949d763cef2SBarry Smith 
1950d763cef2SBarry Smith    Notes:
1951d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1952d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
195394b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1954d763cef2SBarry Smith 
1955d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
19560298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
1957d763cef2SBarry Smith 
1958d763cef2SBarry Smith    Level: beginner
1959d763cef2SBarry Smith 
1960d763cef2SBarry Smith .keywords: timestep, get, SNES
1961d763cef2SBarry Smith @*/
19627087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1963d763cef2SBarry Smith {
1964d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1965d372ba47SLisandro Dalcin 
1966d763cef2SBarry Smith   PetscFunctionBegin;
19670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19684482741eSBarry Smith   PetscValidPointer(snes,2);
1969d372ba47SLisandro Dalcin   if (!ts->snes) {
1970ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
19710298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
19723bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
1973d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1974496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
19759e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
19769e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
19779e2a6581SJed Brown     }
1978d372ba47SLisandro Dalcin   }
1979d763cef2SBarry Smith   *snes = ts->snes;
1980d763cef2SBarry Smith   PetscFunctionReturn(0);
1981d763cef2SBarry Smith }
1982d763cef2SBarry Smith 
19834a2ae208SSatish Balay #undef __FUNCT__
1984deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1985deb2cd25SJed Brown /*@
1986deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1987deb2cd25SJed Brown 
1988deb2cd25SJed Brown    Collective
1989deb2cd25SJed Brown 
1990deb2cd25SJed Brown    Input Parameter:
1991deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1992deb2cd25SJed Brown -  snes - the nonlinear solver context
1993deb2cd25SJed Brown 
1994deb2cd25SJed Brown    Notes:
1995deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1996deb2cd25SJed Brown 
1997deb2cd25SJed Brown    Level: developer
1998deb2cd25SJed Brown 
1999deb2cd25SJed Brown .keywords: timestep, set, SNES
2000deb2cd25SJed Brown @*/
2001deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2002deb2cd25SJed Brown {
2003deb2cd25SJed Brown   PetscErrorCode ierr;
2004740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
2005deb2cd25SJed Brown 
2006deb2cd25SJed Brown   PetscFunctionBegin;
2007deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2008deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2009deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2010deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2011bbd56ea5SKarl Rupp 
2012deb2cd25SJed Brown   ts->snes = snes;
2013bbd56ea5SKarl Rupp 
20140298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20150298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2016740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
20170298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2018740132f1SEmil Constantinescu   }
2019deb2cd25SJed Brown   PetscFunctionReturn(0);
2020deb2cd25SJed Brown }
2021deb2cd25SJed Brown 
2022deb2cd25SJed Brown #undef __FUNCT__
202394b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2024d8e5e3e6SSatish Balay /*@
202594b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2026d763cef2SBarry Smith    a TS (timestepper) context.
2027d763cef2SBarry Smith 
202894b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2029d763cef2SBarry Smith 
2030d763cef2SBarry Smith    Input Parameter:
2031d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2032d763cef2SBarry Smith 
2033d763cef2SBarry Smith    Output Parameter:
203494b7f48cSBarry Smith .  ksp - the nonlinear solver context
2035d763cef2SBarry Smith 
2036d763cef2SBarry Smith    Notes:
203794b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2038d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2039d763cef2SBarry Smith    KSP and PC contexts as well.
2040d763cef2SBarry Smith 
204194b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
20420298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2043d763cef2SBarry Smith 
2044d763cef2SBarry Smith    Level: beginner
2045d763cef2SBarry Smith 
204694b7f48cSBarry Smith .keywords: timestep, get, KSP
2047d763cef2SBarry Smith @*/
20487087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2049d763cef2SBarry Smith {
2050d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2051089b2837SJed Brown   SNES           snes;
2052d372ba47SLisandro Dalcin 
2053d763cef2SBarry Smith   PetscFunctionBegin;
20540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20554482741eSBarry Smith   PetscValidPointer(ksp,2);
205617186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2057e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2058089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2059089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2060d763cef2SBarry Smith   PetscFunctionReturn(0);
2061d763cef2SBarry Smith }
2062d763cef2SBarry Smith 
2063d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2064d763cef2SBarry Smith 
20654a2ae208SSatish Balay #undef __FUNCT__
2066adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2067adb62b0dSMatthew Knepley /*@
2068adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2069adb62b0dSMatthew Knepley    maximum time for iteration.
2070adb62b0dSMatthew Knepley 
20713f9fe445SBarry Smith    Not Collective
2072adb62b0dSMatthew Knepley 
2073adb62b0dSMatthew Knepley    Input Parameters:
2074adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
20750298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
20760298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2077adb62b0dSMatthew Knepley 
2078adb62b0dSMatthew Knepley    Level: intermediate
2079adb62b0dSMatthew Knepley 
2080adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2081adb62b0dSMatthew Knepley @*/
20827087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2083adb62b0dSMatthew Knepley {
2084adb62b0dSMatthew Knepley   PetscFunctionBegin;
20850700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2086abc0a331SBarry Smith   if (maxsteps) {
20874482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2088adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2089adb62b0dSMatthew Knepley   }
2090abc0a331SBarry Smith   if (maxtime) {
20914482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2092adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2093adb62b0dSMatthew Knepley   }
2094adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2095adb62b0dSMatthew Knepley }
2096adb62b0dSMatthew Knepley 
2097adb62b0dSMatthew Knepley #undef __FUNCT__
20984a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2099d763cef2SBarry Smith /*@
2100d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2101d763cef2SBarry Smith    maximum time for iteration.
2102d763cef2SBarry Smith 
21033f9fe445SBarry Smith    Logically Collective on TS
2104d763cef2SBarry Smith 
2105d763cef2SBarry Smith    Input Parameters:
2106d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2107d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2108d763cef2SBarry Smith -  maxtime - final time to iterate to
2109d763cef2SBarry Smith 
2110d763cef2SBarry Smith    Options Database Keys:
2111d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
21123bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2113d763cef2SBarry Smith 
2114d763cef2SBarry Smith    Notes:
2115d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2116d763cef2SBarry Smith 
2117d763cef2SBarry Smith    Level: intermediate
2118d763cef2SBarry Smith 
2119d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2120a43b19c4SJed Brown 
2121a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2122d763cef2SBarry Smith @*/
21237087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2124d763cef2SBarry Smith {
2125d763cef2SBarry Smith   PetscFunctionBegin;
21260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2127c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2128c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
212939b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
213039b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2131d763cef2SBarry Smith   PetscFunctionReturn(0);
2132d763cef2SBarry Smith }
2133d763cef2SBarry Smith 
21344a2ae208SSatish Balay #undef __FUNCT__
21354a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2136d763cef2SBarry Smith /*@
2137d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2138d763cef2SBarry Smith    for use by the TS routines.
2139d763cef2SBarry Smith 
21403f9fe445SBarry Smith    Logically Collective on TS and Vec
2141d763cef2SBarry Smith 
2142d763cef2SBarry Smith    Input Parameters:
2143d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
21440910c330SBarry Smith -  u - the solution vector
2145d763cef2SBarry Smith 
2146d763cef2SBarry Smith    Level: beginner
2147d763cef2SBarry Smith 
2148d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2149d763cef2SBarry Smith @*/
21500910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2151d763cef2SBarry Smith {
21528737fe31SLisandro Dalcin   PetscErrorCode ierr;
2153496e6a7aSJed Brown   DM             dm;
21548737fe31SLisandro Dalcin 
2155d763cef2SBarry Smith   PetscFunctionBegin;
21560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21570910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
21580910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
21596bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2160bbd56ea5SKarl Rupp 
21610910c330SBarry Smith   ts->vec_sol = u;
2162bbd56ea5SKarl Rupp 
2163496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
21640910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2165d763cef2SBarry Smith   PetscFunctionReturn(0);
2166d763cef2SBarry Smith }
2167d763cef2SBarry Smith 
2168e74ef692SMatthew Knepley #undef __FUNCT__
2169e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2170ac226902SBarry Smith /*@C
2171000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
21723f2090d5SJed Brown   called once at the beginning of each time step.
2173000e7ae3SMatthew Knepley 
21743f9fe445SBarry Smith   Logically Collective on TS
2175000e7ae3SMatthew Knepley 
2176000e7ae3SMatthew Knepley   Input Parameters:
2177000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2178000e7ae3SMatthew Knepley - func - The function
2179000e7ae3SMatthew Knepley 
2180000e7ae3SMatthew Knepley   Calling sequence of func:
2181000e7ae3SMatthew Knepley . func (TS ts);
2182000e7ae3SMatthew Knepley 
2183000e7ae3SMatthew Knepley   Level: intermediate
2184000e7ae3SMatthew Knepley 
2185b8123daeSJed Brown   Note:
2186b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2187b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2188b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2189b8123daeSJed Brown 
2190000e7ae3SMatthew Knepley .keywords: TS, timestep
21919be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2192000e7ae3SMatthew Knepley @*/
21937087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2194000e7ae3SMatthew Knepley {
2195000e7ae3SMatthew Knepley   PetscFunctionBegin;
21960700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2197ae60f76fSBarry Smith   ts->prestep = func;
2198000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2199000e7ae3SMatthew Knepley }
2200000e7ae3SMatthew Knepley 
2201e74ef692SMatthew Knepley #undef __FUNCT__
22023f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
220309ee8438SJed Brown /*@
22043f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
22053f2090d5SJed Brown 
22063f2090d5SJed Brown   Collective on TS
22073f2090d5SJed Brown 
22083f2090d5SJed Brown   Input Parameters:
22093f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
22103f2090d5SJed Brown 
22113f2090d5SJed Brown   Notes:
22123f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
22133f2090d5SJed Brown   so most users would not generally call this routine themselves.
22143f2090d5SJed Brown 
22153f2090d5SJed Brown   Level: developer
22163f2090d5SJed Brown 
22173f2090d5SJed Brown .keywords: TS, timestep
22189be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
22193f2090d5SJed Brown @*/
22207087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
22213f2090d5SJed Brown {
22223f2090d5SJed Brown   PetscErrorCode ierr;
22233f2090d5SJed Brown 
22243f2090d5SJed Brown   PetscFunctionBegin;
22250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2226ae60f76fSBarry Smith   if (ts->prestep) {
2227ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2228312ce896SJed Brown   }
22293f2090d5SJed Brown   PetscFunctionReturn(0);
22303f2090d5SJed Brown }
22313f2090d5SJed Brown 
22323f2090d5SJed Brown #undef __FUNCT__
2233b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2234b8123daeSJed Brown /*@C
2235b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2236b8123daeSJed Brown   called once at the beginning of each stage.
2237b8123daeSJed Brown 
2238b8123daeSJed Brown   Logically Collective on TS
2239b8123daeSJed Brown 
2240b8123daeSJed Brown   Input Parameters:
2241b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2242b8123daeSJed Brown - func - The function
2243b8123daeSJed Brown 
2244b8123daeSJed Brown   Calling sequence of func:
2245b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2246b8123daeSJed Brown 
2247b8123daeSJed Brown   Level: intermediate
2248b8123daeSJed Brown 
2249b8123daeSJed Brown   Note:
2250b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2251b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2252b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2253b8123daeSJed Brown 
2254b8123daeSJed Brown .keywords: TS, timestep
22559be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2256b8123daeSJed Brown @*/
2257b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2258b8123daeSJed Brown {
2259b8123daeSJed Brown   PetscFunctionBegin;
2260b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2261ae60f76fSBarry Smith   ts->prestage = func;
2262b8123daeSJed Brown   PetscFunctionReturn(0);
2263b8123daeSJed Brown }
2264b8123daeSJed Brown 
2265b8123daeSJed Brown #undef __FUNCT__
22669be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
22679be3e283SDebojyoti Ghosh /*@C
22689be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
22699be3e283SDebojyoti Ghosh   called once at the end of each stage.
22709be3e283SDebojyoti Ghosh 
22719be3e283SDebojyoti Ghosh   Logically Collective on TS
22729be3e283SDebojyoti Ghosh 
22739be3e283SDebojyoti Ghosh   Input Parameters:
22749be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
22759be3e283SDebojyoti Ghosh - func - The function
22769be3e283SDebojyoti Ghosh 
22779be3e283SDebojyoti Ghosh   Calling sequence of func:
22789be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
22799be3e283SDebojyoti Ghosh 
22809be3e283SDebojyoti Ghosh   Level: intermediate
22819be3e283SDebojyoti Ghosh 
22829be3e283SDebojyoti Ghosh   Note:
22839be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
22849be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
22859be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
22869be3e283SDebojyoti Ghosh 
22879be3e283SDebojyoti Ghosh .keywords: TS, timestep
22889be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
22899be3e283SDebojyoti Ghosh @*/
22909be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
22919be3e283SDebojyoti Ghosh {
22929be3e283SDebojyoti Ghosh   PetscFunctionBegin;
22939be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
22949be3e283SDebojyoti Ghosh   ts->poststage = func;
22959be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
22969be3e283SDebojyoti Ghosh }
22979be3e283SDebojyoti Ghosh 
22989be3e283SDebojyoti Ghosh #undef __FUNCT__
2299b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2300b8123daeSJed Brown /*@
2301b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2302b8123daeSJed Brown 
2303b8123daeSJed Brown   Collective on TS
2304b8123daeSJed Brown 
2305b8123daeSJed Brown   Input Parameters:
2306b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
23079be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2308b8123daeSJed Brown 
2309b8123daeSJed Brown   Notes:
2310b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2311b8123daeSJed Brown   most users would not generally call this routine themselves.
2312b8123daeSJed Brown 
2313b8123daeSJed Brown   Level: developer
2314b8123daeSJed Brown 
2315b8123daeSJed Brown .keywords: TS, timestep
23169be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2317b8123daeSJed Brown @*/
2318b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2319b8123daeSJed Brown {
2320b8123daeSJed Brown   PetscErrorCode ierr;
2321b8123daeSJed Brown 
2322b8123daeSJed Brown   PetscFunctionBegin;
2323b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2324ae60f76fSBarry Smith   if (ts->prestage) {
2325ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2326b8123daeSJed Brown   }
2327b8123daeSJed Brown   PetscFunctionReturn(0);
2328b8123daeSJed Brown }
2329b8123daeSJed Brown 
2330b8123daeSJed Brown #undef __FUNCT__
23319be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
23329be3e283SDebojyoti Ghosh /*@
23339be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
23349be3e283SDebojyoti Ghosh 
23359be3e283SDebojyoti Ghosh   Collective on TS
23369be3e283SDebojyoti Ghosh 
23379be3e283SDebojyoti Ghosh   Input Parameters:
23389be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
23399be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
23409be3e283SDebojyoti Ghosh   stageindex  - Stage number
23419be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
23429be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
23439be3e283SDebojyoti Ghosh 
23449be3e283SDebojyoti Ghosh   Notes:
23459be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
23469be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
23479be3e283SDebojyoti Ghosh 
23489be3e283SDebojyoti Ghosh   Level: developer
23499be3e283SDebojyoti Ghosh 
23509be3e283SDebojyoti Ghosh .keywords: TS, timestep
23519be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
23529be3e283SDebojyoti Ghosh @*/
23539be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
23549be3e283SDebojyoti Ghosh {
23559be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
23569be3e283SDebojyoti Ghosh 
23579be3e283SDebojyoti Ghosh   PetscFunctionBegin;
23589be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23599be3e283SDebojyoti Ghosh   if (ts->prestage) {
23609be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
23619be3e283SDebojyoti Ghosh   }
23629be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
23639be3e283SDebojyoti Ghosh }
23649be3e283SDebojyoti Ghosh 
23659be3e283SDebojyoti Ghosh #undef __FUNCT__
2366e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2367ac226902SBarry Smith /*@C
2368000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
23693f2090d5SJed Brown   called once at the end of each time step.
2370000e7ae3SMatthew Knepley 
23713f9fe445SBarry Smith   Logically Collective on TS
2372000e7ae3SMatthew Knepley 
2373000e7ae3SMatthew Knepley   Input Parameters:
2374000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2375000e7ae3SMatthew Knepley - func - The function
2376000e7ae3SMatthew Knepley 
2377000e7ae3SMatthew Knepley   Calling sequence of func:
2378b8123daeSJed Brown $ func (TS ts);
2379000e7ae3SMatthew Knepley 
2380000e7ae3SMatthew Knepley   Level: intermediate
2381000e7ae3SMatthew Knepley 
2382000e7ae3SMatthew Knepley .keywords: TS, timestep
2383b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2384000e7ae3SMatthew Knepley @*/
23857087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2386000e7ae3SMatthew Knepley {
2387000e7ae3SMatthew Knepley   PetscFunctionBegin;
23880700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2389ae60f76fSBarry Smith   ts->poststep = func;
2390000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2391000e7ae3SMatthew Knepley }
2392000e7ae3SMatthew Knepley 
2393e74ef692SMatthew Knepley #undef __FUNCT__
23943f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
239509ee8438SJed Brown /*@
23963f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
23973f2090d5SJed Brown 
23983f2090d5SJed Brown   Collective on TS
23993f2090d5SJed Brown 
24003f2090d5SJed Brown   Input Parameters:
24013f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
24023f2090d5SJed Brown 
24033f2090d5SJed Brown   Notes:
24043f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
24053f2090d5SJed Brown   so most users would not generally call this routine themselves.
24063f2090d5SJed Brown 
24073f2090d5SJed Brown   Level: developer
24083f2090d5SJed Brown 
24093f2090d5SJed Brown .keywords: TS, timestep
24103f2090d5SJed Brown @*/
24117087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
24123f2090d5SJed Brown {
24133f2090d5SJed Brown   PetscErrorCode ierr;
24143f2090d5SJed Brown 
24153f2090d5SJed Brown   PetscFunctionBegin;
24160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2417ae60f76fSBarry Smith   if (ts->poststep) {
2418ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
241972ac3e02SJed Brown   }
24203f2090d5SJed Brown   PetscFunctionReturn(0);
24213f2090d5SJed Brown }
24223f2090d5SJed Brown 
2423d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2424d763cef2SBarry Smith 
24254a2ae208SSatish Balay #undef __FUNCT__
2426a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2427d763cef2SBarry Smith /*@C
2428a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2429d763cef2SBarry Smith    timestep to display the iteration's  progress.
2430d763cef2SBarry Smith 
24313f9fe445SBarry Smith    Logically Collective on TS
2432d763cef2SBarry Smith 
2433d763cef2SBarry Smith    Input Parameters:
2434d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2435e213d8f1SJed Brown .  monitor - monitoring routine
2436329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
24370298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2438b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
24390298fd71SBarry Smith           (may be NULL)
2440d763cef2SBarry Smith 
2441e213d8f1SJed Brown    Calling sequence of monitor:
24420910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2443d763cef2SBarry Smith 
2444d763cef2SBarry Smith +    ts - the TS context
244588c05cc5SBarry 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
244688c05cc5SBarry Smith                                been interpolated to)
24471f06c33eSBarry Smith .    time - current time
24480910c330SBarry Smith .    u - current iterate
2449d763cef2SBarry Smith -    mctx - [optional] monitoring context
2450d763cef2SBarry Smith 
2451d763cef2SBarry Smith    Notes:
2452d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2453d763cef2SBarry Smith    already has been loaded.
2454d763cef2SBarry Smith 
2455025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2456025f1a04SBarry Smith 
2457d763cef2SBarry Smith    Level: intermediate
2458d763cef2SBarry Smith 
2459d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2460d763cef2SBarry Smith 
2461a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2462d763cef2SBarry Smith @*/
2463c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2464d763cef2SBarry Smith {
2465d763cef2SBarry Smith   PetscFunctionBegin;
24660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
246717186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2468d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
24698704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2470d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2471d763cef2SBarry Smith   PetscFunctionReturn(0);
2472d763cef2SBarry Smith }
2473d763cef2SBarry Smith 
24744a2ae208SSatish Balay #undef __FUNCT__
2475a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2476d763cef2SBarry Smith /*@C
2477a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2478d763cef2SBarry Smith 
24793f9fe445SBarry Smith    Logically Collective on TS
2480d763cef2SBarry Smith 
2481d763cef2SBarry Smith    Input Parameters:
2482d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2483d763cef2SBarry Smith 
2484d763cef2SBarry Smith    Notes:
2485d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2486d763cef2SBarry Smith 
2487d763cef2SBarry Smith    Level: intermediate
2488d763cef2SBarry Smith 
2489d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2490d763cef2SBarry Smith 
2491a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2492d763cef2SBarry Smith @*/
24937087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2494d763cef2SBarry Smith {
2495d952e501SBarry Smith   PetscErrorCode ierr;
2496d952e501SBarry Smith   PetscInt       i;
2497d952e501SBarry Smith 
2498d763cef2SBarry Smith   PetscFunctionBegin;
24990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2500d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
25018704b422SBarry Smith     if (ts->monitordestroy[i]) {
25028704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2503d952e501SBarry Smith     }
2504d952e501SBarry Smith   }
2505d763cef2SBarry Smith   ts->numbermonitors = 0;
2506d763cef2SBarry Smith   PetscFunctionReturn(0);
2507d763cef2SBarry Smith }
2508d763cef2SBarry Smith 
25094a2ae208SSatish Balay #undef __FUNCT__
2510a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2511d8e5e3e6SSatish Balay /*@
2512a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
25135516499fSSatish Balay 
25145516499fSSatish Balay    Level: intermediate
251541251cbbSSatish Balay 
25165516499fSSatish Balay .keywords: TS, set, monitor
25175516499fSSatish Balay 
251841251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
251941251cbbSSatish Balay @*/
2520649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2521d763cef2SBarry Smith {
2522dfbe8321SBarry Smith   PetscErrorCode ierr;
2523ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2524d132466eSBarry Smith 
2525d763cef2SBarry Smith   PetscFunctionBegin;
2526649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2527971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2528649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2529d763cef2SBarry Smith   PetscFunctionReturn(0);
2530d763cef2SBarry Smith }
2531d763cef2SBarry Smith 
25324a2ae208SSatish Balay #undef __FUNCT__
2533cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2534cd652676SJed Brown /*@
2535cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2536cd652676SJed Brown 
2537cd652676SJed Brown    Logically Collective on TS
2538cd652676SJed Brown 
2539cd652676SJed Brown    Input Argument:
2540cd652676SJed Brown .  ts - time stepping context
2541cd652676SJed Brown 
2542cd652676SJed Brown    Output Argument:
2543cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2544cd652676SJed Brown 
2545cd652676SJed Brown    Level: intermediate
2546cd652676SJed Brown 
2547cd652676SJed Brown .keywords: TS, set
2548cd652676SJed Brown 
2549cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2550cd652676SJed Brown @*/
2551cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2552cd652676SJed Brown {
2553cd652676SJed Brown   PetscFunctionBegin;
2554cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2555cd652676SJed Brown   ts->retain_stages = flg;
2556cd652676SJed Brown   PetscFunctionReturn(0);
2557cd652676SJed Brown }
2558cd652676SJed Brown 
2559cd652676SJed Brown #undef __FUNCT__
2560cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2561cd652676SJed Brown /*@
2562cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2563cd652676SJed Brown 
2564cd652676SJed Brown    Collective on TS
2565cd652676SJed Brown 
2566cd652676SJed Brown    Input Argument:
2567cd652676SJed Brown +  ts - time stepping context
2568cd652676SJed Brown -  t - time to interpolate to
2569cd652676SJed Brown 
2570cd652676SJed Brown    Output Argument:
25710910c330SBarry Smith .  U - state at given time
2572cd652676SJed Brown 
2573cd652676SJed Brown    Notes:
2574cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2575cd652676SJed Brown 
2576cd652676SJed Brown    Level: intermediate
2577cd652676SJed Brown 
2578cd652676SJed Brown    Developer Notes:
2579cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2580cd652676SJed Brown 
2581cd652676SJed Brown .keywords: TS, set
2582cd652676SJed Brown 
2583cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2584cd652676SJed Brown @*/
25850910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2586cd652676SJed Brown {
2587cd652676SJed Brown   PetscErrorCode ierr;
2588cd652676SJed Brown 
2589cd652676SJed Brown   PetscFunctionBegin;
2590cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2591b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2592ce94432eSBarry 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,ts->ptime-ts->time_step_prev,ts->ptime);
2593ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
25940910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2595cd652676SJed Brown   PetscFunctionReturn(0);
2596cd652676SJed Brown }
2597cd652676SJed Brown 
2598cd652676SJed Brown #undef __FUNCT__
25994a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2600d763cef2SBarry Smith /*@
26016d9e5789SSean Farley    TSStep - Steps one time step
2602d763cef2SBarry Smith 
2603d763cef2SBarry Smith    Collective on TS
2604d763cef2SBarry Smith 
2605d763cef2SBarry Smith    Input Parameter:
2606d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2607d763cef2SBarry Smith 
26086d9e5789SSean Farley    Level: intermediate
2609d763cef2SBarry Smith 
2610b8123daeSJed Brown    Notes:
2611b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2612b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2613b8123daeSJed Brown 
261425cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
261525cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
261625cb2221SBarry Smith 
2617d763cef2SBarry Smith .keywords: TS, timestep, solve
2618d763cef2SBarry Smith 
26199be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
2620d763cef2SBarry Smith @*/
2621193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2622d763cef2SBarry Smith {
2623362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2624dfbe8321SBarry Smith   PetscErrorCode ierr;
2625d763cef2SBarry Smith 
2626d763cef2SBarry Smith   PetscFunctionBegin;
26270700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2628d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2629d405a339SMatthew Knepley 
2630362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2631362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2632bbd56ea5SKarl Rupp 
2633d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2634193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2635d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2636bbd56ea5SKarl Rupp 
2637362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2638362cd11cSLisandro Dalcin 
2639362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2640cef5090cSJed Brown     if (ts->errorifstepfailed) {
2641cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2642ce94432eSBarry 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]);
2643ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2644cef5090cSJed Brown     }
2645362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2646db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2647db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2648362cd11cSLisandro Dalcin   }
2649d763cef2SBarry Smith   PetscFunctionReturn(0);
2650d763cef2SBarry Smith }
2651d763cef2SBarry Smith 
26524a2ae208SSatish Balay #undef __FUNCT__
265305175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
265405175c85SJed Brown /*@
265505175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
265605175c85SJed Brown 
26571c3436cfSJed Brown    Collective on TS
265805175c85SJed Brown 
265905175c85SJed Brown    Input Arguments:
26601c3436cfSJed Brown +  ts - time stepping context
26611c3436cfSJed Brown .  order - desired order of accuracy
26620298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
266305175c85SJed Brown 
266405175c85SJed Brown    Output Arguments:
26650910c330SBarry Smith .  U - state at the end of the current step
266605175c85SJed Brown 
266705175c85SJed Brown    Level: advanced
266805175c85SJed Brown 
2669108c343cSJed Brown    Notes:
2670108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2671108c343cSJed 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.
2672108c343cSJed Brown 
26731c3436cfSJed Brown .seealso: TSStep(), TSAdapt
267405175c85SJed Brown @*/
26750910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
267605175c85SJed Brown {
267705175c85SJed Brown   PetscErrorCode ierr;
267805175c85SJed Brown 
267905175c85SJed Brown   PetscFunctionBegin;
268005175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
268105175c85SJed Brown   PetscValidType(ts,1);
26820910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2683ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
26840910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
268505175c85SJed Brown   PetscFunctionReturn(0);
268605175c85SJed Brown }
268705175c85SJed Brown 
268805175c85SJed Brown #undef __FUNCT__
26896a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
26906a4d4014SLisandro Dalcin /*@
26916a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
26926a4d4014SLisandro Dalcin 
26936a4d4014SLisandro Dalcin    Collective on TS
26946a4d4014SLisandro Dalcin 
26956a4d4014SLisandro Dalcin    Input Parameter:
26966a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2697cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
26985a3a76d0SJed Brown 
26996a4d4014SLisandro Dalcin    Level: beginner
27006a4d4014SLisandro Dalcin 
27015a3a76d0SJed Brown    Notes:
27025a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
27035a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
27045a3a76d0SJed Brown    stepped over the final time.
27055a3a76d0SJed Brown 
27066a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
27076a4d4014SLisandro Dalcin 
27086a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
27096a4d4014SLisandro Dalcin @*/
2710cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
27116a4d4014SLisandro Dalcin {
27124d7d938eSLisandro Dalcin   PetscBool         flg;
27134d7d938eSLisandro Dalcin   PetscViewer       viewer;
2714b06615a5SLisandro Dalcin   Vec               solution;
27156a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2716cffb1e40SBarry Smith   PetscViewerFormat format;
2717f22f69f0SBarry Smith 
27186a4d4014SLisandro Dalcin   PetscFunctionBegin;
27190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2720f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
272149354f04SShri Abhyankar   if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
2722b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
27230910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
2724b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
2725b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
2726b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
27275a3a76d0SJed Brown     }
27280910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2729bbd56ea5SKarl Rupp   } else if (u) {
27300910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
27315a3a76d0SJed Brown   }
2732b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
27336a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2734193ac0bcSJed Brown   ts->steps             = 0;
27355ef26d82SJed Brown   ts->ksp_its           = 0;
27365ef26d82SJed Brown   ts->snes_its          = 0;
2737c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2738c610991cSLisandro Dalcin   ts->reject            = 0;
2739193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2740193ac0bcSJed Brown 
2741f05ece33SBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view_pre",&viewer,&format,&flg);CHKERRQ(ierr);
2742f05ece33SBarry Smith   if (flg && !PetscPreLoadingOn) {
2743f05ece33SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
2744f05ece33SBarry Smith     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2745f05ece33SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2746f05ece33SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2747f05ece33SBarry Smith   }
2748f05ece33SBarry Smith 
2749193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2750193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
27510910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2752cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2753193ac0bcSJed Brown   } else {
27546a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2755db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2756db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2757e1a7a14fSJed Brown     while (!ts->reason) {
2758b06615a5SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2759193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2760193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2761193ac0bcSJed Brown     }
276249354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
27630910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2764cc708dedSBarry Smith       ts->solvetime = ts->max_time;
2765b06615a5SLisandro Dalcin       solution = u;
27660574a7fbSJed Brown     } else {
2767ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2768cc708dedSBarry Smith       ts->solvetime = ts->ptime;
2769b06615a5SLisandro Dalcin       solution = ts->vec_sol;
27700574a7fbSJed Brown     }
2771b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
2772193ac0bcSJed Brown   }
2773ce94432eSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
27744d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2775cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
27764d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2777cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2778cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
27792b0a91c0SBarry Smith   }
278056f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
27816a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
27826a4d4014SLisandro Dalcin }
27836a4d4014SLisandro Dalcin 
27846a4d4014SLisandro Dalcin #undef __FUNCT__
27854a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2786228d79bcSJed Brown /*@
2787228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2788228d79bcSJed Brown 
2789228d79bcSJed Brown    Collective on TS
2790228d79bcSJed Brown 
2791228d79bcSJed Brown    Input Parameters:
2792228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2793228d79bcSJed Brown .  step - step number that has just completed
2794228d79bcSJed Brown .  ptime - model time of the state
27950910c330SBarry Smith -  u - state at the current model time
2796228d79bcSJed Brown 
2797228d79bcSJed Brown    Notes:
2798228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2799228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2800228d79bcSJed Brown 
2801228d79bcSJed Brown    Level: advanced
2802228d79bcSJed Brown 
2803228d79bcSJed Brown .keywords: TS, timestep
2804228d79bcSJed Brown @*/
28050910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2806d763cef2SBarry Smith {
28076849ba73SBarry Smith   PetscErrorCode ierr;
2808a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2809d763cef2SBarry Smith 
2810d763cef2SBarry Smith   PetscFunctionBegin;
2811b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2812b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
2813d763cef2SBarry Smith   for (i=0; i<n; i++) {
28140910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2815d763cef2SBarry Smith   }
2816d763cef2SBarry Smith   PetscFunctionReturn(0);
2817d763cef2SBarry Smith }
2818d763cef2SBarry Smith 
2819d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
28204a2ae208SSatish Balay #undef __FUNCT__
2821a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2822d763cef2SBarry Smith /*@C
2823a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2824a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2825d763cef2SBarry Smith 
2826d763cef2SBarry Smith    Collective on TS
2827d763cef2SBarry Smith 
2828d763cef2SBarry Smith    Input Parameters:
2829d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2830d763cef2SBarry Smith .  label - the title to put in the title bar
28317c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2832a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2833a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2834d763cef2SBarry Smith 
2835d763cef2SBarry Smith    Output Parameter:
28360b039ecaSBarry Smith .  ctx - the context
2837d763cef2SBarry Smith 
2838d763cef2SBarry Smith    Options Database Key:
28394f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
28404f09c107SBarry Smith .  -ts_monitor_lg_solution -
284199fdda47SBarry Smith .  -ts_monitor_lg_error -
284299fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
284399fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
284499fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2845d763cef2SBarry Smith 
2846d763cef2SBarry Smith    Notes:
2847a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2848d763cef2SBarry Smith 
2849d763cef2SBarry Smith    Level: intermediate
2850d763cef2SBarry Smith 
28517c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2852d763cef2SBarry Smith 
28534f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
28547c922b88SBarry Smith 
2855d763cef2SBarry Smith @*/
2856a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2857d763cef2SBarry Smith {
2858b0a32e0cSBarry Smith   PetscDraw      win;
2859dfbe8321SBarry Smith   PetscErrorCode ierr;
2860d763cef2SBarry Smith 
2861d763cef2SBarry Smith   PetscFunctionBegin;
2862201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2863a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
28643fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
28650b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
28663bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr);
2867287de1a7SBarry Smith   ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr);
2868287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
2869a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2870d763cef2SBarry Smith   PetscFunctionReturn(0);
2871d763cef2SBarry Smith }
2872d763cef2SBarry Smith 
28734a2ae208SSatish Balay #undef __FUNCT__
28744f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
2875b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
2876d763cef2SBarry Smith {
28770b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2878c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2879dfbe8321SBarry Smith   PetscErrorCode ierr;
2880d763cef2SBarry Smith 
2881d763cef2SBarry Smith   PetscFunctionBegin;
2882b06615a5SLisandro Dalcin   if (!step) {
2883a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2884a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2885a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2886a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2887287de1a7SBarry Smith     ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr);
2888a9f9c1f6SBarry Smith   }
2889c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
28900b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
2891b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
28920b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
28933923b477SBarry Smith   }
2894d763cef2SBarry Smith   PetscFunctionReturn(0);
2895d763cef2SBarry Smith }
2896d763cef2SBarry Smith 
28974a2ae208SSatish Balay #undef __FUNCT__
2898a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2899d763cef2SBarry Smith /*@C
2900a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2901a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2902d763cef2SBarry Smith 
29030b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2904d763cef2SBarry Smith 
2905d763cef2SBarry Smith    Input Parameter:
29060b039ecaSBarry Smith .  ctx - the monitor context
2907d763cef2SBarry Smith 
2908d763cef2SBarry Smith    Level: intermediate
2909d763cef2SBarry Smith 
2910d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2911d763cef2SBarry Smith 
29124f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2913d763cef2SBarry Smith @*/
2914a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2915d763cef2SBarry Smith {
2916b0a32e0cSBarry Smith   PetscDraw      draw;
2917dfbe8321SBarry Smith   PetscErrorCode ierr;
2918d763cef2SBarry Smith 
2919d763cef2SBarry Smith   PetscFunctionBegin;
29200b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
29216bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
29220b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
292331152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
2924387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
2925387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
2926387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
29270b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2928d763cef2SBarry Smith   PetscFunctionReturn(0);
2929d763cef2SBarry Smith }
2930d763cef2SBarry Smith 
29314a2ae208SSatish Balay #undef __FUNCT__
29324a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2933d763cef2SBarry Smith /*@
2934b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2935d763cef2SBarry Smith 
2936d763cef2SBarry Smith    Not Collective
2937d763cef2SBarry Smith 
2938d763cef2SBarry Smith    Input Parameter:
2939d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2940d763cef2SBarry Smith 
2941d763cef2SBarry Smith    Output Parameter:
2942d763cef2SBarry Smith .  t  - the current time
2943d763cef2SBarry Smith 
2944d763cef2SBarry Smith    Level: beginner
2945d763cef2SBarry Smith 
2946b8123daeSJed Brown    Note:
2947b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
29489be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2949b8123daeSJed Brown 
2950d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2951d763cef2SBarry Smith 
2952d763cef2SBarry Smith .keywords: TS, get, time
2953d763cef2SBarry Smith @*/
29547087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2955d763cef2SBarry Smith {
2956d763cef2SBarry Smith   PetscFunctionBegin;
29570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2958f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2959d763cef2SBarry Smith   *t = ts->ptime;
2960d763cef2SBarry Smith   PetscFunctionReturn(0);
2961d763cef2SBarry Smith }
2962d763cef2SBarry Smith 
29634a2ae208SSatish Balay #undef __FUNCT__
29646a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
29656a4d4014SLisandro Dalcin /*@
29666a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
29676a4d4014SLisandro Dalcin 
29683f9fe445SBarry Smith    Logically Collective on TS
29696a4d4014SLisandro Dalcin 
29706a4d4014SLisandro Dalcin    Input Parameters:
29716a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
29726a4d4014SLisandro Dalcin -  time - the time
29736a4d4014SLisandro Dalcin 
29746a4d4014SLisandro Dalcin    Level: intermediate
29756a4d4014SLisandro Dalcin 
29766a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
29776a4d4014SLisandro Dalcin 
29786a4d4014SLisandro Dalcin .keywords: TS, set, time
29796a4d4014SLisandro Dalcin @*/
29807087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
29816a4d4014SLisandro Dalcin {
29826a4d4014SLisandro Dalcin   PetscFunctionBegin;
29830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2984c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
29856a4d4014SLisandro Dalcin   ts->ptime = t;
29866a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
29876a4d4014SLisandro Dalcin }
29886a4d4014SLisandro Dalcin 
29896a4d4014SLisandro Dalcin #undef __FUNCT__
29904a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2991d763cef2SBarry Smith /*@C
2992d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2993d763cef2SBarry Smith    TS options in the database.
2994d763cef2SBarry Smith 
29953f9fe445SBarry Smith    Logically Collective on TS
2996d763cef2SBarry Smith 
2997d763cef2SBarry Smith    Input Parameter:
2998d763cef2SBarry Smith +  ts     - The TS context
2999d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3000d763cef2SBarry Smith 
3001d763cef2SBarry Smith    Notes:
3002d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3003d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3004d763cef2SBarry Smith    hyphen.
3005d763cef2SBarry Smith 
3006d763cef2SBarry Smith    Level: advanced
3007d763cef2SBarry Smith 
3008d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
3009d763cef2SBarry Smith 
3010d763cef2SBarry Smith .seealso: TSSetFromOptions()
3011d763cef2SBarry Smith 
3012d763cef2SBarry Smith @*/
30137087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
3014d763cef2SBarry Smith {
3015dfbe8321SBarry Smith   PetscErrorCode ierr;
3016089b2837SJed Brown   SNES           snes;
3017d763cef2SBarry Smith 
3018d763cef2SBarry Smith   PetscFunctionBegin;
30190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3020d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3021089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3022089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3023d763cef2SBarry Smith   PetscFunctionReturn(0);
3024d763cef2SBarry Smith }
3025d763cef2SBarry Smith 
3026d763cef2SBarry Smith 
30274a2ae208SSatish Balay #undef __FUNCT__
30284a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
3029d763cef2SBarry Smith /*@C
3030d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
3031d763cef2SBarry Smith    TS options in the database.
3032d763cef2SBarry Smith 
30333f9fe445SBarry Smith    Logically Collective on TS
3034d763cef2SBarry Smith 
3035d763cef2SBarry Smith    Input Parameter:
3036d763cef2SBarry Smith +  ts     - The TS context
3037d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3038d763cef2SBarry Smith 
3039d763cef2SBarry Smith    Notes:
3040d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3041d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3042d763cef2SBarry Smith    hyphen.
3043d763cef2SBarry Smith 
3044d763cef2SBarry Smith    Level: advanced
3045d763cef2SBarry Smith 
3046d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
3047d763cef2SBarry Smith 
3048d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
3049d763cef2SBarry Smith 
3050d763cef2SBarry Smith @*/
30517087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
3052d763cef2SBarry Smith {
3053dfbe8321SBarry Smith   PetscErrorCode ierr;
3054089b2837SJed Brown   SNES           snes;
3055d763cef2SBarry Smith 
3056d763cef2SBarry Smith   PetscFunctionBegin;
30570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3058d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3059089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3060089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3061d763cef2SBarry Smith   PetscFunctionReturn(0);
3062d763cef2SBarry Smith }
3063d763cef2SBarry Smith 
30644a2ae208SSatish Balay #undef __FUNCT__
30654a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
3066d763cef2SBarry Smith /*@C
3067d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
3068d763cef2SBarry Smith    TS options in the database.
3069d763cef2SBarry Smith 
3070d763cef2SBarry Smith    Not Collective
3071d763cef2SBarry Smith 
3072d763cef2SBarry Smith    Input Parameter:
3073d763cef2SBarry Smith .  ts - The TS context
3074d763cef2SBarry Smith 
3075d763cef2SBarry Smith    Output Parameter:
3076d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
3077d763cef2SBarry Smith 
3078d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
3079d763cef2SBarry Smith    sufficient length to hold the prefix.
3080d763cef2SBarry Smith 
3081d763cef2SBarry Smith    Level: intermediate
3082d763cef2SBarry Smith 
3083d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
3084d763cef2SBarry Smith 
3085d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
3086d763cef2SBarry Smith @*/
30877087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
3088d763cef2SBarry Smith {
3089dfbe8321SBarry Smith   PetscErrorCode ierr;
3090d763cef2SBarry Smith 
3091d763cef2SBarry Smith   PetscFunctionBegin;
30920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30934482741eSBarry Smith   PetscValidPointer(prefix,2);
3094d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3095d763cef2SBarry Smith   PetscFunctionReturn(0);
3096d763cef2SBarry Smith }
3097d763cef2SBarry Smith 
30984a2ae208SSatish Balay #undef __FUNCT__
30994a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3100d763cef2SBarry Smith /*@C
3101d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3102d763cef2SBarry Smith 
3103d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3104d763cef2SBarry Smith 
3105d763cef2SBarry Smith    Input Parameter:
3106d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3107d763cef2SBarry Smith 
3108d763cef2SBarry Smith    Output Parameters:
3109e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3110e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3111e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3112e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3113d763cef2SBarry Smith 
31140298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3115d763cef2SBarry Smith 
3116d763cef2SBarry Smith    Level: intermediate
3117d763cef2SBarry Smith 
311826d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3119d763cef2SBarry Smith 
3120d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3121d763cef2SBarry Smith @*/
3122e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3123d763cef2SBarry Smith {
3124089b2837SJed Brown   PetscErrorCode ierr;
3125089b2837SJed Brown   SNES           snes;
312624989b8cSPeter Brune   DM             dm;
3127089b2837SJed Brown 
3128d763cef2SBarry Smith   PetscFunctionBegin;
3129089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3130e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
313124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
313224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3133d763cef2SBarry Smith   PetscFunctionReturn(0);
3134d763cef2SBarry Smith }
3135d763cef2SBarry Smith 
31361713a123SBarry Smith #undef __FUNCT__
31372eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
31382eca1d9cSJed Brown /*@C
31392eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
31402eca1d9cSJed Brown 
31412eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
31422eca1d9cSJed Brown 
31432eca1d9cSJed Brown    Input Parameter:
31442eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
31452eca1d9cSJed Brown 
31462eca1d9cSJed Brown    Output Parameters:
3147e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3148e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
31492eca1d9cSJed Brown .  f   - The function to compute the matrices
31502eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
31512eca1d9cSJed Brown 
31520298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
31532eca1d9cSJed Brown 
31542eca1d9cSJed Brown    Level: advanced
31552eca1d9cSJed Brown 
31562eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
31572eca1d9cSJed Brown 
31582eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
31592eca1d9cSJed Brown @*/
3160e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
31612eca1d9cSJed Brown {
3162089b2837SJed Brown   PetscErrorCode ierr;
3163089b2837SJed Brown   SNES           snes;
316424989b8cSPeter Brune   DM             dm;
31650910c330SBarry Smith 
31662eca1d9cSJed Brown   PetscFunctionBegin;
3167089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3168f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3169e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
317024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
317124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
31722eca1d9cSJed Brown   PetscFunctionReturn(0);
31732eca1d9cSJed Brown }
31742eca1d9cSJed Brown 
31756083293cSBarry Smith 
31762eca1d9cSJed Brown #undef __FUNCT__
317783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
31781713a123SBarry Smith /*@C
317983a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
31801713a123SBarry Smith    VecView() for the solution at each timestep
31811713a123SBarry Smith 
31821713a123SBarry Smith    Collective on TS
31831713a123SBarry Smith 
31841713a123SBarry Smith    Input Parameters:
31851713a123SBarry Smith +  ts - the TS context
31861713a123SBarry Smith .  step - current time-step
3187142b95e3SSatish Balay .  ptime - current time
31880298fd71SBarry Smith -  dummy - either a viewer or NULL
31891713a123SBarry Smith 
319099fdda47SBarry Smith    Options Database:
319199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
319299fdda47SBarry Smith 
3193387f4636SBarry Smith    Notes: the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
319499fdda47SBarry Smith        will look bad
319599fdda47SBarry Smith 
31961713a123SBarry Smith    Level: intermediate
31971713a123SBarry Smith 
31981713a123SBarry Smith .keywords: TS,  vector, monitor, view
31991713a123SBarry Smith 
3200a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
32011713a123SBarry Smith @*/
32020910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
32031713a123SBarry Smith {
3204dfbe8321SBarry Smith   PetscErrorCode   ierr;
320583a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3206473a3ab2SBarry Smith   PetscDraw        draw;
32071713a123SBarry Smith 
32081713a123SBarry Smith   PetscFunctionBegin;
32096083293cSBarry Smith   if (!step && ictx->showinitial) {
32106083293cSBarry Smith     if (!ictx->initialsolution) {
32110910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
32121713a123SBarry Smith     }
32130910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
32146083293cSBarry Smith   }
3215b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
32160dcf80beSBarry Smith 
32176083293cSBarry Smith   if (ictx->showinitial) {
32186083293cSBarry Smith     PetscReal pause;
32196083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
32206083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
32216083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
32226083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
32236083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
32246083293cSBarry Smith   }
32250910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3226473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3227473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3228473a3ab2SBarry Smith     char      time[32];
3229473a3ab2SBarry Smith     size_t    len;
3230473a3ab2SBarry Smith 
3231473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3232473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3233473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3234473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
32350298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3236473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3237473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3238473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3239473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3240473a3ab2SBarry Smith   }
3241473a3ab2SBarry Smith 
32426083293cSBarry Smith   if (ictx->showinitial) {
32436083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
32446083293cSBarry Smith   }
32451713a123SBarry Smith   PetscFunctionReturn(0);
32461713a123SBarry Smith }
32471713a123SBarry Smith 
32482d5ee99bSBarry Smith #undef __FUNCT__
32492d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
32502d5ee99bSBarry Smith /*@C
32512d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
32522d5ee99bSBarry Smith 
32532d5ee99bSBarry Smith    Collective on TS
32542d5ee99bSBarry Smith 
32552d5ee99bSBarry Smith    Input Parameters:
32562d5ee99bSBarry Smith +  ts - the TS context
32572d5ee99bSBarry Smith .  step - current time-step
32582d5ee99bSBarry Smith .  ptime - current time
32592d5ee99bSBarry Smith -  dummy - either a viewer or NULL
32602d5ee99bSBarry Smith 
32612d5ee99bSBarry Smith    Level: intermediate
32622d5ee99bSBarry Smith 
32632d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
32642d5ee99bSBarry Smith 
32652d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
32662d5ee99bSBarry Smith @*/
32672d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
32682d5ee99bSBarry Smith {
32692d5ee99bSBarry Smith   PetscErrorCode    ierr;
32702d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
32712d5ee99bSBarry Smith   PetscDraw         draw;
32722d5ee99bSBarry Smith   MPI_Comm          comm;
32732d5ee99bSBarry Smith   PetscInt          n;
32742d5ee99bSBarry Smith   PetscMPIInt       size;
32752d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
32762d5ee99bSBarry Smith   char              time[32];
32772d5ee99bSBarry Smith   size_t            len;
32782d5ee99bSBarry Smith   const PetscScalar *U;
32792d5ee99bSBarry Smith 
32802d5ee99bSBarry Smith   PetscFunctionBegin;
32812d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
32822d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
32832d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
32842d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
32852d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
32862d5ee99bSBarry Smith 
32872d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
32882d5ee99bSBarry Smith 
32892d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
32904b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3291ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3292a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3293a4494fc1SBarry Smith       PetscFunctionReturn(0);
3294a4494fc1SBarry Smith   }
32952d5ee99bSBarry Smith   if (!step) ictx->color++;
32962d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
32972d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
32982d5ee99bSBarry Smith 
32992d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
33004b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
33012d5ee99bSBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
33022d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
33032d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
33042d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
33052d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
33062d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
33072d5ee99bSBarry Smith   }
33082d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
33092d5ee99bSBarry Smith   PetscFunctionReturn(0);
33102d5ee99bSBarry Smith }
33112d5ee99bSBarry Smith 
33121713a123SBarry Smith 
33136c699258SBarry Smith #undef __FUNCT__
331483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
33156083293cSBarry Smith /*@C
331683a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
33176083293cSBarry Smith 
33186083293cSBarry Smith    Collective on TS
33196083293cSBarry Smith 
33206083293cSBarry Smith    Input Parameters:
33216083293cSBarry Smith .    ctx - the monitor context
33226083293cSBarry Smith 
33236083293cSBarry Smith    Level: intermediate
33246083293cSBarry Smith 
33256083293cSBarry Smith .keywords: TS,  vector, monitor, view
33266083293cSBarry Smith 
332783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
33286083293cSBarry Smith @*/
332983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
33306083293cSBarry Smith {
33316083293cSBarry Smith   PetscErrorCode ierr;
33326083293cSBarry Smith 
33336083293cSBarry Smith   PetscFunctionBegin;
33344b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
333583a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
333683a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
333783a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
33386083293cSBarry Smith   PetscFunctionReturn(0);
33396083293cSBarry Smith }
33406083293cSBarry Smith 
33416083293cSBarry Smith #undef __FUNCT__
334283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
33436083293cSBarry Smith /*@C
334483a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
33456083293cSBarry Smith 
33466083293cSBarry Smith    Collective on TS
33476083293cSBarry Smith 
33486083293cSBarry Smith    Input Parameter:
33496083293cSBarry Smith .    ts - time-step context
33506083293cSBarry Smith 
33516083293cSBarry Smith    Output Patameter:
33526083293cSBarry Smith .    ctx - the monitor context
33536083293cSBarry Smith 
335499fdda47SBarry Smith    Options Database:
335599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
335699fdda47SBarry Smith 
33576083293cSBarry Smith    Level: intermediate
33586083293cSBarry Smith 
33596083293cSBarry Smith .keywords: TS,  vector, monitor, view
33606083293cSBarry Smith 
336183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
33626083293cSBarry Smith @*/
336383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
33646083293cSBarry Smith {
33656083293cSBarry Smith   PetscErrorCode   ierr;
33666083293cSBarry Smith 
33676083293cSBarry Smith   PetscFunctionBegin;
336883a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
336983a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3370e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3371bbd56ea5SKarl Rupp 
337283a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3373473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
33740298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3375473a3ab2SBarry Smith 
3376473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
33770298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
33782d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
33796083293cSBarry Smith   PetscFunctionReturn(0);
33806083293cSBarry Smith }
33816083293cSBarry Smith 
33826083293cSBarry Smith #undef __FUNCT__
338383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
33843a471f94SBarry Smith /*@C
338583a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
33863a471f94SBarry Smith    VecView() for the error at each timestep
33873a471f94SBarry Smith 
33883a471f94SBarry Smith    Collective on TS
33893a471f94SBarry Smith 
33903a471f94SBarry Smith    Input Parameters:
33913a471f94SBarry Smith +  ts - the TS context
33923a471f94SBarry Smith .  step - current time-step
33933a471f94SBarry Smith .  ptime - current time
33940298fd71SBarry Smith -  dummy - either a viewer or NULL
33953a471f94SBarry Smith 
33963a471f94SBarry Smith    Level: intermediate
33973a471f94SBarry Smith 
33983a471f94SBarry Smith .keywords: TS,  vector, monitor, view
33993a471f94SBarry Smith 
34003a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
34013a471f94SBarry Smith @*/
34020910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
34033a471f94SBarry Smith {
34043a471f94SBarry Smith   PetscErrorCode   ierr;
340583a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
340683a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
34073a471f94SBarry Smith   Vec              work;
34083a471f94SBarry Smith 
34093a471f94SBarry Smith   PetscFunctionBegin;
3410b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
34110910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
34123a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
34130910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
34143a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
34153a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
34163a471f94SBarry Smith   PetscFunctionReturn(0);
34173a471f94SBarry Smith }
34183a471f94SBarry Smith 
34192a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
34203a471f94SBarry Smith #undef __FUNCT__
34216c699258SBarry Smith #define __FUNCT__ "TSSetDM"
34226c699258SBarry Smith /*@
34236c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
34246c699258SBarry Smith 
34253f9fe445SBarry Smith    Logically Collective on TS and DM
34266c699258SBarry Smith 
34276c699258SBarry Smith    Input Parameters:
34286c699258SBarry Smith +  ts - the preconditioner context
34296c699258SBarry Smith -  dm - the dm
34306c699258SBarry Smith 
34316c699258SBarry Smith    Level: intermediate
34326c699258SBarry Smith 
34336c699258SBarry Smith 
34346c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
34356c699258SBarry Smith @*/
34367087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
34376c699258SBarry Smith {
34386c699258SBarry Smith   PetscErrorCode ierr;
3439089b2837SJed Brown   SNES           snes;
3440942e3340SBarry Smith   DMTS           tsdm;
34416c699258SBarry Smith 
34426c699258SBarry Smith   PetscFunctionBegin;
34430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
344470663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3445942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
34462a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3447942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3448942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
344924989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
345024989b8cSPeter Brune         tsdm->originaldm = dm;
345124989b8cSPeter Brune       }
345224989b8cSPeter Brune     }
34536bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
345424989b8cSPeter Brune   }
34556c699258SBarry Smith   ts->dm = dm;
3456bbd56ea5SKarl Rupp 
3457089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3458089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
34596c699258SBarry Smith   PetscFunctionReturn(0);
34606c699258SBarry Smith }
34616c699258SBarry Smith 
34626c699258SBarry Smith #undef __FUNCT__
34636c699258SBarry Smith #define __FUNCT__ "TSGetDM"
34646c699258SBarry Smith /*@
34656c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
34666c699258SBarry Smith 
34673f9fe445SBarry Smith    Not Collective
34686c699258SBarry Smith 
34696c699258SBarry Smith    Input Parameter:
34706c699258SBarry Smith . ts - the preconditioner context
34716c699258SBarry Smith 
34726c699258SBarry Smith    Output Parameter:
34736c699258SBarry Smith .  dm - the dm
34746c699258SBarry Smith 
34756c699258SBarry Smith    Level: intermediate
34766c699258SBarry Smith 
34776c699258SBarry Smith 
34786c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
34796c699258SBarry Smith @*/
34807087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
34816c699258SBarry Smith {
3482496e6a7aSJed Brown   PetscErrorCode ierr;
3483496e6a7aSJed Brown 
34846c699258SBarry Smith   PetscFunctionBegin;
34850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3486496e6a7aSJed Brown   if (!ts->dm) {
3487ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
3488496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3489496e6a7aSJed Brown   }
34906c699258SBarry Smith   *dm = ts->dm;
34916c699258SBarry Smith   PetscFunctionReturn(0);
34926c699258SBarry Smith }
34931713a123SBarry Smith 
34940f5c6efeSJed Brown #undef __FUNCT__
34950f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
34960f5c6efeSJed Brown /*@
34970f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
34980f5c6efeSJed Brown 
34993f9fe445SBarry Smith    Logically Collective on SNES
35000f5c6efeSJed Brown 
35010f5c6efeSJed Brown    Input Parameter:
3502d42a1c89SJed Brown + snes - nonlinear solver
35030910c330SBarry Smith . U - the current state at which to evaluate the residual
3504d42a1c89SJed Brown - ctx - user context, must be a TS
35050f5c6efeSJed Brown 
35060f5c6efeSJed Brown    Output Parameter:
35070f5c6efeSJed Brown . F - the nonlinear residual
35080f5c6efeSJed Brown 
35090f5c6efeSJed Brown    Notes:
35100f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
35110f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
35120f5c6efeSJed Brown 
35130f5c6efeSJed Brown    Level: advanced
35140f5c6efeSJed Brown 
35150f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
35160f5c6efeSJed Brown @*/
35170910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
35180f5c6efeSJed Brown {
35190f5c6efeSJed Brown   TS             ts = (TS)ctx;
35200f5c6efeSJed Brown   PetscErrorCode ierr;
35210f5c6efeSJed Brown 
35220f5c6efeSJed Brown   PetscFunctionBegin;
35230f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
35240910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
35250f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
35260f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
35270910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
35280f5c6efeSJed Brown   PetscFunctionReturn(0);
35290f5c6efeSJed Brown }
35300f5c6efeSJed Brown 
35310f5c6efeSJed Brown #undef __FUNCT__
35320f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
35330f5c6efeSJed Brown /*@
35340f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
35350f5c6efeSJed Brown 
35360f5c6efeSJed Brown    Collective on SNES
35370f5c6efeSJed Brown 
35380f5c6efeSJed Brown    Input Parameter:
35390f5c6efeSJed Brown + snes - nonlinear solver
35400910c330SBarry Smith . U - the current state at which to evaluate the residual
35410f5c6efeSJed Brown - ctx - user context, must be a TS
35420f5c6efeSJed Brown 
35430f5c6efeSJed Brown    Output Parameter:
35440f5c6efeSJed Brown + A - the Jacobian
35450f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
35460f5c6efeSJed Brown - flag - indicates any structure change in the matrix
35470f5c6efeSJed Brown 
35480f5c6efeSJed Brown    Notes:
35490f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
35500f5c6efeSJed Brown 
35510f5c6efeSJed Brown    Level: developer
35520f5c6efeSJed Brown 
35530f5c6efeSJed Brown .seealso: SNESSetJacobian()
35540f5c6efeSJed Brown @*/
35550910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
35560f5c6efeSJed Brown {
35570f5c6efeSJed Brown   TS             ts = (TS)ctx;
35580f5c6efeSJed Brown   PetscErrorCode ierr;
35590f5c6efeSJed Brown 
35600f5c6efeSJed Brown   PetscFunctionBegin;
35610f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
35620910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
35630f5c6efeSJed Brown   PetscValidPointer(A,3);
35640f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
35650f5c6efeSJed Brown   PetscValidPointer(B,4);
35660f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
35670f5c6efeSJed Brown   PetscValidPointer(flag,5);
35680f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
35690910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
35700f5c6efeSJed Brown   PetscFunctionReturn(0);
35710f5c6efeSJed Brown }
3572325fc9f4SBarry Smith 
35730e4ef248SJed Brown #undef __FUNCT__
35740e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
35750e4ef248SJed Brown /*@C
35760e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
35770e4ef248SJed Brown 
35780e4ef248SJed Brown    Collective on TS
35790e4ef248SJed Brown 
35800e4ef248SJed Brown    Input Arguments:
35810e4ef248SJed Brown +  ts - time stepping context
35820e4ef248SJed Brown .  t - time at which to evaluate
35830910c330SBarry Smith .  U - state at which to evaluate
35840e4ef248SJed Brown -  ctx - context
35850e4ef248SJed Brown 
35860e4ef248SJed Brown    Output Arguments:
35870e4ef248SJed Brown .  F - right hand side
35880e4ef248SJed Brown 
35890e4ef248SJed Brown    Level: intermediate
35900e4ef248SJed Brown 
35910e4ef248SJed Brown    Notes:
35920e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
35930e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
35940e4ef248SJed Brown 
35950e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
35960e4ef248SJed Brown @*/
35970910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
35980e4ef248SJed Brown {
35990e4ef248SJed Brown   PetscErrorCode ierr;
36000e4ef248SJed Brown   Mat            Arhs,Brhs;
36010e4ef248SJed Brown   MatStructure   flg2;
36020e4ef248SJed Brown 
36030e4ef248SJed Brown   PetscFunctionBegin;
36040e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
36050910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
36060910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
36070e4ef248SJed Brown   PetscFunctionReturn(0);
36080e4ef248SJed Brown }
36090e4ef248SJed Brown 
36100e4ef248SJed Brown #undef __FUNCT__
36110e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
36120e4ef248SJed Brown /*@C
36130e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
36140e4ef248SJed Brown 
36150e4ef248SJed Brown    Collective on TS
36160e4ef248SJed Brown 
36170e4ef248SJed Brown    Input Arguments:
36180e4ef248SJed Brown +  ts - time stepping context
36190e4ef248SJed Brown .  t - time at which to evaluate
36200910c330SBarry Smith .  U - state at which to evaluate
36210e4ef248SJed Brown -  ctx - context
36220e4ef248SJed Brown 
36230e4ef248SJed Brown    Output Arguments:
36240e4ef248SJed Brown +  A - pointer to operator
36250e4ef248SJed Brown .  B - pointer to preconditioning matrix
36260e4ef248SJed Brown -  flg - matrix structure flag
36270e4ef248SJed Brown 
36280e4ef248SJed Brown    Level: intermediate
36290e4ef248SJed Brown 
36300e4ef248SJed Brown    Notes:
36310e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
36320e4ef248SJed Brown 
36330e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
36340e4ef248SJed Brown @*/
36350910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
36360e4ef248SJed Brown {
36370e4ef248SJed Brown   PetscFunctionBegin;
36380e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
36390e4ef248SJed Brown   PetscFunctionReturn(0);
36400e4ef248SJed Brown }
36410e4ef248SJed Brown 
36420026cea9SSean Farley #undef __FUNCT__
36430026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
36440026cea9SSean Farley /*@C
36450026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
36460026cea9SSean Farley 
36470026cea9SSean Farley    Collective on TS
36480026cea9SSean Farley 
36490026cea9SSean Farley    Input Arguments:
36500026cea9SSean Farley +  ts - time stepping context
36510026cea9SSean Farley .  t - time at which to evaluate
36520910c330SBarry Smith .  U - state at which to evaluate
36530910c330SBarry Smith .  Udot - time derivative of state vector
36540026cea9SSean Farley -  ctx - context
36550026cea9SSean Farley 
36560026cea9SSean Farley    Output Arguments:
36570026cea9SSean Farley .  F - left hand side
36580026cea9SSean Farley 
36590026cea9SSean Farley    Level: intermediate
36600026cea9SSean Farley 
36610026cea9SSean Farley    Notes:
36620910c330SBarry 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
36630026cea9SSean Farley    user is required to write their own TSComputeIFunction.
36640026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
36650026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
36660026cea9SSean Farley 
36670026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
36680026cea9SSean Farley @*/
36690910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
36700026cea9SSean Farley {
36710026cea9SSean Farley   PetscErrorCode ierr;
36720026cea9SSean Farley   Mat            A,B;
36730026cea9SSean Farley   MatStructure   flg2;
36740026cea9SSean Farley 
36750026cea9SSean Farley   PetscFunctionBegin;
36760298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
36770910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
36780910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
36790026cea9SSean Farley   PetscFunctionReturn(0);
36800026cea9SSean Farley }
36810026cea9SSean Farley 
36820026cea9SSean Farley #undef __FUNCT__
36830026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
36840026cea9SSean Farley /*@C
3685b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
36860026cea9SSean Farley 
36870026cea9SSean Farley    Collective on TS
36880026cea9SSean Farley 
36890026cea9SSean Farley    Input Arguments:
36900026cea9SSean Farley +  ts - time stepping context
36910026cea9SSean Farley .  t - time at which to evaluate
36920910c330SBarry Smith .  U - state at which to evaluate
36930910c330SBarry Smith .  Udot - time derivative of state vector
36940026cea9SSean Farley .  shift - shift to apply
36950026cea9SSean Farley -  ctx - context
36960026cea9SSean Farley 
36970026cea9SSean Farley    Output Arguments:
36980026cea9SSean Farley +  A - pointer to operator
36990026cea9SSean Farley .  B - pointer to preconditioning matrix
37000026cea9SSean Farley -  flg - matrix structure flag
37010026cea9SSean Farley 
3702b41af12eSJed Brown    Level: advanced
37030026cea9SSean Farley 
37040026cea9SSean Farley    Notes:
37050026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
37060026cea9SSean Farley 
3707b41af12eSJed Brown    It is only appropriate for problems of the form
3708b41af12eSJed Brown 
3709b41af12eSJed Brown $     M Udot = F(U,t)
3710b41af12eSJed Brown 
3711b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
3712b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
3713b41af12eSJed Brown   an implicit operator of the form
3714b41af12eSJed Brown 
3715b41af12eSJed Brown $    shift*M + J
3716b41af12eSJed Brown 
3717b41af12eSJed 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
3718b41af12eSJed Brown   a copy of M or reassemble it when requested.
3719b41af12eSJed Brown 
37200026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
37210026cea9SSean Farley @*/
37220910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
37230026cea9SSean Farley {
3724b41af12eSJed Brown   PetscErrorCode ierr;
3725b41af12eSJed Brown 
37260026cea9SSean Farley   PetscFunctionBegin;
3727b41af12eSJed Brown   ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
3728b41af12eSJed Brown   ts->ijacobian.shift = shift;
37290026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
37300026cea9SSean Farley   PetscFunctionReturn(0);
37310026cea9SSean Farley }
3732b41af12eSJed Brown 
3733e817cc15SEmil Constantinescu #undef __FUNCT__
3734e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3735e817cc15SEmil Constantinescu /*@
3736e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3737e817cc15SEmil Constantinescu 
3738e817cc15SEmil Constantinescu    Not Collective
3739e817cc15SEmil Constantinescu 
3740e817cc15SEmil Constantinescu    Input Parameter:
3741e817cc15SEmil Constantinescu .  ts - the TS context
3742e817cc15SEmil Constantinescu 
3743e817cc15SEmil Constantinescu    Output Parameter:
37444e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3745e817cc15SEmil Constantinescu 
3746e817cc15SEmil Constantinescu    Level: beginner
3747e817cc15SEmil Constantinescu 
3748e817cc15SEmil Constantinescu .keywords: TS, equation type
3749e817cc15SEmil Constantinescu 
3750e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3751e817cc15SEmil Constantinescu @*/
3752e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3753e817cc15SEmil Constantinescu {
3754e817cc15SEmil Constantinescu   PetscFunctionBegin;
3755e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3756e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3757e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3758e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3759e817cc15SEmil Constantinescu }
3760e817cc15SEmil Constantinescu 
3761e817cc15SEmil Constantinescu #undef __FUNCT__
3762e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3763e817cc15SEmil Constantinescu /*@
3764e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3765e817cc15SEmil Constantinescu 
3766e817cc15SEmil Constantinescu    Not Collective
3767e817cc15SEmil Constantinescu 
3768e817cc15SEmil Constantinescu    Input Parameter:
3769e817cc15SEmil Constantinescu +  ts - the TS context
37704e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3771e817cc15SEmil Constantinescu 
3772e817cc15SEmil Constantinescu    Level: advanced
3773e817cc15SEmil Constantinescu 
3774e817cc15SEmil Constantinescu .keywords: TS, equation type
3775e817cc15SEmil Constantinescu 
3776e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3777e817cc15SEmil Constantinescu @*/
3778e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3779e817cc15SEmil Constantinescu {
3780e817cc15SEmil Constantinescu   PetscFunctionBegin;
3781e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3782e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3783e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3784e817cc15SEmil Constantinescu }
37850026cea9SSean Farley 
37864af1b03aSJed Brown #undef __FUNCT__
37874af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
37884af1b03aSJed Brown /*@
37894af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
37904af1b03aSJed Brown 
37914af1b03aSJed Brown    Not Collective
37924af1b03aSJed Brown 
37934af1b03aSJed Brown    Input Parameter:
37944af1b03aSJed Brown .  ts - the TS context
37954af1b03aSJed Brown 
37964af1b03aSJed Brown    Output Parameter:
37974af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
37984af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
37994af1b03aSJed Brown 
3800487e0bb9SJed Brown    Level: beginner
38014af1b03aSJed Brown 
3802cd652676SJed Brown    Notes:
3803cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
38044af1b03aSJed Brown 
38054af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
38064af1b03aSJed Brown 
38074af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
38084af1b03aSJed Brown @*/
38094af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
38104af1b03aSJed Brown {
38114af1b03aSJed Brown   PetscFunctionBegin;
38124af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38134af1b03aSJed Brown   PetscValidPointer(reason,2);
38144af1b03aSJed Brown   *reason = ts->reason;
38154af1b03aSJed Brown   PetscFunctionReturn(0);
38164af1b03aSJed Brown }
38174af1b03aSJed Brown 
3818fb1732b5SBarry Smith #undef __FUNCT__
3819d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3820d6ad946cSShri Abhyankar /*@
3821d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3822d6ad946cSShri Abhyankar 
3823d6ad946cSShri Abhyankar    Not Collective
3824d6ad946cSShri Abhyankar 
3825d6ad946cSShri Abhyankar    Input Parameter:
3826d6ad946cSShri Abhyankar +  ts - the TS context
3827d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3828d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3829d6ad946cSShri Abhyankar 
3830f5abba47SShri Abhyankar    Level: advanced
3831d6ad946cSShri Abhyankar 
3832d6ad946cSShri Abhyankar    Notes:
3833d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3834d6ad946cSShri Abhyankar 
3835d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3836d6ad946cSShri Abhyankar 
3837d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3838d6ad946cSShri Abhyankar @*/
3839d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3840d6ad946cSShri Abhyankar {
3841d6ad946cSShri Abhyankar   PetscFunctionBegin;
3842d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3843d6ad946cSShri Abhyankar   ts->reason = reason;
3844d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3845d6ad946cSShri Abhyankar }
3846d6ad946cSShri Abhyankar 
3847d6ad946cSShri Abhyankar #undef __FUNCT__
3848cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3849cc708dedSBarry Smith /*@
3850cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3851cc708dedSBarry Smith 
3852cc708dedSBarry Smith    Not Collective
3853cc708dedSBarry Smith 
3854cc708dedSBarry Smith    Input Parameter:
3855cc708dedSBarry Smith .  ts - the TS context
3856cc708dedSBarry Smith 
3857cc708dedSBarry Smith    Output Parameter:
3858cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3859cc708dedSBarry Smith 
3860487e0bb9SJed Brown    Level: beginner
3861cc708dedSBarry Smith 
3862cc708dedSBarry Smith    Notes:
3863cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3864cc708dedSBarry Smith 
3865cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3866cc708dedSBarry Smith 
3867cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3868cc708dedSBarry Smith @*/
3869cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3870cc708dedSBarry Smith {
3871cc708dedSBarry Smith   PetscFunctionBegin;
3872cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3873cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3874cc708dedSBarry Smith   *ftime = ts->solvetime;
3875cc708dedSBarry Smith   PetscFunctionReturn(0);
3876cc708dedSBarry Smith }
3877cc708dedSBarry Smith 
3878cc708dedSBarry Smith #undef __FUNCT__
38795ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
38809f67acb7SJed Brown /*@
38815ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
38829f67acb7SJed Brown    used by the time integrator.
38839f67acb7SJed Brown 
38849f67acb7SJed Brown    Not Collective
38859f67acb7SJed Brown 
38869f67acb7SJed Brown    Input Parameter:
38879f67acb7SJed Brown .  ts - TS context
38889f67acb7SJed Brown 
38899f67acb7SJed Brown    Output Parameter:
38909f67acb7SJed Brown .  nits - number of nonlinear iterations
38919f67acb7SJed Brown 
38929f67acb7SJed Brown    Notes:
38939f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
38949f67acb7SJed Brown 
38959f67acb7SJed Brown    Level: intermediate
38969f67acb7SJed Brown 
38979f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
38989f67acb7SJed Brown 
38995ef26d82SJed Brown .seealso:  TSGetKSPIterations()
39009f67acb7SJed Brown @*/
39015ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
39029f67acb7SJed Brown {
39039f67acb7SJed Brown   PetscFunctionBegin;
39049f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39059f67acb7SJed Brown   PetscValidIntPointer(nits,2);
39065ef26d82SJed Brown   *nits = ts->snes_its;
39079f67acb7SJed Brown   PetscFunctionReturn(0);
39089f67acb7SJed Brown }
39099f67acb7SJed Brown 
39109f67acb7SJed Brown #undef __FUNCT__
39115ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
39129f67acb7SJed Brown /*@
39135ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
39149f67acb7SJed Brown    used by the time integrator.
39159f67acb7SJed Brown 
39169f67acb7SJed Brown    Not Collective
39179f67acb7SJed Brown 
39189f67acb7SJed Brown    Input Parameter:
39199f67acb7SJed Brown .  ts - TS context
39209f67acb7SJed Brown 
39219f67acb7SJed Brown    Output Parameter:
39229f67acb7SJed Brown .  lits - number of linear iterations
39239f67acb7SJed Brown 
39249f67acb7SJed Brown    Notes:
39259f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
39269f67acb7SJed Brown 
39279f67acb7SJed Brown    Level: intermediate
39289f67acb7SJed Brown 
39299f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
39309f67acb7SJed Brown 
39315ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
39329f67acb7SJed Brown @*/
39335ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
39349f67acb7SJed Brown {
39359f67acb7SJed Brown   PetscFunctionBegin;
39369f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39379f67acb7SJed Brown   PetscValidIntPointer(lits,2);
39385ef26d82SJed Brown   *lits = ts->ksp_its;
39399f67acb7SJed Brown   PetscFunctionReturn(0);
39409f67acb7SJed Brown }
39419f67acb7SJed Brown 
39429f67acb7SJed Brown #undef __FUNCT__
3943cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3944cef5090cSJed Brown /*@
3945cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3946cef5090cSJed Brown 
3947cef5090cSJed Brown    Not Collective
3948cef5090cSJed Brown 
3949cef5090cSJed Brown    Input Parameter:
3950cef5090cSJed Brown .  ts - TS context
3951cef5090cSJed Brown 
3952cef5090cSJed Brown    Output Parameter:
3953cef5090cSJed Brown .  rejects - number of steps rejected
3954cef5090cSJed Brown 
3955cef5090cSJed Brown    Notes:
3956cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3957cef5090cSJed Brown 
3958cef5090cSJed Brown    Level: intermediate
3959cef5090cSJed Brown 
3960cef5090cSJed Brown .keywords: TS, get, number
3961cef5090cSJed Brown 
39625ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3963cef5090cSJed Brown @*/
3964cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3965cef5090cSJed Brown {
3966cef5090cSJed Brown   PetscFunctionBegin;
3967cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3968cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3969cef5090cSJed Brown   *rejects = ts->reject;
3970cef5090cSJed Brown   PetscFunctionReturn(0);
3971cef5090cSJed Brown }
3972cef5090cSJed Brown 
3973cef5090cSJed Brown #undef __FUNCT__
3974cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3975cef5090cSJed Brown /*@
3976cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3977cef5090cSJed Brown 
3978cef5090cSJed Brown    Not Collective
3979cef5090cSJed Brown 
3980cef5090cSJed Brown    Input Parameter:
3981cef5090cSJed Brown .  ts - TS context
3982cef5090cSJed Brown 
3983cef5090cSJed Brown    Output Parameter:
3984cef5090cSJed Brown .  fails - number of failed nonlinear solves
3985cef5090cSJed Brown 
3986cef5090cSJed Brown    Notes:
3987cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3988cef5090cSJed Brown 
3989cef5090cSJed Brown    Level: intermediate
3990cef5090cSJed Brown 
3991cef5090cSJed Brown .keywords: TS, get, number
3992cef5090cSJed Brown 
39935ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3994cef5090cSJed Brown @*/
3995cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3996cef5090cSJed Brown {
3997cef5090cSJed Brown   PetscFunctionBegin;
3998cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3999cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4000cef5090cSJed Brown   *fails = ts->num_snes_failures;
4001cef5090cSJed Brown   PetscFunctionReturn(0);
4002cef5090cSJed Brown }
4003cef5090cSJed Brown 
4004cef5090cSJed Brown #undef __FUNCT__
4005cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
4006cef5090cSJed Brown /*@
4007cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4008cef5090cSJed Brown 
4009cef5090cSJed Brown    Not Collective
4010cef5090cSJed Brown 
4011cef5090cSJed Brown    Input Parameter:
4012cef5090cSJed Brown +  ts - TS context
4013cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4014cef5090cSJed Brown 
4015cef5090cSJed Brown    Notes:
4016cef5090cSJed Brown    The counter is reset to zero for each step
4017cef5090cSJed Brown 
4018cef5090cSJed Brown    Options Database Key:
4019cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4020cef5090cSJed Brown 
4021cef5090cSJed Brown    Level: intermediate
4022cef5090cSJed Brown 
4023cef5090cSJed Brown .keywords: TS, set, maximum, number
4024cef5090cSJed Brown 
40255ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4026cef5090cSJed Brown @*/
4027cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4028cef5090cSJed Brown {
4029cef5090cSJed Brown   PetscFunctionBegin;
4030cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4031cef5090cSJed Brown   ts->max_reject = rejects;
4032cef5090cSJed Brown   PetscFunctionReturn(0);
4033cef5090cSJed Brown }
4034cef5090cSJed Brown 
4035cef5090cSJed Brown #undef __FUNCT__
4036cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
4037cef5090cSJed Brown /*@
4038cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4039cef5090cSJed Brown 
4040cef5090cSJed Brown    Not Collective
4041cef5090cSJed Brown 
4042cef5090cSJed Brown    Input Parameter:
4043cef5090cSJed Brown +  ts - TS context
4044cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4045cef5090cSJed Brown 
4046cef5090cSJed Brown    Notes:
4047cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4048cef5090cSJed Brown 
4049cef5090cSJed Brown    Options Database Key:
4050cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4051cef5090cSJed Brown 
4052cef5090cSJed Brown    Level: intermediate
4053cef5090cSJed Brown 
4054cef5090cSJed Brown .keywords: TS, set, maximum, number
4055cef5090cSJed Brown 
40565ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4057cef5090cSJed Brown @*/
4058cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4059cef5090cSJed Brown {
4060cef5090cSJed Brown   PetscFunctionBegin;
4061cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4062cef5090cSJed Brown   ts->max_snes_failures = fails;
4063cef5090cSJed Brown   PetscFunctionReturn(0);
4064cef5090cSJed Brown }
4065cef5090cSJed Brown 
4066cef5090cSJed Brown #undef __FUNCT__
40674e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
4068cef5090cSJed Brown /*@
4069cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4070cef5090cSJed Brown 
4071cef5090cSJed Brown    Not Collective
4072cef5090cSJed Brown 
4073cef5090cSJed Brown    Input Parameter:
4074cef5090cSJed Brown +  ts - TS context
4075cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4076cef5090cSJed Brown 
4077cef5090cSJed Brown    Options Database Key:
4078cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4079cef5090cSJed Brown 
4080cef5090cSJed Brown    Level: intermediate
4081cef5090cSJed Brown 
4082cef5090cSJed Brown .keywords: TS, set, error
4083cef5090cSJed Brown 
40845ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4085cef5090cSJed Brown @*/
4086cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4087cef5090cSJed Brown {
4088cef5090cSJed Brown   PetscFunctionBegin;
4089cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4090cef5090cSJed Brown   ts->errorifstepfailed = err;
4091cef5090cSJed Brown   PetscFunctionReturn(0);
4092cef5090cSJed Brown }
4093cef5090cSJed Brown 
4094cef5090cSJed Brown #undef __FUNCT__
4095fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
4096fb1732b5SBarry Smith /*@C
4097fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
4098fb1732b5SBarry Smith 
4099fb1732b5SBarry Smith    Collective on TS
4100fb1732b5SBarry Smith 
4101fb1732b5SBarry Smith    Input Parameters:
4102fb1732b5SBarry Smith +  ts - the TS context
4103fb1732b5SBarry Smith .  step - current time-step
4104fb1732b5SBarry Smith .  ptime - current time
41050910c330SBarry Smith .  u - current state
4106fb1732b5SBarry Smith -  viewer - binary viewer
4107fb1732b5SBarry Smith 
4108fb1732b5SBarry Smith    Level: intermediate
4109fb1732b5SBarry Smith 
4110fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4111fb1732b5SBarry Smith 
4112fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4113fb1732b5SBarry Smith @*/
41140910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4115fb1732b5SBarry Smith {
4116fb1732b5SBarry Smith   PetscErrorCode ierr;
4117ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4118fb1732b5SBarry Smith 
4119fb1732b5SBarry Smith   PetscFunctionBegin;
41200910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4121ed81e22dSJed Brown   PetscFunctionReturn(0);
4122ed81e22dSJed Brown }
4123ed81e22dSJed Brown 
4124ed81e22dSJed Brown #undef __FUNCT__
4125ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4126ed81e22dSJed Brown /*@C
4127ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4128ed81e22dSJed Brown 
4129ed81e22dSJed Brown    Collective on TS
4130ed81e22dSJed Brown 
4131ed81e22dSJed Brown    Input Parameters:
4132ed81e22dSJed Brown +  ts - the TS context
4133ed81e22dSJed Brown .  step - current time-step
4134ed81e22dSJed Brown .  ptime - current time
41350910c330SBarry Smith .  u - current state
4136ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4137ed81e22dSJed Brown 
4138ed81e22dSJed Brown    Level: intermediate
4139ed81e22dSJed Brown 
4140ed81e22dSJed Brown    Notes:
4141ed81e22dSJed 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.
4142ed81e22dSJed Brown    These are named according to the file name template.
4143ed81e22dSJed Brown 
4144ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4145ed81e22dSJed Brown 
4146ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4147ed81e22dSJed Brown 
4148ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4149ed81e22dSJed Brown @*/
41500910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4151ed81e22dSJed Brown {
4152ed81e22dSJed Brown   PetscErrorCode ierr;
4153ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4154ed81e22dSJed Brown   PetscViewer    viewer;
4155ed81e22dSJed Brown 
4156ed81e22dSJed Brown   PetscFunctionBegin;
41578caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4158ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
41590910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4160ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4161ed81e22dSJed Brown   PetscFunctionReturn(0);
4162ed81e22dSJed Brown }
4163ed81e22dSJed Brown 
4164ed81e22dSJed Brown #undef __FUNCT__
4165ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4166ed81e22dSJed Brown /*@C
4167ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4168ed81e22dSJed Brown 
4169ed81e22dSJed Brown    Collective on TS
4170ed81e22dSJed Brown 
4171ed81e22dSJed Brown    Input Parameters:
4172ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4173ed81e22dSJed Brown 
4174ed81e22dSJed Brown    Level: intermediate
4175ed81e22dSJed Brown 
4176ed81e22dSJed Brown    Note:
4177ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4178ed81e22dSJed Brown 
4179ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4180ed81e22dSJed Brown 
4181ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4182ed81e22dSJed Brown @*/
4183ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4184ed81e22dSJed Brown {
4185ed81e22dSJed Brown   PetscErrorCode ierr;
4186ed81e22dSJed Brown 
4187ed81e22dSJed Brown   PetscFunctionBegin;
4188ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4189fb1732b5SBarry Smith   PetscFunctionReturn(0);
4190fb1732b5SBarry Smith }
4191fb1732b5SBarry Smith 
419284df9cb4SJed Brown #undef __FUNCT__
4193552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
419484df9cb4SJed Brown /*@
4195552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
419684df9cb4SJed Brown 
4197ed81e22dSJed Brown    Collective on TS if controller has not been created yet
419884df9cb4SJed Brown 
419984df9cb4SJed Brown    Input Arguments:
4200ed81e22dSJed Brown .  ts - time stepping context
420184df9cb4SJed Brown 
420284df9cb4SJed Brown    Output Arguments:
4203ed81e22dSJed Brown .  adapt - adaptive controller
420484df9cb4SJed Brown 
420584df9cb4SJed Brown    Level: intermediate
420684df9cb4SJed Brown 
4207ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
420884df9cb4SJed Brown @*/
4209552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
421084df9cb4SJed Brown {
421184df9cb4SJed Brown   PetscErrorCode ierr;
421284df9cb4SJed Brown 
421384df9cb4SJed Brown   PetscFunctionBegin;
421484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
421584df9cb4SJed Brown   PetscValidPointer(adapt,2);
421684df9cb4SJed Brown   if (!ts->adapt) {
4217ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
42183bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
42191c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
422084df9cb4SJed Brown   }
422184df9cb4SJed Brown   *adapt = ts->adapt;
422284df9cb4SJed Brown   PetscFunctionReturn(0);
422384df9cb4SJed Brown }
4224d6ebe24aSShri Abhyankar 
4225d6ebe24aSShri Abhyankar #undef __FUNCT__
42261c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
42271c3436cfSJed Brown /*@
42281c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
42291c3436cfSJed Brown 
42301c3436cfSJed Brown    Logically Collective
42311c3436cfSJed Brown 
42321c3436cfSJed Brown    Input Arguments:
42331c3436cfSJed Brown +  ts - time integration context
42341c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
42350298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
42361c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
42370298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
42381c3436cfSJed Brown 
42391c3436cfSJed Brown    Level: beginner
42401c3436cfSJed Brown 
4241c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
42421c3436cfSJed Brown @*/
42431c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
42441c3436cfSJed Brown {
42451c3436cfSJed Brown   PetscErrorCode ierr;
42461c3436cfSJed Brown 
42471c3436cfSJed Brown   PetscFunctionBegin;
4248c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
42491c3436cfSJed Brown   if (vatol) {
42501c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
42511c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4252bbd56ea5SKarl Rupp 
42531c3436cfSJed Brown     ts->vatol = vatol;
42541c3436cfSJed Brown   }
4255c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
42561c3436cfSJed Brown   if (vrtol) {
42571c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
42581c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4259bbd56ea5SKarl Rupp 
42601c3436cfSJed Brown     ts->vrtol = vrtol;
42611c3436cfSJed Brown   }
42621c3436cfSJed Brown   PetscFunctionReturn(0);
42631c3436cfSJed Brown }
42641c3436cfSJed Brown 
42651c3436cfSJed Brown #undef __FUNCT__
4266c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4267c5033834SJed Brown /*@
4268c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4269c5033834SJed Brown 
4270c5033834SJed Brown    Logically Collective
4271c5033834SJed Brown 
4272c5033834SJed Brown    Input Arguments:
4273c5033834SJed Brown .  ts - time integration context
4274c5033834SJed Brown 
4275c5033834SJed Brown    Output Arguments:
42760298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
42770298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
42780298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
42790298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4280c5033834SJed Brown 
4281c5033834SJed Brown    Level: beginner
4282c5033834SJed Brown 
4283c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4284c5033834SJed Brown @*/
4285c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4286c5033834SJed Brown {
4287c5033834SJed Brown   PetscFunctionBegin;
4288c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4289c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4290c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4291c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4292c5033834SJed Brown   PetscFunctionReturn(0);
4293c5033834SJed Brown }
4294c5033834SJed Brown 
4295c5033834SJed Brown #undef __FUNCT__
42961c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
42971c3436cfSJed Brown /*@
42981c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
42991c3436cfSJed Brown 
43001c3436cfSJed Brown    Collective on TS
43011c3436cfSJed Brown 
43021c3436cfSJed Brown    Input Arguments:
43031c3436cfSJed Brown +  ts - time stepping context
43041c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
43051c3436cfSJed Brown 
43061c3436cfSJed Brown    Output Arguments:
43071c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
43081c3436cfSJed Brown 
43091c3436cfSJed Brown    Level: developer
43101c3436cfSJed Brown 
43111c3436cfSJed Brown .seealso: TSSetTolerances()
43121c3436cfSJed Brown @*/
43131c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
43141c3436cfSJed Brown {
43151c3436cfSJed Brown   PetscErrorCode    ierr;
43161c3436cfSJed Brown   PetscInt          i,n,N;
43170910c330SBarry Smith   const PetscScalar *u,*y;
43180910c330SBarry Smith   Vec               U;
43191c3436cfSJed Brown   PetscReal         sum,gsum;
43201c3436cfSJed Brown 
43211c3436cfSJed Brown   PetscFunctionBegin;
43221c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43231c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
43241c3436cfSJed Brown   PetscValidPointer(norm,3);
43250910c330SBarry Smith   U = ts->vec_sol;
43260910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4327ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
43281c3436cfSJed Brown 
43290910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
43300910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
43310910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
43321c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
43331c3436cfSJed Brown   sum  = 0.;
4334ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4335ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4336ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4337ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4338ceefc113SJed Brown     for (i=0; i<n; i++) {
43390910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
43400910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4341ceefc113SJed Brown     }
4342ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4343ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4344ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4345ceefc113SJed Brown     const PetscScalar *atol;
4346ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4347ceefc113SJed Brown     for (i=0; i<n; i++) {
43480910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
43490910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4350ceefc113SJed Brown     }
4351ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4352ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4353ceefc113SJed Brown     const PetscScalar *rtol;
4354ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4355ceefc113SJed Brown     for (i=0; i<n; i++) {
43560910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
43570910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4358ceefc113SJed Brown     }
4359ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4360ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
43611c3436cfSJed Brown     for (i=0; i<n; i++) {
43620910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
43630910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
43641c3436cfSJed Brown     }
4365ceefc113SJed Brown   }
43660910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
43671c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
43681c3436cfSJed Brown 
4369ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
43701c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4371ce94432eSBarry Smith   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
43721c3436cfSJed Brown   PetscFunctionReturn(0);
43731c3436cfSJed Brown }
43741c3436cfSJed Brown 
43751c3436cfSJed Brown #undef __FUNCT__
43768d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
43778d59e960SJed Brown /*@
43788d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
43798d59e960SJed Brown 
43808d59e960SJed Brown    Logically Collective on TS
43818d59e960SJed Brown 
43828d59e960SJed Brown    Input Arguments:
43838d59e960SJed Brown +  ts - time stepping context
43848d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
43858d59e960SJed Brown 
43868d59e960SJed Brown    Note:
43878d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
43888d59e960SJed Brown 
43898d59e960SJed Brown    Level: intermediate
43908d59e960SJed Brown 
43918d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
43928d59e960SJed Brown @*/
43938d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
43948d59e960SJed Brown {
43958d59e960SJed Brown   PetscFunctionBegin;
43968d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43978d59e960SJed Brown   ts->cfltime_local = cfltime;
43988d59e960SJed Brown   ts->cfltime       = -1.;
43998d59e960SJed Brown   PetscFunctionReturn(0);
44008d59e960SJed Brown }
44018d59e960SJed Brown 
44028d59e960SJed Brown #undef __FUNCT__
44038d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
44048d59e960SJed Brown /*@
44058d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
44068d59e960SJed Brown 
44078d59e960SJed Brown    Collective on TS
44088d59e960SJed Brown 
44098d59e960SJed Brown    Input Arguments:
44108d59e960SJed Brown .  ts - time stepping context
44118d59e960SJed Brown 
44128d59e960SJed Brown    Output Arguments:
44138d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
44148d59e960SJed Brown 
44158d59e960SJed Brown    Level: advanced
44168d59e960SJed Brown 
44178d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
44188d59e960SJed Brown @*/
44198d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
44208d59e960SJed Brown {
44218d59e960SJed Brown   PetscErrorCode ierr;
44228d59e960SJed Brown 
44238d59e960SJed Brown   PetscFunctionBegin;
44248d59e960SJed Brown   if (ts->cfltime < 0) {
4425ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
44268d59e960SJed Brown   }
44278d59e960SJed Brown   *cfltime = ts->cfltime;
44288d59e960SJed Brown   PetscFunctionReturn(0);
44298d59e960SJed Brown }
44308d59e960SJed Brown 
44318d59e960SJed Brown #undef __FUNCT__
4432d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4433d6ebe24aSShri Abhyankar /*@
4434d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4435d6ebe24aSShri Abhyankar 
4436d6ebe24aSShri Abhyankar    Input Parameters:
4437d6ebe24aSShri Abhyankar .  ts   - the TS context.
4438d6ebe24aSShri Abhyankar .  xl   - lower bound.
4439d6ebe24aSShri Abhyankar .  xu   - upper bound.
4440d6ebe24aSShri Abhyankar 
4441d6ebe24aSShri Abhyankar    Notes:
4442d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
444333c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4444d6ebe24aSShri Abhyankar 
44452bd2b0e6SSatish Balay    Level: advanced
44462bd2b0e6SSatish Balay 
4447d6ebe24aSShri Abhyankar @*/
4448d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4449d6ebe24aSShri Abhyankar {
4450d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4451d6ebe24aSShri Abhyankar   SNES           snes;
4452d6ebe24aSShri Abhyankar 
4453d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4454d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4455d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4456d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4457d6ebe24aSShri Abhyankar }
4458d6ebe24aSShri Abhyankar 
4459325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4460c6db04a5SJed Brown #include <mex.h>
4461325fc9f4SBarry Smith 
4462325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4463325fc9f4SBarry Smith 
4464325fc9f4SBarry Smith #undef __FUNCT__
4465325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4466325fc9f4SBarry Smith /*
4467325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4468325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4469325fc9f4SBarry Smith 
4470325fc9f4SBarry Smith    Collective on TS
4471325fc9f4SBarry Smith 
4472325fc9f4SBarry Smith    Input Parameters:
4473325fc9f4SBarry Smith +  snes - the TS context
44740910c330SBarry Smith -  u - input vector
4475325fc9f4SBarry Smith 
4476325fc9f4SBarry Smith    Output Parameter:
4477325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4478325fc9f4SBarry Smith 
4479325fc9f4SBarry Smith    Notes:
4480325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4481325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4482325fc9f4SBarry Smith    themselves.
4483325fc9f4SBarry Smith 
4484325fc9f4SBarry Smith    Level: developer
4485325fc9f4SBarry Smith 
4486325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4487325fc9f4SBarry Smith 
4488325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4489325fc9f4SBarry Smith */
44900910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4491325fc9f4SBarry Smith {
4492325fc9f4SBarry Smith   PetscErrorCode  ierr;
4493325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4494325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4495325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4496325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4497325fc9f4SBarry Smith 
4498325fc9f4SBarry Smith   PetscFunctionBegin;
4499325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
45000910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
45010910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4502325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
45030910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4504325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4505325fc9f4SBarry Smith 
4506325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
45070910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
45080910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
45090910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4510bbd56ea5SKarl Rupp 
4511325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4512325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4513325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4514325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4515325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4516325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4517325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4518325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4519325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4520325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4521325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4522325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4523325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4524325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4525325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4526325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4527325fc9f4SBarry Smith   PetscFunctionReturn(0);
4528325fc9f4SBarry Smith }
4529325fc9f4SBarry Smith 
4530325fc9f4SBarry Smith 
4531325fc9f4SBarry Smith #undef __FUNCT__
4532325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4533325fc9f4SBarry Smith /*
4534325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4535325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4536e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4537325fc9f4SBarry Smith 
4538325fc9f4SBarry Smith    Logically Collective on TS
4539325fc9f4SBarry Smith 
4540325fc9f4SBarry Smith    Input Parameters:
4541325fc9f4SBarry Smith +  ts - the TS context
4542325fc9f4SBarry Smith -  func - function evaluation routine
4543325fc9f4SBarry Smith 
4544325fc9f4SBarry Smith    Calling sequence of func:
45450910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4546325fc9f4SBarry Smith 
4547325fc9f4SBarry Smith    Level: beginner
4548325fc9f4SBarry Smith 
4549325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4550325fc9f4SBarry Smith 
4551325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4552325fc9f4SBarry Smith */
4553cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4554325fc9f4SBarry Smith {
4555325fc9f4SBarry Smith   PetscErrorCode  ierr;
4556325fc9f4SBarry Smith   TSMatlabContext *sctx;
4557325fc9f4SBarry Smith 
4558325fc9f4SBarry Smith   PetscFunctionBegin;
4559325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4560325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4561325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4562325fc9f4SBarry Smith   /*
4563325fc9f4SBarry Smith      This should work, but it doesn't
4564325fc9f4SBarry Smith   sctx->ctx = ctx;
4565325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4566325fc9f4SBarry Smith   */
4567325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4568bbd56ea5SKarl Rupp 
45690298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4570325fc9f4SBarry Smith   PetscFunctionReturn(0);
4571325fc9f4SBarry Smith }
4572325fc9f4SBarry Smith 
4573325fc9f4SBarry Smith #undef __FUNCT__
4574325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4575325fc9f4SBarry Smith /*
4576325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4577325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4578325fc9f4SBarry Smith 
4579325fc9f4SBarry Smith    Collective on TS
4580325fc9f4SBarry Smith 
4581325fc9f4SBarry Smith    Input Parameters:
4582cdcf91faSSean Farley +  ts - the TS context
45830910c330SBarry Smith .  u - input vector
4584325fc9f4SBarry Smith .  A, B - the matrices
4585325fc9f4SBarry Smith -  ctx - user context
4586325fc9f4SBarry Smith 
4587325fc9f4SBarry Smith    Output Parameter:
4588325fc9f4SBarry Smith .  flag - structure of the matrix
4589325fc9f4SBarry Smith 
4590325fc9f4SBarry Smith    Level: developer
4591325fc9f4SBarry Smith 
4592325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4593325fc9f4SBarry Smith 
4594325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4595325fc9f4SBarry Smith @*/
45960910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4597325fc9f4SBarry Smith {
4598325fc9f4SBarry Smith   PetscErrorCode  ierr;
4599325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4600325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4601325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4602325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4603325fc9f4SBarry Smith 
4604325fc9f4SBarry Smith   PetscFunctionBegin;
4605cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46060910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4607325fc9f4SBarry Smith 
46080910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4609325fc9f4SBarry Smith 
4610cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
46110910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
46120910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
46130910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
46140910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4615bbd56ea5SKarl Rupp 
4616325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4617325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4618325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4619325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4620325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4621325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4622325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4623325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4624325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4625325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4626325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4627325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4628325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4629325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4630325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4631325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4632325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4633325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4634325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4635325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4636325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4637325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4638325fc9f4SBarry Smith   PetscFunctionReturn(0);
4639325fc9f4SBarry Smith }
4640325fc9f4SBarry Smith 
4641325fc9f4SBarry Smith 
4642325fc9f4SBarry Smith #undef __FUNCT__
4643325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4644325fc9f4SBarry Smith /*
4645325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4646e3c5b3baSBarry 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
4647325fc9f4SBarry Smith 
4648325fc9f4SBarry Smith    Logically Collective on TS
4649325fc9f4SBarry Smith 
4650325fc9f4SBarry Smith    Input Parameters:
4651cdcf91faSSean Farley +  ts - the TS context
4652325fc9f4SBarry Smith .  A,B - Jacobian matrices
4653325fc9f4SBarry Smith .  func - function evaluation routine
4654325fc9f4SBarry Smith -  ctx - user context
4655325fc9f4SBarry Smith 
4656325fc9f4SBarry Smith    Calling sequence of func:
46570910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4658325fc9f4SBarry Smith 
4659325fc9f4SBarry Smith 
4660325fc9f4SBarry Smith    Level: developer
4661325fc9f4SBarry Smith 
4662325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4663325fc9f4SBarry Smith 
4664325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4665325fc9f4SBarry Smith */
4666cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4667325fc9f4SBarry Smith {
4668325fc9f4SBarry Smith   PetscErrorCode  ierr;
4669325fc9f4SBarry Smith   TSMatlabContext *sctx;
4670325fc9f4SBarry Smith 
4671325fc9f4SBarry Smith   PetscFunctionBegin;
4672325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4673325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4674325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4675325fc9f4SBarry Smith   /*
4676325fc9f4SBarry Smith      This should work, but it doesn't
4677325fc9f4SBarry Smith   sctx->ctx = ctx;
4678325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4679325fc9f4SBarry Smith   */
4680325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4681bbd56ea5SKarl Rupp 
4682cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4683325fc9f4SBarry Smith   PetscFunctionReturn(0);
4684325fc9f4SBarry Smith }
4685325fc9f4SBarry Smith 
4686b5b1a830SBarry Smith #undef __FUNCT__
4687b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4688b5b1a830SBarry Smith /*
4689b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4690b5b1a830SBarry Smith 
4691b5b1a830SBarry Smith    Collective on TS
4692b5b1a830SBarry Smith 
4693b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4694b5b1a830SBarry Smith @*/
46950910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4696b5b1a830SBarry Smith {
4697b5b1a830SBarry Smith   PetscErrorCode  ierr;
4698b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4699a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4700b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4701b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4702b5b1a830SBarry Smith 
4703b5b1a830SBarry Smith   PetscFunctionBegin;
4704cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47050910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4706b5b1a830SBarry Smith 
4707cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
47080910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4709bbd56ea5SKarl Rupp 
4710b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4711b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4712b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4713b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4714b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4715b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4716b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4717b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4718b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4719b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4720b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4721b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4722b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4723b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4724b5b1a830SBarry Smith   PetscFunctionReturn(0);
4725b5b1a830SBarry Smith }
4726b5b1a830SBarry Smith 
4727b5b1a830SBarry Smith 
4728b5b1a830SBarry Smith #undef __FUNCT__
4729b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4730b5b1a830SBarry Smith /*
4731b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4732b5b1a830SBarry Smith 
4733b5b1a830SBarry Smith    Level: developer
4734b5b1a830SBarry Smith 
4735b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4736b5b1a830SBarry Smith 
4737b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4738b5b1a830SBarry Smith */
4739cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4740b5b1a830SBarry Smith {
4741b5b1a830SBarry Smith   PetscErrorCode  ierr;
4742b5b1a830SBarry Smith   TSMatlabContext *sctx;
4743b5b1a830SBarry Smith 
4744b5b1a830SBarry Smith   PetscFunctionBegin;
4745b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4746b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4747b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4748b5b1a830SBarry Smith   /*
4749b5b1a830SBarry Smith      This should work, but it doesn't
4750b5b1a830SBarry Smith   sctx->ctx = ctx;
4751b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4752b5b1a830SBarry Smith   */
4753b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4754bbd56ea5SKarl Rupp 
47550298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
4756b5b1a830SBarry Smith   PetscFunctionReturn(0);
4757b5b1a830SBarry Smith }
4758325fc9f4SBarry Smith #endif
4759b3603a34SBarry Smith 
4760b3603a34SBarry Smith #undef __FUNCT__
47614f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4762b3603a34SBarry Smith /*@C
47634f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4764b3603a34SBarry Smith        in a time based line graph
4765b3603a34SBarry Smith 
4766b3603a34SBarry Smith    Collective on TS
4767b3603a34SBarry Smith 
4768b3603a34SBarry Smith    Input Parameters:
4769b3603a34SBarry Smith +  ts - the TS context
4770b3603a34SBarry Smith .  step - current time-step
4771b3603a34SBarry Smith .  ptime - current time
4772b3603a34SBarry Smith -  lg - a line graph object
4773b3603a34SBarry Smith 
4774*b3d3934dSBarry Smith    Options Database:
4775*b3d3934dSBarry Smith .   -ts_lg_monitor_solution_variables
4776*b3d3934dSBarry Smith 
4777b3603a34SBarry Smith    Level: intermediate
4778b3603a34SBarry Smith 
47790b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4780b3603a34SBarry Smith 
4781b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4782b3603a34SBarry Smith 
4783b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4784b3603a34SBarry Smith @*/
47850910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4786b3603a34SBarry Smith {
4787b3603a34SBarry Smith   PetscErrorCode    ierr;
47880b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4789b3603a34SBarry Smith   const PetscScalar *yy;
479058ff32f7SBarry Smith   PetscInt          dim;
479180666b62SBarry Smith   Vec               v;
4792b3603a34SBarry Smith 
4793b3603a34SBarry Smith   PetscFunctionBegin;
479458ff32f7SBarry Smith   if (!step) {
4795a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4796a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4797a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
4798387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
4799387f4636SBarry Smith       char      **displaynames;
4800387f4636SBarry Smith       PetscBool flg;
4801387f4636SBarry Smith 
4802387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4803387f4636SBarry Smith       ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr);
4804387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
4805387f4636SBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->prefix,"-ts_lg_monitor_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
4806387f4636SBarry Smith       if (flg) {
4807387f4636SBarry Smith         ierr = TSMonitorLGSetDisplayVariables(ts,(const char *const *)displaynames);CHKERRQ(ierr);
4808387f4636SBarry Smith       }
4809387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
4810387f4636SBarry Smith     }
4811387f4636SBarry Smith     if (ctx->displaynames) {
4812387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
4813387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
4814387f4636SBarry Smith     } else if (ctx->names) {
48150910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
48160b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4817387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
4818387f4636SBarry Smith     }
48190b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
482058ff32f7SBarry Smith   }
482180666b62SBarry Smith   if (ctx->transform) {
482280666b62SBarry Smith     ierr = VecDuplicate(u,&v);CHKERRQ(ierr);
482380666b62SBarry Smith     ierr = (*ctx->transform)(ctx->transformctx,u,v);CHKERRQ(ierr);
482480666b62SBarry Smith   } else {
482580666b62SBarry Smith     v = u;
482680666b62SBarry Smith   }
482780666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
4828e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4829e3efe391SJed Brown   {
4830e3efe391SJed Brown     PetscReal *yreal;
4831e3efe391SJed Brown     PetscInt  i,n;
483280666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
4833e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4834e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
48350b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4836e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4837e3efe391SJed Brown   }
4838e3efe391SJed Brown #else
4839387f4636SBarry Smith   if (ctx->displaynames) {
4840387f4636SBarry Smith     PetscInt i;
4841387f4636SBarry Smith     for (i=0; i<ctx->ndisplayvariables; i++) {
4842387f4636SBarry Smith       ctx->displayvalues[i] = yy[ctx->displayvariables[i]];
4843387f4636SBarry Smith     }
4844387f4636SBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
4845387f4636SBarry Smith   } else {
48460b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4847387f4636SBarry Smith   }
4848e3efe391SJed Brown #endif
484980666b62SBarry Smith   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
485080666b62SBarry Smith   if (ctx->transform) {
485180666b62SBarry Smith     ierr = VecDestroy(&v);CHKERRQ(ierr);
485280666b62SBarry Smith   }
4853b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
48540b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
48553923b477SBarry Smith   }
4856b3603a34SBarry Smith   PetscFunctionReturn(0);
4857b3603a34SBarry Smith }
4858b3603a34SBarry Smith 
4859387f4636SBarry Smith 
4860b3603a34SBarry Smith #undef __FUNCT__
486131152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames"
4862b037adc7SBarry Smith /*@C
486331152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
4864b037adc7SBarry Smith 
4865b037adc7SBarry Smith    Collective on TS
4866b037adc7SBarry Smith 
4867b037adc7SBarry Smith    Input Parameters:
4868b037adc7SBarry Smith +  ts - the TS context
4869*b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
4870b037adc7SBarry Smith 
4871b037adc7SBarry Smith    Level: intermediate
4872b037adc7SBarry Smith 
4873b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
4874b037adc7SBarry Smith 
4875387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
4876b037adc7SBarry Smith @*/
487731152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
4878b037adc7SBarry Smith {
4879b037adc7SBarry Smith   PetscErrorCode    ierr;
4880b037adc7SBarry Smith   PetscInt          i;
4881b037adc7SBarry Smith 
4882b037adc7SBarry Smith   PetscFunctionBegin;
4883b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
4884b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
4885b037adc7SBarry Smith       TSMonitorLGCtx  ctx = ts->monitorcontext[i];
4886387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
488731152f8aSBarry Smith       ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
4888*b3d3934dSBarry Smith       break;
4889*b3d3934dSBarry Smith     }
4890*b3d3934dSBarry Smith   }
4891*b3d3934dSBarry Smith   PetscFunctionReturn(0);
4892*b3d3934dSBarry Smith }
4893*b3d3934dSBarry Smith 
4894*b3d3934dSBarry Smith #undef __FUNCT__
4895*b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames"
4896*b3d3934dSBarry Smith /*@C
4897*b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
4898*b3d3934dSBarry Smith 
4899*b3d3934dSBarry Smith    Collective on TS
4900*b3d3934dSBarry Smith 
4901*b3d3934dSBarry Smith    Input Parameter:
4902*b3d3934dSBarry Smith .  ts - the TS context
4903*b3d3934dSBarry Smith 
4904*b3d3934dSBarry Smith    Output Parameter:
4905*b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
4906*b3d3934dSBarry Smith 
4907*b3d3934dSBarry Smith    Level: intermediate
4908*b3d3934dSBarry Smith 
4909*b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
4910*b3d3934dSBarry Smith 
4911*b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
4912*b3d3934dSBarry Smith @*/
4913*b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
4914*b3d3934dSBarry Smith {
4915*b3d3934dSBarry Smith   PetscInt       i;
4916*b3d3934dSBarry Smith 
4917*b3d3934dSBarry Smith   PetscFunctionBegin;
4918*b3d3934dSBarry Smith   *names = NULL;
4919*b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
4920*b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
4921*b3d3934dSBarry Smith       TSMonitorLGCtx  ctx = ts->monitorcontext[i];
4922*b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
4923*b3d3934dSBarry Smith       break;
4924387f4636SBarry Smith     }
4925387f4636SBarry Smith   }
4926387f4636SBarry Smith   PetscFunctionReturn(0);
4927387f4636SBarry Smith }
4928387f4636SBarry Smith 
4929387f4636SBarry Smith #undef __FUNCT__
4930387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables"
4931387f4636SBarry Smith /*@C
4932387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
4933387f4636SBarry Smith 
4934387f4636SBarry Smith    Collective on TS
4935387f4636SBarry Smith 
4936387f4636SBarry Smith    Input Parameters:
4937387f4636SBarry Smith +  ts - the TS context
4938387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
4939387f4636SBarry Smith 
4940387f4636SBarry Smith    Level: intermediate
4941387f4636SBarry Smith 
4942387f4636SBarry Smith .keywords: TS,  vector, monitor, view
4943387f4636SBarry Smith 
4944387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
4945387f4636SBarry Smith @*/
4946387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
4947387f4636SBarry Smith {
4948387f4636SBarry Smith   PetscInt          i,j = 0,k;
4949387f4636SBarry Smith   PetscErrorCode    ierr;
4950387f4636SBarry Smith 
4951387f4636SBarry Smith   PetscFunctionBegin;
4952387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
4953387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
4954387f4636SBarry Smith       TSMonitorLGCtx  ctx = ts->monitorcontext[i];
4955387f4636SBarry Smith 
4956387f4636SBarry Smith       if (!ctx->names) PetscFunctionReturn(0);
4957387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
4958387f4636SBarry Smith       ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
4959387f4636SBarry Smith       while (displaynames[j]) j++;
4960387f4636SBarry Smith       ctx->ndisplayvariables = j;
4961387f4636SBarry Smith       ierr = PetscMalloc(ctx->ndisplayvariables*sizeof(PetscInt),&ctx->displayvariables);CHKERRQ(ierr);
4962387f4636SBarry Smith       ierr = PetscMalloc(ctx->ndisplayvariables*sizeof(PetscReal),&ctx->displayvalues);CHKERRQ(ierr);
4963387f4636SBarry Smith       j = 0;
4964387f4636SBarry Smith       while (displaynames[j]) {
4965387f4636SBarry Smith         k = 0;
4966387f4636SBarry Smith         while (ctx->names[k]) {
4967387f4636SBarry Smith           PetscBool flg;
4968387f4636SBarry Smith           ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
4969387f4636SBarry Smith           if (flg) {
4970387f4636SBarry Smith             ctx->displayvariables[j] = k;
4971387f4636SBarry Smith             break;
4972387f4636SBarry Smith           }
4973387f4636SBarry Smith           k++;
4974387f4636SBarry Smith         }
4975387f4636SBarry Smith         j++;
4976b037adc7SBarry Smith       }
4977*b3d3934dSBarry Smith       break;
4978b037adc7SBarry Smith     }
4979b037adc7SBarry Smith   }
4980b037adc7SBarry Smith   PetscFunctionReturn(0);
4981b037adc7SBarry Smith }
4982b037adc7SBarry Smith 
4983b037adc7SBarry Smith #undef __FUNCT__
498480666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform"
498580666b62SBarry Smith /*@C
498680666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
498780666b62SBarry Smith 
498880666b62SBarry Smith    Collective on TS
498980666b62SBarry Smith 
499080666b62SBarry Smith    Input Parameters:
499180666b62SBarry Smith +  ts - the TS context
499280666b62SBarry Smith .  transform - the transform function
499380666b62SBarry Smith -  ctx - optional context used by transform function
499480666b62SBarry Smith 
499580666b62SBarry Smith    Level: intermediate
499680666b62SBarry Smith 
499780666b62SBarry Smith .keywords: TS,  vector, monitor, view
499880666b62SBarry Smith 
499980666b62SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
500080666b62SBarry Smith @*/
500180666b62SBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec),void *tctx)
500280666b62SBarry Smith {
500380666b62SBarry Smith   PetscInt          i;
500480666b62SBarry Smith 
500580666b62SBarry Smith   PetscFunctionBegin;
500680666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
500780666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
500880666b62SBarry Smith       TSMonitorLGCtx  ctx = ts->monitorcontext[i];
500980666b62SBarry Smith       ctx->transform    = transform;
501080666b62SBarry Smith       ctx->transformctx = tctx;
501180666b62SBarry Smith     }
501280666b62SBarry Smith   }
501380666b62SBarry Smith   PetscFunctionReturn(0);
501480666b62SBarry Smith }
501580666b62SBarry Smith 
501680666b62SBarry Smith #undef __FUNCT__
50174f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
5018ef20d060SBarry Smith /*@C
50194f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
5020ef20d060SBarry Smith        in a time based line graph
5021ef20d060SBarry Smith 
5022ef20d060SBarry Smith    Collective on TS
5023ef20d060SBarry Smith 
5024ef20d060SBarry Smith    Input Parameters:
5025ef20d060SBarry Smith +  ts - the TS context
5026ef20d060SBarry Smith .  step - current time-step
5027ef20d060SBarry Smith .  ptime - current time
5028ef20d060SBarry Smith -  lg - a line graph object
5029ef20d060SBarry Smith 
5030ef20d060SBarry Smith    Level: intermediate
5031ef20d060SBarry Smith 
5032abd5a294SJed Brown    Notes:
5033abd5a294SJed Brown    Only for sequential solves.
5034abd5a294SJed Brown 
5035abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
5036abd5a294SJed Brown 
5037abd5a294SJed Brown    Options Database Keys:
50384f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
5039ef20d060SBarry Smith 
5040ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
5041ef20d060SBarry Smith 
5042abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
5043ef20d060SBarry Smith @*/
50440910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5045ef20d060SBarry Smith {
5046ef20d060SBarry Smith   PetscErrorCode    ierr;
50470b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5048ef20d060SBarry Smith   const PetscScalar *yy;
5049ef20d060SBarry Smith   Vec               y;
5050a9f9c1f6SBarry Smith   PetscInt          dim;
5051ef20d060SBarry Smith 
5052ef20d060SBarry Smith   PetscFunctionBegin;
5053a9f9c1f6SBarry Smith   if (!step) {
5054a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5055a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
50561ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
50570910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5058a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5059a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5060a9f9c1f6SBarry Smith   }
50610910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
5062ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
50630910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
5064ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
5065e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5066e3efe391SJed Brown   {
5067e3efe391SJed Brown     PetscReal *yreal;
5068e3efe391SJed Brown     PetscInt  i,n;
5069e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
5070e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
5071e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
50720b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5073e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5074e3efe391SJed Brown   }
5075e3efe391SJed Brown #else
50760b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5077e3efe391SJed Brown #endif
5078ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
5079ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
5080b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
50810b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
50823923b477SBarry Smith   }
5083ef20d060SBarry Smith   PetscFunctionReturn(0);
5084ef20d060SBarry Smith }
5085ef20d060SBarry Smith 
5086201da799SBarry Smith #undef __FUNCT__
5087201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
5088201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5089201da799SBarry Smith {
5090201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5091201da799SBarry Smith   PetscReal      x   = ptime,y;
5092201da799SBarry Smith   PetscErrorCode ierr;
5093201da799SBarry Smith   PetscInt       its;
5094201da799SBarry Smith 
5095201da799SBarry Smith   PetscFunctionBegin;
5096201da799SBarry Smith   if (!n) {
5097201da799SBarry Smith     PetscDrawAxis axis;
5098bbd56ea5SKarl Rupp 
5099201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5100201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
5101201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5102bbd56ea5SKarl Rupp 
5103201da799SBarry Smith     ctx->snes_its = 0;
5104201da799SBarry Smith   }
5105201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
5106201da799SBarry Smith   y    = its - ctx->snes_its;
5107201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
51083fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5109201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5110201da799SBarry Smith   }
5111201da799SBarry Smith   ctx->snes_its = its;
5112201da799SBarry Smith   PetscFunctionReturn(0);
5113201da799SBarry Smith }
5114201da799SBarry Smith 
5115201da799SBarry Smith #undef __FUNCT__
5116201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
5117201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5118201da799SBarry Smith {
5119201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5120201da799SBarry Smith   PetscReal      x   = ptime,y;
5121201da799SBarry Smith   PetscErrorCode ierr;
5122201da799SBarry Smith   PetscInt       its;
5123201da799SBarry Smith 
5124201da799SBarry Smith   PetscFunctionBegin;
5125201da799SBarry Smith   if (!n) {
5126201da799SBarry Smith     PetscDrawAxis axis;
5127bbd56ea5SKarl Rupp 
5128201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5129201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
5130201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5131bbd56ea5SKarl Rupp 
5132201da799SBarry Smith     ctx->ksp_its = 0;
5133201da799SBarry Smith   }
5134201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
5135201da799SBarry Smith   y    = its - ctx->ksp_its;
5136201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
513799fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5138201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5139201da799SBarry Smith   }
5140201da799SBarry Smith   ctx->ksp_its = its;
5141201da799SBarry Smith   PetscFunctionReturn(0);
5142201da799SBarry Smith }
5143f9c1d6abSBarry Smith 
5144f9c1d6abSBarry Smith #undef __FUNCT__
5145f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
5146f9c1d6abSBarry Smith /*@
5147f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
5148f9c1d6abSBarry Smith 
5149f9c1d6abSBarry Smith    Collective on TS and Vec
5150f9c1d6abSBarry Smith 
5151f9c1d6abSBarry Smith    Input Parameters:
5152f9c1d6abSBarry Smith +  ts - the TS context
5153f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
5154f9c1d6abSBarry Smith 
5155f9c1d6abSBarry Smith    Output Parameters:
5156f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
5157f9c1d6abSBarry Smith 
5158f9c1d6abSBarry Smith    Level: developer
5159f9c1d6abSBarry Smith 
5160f9c1d6abSBarry Smith .keywords: TS, compute
5161f9c1d6abSBarry Smith 
5162f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
5163f9c1d6abSBarry Smith @*/
5164f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
5165f9c1d6abSBarry Smith {
5166f9c1d6abSBarry Smith   PetscErrorCode ierr;
5167f9c1d6abSBarry Smith 
5168f9c1d6abSBarry Smith   PetscFunctionBegin;
5169f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5170ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
5171f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
5172f9c1d6abSBarry Smith   PetscFunctionReturn(0);
5173f9c1d6abSBarry Smith }
5174*b3d3934dSBarry Smith 
5175*b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
5176*b3d3934dSBarry Smith #undef __FUNCT__
5177*b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate"
5178*b3d3934dSBarry Smith /*@C
5179*b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
5180*b3d3934dSBarry Smith 
5181*b3d3934dSBarry Smith    Collective on TS
5182*b3d3934dSBarry Smith 
5183*b3d3934dSBarry Smith    Input Parameters:
5184*b3d3934dSBarry Smith .  ts  - the ODE solver object
5185*b3d3934dSBarry Smith 
5186*b3d3934dSBarry Smith    Output Parameter:
5187*b3d3934dSBarry Smith .  ctx - the context
5188*b3d3934dSBarry Smith 
5189*b3d3934dSBarry Smith    Level: intermediate
5190*b3d3934dSBarry Smith 
5191*b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
5192*b3d3934dSBarry Smith 
5193*b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
5194*b3d3934dSBarry Smith 
5195*b3d3934dSBarry Smith @*/
5196*b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
5197*b3d3934dSBarry Smith {
5198*b3d3934dSBarry Smith   PetscErrorCode ierr;
5199*b3d3934dSBarry Smith 
5200*b3d3934dSBarry Smith   PetscFunctionBegin;
5201*b3d3934dSBarry Smith   ierr = PetscNew(struct _n_TSMonitorEnvelopeCtx,ctx);CHKERRQ(ierr);
5202*b3d3934dSBarry Smith   PetscFunctionReturn(0);
5203*b3d3934dSBarry Smith }
5204*b3d3934dSBarry Smith 
5205*b3d3934dSBarry Smith #undef __FUNCT__
5206*b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope"
5207*b3d3934dSBarry Smith /*@C
5208*b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
5209*b3d3934dSBarry Smith 
5210*b3d3934dSBarry Smith    Collective on TS
5211*b3d3934dSBarry Smith 
5212*b3d3934dSBarry Smith    Input Parameters:
5213*b3d3934dSBarry Smith +  ts - the TS context
5214*b3d3934dSBarry Smith .  step - current time-step
5215*b3d3934dSBarry Smith .  ptime - current time
5216*b3d3934dSBarry Smith -  ctx - the envelope context
5217*b3d3934dSBarry Smith 
5218*b3d3934dSBarry Smith    Options Database:
5219*b3d3934dSBarry Smith .  -ts_monitor_envelope
5220*b3d3934dSBarry Smith 
5221*b3d3934dSBarry Smith    Level: intermediate
5222*b3d3934dSBarry Smith 
5223*b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
5224*b3d3934dSBarry Smith 
5225*b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
5226*b3d3934dSBarry Smith 
5227*b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds()
5228*b3d3934dSBarry Smith @*/
5229*b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5230*b3d3934dSBarry Smith {
5231*b3d3934dSBarry Smith   PetscErrorCode       ierr;
5232*b3d3934dSBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dummy;
5233*b3d3934dSBarry Smith 
5234*b3d3934dSBarry Smith   PetscFunctionBegin;
5235*b3d3934dSBarry Smith   if (!ctx->max) {
5236*b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
5237*b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
5238*b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
5239*b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
5240*b3d3934dSBarry Smith   } else {
5241*b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
5242*b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
5243*b3d3934dSBarry Smith   }
5244*b3d3934dSBarry Smith   PetscFunctionReturn(0);
5245*b3d3934dSBarry Smith }
5246*b3d3934dSBarry Smith 
5247*b3d3934dSBarry Smith 
5248*b3d3934dSBarry Smith #undef __FUNCT__
5249*b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds"
5250*b3d3934dSBarry Smith /*@C
5251*b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
5252*b3d3934dSBarry Smith 
5253*b3d3934dSBarry Smith    Collective on TS
5254*b3d3934dSBarry Smith 
5255*b3d3934dSBarry Smith    Input Parameter:
5256*b3d3934dSBarry Smith .  ts - the TS context
5257*b3d3934dSBarry Smith 
5258*b3d3934dSBarry Smith    Output Parameter:
5259*b3d3934dSBarry Smith +  max - the maximum values
5260*b3d3934dSBarry Smith -  min - the minimum values
5261*b3d3934dSBarry Smith 
5262*b3d3934dSBarry Smith    Level: intermediate
5263*b3d3934dSBarry Smith 
5264*b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
5265*b3d3934dSBarry Smith 
5266*b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
5267*b3d3934dSBarry Smith @*/
5268*b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
5269*b3d3934dSBarry Smith {
5270*b3d3934dSBarry Smith   PetscInt i;
5271*b3d3934dSBarry Smith 
5272*b3d3934dSBarry Smith   PetscFunctionBegin;
5273*b3d3934dSBarry Smith   if (max) *max = NULL;
5274*b3d3934dSBarry Smith   if (min) *min = NULL;
5275*b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
5276*b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
5277*b3d3934dSBarry Smith       TSMonitorEnvelopeCtx  ctx = ts->monitorcontext[i];
5278*b3d3934dSBarry Smith       if (max) *max = ctx->max;
5279*b3d3934dSBarry Smith       if (min) *min = ctx->min;
5280*b3d3934dSBarry Smith       break;
5281*b3d3934dSBarry Smith     }
5282*b3d3934dSBarry Smith   }
5283*b3d3934dSBarry Smith   PetscFunctionReturn(0);
5284*b3d3934dSBarry Smith }
5285*b3d3934dSBarry Smith 
5286*b3d3934dSBarry Smith #undef __FUNCT__
5287*b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy"
5288*b3d3934dSBarry Smith /*@C
5289*b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
5290*b3d3934dSBarry Smith 
5291*b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
5292*b3d3934dSBarry Smith 
5293*b3d3934dSBarry Smith    Input Parameter:
5294*b3d3934dSBarry Smith .  ctx - the monitor context
5295*b3d3934dSBarry Smith 
5296*b3d3934dSBarry Smith    Level: intermediate
5297*b3d3934dSBarry Smith 
5298*b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
5299*b3d3934dSBarry Smith 
5300*b3d3934dSBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
5301*b3d3934dSBarry Smith @*/
5302*b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
5303*b3d3934dSBarry Smith {
5304*b3d3934dSBarry Smith   PetscErrorCode ierr;
5305*b3d3934dSBarry Smith 
5306*b3d3934dSBarry Smith   PetscFunctionBegin;
5307*b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
5308*b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
5309*b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
5310*b3d3934dSBarry Smith   PetscFunctionReturn(0);
5311*b3d3934dSBarry Smith }
5312