xref: /petsc/src/ts/interface/ts.c (revision 4b363bab6b21d7ed904c2430210a7b5e5168401a)
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 
400298fd71SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(NULL);CHKERRQ(ierr);}
41bdad233fSMatthew Knepley   ierr = PetscOptionsList("-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;
52*4b363babSBarry 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
8691b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
8791b97e58SBarry Smith 
8891b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
89bdad233fSMatthew Knepley 
90bdad233fSMatthew Knepley    Level: beginner
91bdad233fSMatthew Knepley 
92bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
93bdad233fSMatthew Knepley 
94a313700dSBarry Smith .seealso: TSGetType()
95bdad233fSMatthew Knepley @*/
967087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
97bdad233fSMatthew Knepley {
98ace3abfcSBarry Smith   PetscBool              opt,flg;
99dfbe8321SBarry Smith   PetscErrorCode         ierr;
100649052a6SBarry Smith   PetscViewer            monviewer;
101eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
102089b2837SJed Brown   SNES                   snes;
1031c3436cfSJed Brown   TSAdapt                adapt;
10431748224SBarry Smith   PetscReal              time_step;
10549354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
106d1212d36SBarry Smith   char                   dir[16];
107bdad233fSMatthew Knepley 
108bdad233fSMatthew Knepley   PetscFunctionBegin;
1090700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1103194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
111a43b19c4SJed Brown   /* Handle TS type options */
112a43b19c4SJed Brown   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley   /* Handle generic TS options */
1150298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1160298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1170298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
11831748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
11931748224SBarry Smith   if (flg) {
12031748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
12131748224SBarry Smith   }
12249354f04SShri 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);
12349354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1240298fd71SBarry 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);
1250298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1260298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1270298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1280298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
129bdad233fSMatthew Knepley 
130bdad233fSMatthew Knepley   /* Monitor options */
131a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
132eabae89aSBarry Smith   if (flg) {
133ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
134649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
135bdad233fSMatthew Knepley   }
1365180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1375180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1385180491cSLisandro Dalcin 
1394f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
140a7cc72afSBarry Smith   if (opt) {
1410b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1423923b477SBarry Smith     PetscInt       howoften = 1;
143a80ad3e0SBarry Smith 
1440298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
145ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1464f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
147b3603a34SBarry Smith   }
1484f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
149b3603a34SBarry Smith   if (opt) {
1500b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1513923b477SBarry Smith     PetscInt       howoften = 1;
152b3603a34SBarry Smith 
1530298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
15422d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1554f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
156bdad233fSMatthew Knepley   }
1574f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
158ef20d060SBarry Smith   if (opt) {
1590b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1603923b477SBarry Smith     PetscInt       howoften = 1;
161ef20d060SBarry Smith 
1620298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
16322d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1644f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
165ef20d060SBarry Smith   }
166201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
167201da799SBarry Smith   if (opt) {
168201da799SBarry Smith     TSMonitorLGCtx ctx;
169201da799SBarry Smith     PetscInt       howoften = 1;
170201da799SBarry Smith 
1710298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
17222d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
173201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
174201da799SBarry Smith   }
175201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
176201da799SBarry Smith   if (opt) {
177201da799SBarry Smith     TSMonitorLGCtx ctx;
178201da799SBarry Smith     PetscInt       howoften = 1;
179201da799SBarry Smith 
1800298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
18122d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
182201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
183201da799SBarry Smith   }
1848189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1858189c53fSBarry Smith   if (opt) {
1868189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
1878189c53fSBarry Smith     PetscInt          howoften = 1;
1888189c53fSBarry Smith 
1890298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
19022d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1918189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
1928189c53fSBarry Smith   }
193ef20d060SBarry Smith   opt  = PETSC_FALSE;
1940dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
195a7cc72afSBarry Smith   if (opt) {
19683a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
19783a4ac43SBarry Smith     PetscInt         howoften = 1;
198a80ad3e0SBarry Smith 
1990298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
200ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
20183a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
202bdad233fSMatthew Knepley   }
203fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2042d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2052d5ee99bSBarry Smith   if (opt) {
2062d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2072d5ee99bSBarry Smith     PetscReal        bounds[4];
2082d5ee99bSBarry Smith     PetscInt         n = 4;
2092d5ee99bSBarry Smith     PetscDraw        draw;
2102d5ee99bSBarry Smith 
2112d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2122d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2132d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2142d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2152d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
216*4b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
217*4b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
218*4b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
219*4b363babSBarry Smith     //    ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr);
2202d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2212d5ee99bSBarry Smith   }
2222d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2230dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2243a471f94SBarry Smith   if (opt) {
22583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
22683a4ac43SBarry Smith     PetscInt         howoften = 1;
2273a471f94SBarry Smith 
2280298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
229ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
23083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2313a471f94SBarry Smith   }
2323a471f94SBarry Smith   opt  = PETSC_FALSE;
23391b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
234fb1732b5SBarry Smith   if (flg) {
235fb1732b5SBarry Smith     PetscViewer ctx;
236fb1732b5SBarry Smith     if (monfilename[0]) {
237ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
238c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
239fb1732b5SBarry Smith     } else {
240ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2410298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
242fb1732b5SBarry Smith     }
243fb1732b5SBarry Smith   }
244ed81e22dSJed Brown   opt  = PETSC_FALSE;
24591b97e58SBarry 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);
246ed81e22dSJed Brown   if (flg) {
247ed81e22dSJed Brown     const char *ptr,*ptr2;
248ed81e22dSJed Brown     char       *filetemplate;
249ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
250ed81e22dSJed Brown     /* Do some cursory validation of the input. */
251ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
252ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
253ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
254ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
255ce94432eSBarry 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");
256ed81e22dSJed Brown       if (ptr2) break;
257ed81e22dSJed Brown     }
258ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
259ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
260ed81e22dSJed Brown   }
261bdad233fSMatthew Knepley 
262d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
263d1212d36SBarry Smith   if (flg) {
264d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
265d1212d36SBarry Smith     int                 ray = 0;
266d1212d36SBarry Smith     DMDADirection       ddir;
267d1212d36SBarry Smith     DM                  da;
268d1212d36SBarry Smith     PetscMPIInt         rank;
269d1212d36SBarry Smith 
270ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
271d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
272d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
273ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
274d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
275d1212d36SBarry Smith 
276d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
277d1212d36SBarry Smith     ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr);
278d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
279d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
280ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
281d1212d36SBarry Smith     if (!rank) {
282d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
283d1212d36SBarry Smith     }
284d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
285d1212d36SBarry Smith   }
286d1212d36SBarry Smith 
287ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&adapt);CHKERRQ(ierr);
2881c3436cfSJed Brown   ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2891c3436cfSJed Brown 
290d52bd9f3SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
291d52bd9f3SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
292d52bd9f3SBarry Smith 
293bdad233fSMatthew Knepley   /* Handle specific TS options */
294abc0a331SBarry Smith   if (ts->ops->setfromoptions) {
295bdad233fSMatthew Knepley     ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
296bdad233fSMatthew Knepley   }
2975d973c19SBarry Smith 
2985d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
2995d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
300bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
301bdad233fSMatthew Knepley   PetscFunctionReturn(0);
302bdad233fSMatthew Knepley }
303bdad233fSMatthew Knepley 
304bdad233fSMatthew Knepley #undef __FUNCT__
305cdcf91faSSean Farley #undef __FUNCT__
3064a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
307a7a1495cSBarry Smith /*@
3088c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
309a7a1495cSBarry Smith       set with TSSetRHSJacobian().
310a7a1495cSBarry Smith 
311a7a1495cSBarry Smith    Collective on TS and Vec
312a7a1495cSBarry Smith 
313a7a1495cSBarry Smith    Input Parameters:
314316643e7SJed Brown +  ts - the TS context
315a7a1495cSBarry Smith .  t - current timestep
3160910c330SBarry Smith -  U - input vector
317a7a1495cSBarry Smith 
318a7a1495cSBarry Smith    Output Parameters:
319a7a1495cSBarry Smith +  A - Jacobian matrix
320a7a1495cSBarry Smith .  B - optional preconditioning matrix
321a7a1495cSBarry Smith -  flag - flag indicating matrix structure
322a7a1495cSBarry Smith 
323a7a1495cSBarry Smith    Notes:
324a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
325a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
326a7a1495cSBarry Smith 
32794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
328a7a1495cSBarry Smith    flag parameter.
329a7a1495cSBarry Smith 
330a7a1495cSBarry Smith    Level: developer
331a7a1495cSBarry Smith 
332a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
333a7a1495cSBarry Smith 
33494b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
335a7a1495cSBarry Smith @*/
3360910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
337a7a1495cSBarry Smith {
338dfbe8321SBarry Smith   PetscErrorCode ierr;
3390910c330SBarry Smith   PetscInt       Ustate;
34024989b8cSPeter Brune   DM             dm;
341942e3340SBarry Smith   DMTS           tsdm;
34224989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
34324989b8cSPeter Brune   void           *ctx;
34424989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
345a7a1495cSBarry Smith 
346a7a1495cSBarry Smith   PetscFunctionBegin;
3470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3480910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3490910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
35024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
351942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
35224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
3530298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
3540910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
3550910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
3560e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
3570e4ef248SJed Brown     PetscFunctionReturn(0);
358f8ede8e7SJed Brown   }
359d90be118SSean Farley 
360ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
361d90be118SSean Farley 
36224989b8cSPeter Brune   if (rhsjacobianfunc) {
3630910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
364a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
365a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3660910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
367a7a1495cSBarry Smith     PetscStackPop;
3680910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
369a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3700700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3710700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
372ef66eb69SBarry Smith   } else {
373214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
374214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
375214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
376ef66eb69SBarry Smith   }
3770e4ef248SJed Brown   ts->rhsjacobian.time       = t;
3780910c330SBarry Smith   ts->rhsjacobian.X          = U;
3790910c330SBarry Smith   ierr                       = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3800e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
381a7a1495cSBarry Smith   PetscFunctionReturn(0);
382a7a1495cSBarry Smith }
383a7a1495cSBarry Smith 
3844a2ae208SSatish Balay #undef __FUNCT__
3854a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
386316643e7SJed Brown /*@
387d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
388d763cef2SBarry Smith 
389316643e7SJed Brown    Collective on TS and Vec
390316643e7SJed Brown 
391316643e7SJed Brown    Input Parameters:
392316643e7SJed Brown +  ts - the TS context
393316643e7SJed Brown .  t - current time
3940910c330SBarry Smith -  U - state vector
395316643e7SJed Brown 
396316643e7SJed Brown    Output Parameter:
397316643e7SJed Brown .  y - right hand side
398316643e7SJed Brown 
399316643e7SJed Brown    Note:
400316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
401316643e7SJed Brown    is used internally within the nonlinear solvers.
402316643e7SJed Brown 
403316643e7SJed Brown    Level: developer
404316643e7SJed Brown 
405316643e7SJed Brown .keywords: TS, compute
406316643e7SJed Brown 
407316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
408316643e7SJed Brown @*/
4090910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
410d763cef2SBarry Smith {
411dfbe8321SBarry Smith   PetscErrorCode ierr;
41224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
41324989b8cSPeter Brune   TSIFunction    ifunction;
41424989b8cSPeter Brune   void           *ctx;
41524989b8cSPeter Brune   DM             dm;
41624989b8cSPeter Brune 
417d763cef2SBarry Smith   PetscFunctionBegin;
4180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4190910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4200700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
42124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
42224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
4230298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
424d763cef2SBarry Smith 
425ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
426d763cef2SBarry Smith 
4270910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
42824989b8cSPeter Brune   if (rhsfunction) {
429d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
4300910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
431d763cef2SBarry Smith     PetscStackPop;
432214bc6a2SJed Brown   } else {
433214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
434b2cd27e8SJed Brown   }
43544a41b28SSean Farley 
4360910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
437d763cef2SBarry Smith   PetscFunctionReturn(0);
438d763cef2SBarry Smith }
439d763cef2SBarry Smith 
4404a2ae208SSatish Balay #undef __FUNCT__
441ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
442ef20d060SBarry Smith /*@
443ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
444ef20d060SBarry Smith 
445ef20d060SBarry Smith    Collective on TS and Vec
446ef20d060SBarry Smith 
447ef20d060SBarry Smith    Input Parameters:
448ef20d060SBarry Smith +  ts - the TS context
449ef20d060SBarry Smith -  t - current time
450ef20d060SBarry Smith 
451ef20d060SBarry Smith    Output Parameter:
4520910c330SBarry Smith .  U - the solution
453ef20d060SBarry Smith 
454ef20d060SBarry Smith    Note:
455ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
456ef20d060SBarry Smith    is used internally within the nonlinear solvers.
457ef20d060SBarry Smith 
458ef20d060SBarry Smith    Level: developer
459ef20d060SBarry Smith 
460ef20d060SBarry Smith .keywords: TS, compute
461ef20d060SBarry Smith 
462abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
463ef20d060SBarry Smith @*/
4640910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
465ef20d060SBarry Smith {
466ef20d060SBarry Smith   PetscErrorCode     ierr;
467ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
468ef20d060SBarry Smith   void               *ctx;
469ef20d060SBarry Smith   DM                 dm;
470ef20d060SBarry Smith 
471ef20d060SBarry Smith   PetscFunctionBegin;
472ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4730910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
474ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
475ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
476ef20d060SBarry Smith 
477ef20d060SBarry Smith   if (solutionfunction) {
4789b7cd975SBarry Smith     PetscStackPush("TS user solution function");
4790910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
480ef20d060SBarry Smith     PetscStackPop;
481ef20d060SBarry Smith   }
482ef20d060SBarry Smith   PetscFunctionReturn(0);
483ef20d060SBarry Smith }
4849b7cd975SBarry Smith #undef __FUNCT__
4859b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
4869b7cd975SBarry Smith /*@
4879b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
4889b7cd975SBarry Smith 
4899b7cd975SBarry Smith    Collective on TS and Vec
4909b7cd975SBarry Smith 
4919b7cd975SBarry Smith    Input Parameters:
4929b7cd975SBarry Smith +  ts - the TS context
4939b7cd975SBarry Smith -  t - current time
4949b7cd975SBarry Smith 
4959b7cd975SBarry Smith    Output Parameter:
4969b7cd975SBarry Smith .  U - the function value
4979b7cd975SBarry Smith 
4989b7cd975SBarry Smith    Note:
4999b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5009b7cd975SBarry Smith    is used internally within the nonlinear solvers.
5019b7cd975SBarry Smith 
5029b7cd975SBarry Smith    Level: developer
5039b7cd975SBarry Smith 
5049b7cd975SBarry Smith .keywords: TS, compute
5059b7cd975SBarry Smith 
5069b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
5079b7cd975SBarry Smith @*/
5089b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
5099b7cd975SBarry Smith {
5109b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
5119b7cd975SBarry Smith   void               *ctx;
5129b7cd975SBarry Smith   DM                 dm;
5139b7cd975SBarry Smith 
5149b7cd975SBarry Smith   PetscFunctionBegin;
5159b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5169b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5179b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
5189b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
5199b7cd975SBarry Smith 
5209b7cd975SBarry Smith   if (forcing) {
5219b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
5229b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
5239b7cd975SBarry Smith     PetscStackPop;
5249b7cd975SBarry Smith   }
5259b7cd975SBarry Smith   PetscFunctionReturn(0);
5269b7cd975SBarry Smith }
527ef20d060SBarry Smith 
528ef20d060SBarry Smith #undef __FUNCT__
529214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
530214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
531214bc6a2SJed Brown {
5322dd45cf8SJed Brown   Vec            F;
533214bc6a2SJed Brown   PetscErrorCode ierr;
534214bc6a2SJed Brown 
535214bc6a2SJed Brown   PetscFunctionBegin;
5360298fd71SBarry Smith   *Frhs = NULL;
5370298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
538214bc6a2SJed Brown   if (!ts->Frhs) {
5392dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
540214bc6a2SJed Brown   }
541214bc6a2SJed Brown   *Frhs = ts->Frhs;
542214bc6a2SJed Brown   PetscFunctionReturn(0);
543214bc6a2SJed Brown }
544214bc6a2SJed Brown 
545214bc6a2SJed Brown #undef __FUNCT__
546214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
547214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
548214bc6a2SJed Brown {
549214bc6a2SJed Brown   Mat            A,B;
5502dd45cf8SJed Brown   PetscErrorCode ierr;
551214bc6a2SJed Brown 
552214bc6a2SJed Brown   PetscFunctionBegin;
5530298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
554214bc6a2SJed Brown   if (Arhs) {
555214bc6a2SJed Brown     if (!ts->Arhs) {
556214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
557214bc6a2SJed Brown     }
558214bc6a2SJed Brown     *Arhs = ts->Arhs;
559214bc6a2SJed Brown   }
560214bc6a2SJed Brown   if (Brhs) {
561214bc6a2SJed Brown     if (!ts->Brhs) {
562214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
563214bc6a2SJed Brown     }
564214bc6a2SJed Brown     *Brhs = ts->Brhs;
565214bc6a2SJed Brown   }
566214bc6a2SJed Brown   PetscFunctionReturn(0);
567214bc6a2SJed Brown }
568214bc6a2SJed Brown 
569214bc6a2SJed Brown #undef __FUNCT__
570316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
571316643e7SJed Brown /*@
5720910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
573316643e7SJed Brown 
574316643e7SJed Brown    Collective on TS and Vec
575316643e7SJed Brown 
576316643e7SJed Brown    Input Parameters:
577316643e7SJed Brown +  ts - the TS context
578316643e7SJed Brown .  t - current time
5790910c330SBarry Smith .  U - state vector
5800910c330SBarry Smith .  Udot - time derivative of state vector
581214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
582316643e7SJed Brown 
583316643e7SJed Brown    Output Parameter:
584316643e7SJed Brown .  Y - right hand side
585316643e7SJed Brown 
586316643e7SJed Brown    Note:
587316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
588316643e7SJed Brown    is used internally within the nonlinear solvers.
589316643e7SJed Brown 
590316643e7SJed Brown    If the user did did not write their equations in implicit form, this
591316643e7SJed Brown    function recasts them in implicit form.
592316643e7SJed Brown 
593316643e7SJed Brown    Level: developer
594316643e7SJed Brown 
595316643e7SJed Brown .keywords: TS, compute
596316643e7SJed Brown 
597316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
598316643e7SJed Brown @*/
5990910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
600316643e7SJed Brown {
601316643e7SJed Brown   PetscErrorCode ierr;
60224989b8cSPeter Brune   TSIFunction    ifunction;
60324989b8cSPeter Brune   TSRHSFunction  rhsfunction;
60424989b8cSPeter Brune   void           *ctx;
60524989b8cSPeter Brune   DM             dm;
606316643e7SJed Brown 
607316643e7SJed Brown   PetscFunctionBegin;
6080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6090910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6100910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
6110700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
612316643e7SJed Brown 
61324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
61424989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
6150298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
61624989b8cSPeter Brune 
617ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
618d90be118SSean Farley 
6190910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
62024989b8cSPeter Brune   if (ifunction) {
621316643e7SJed Brown     PetscStackPush("TS user implicit function");
6220910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
623316643e7SJed Brown     PetscStackPop;
624214bc6a2SJed Brown   }
625214bc6a2SJed Brown   if (imex) {
62624989b8cSPeter Brune     if (!ifunction) {
6270910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
6282dd45cf8SJed Brown     }
62924989b8cSPeter Brune   } else if (rhsfunction) {
63024989b8cSPeter Brune     if (ifunction) {
631214bc6a2SJed Brown       Vec Frhs;
632214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
6330910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
634214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
6352dd45cf8SJed Brown     } else {
6360910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
6370910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
638316643e7SJed Brown     }
6394a6899ffSJed Brown   }
6400910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
641316643e7SJed Brown   PetscFunctionReturn(0);
642316643e7SJed Brown }
643316643e7SJed Brown 
644316643e7SJed Brown #undef __FUNCT__
645316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
646316643e7SJed Brown /*@
647316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
648316643e7SJed Brown 
649316643e7SJed Brown    Collective on TS and Vec
650316643e7SJed Brown 
651316643e7SJed Brown    Input
652316643e7SJed Brown       Input Parameters:
653316643e7SJed Brown +  ts - the TS context
654316643e7SJed Brown .  t - current timestep
6550910c330SBarry Smith .  U - state vector
6560910c330SBarry Smith .  Udot - time derivative of state vector
657214bc6a2SJed Brown .  shift - shift to apply, see note below
658214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
659316643e7SJed Brown 
660316643e7SJed Brown    Output Parameters:
661316643e7SJed Brown +  A - Jacobian matrix
662316643e7SJed Brown .  B - optional preconditioning matrix
663316643e7SJed Brown -  flag - flag indicating matrix structure
664316643e7SJed Brown 
665316643e7SJed Brown    Notes:
6660910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
667316643e7SJed Brown 
6680910c330SBarry Smith    dF/dU + shift*dF/dUdot
669316643e7SJed Brown 
670316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
671316643e7SJed Brown    is used internally within the nonlinear solvers.
672316643e7SJed Brown 
673316643e7SJed Brown    Level: developer
674316643e7SJed Brown 
675316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
676316643e7SJed Brown 
677316643e7SJed Brown .seealso:  TSSetIJacobian()
67863495f91SJed Brown @*/
6790910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
680316643e7SJed Brown {
6810910c330SBarry Smith   PetscInt       Ustate, Udotstate;
682316643e7SJed Brown   PetscErrorCode ierr;
68324989b8cSPeter Brune   TSIJacobian    ijacobian;
68424989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
68524989b8cSPeter Brune   DM             dm;
68624989b8cSPeter Brune   void           *ctx;
687316643e7SJed Brown 
688316643e7SJed Brown   PetscFunctionBegin;
6890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6900910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6910910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
692316643e7SJed Brown   PetscValidPointer(A,6);
6930700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
694316643e7SJed Brown   PetscValidPointer(B,7);
6950700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
696316643e7SJed Brown   PetscValidPointer(flg,8);
69724989b8cSPeter Brune 
69824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
69924989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
7000298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
70124989b8cSPeter Brune 
7020910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
7030910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
7040910c330SBarry Smith   if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == U && ts->ijacobian.Xstate == Ustate && ts->ijacobian.Xdot == Udot && ts->ijacobian.Xdotstate == Udotstate && ts->ijacobian.imex == imex))) {
7050026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
7060026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
7070026cea9SSean Farley     PetscFunctionReturn(0);
7080026cea9SSean Farley   }
709316643e7SJed Brown 
710ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
711316643e7SJed Brown 
7124e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
7130910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
71424989b8cSPeter Brune   if (ijacobian) {
7152dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
716316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
7170910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
718316643e7SJed Brown     PetscStackPop;
719214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
720214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
721214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
7224a6899ffSJed Brown   }
723214bc6a2SJed Brown   if (imex) {
724b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
725214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
7262dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
727214bc6a2SJed Brown       if (*A != *B) {
728214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
729214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
730214bc6a2SJed Brown       }
731214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
732214bc6a2SJed Brown     }
733214bc6a2SJed Brown   } else {
73424989b8cSPeter Brune     if (!ijacobian) {
7350910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr);
736214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
737214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
738316643e7SJed Brown       if (*A != *B) {
739316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
740316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
741316643e7SJed Brown       }
74224989b8cSPeter Brune     } else if (rhsjacobian) {
743214bc6a2SJed Brown       Mat          Arhs,Brhs;
744214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
745214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
7460910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
747214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
748214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
749214bc6a2SJed Brown       if (*A != *B) {
750214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
751214bc6a2SJed Brown       }
752214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
753214bc6a2SJed Brown     }
754316643e7SJed Brown   }
7550026cea9SSean Farley 
7560026cea9SSean Farley   ts->ijacobian.time = t;
7570910c330SBarry Smith   ts->ijacobian.X    = U;
7580910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
759bbd56ea5SKarl Rupp 
7600910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
7610910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
762bbd56ea5SKarl Rupp 
7630026cea9SSean Farley   ts->ijacobian.shift      = shift;
7640026cea9SSean Farley   ts->ijacobian.imex       = imex;
7650026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
766bbd56ea5SKarl Rupp 
7670910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
768316643e7SJed Brown   PetscFunctionReturn(0);
769316643e7SJed Brown }
770316643e7SJed Brown 
771316643e7SJed Brown #undef __FUNCT__
7724a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
773d763cef2SBarry Smith /*@C
774d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
775b5abc632SBarry Smith     where U_t = G(t,u).
776d763cef2SBarry Smith 
7773f9fe445SBarry Smith     Logically Collective on TS
778d763cef2SBarry Smith 
779d763cef2SBarry Smith     Input Parameters:
780d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
7810298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
782d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
783d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
7840298fd71SBarry Smith           function evaluation routine (may be NULL)
785d763cef2SBarry Smith 
786d763cef2SBarry Smith     Calling sequence of func:
78787828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
788d763cef2SBarry Smith 
789d763cef2SBarry Smith +   t - current timestep
790d763cef2SBarry Smith .   u - input vector
791d763cef2SBarry Smith .   F - function vector
792d763cef2SBarry Smith -   ctx - [optional] user-defined function context
793d763cef2SBarry Smith 
794d763cef2SBarry Smith     Level: beginner
795d763cef2SBarry Smith 
796d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
797d763cef2SBarry Smith 
798d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
799d763cef2SBarry Smith @*/
800089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
801d763cef2SBarry Smith {
802089b2837SJed Brown   PetscErrorCode ierr;
803089b2837SJed Brown   SNES           snes;
8040298fd71SBarry Smith   Vec            ralloc = NULL;
80524989b8cSPeter Brune   DM             dm;
806d763cef2SBarry Smith 
807089b2837SJed Brown   PetscFunctionBegin;
8080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
809ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
81024989b8cSPeter Brune 
81124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
81224989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
813089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
814e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
815e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
816e856ceecSJed Brown     r    = ralloc;
817e856ceecSJed Brown   }
818089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
819e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
820d763cef2SBarry Smith   PetscFunctionReturn(0);
821d763cef2SBarry Smith }
822d763cef2SBarry Smith 
8234a2ae208SSatish Balay #undef __FUNCT__
824ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
825ef20d060SBarry Smith /*@C
826abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
827ef20d060SBarry Smith 
828ef20d060SBarry Smith     Logically Collective on TS
829ef20d060SBarry Smith 
830ef20d060SBarry Smith     Input Parameters:
831ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
832ef20d060SBarry Smith .   f - routine for evaluating the solution
833ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
8340298fd71SBarry Smith           function evaluation routine (may be NULL)
835ef20d060SBarry Smith 
836ef20d060SBarry Smith     Calling sequence of func:
837ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
838ef20d060SBarry Smith 
839ef20d060SBarry Smith +   t - current timestep
840ef20d060SBarry Smith .   u - output vector
841ef20d060SBarry Smith -   ctx - [optional] user-defined function context
842ef20d060SBarry Smith 
843abd5a294SJed Brown     Notes:
844abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
845abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
846abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
847abd5a294SJed Brown 
8484f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
849abd5a294SJed Brown 
850ef20d060SBarry Smith     Level: beginner
851ef20d060SBarry Smith 
852ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
853ef20d060SBarry Smith 
8549b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
855ef20d060SBarry Smith @*/
856ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
857ef20d060SBarry Smith {
858ef20d060SBarry Smith   PetscErrorCode ierr;
859ef20d060SBarry Smith   DM             dm;
860ef20d060SBarry Smith 
861ef20d060SBarry Smith   PetscFunctionBegin;
862ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
863ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
864ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
865ef20d060SBarry Smith   PetscFunctionReturn(0);
866ef20d060SBarry Smith }
867ef20d060SBarry Smith 
868ef20d060SBarry Smith #undef __FUNCT__
8699b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
8709b7cd975SBarry Smith /*@C
8719b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
8729b7cd975SBarry Smith 
8739b7cd975SBarry Smith     Logically Collective on TS
8749b7cd975SBarry Smith 
8759b7cd975SBarry Smith     Input Parameters:
8769b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
8779b7cd975SBarry Smith .   f - routine for evaluating the forcing function
8789b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
8790298fd71SBarry Smith           function evaluation routine (may be NULL)
8809b7cd975SBarry Smith 
8819b7cd975SBarry Smith     Calling sequence of func:
8829b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
8839b7cd975SBarry Smith 
8849b7cd975SBarry Smith +   t - current timestep
8859b7cd975SBarry Smith .   u - output vector
8869b7cd975SBarry Smith -   ctx - [optional] user-defined function context
8879b7cd975SBarry Smith 
8889b7cd975SBarry Smith     Notes:
8899b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
8909b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
8919b7cd975SBarry Smith 
8929b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
8939b7cd975SBarry Smith 
8949b7cd975SBarry Smith     Level: beginner
8959b7cd975SBarry Smith 
8969b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
8979b7cd975SBarry Smith 
8989b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
8999b7cd975SBarry Smith @*/
9009b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9019b7cd975SBarry Smith {
9029b7cd975SBarry Smith   PetscErrorCode ierr;
9039b7cd975SBarry Smith   DM             dm;
9049b7cd975SBarry Smith 
9059b7cd975SBarry Smith   PetscFunctionBegin;
9069b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9079b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
9089b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
9099b7cd975SBarry Smith   PetscFunctionReturn(0);
9109b7cd975SBarry Smith }
9119b7cd975SBarry Smith 
9129b7cd975SBarry Smith #undef __FUNCT__
9134a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
914d763cef2SBarry Smith /*@C
915d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
916b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
917d763cef2SBarry Smith 
9183f9fe445SBarry Smith    Logically Collective on TS
919d763cef2SBarry Smith 
920d763cef2SBarry Smith    Input Parameters:
921d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
922d763cef2SBarry Smith .  A   - Jacobian matrix
923d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
924d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
925d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
9260298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
927d763cef2SBarry Smith 
928d763cef2SBarry Smith    Calling sequence of func:
92987828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
930d763cef2SBarry Smith 
931d763cef2SBarry Smith +  t - current timestep
932d763cef2SBarry Smith .  u - input vector
933d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
934d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
935d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
93694b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
937d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
938d763cef2SBarry Smith 
939d763cef2SBarry Smith    Notes:
94094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
941d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
942d763cef2SBarry Smith 
943d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
944d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
94556335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
946d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
947d763cef2SBarry Smith    throughout the global iterations.
948d763cef2SBarry Smith 
949d763cef2SBarry Smith    Level: beginner
950d763cef2SBarry Smith 
951d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
952d763cef2SBarry Smith 
953ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
954d763cef2SBarry Smith 
955d763cef2SBarry Smith @*/
956089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
957d763cef2SBarry Smith {
958277b19d0SLisandro Dalcin   PetscErrorCode ierr;
959089b2837SJed Brown   SNES           snes;
96024989b8cSPeter Brune   DM             dm;
96124989b8cSPeter Brune   TSIJacobian    ijacobian;
962277b19d0SLisandro Dalcin 
963d763cef2SBarry Smith   PetscFunctionBegin;
9640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
965277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
966277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
967277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
968277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
969d763cef2SBarry Smith 
97024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
97124989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
9720298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
97324989b8cSPeter Brune 
974089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
9755f659677SPeter Brune   if (!ijacobian) {
976089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
9770e4ef248SJed Brown   }
9780e4ef248SJed Brown   if (A) {
9790e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
9800e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
981bbd56ea5SKarl Rupp 
9820e4ef248SJed Brown     ts->Arhs = A;
9830e4ef248SJed Brown   }
9840e4ef248SJed Brown   if (B) {
9850e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
9860e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
987bbd56ea5SKarl Rupp 
9880e4ef248SJed Brown     ts->Brhs = B;
9890e4ef248SJed Brown   }
990d763cef2SBarry Smith   PetscFunctionReturn(0);
991d763cef2SBarry Smith }
992d763cef2SBarry Smith 
993316643e7SJed Brown 
994316643e7SJed Brown #undef __FUNCT__
995316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
996316643e7SJed Brown /*@C
997b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
998316643e7SJed Brown 
9993f9fe445SBarry Smith    Logically Collective on TS
1000316643e7SJed Brown 
1001316643e7SJed Brown    Input Parameters:
1002316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10030298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1004316643e7SJed Brown .  f   - the function evaluation routine
10050298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1006316643e7SJed Brown 
1007316643e7SJed Brown    Calling sequence of f:
1008316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1009316643e7SJed Brown 
1010316643e7SJed Brown +  t   - time at step/stage being solved
1011316643e7SJed Brown .  u   - state vector
1012316643e7SJed Brown .  u_t - time derivative of state vector
1013316643e7SJed Brown .  F   - function vector
1014316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1015316643e7SJed Brown 
1016316643e7SJed Brown    Important:
1017d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
1018316643e7SJed Brown 
1019316643e7SJed Brown    Level: beginner
1020316643e7SJed Brown 
1021316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1022316643e7SJed Brown 
1023d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1024316643e7SJed Brown @*/
1025089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1026316643e7SJed Brown {
1027089b2837SJed Brown   PetscErrorCode ierr;
1028089b2837SJed Brown   SNES           snes;
10290298fd71SBarry Smith   Vec            resalloc = NULL;
103024989b8cSPeter Brune   DM             dm;
1031316643e7SJed Brown 
1032316643e7SJed Brown   PetscFunctionBegin;
10330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1034ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
103524989b8cSPeter Brune 
103624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
103724989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
103824989b8cSPeter Brune 
1039089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1040e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1041e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1042e856ceecSJed Brown     res  = resalloc;
1043e856ceecSJed Brown   }
1044089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1045e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1046089b2837SJed Brown   PetscFunctionReturn(0);
1047089b2837SJed Brown }
1048089b2837SJed Brown 
1049089b2837SJed Brown #undef __FUNCT__
1050089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1051089b2837SJed Brown /*@C
1052089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1053089b2837SJed Brown 
1054089b2837SJed Brown    Not Collective
1055089b2837SJed Brown 
1056089b2837SJed Brown    Input Parameter:
1057089b2837SJed Brown .  ts - the TS context
1058089b2837SJed Brown 
1059089b2837SJed Brown    Output Parameter:
10600298fd71SBarry Smith +  r - vector to hold residual (or NULL)
10610298fd71SBarry Smith .  func - the function to compute residual (or NULL)
10620298fd71SBarry Smith -  ctx - the function context (or NULL)
1063089b2837SJed Brown 
1064089b2837SJed Brown    Level: advanced
1065089b2837SJed Brown 
1066089b2837SJed Brown .keywords: TS, nonlinear, get, function
1067089b2837SJed Brown 
1068089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1069089b2837SJed Brown @*/
1070089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1071089b2837SJed Brown {
1072089b2837SJed Brown   PetscErrorCode ierr;
1073089b2837SJed Brown   SNES           snes;
107424989b8cSPeter Brune   DM             dm;
1075089b2837SJed Brown 
1076089b2837SJed Brown   PetscFunctionBegin;
1077089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1078089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10790298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
108024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
108124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1082089b2837SJed Brown   PetscFunctionReturn(0);
1083089b2837SJed Brown }
1084089b2837SJed Brown 
1085089b2837SJed Brown #undef __FUNCT__
1086089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1087089b2837SJed Brown /*@C
1088089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1089089b2837SJed Brown 
1090089b2837SJed Brown    Not Collective
1091089b2837SJed Brown 
1092089b2837SJed Brown    Input Parameter:
1093089b2837SJed Brown .  ts - the TS context
1094089b2837SJed Brown 
1095089b2837SJed Brown    Output Parameter:
10960298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
10970298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
10980298fd71SBarry Smith -  ctx - the function context (or NULL)
1099089b2837SJed Brown 
1100089b2837SJed Brown    Level: advanced
1101089b2837SJed Brown 
1102089b2837SJed Brown .keywords: TS, nonlinear, get, function
1103089b2837SJed Brown 
1104089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1105089b2837SJed Brown @*/
1106089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1107089b2837SJed Brown {
1108089b2837SJed Brown   PetscErrorCode ierr;
1109089b2837SJed Brown   SNES           snes;
111024989b8cSPeter Brune   DM             dm;
1111089b2837SJed Brown 
1112089b2837SJed Brown   PetscFunctionBegin;
1113089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1114089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11150298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
111624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
111724989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1118316643e7SJed Brown   PetscFunctionReturn(0);
1119316643e7SJed Brown }
1120316643e7SJed Brown 
1121316643e7SJed Brown #undef __FUNCT__
1122316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1123316643e7SJed Brown /*@C
1124a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1125a4f0a591SBarry Smith         you provided with TSSetIFunction().
1126316643e7SJed Brown 
11273f9fe445SBarry Smith    Logically Collective on TS
1128316643e7SJed Brown 
1129316643e7SJed Brown    Input Parameters:
1130316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1131316643e7SJed Brown .  A   - Jacobian matrix
1132316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
1133316643e7SJed Brown .  f   - the Jacobian evaluation routine
11340298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1135316643e7SJed Brown 
1136316643e7SJed Brown    Calling sequence of f:
11371b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
1138316643e7SJed Brown 
1139316643e7SJed Brown +  t    - time at step/stage being solved
11401b4a444bSJed Brown .  U    - state vector
11411b4a444bSJed Brown .  U_t  - time derivative of state vector
1142316643e7SJed Brown .  a    - shift
1143b5abc632SBarry Smith .  A    - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1144316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
1145316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1146316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1147316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1148316643e7SJed Brown 
1149316643e7SJed Brown    Notes:
1150316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
1151316643e7SJed Brown 
1152a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1153b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1154a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1155a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1156a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1157a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1158a4f0a591SBarry Smith 
1159316643e7SJed Brown    Level: beginner
1160316643e7SJed Brown 
1161316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1162316643e7SJed Brown 
1163ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
1164316643e7SJed Brown 
1165316643e7SJed Brown @*/
11667087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1167316643e7SJed Brown {
1168316643e7SJed Brown   PetscErrorCode ierr;
1169089b2837SJed Brown   SNES           snes;
117024989b8cSPeter Brune   DM             dm;
1171316643e7SJed Brown 
1172316643e7SJed Brown   PetscFunctionBegin;
11730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11740700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
11750700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1176316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1177316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
117824989b8cSPeter Brune 
117924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
118024989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
118124989b8cSPeter Brune 
1182089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1183089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1184316643e7SJed Brown   PetscFunctionReturn(0);
1185316643e7SJed Brown }
1186316643e7SJed Brown 
11874a2ae208SSatish Balay #undef __FUNCT__
118855849f57SBarry Smith #define __FUNCT__ "TSLoad"
118955849f57SBarry Smith /*@C
119055849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
119155849f57SBarry Smith 
119255849f57SBarry Smith   Collective on PetscViewer
119355849f57SBarry Smith 
119455849f57SBarry Smith   Input Parameters:
119555849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
119655849f57SBarry Smith            some related function before a call to TSLoad().
119755849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
119855849f57SBarry Smith 
119955849f57SBarry Smith    Level: intermediate
120055849f57SBarry Smith 
120155849f57SBarry Smith   Notes:
120255849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
120355849f57SBarry Smith 
120455849f57SBarry Smith   Notes for advanced users:
120555849f57SBarry Smith   Most users should not need to know the details of the binary storage
120655849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
120755849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
120855849f57SBarry Smith   format is
120955849f57SBarry Smith .vb
121055849f57SBarry Smith      has not yet been determined
121155849f57SBarry Smith .ve
121255849f57SBarry Smith 
121355849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
121455849f57SBarry Smith @*/
1215f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
121655849f57SBarry Smith {
121755849f57SBarry Smith   PetscErrorCode ierr;
121855849f57SBarry Smith   PetscBool      isbinary;
121955849f57SBarry Smith   PetscInt       classid;
122055849f57SBarry Smith   char           type[256];
12212d53ad75SBarry Smith   DMTS           sdm;
1222ad6bc421SBarry Smith   DM             dm;
122355849f57SBarry Smith 
122455849f57SBarry Smith   PetscFunctionBegin;
1225f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
122655849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
122755849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
122855849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
122955849f57SBarry Smith 
123055849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1231ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
123255849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1233f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1234f2c2a1b9SBarry Smith   if (ts->ops->load) {
1235f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1236f2c2a1b9SBarry Smith   }
1237ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1238ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1239ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1240f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1241f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
12422d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12432d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
124455849f57SBarry Smith   PetscFunctionReturn(0);
124555849f57SBarry Smith }
124655849f57SBarry Smith 
12479804daf3SBarry Smith #include <petscdraw.h>
124855849f57SBarry Smith #undef __FUNCT__
12494a2ae208SSatish Balay #define __FUNCT__ "TSView"
12507e2c5f70SBarry Smith /*@C
1251d763cef2SBarry Smith     TSView - Prints the TS data structure.
1252d763cef2SBarry Smith 
12534c49b128SBarry Smith     Collective on TS
1254d763cef2SBarry Smith 
1255d763cef2SBarry Smith     Input Parameters:
1256d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1257d763cef2SBarry Smith -   viewer - visualization context
1258d763cef2SBarry Smith 
1259d763cef2SBarry Smith     Options Database Key:
1260d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1261d763cef2SBarry Smith 
1262d763cef2SBarry Smith     Notes:
1263d763cef2SBarry Smith     The available visualization contexts include
1264b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1265b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1266d763cef2SBarry Smith          output where only the first processor opens
1267d763cef2SBarry Smith          the file.  All other processors send their
1268d763cef2SBarry Smith          data to the first processor to print.
1269d763cef2SBarry Smith 
1270d763cef2SBarry Smith     The user can open an alternative visualization context with
1271b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1272d763cef2SBarry Smith 
1273d763cef2SBarry Smith     Level: beginner
1274d763cef2SBarry Smith 
1275d763cef2SBarry Smith .keywords: TS, timestep, view
1276d763cef2SBarry Smith 
1277b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1278d763cef2SBarry Smith @*/
12797087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1280d763cef2SBarry Smith {
1281dfbe8321SBarry Smith   PetscErrorCode ierr;
128219fd82e9SBarry Smith   TSType         type;
12832b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
12842d53ad75SBarry Smith   DMTS           sdm;
1285d763cef2SBarry Smith 
1286d763cef2SBarry Smith   PetscFunctionBegin;
12870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12883050cee2SBarry Smith   if (!viewer) {
1289ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
12903050cee2SBarry Smith   }
12910700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1292c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1293fd16b177SBarry Smith 
1294251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1295251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
129655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
12972b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
129832077d6dSBarry Smith   if (iascii) {
1299317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
130077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1301a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1302d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
13035ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1304c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1305d763cef2SBarry Smith     }
13065ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1307193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
13082d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13092d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1310d52bd9f3SBarry Smith     if (ts->ops->view) {
1311d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1312d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1313d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1314d52bd9f3SBarry Smith     }
13150f5bd95cSBarry Smith   } else if (isstring) {
1316a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1317b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
131855849f57SBarry Smith   } else if (isbinary) {
131955849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
132055849f57SBarry Smith     MPI_Comm    comm;
132155849f57SBarry Smith     PetscMPIInt rank;
132255849f57SBarry Smith     char        type[256];
132355849f57SBarry Smith 
132455849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
132555849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
132655849f57SBarry Smith     if (!rank) {
132755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
132855849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
132955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
133055849f57SBarry Smith     }
133155849f57SBarry Smith     if (ts->ops->view) {
133255849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
133355849f57SBarry Smith     }
1334f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1335f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
13362d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13372d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
13382b0a91c0SBarry Smith   } else if (isdraw) {
13392b0a91c0SBarry Smith     PetscDraw draw;
13402b0a91c0SBarry Smith     char      str[36];
134189fd9fafSBarry Smith     PetscReal x,y,bottom,h;
13422b0a91c0SBarry Smith 
13432b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
13442b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
13452b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
13462b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
13470298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
134889fd9fafSBarry Smith     bottom = y - h;
13492b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
13502b0a91c0SBarry Smith     if (ts->ops->view) {
13512b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
13522b0a91c0SBarry Smith     }
13532b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1354d763cef2SBarry Smith   }
1355b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1356251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1357b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1358d763cef2SBarry Smith   PetscFunctionReturn(0);
1359d763cef2SBarry Smith }
1360d763cef2SBarry Smith 
1361d763cef2SBarry Smith 
13624a2ae208SSatish Balay #undef __FUNCT__
13634a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1364b07ff414SBarry Smith /*@
1365d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1366d763cef2SBarry Smith    the timesteppers.
1367d763cef2SBarry Smith 
13683f9fe445SBarry Smith    Logically Collective on TS
1369d763cef2SBarry Smith 
1370d763cef2SBarry Smith    Input Parameters:
1371d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1372d763cef2SBarry Smith -  usrP - optional user context
1373d763cef2SBarry Smith 
1374d763cef2SBarry Smith    Level: intermediate
1375d763cef2SBarry Smith 
1376d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1377d763cef2SBarry Smith 
1378d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1379d763cef2SBarry Smith @*/
13807087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1381d763cef2SBarry Smith {
1382d763cef2SBarry Smith   PetscFunctionBegin;
13830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1384d763cef2SBarry Smith   ts->user = usrP;
1385d763cef2SBarry Smith   PetscFunctionReturn(0);
1386d763cef2SBarry Smith }
1387d763cef2SBarry Smith 
13884a2ae208SSatish Balay #undef __FUNCT__
13894a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1390b07ff414SBarry Smith /*@
1391d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1392d763cef2SBarry Smith     timestepper.
1393d763cef2SBarry Smith 
1394d763cef2SBarry Smith     Not Collective
1395d763cef2SBarry Smith 
1396d763cef2SBarry Smith     Input Parameter:
1397d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1398d763cef2SBarry Smith 
1399d763cef2SBarry Smith     Output Parameter:
1400d763cef2SBarry Smith .   usrP - user context
1401d763cef2SBarry Smith 
1402d763cef2SBarry Smith     Level: intermediate
1403d763cef2SBarry Smith 
1404d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1405d763cef2SBarry Smith 
1406d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1407d763cef2SBarry Smith @*/
1408e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1409d763cef2SBarry Smith {
1410d763cef2SBarry Smith   PetscFunctionBegin;
14110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1412e71120c6SJed Brown   *(void**)usrP = ts->user;
1413d763cef2SBarry Smith   PetscFunctionReturn(0);
1414d763cef2SBarry Smith }
1415d763cef2SBarry Smith 
14164a2ae208SSatish Balay #undef __FUNCT__
14174a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1418d763cef2SBarry Smith /*@
1419b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1420d763cef2SBarry Smith 
1421d763cef2SBarry Smith    Not Collective
1422d763cef2SBarry Smith 
1423d763cef2SBarry Smith    Input Parameter:
1424d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1425d763cef2SBarry Smith 
1426d763cef2SBarry Smith    Output Parameter:
1427b8123daeSJed Brown .  iter - number of steps completed so far
1428d763cef2SBarry Smith 
1429d763cef2SBarry Smith    Level: intermediate
1430d763cef2SBarry Smith 
1431d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1432b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1433d763cef2SBarry Smith @*/
14347087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1435d763cef2SBarry Smith {
1436d763cef2SBarry Smith   PetscFunctionBegin;
14370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14384482741eSBarry Smith   PetscValidIntPointer(iter,2);
1439d763cef2SBarry Smith   *iter = ts->steps;
1440d763cef2SBarry Smith   PetscFunctionReturn(0);
1441d763cef2SBarry Smith }
1442d763cef2SBarry Smith 
14434a2ae208SSatish Balay #undef __FUNCT__
14444a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1445d763cef2SBarry Smith /*@
1446d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1447d763cef2SBarry Smith    as well as the initial time.
1448d763cef2SBarry Smith 
14493f9fe445SBarry Smith    Logically Collective on TS
1450d763cef2SBarry Smith 
1451d763cef2SBarry Smith    Input Parameters:
1452d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1453d763cef2SBarry Smith .  initial_time - the initial time
1454d763cef2SBarry Smith -  time_step - the size of the timestep
1455d763cef2SBarry Smith 
1456d763cef2SBarry Smith    Level: intermediate
1457d763cef2SBarry Smith 
1458d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1459d763cef2SBarry Smith 
1460d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1461d763cef2SBarry Smith @*/
14627087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1463d763cef2SBarry Smith {
1464e144a568SJed Brown   PetscErrorCode ierr;
1465e144a568SJed Brown 
1466d763cef2SBarry Smith   PetscFunctionBegin;
14670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1468e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1469d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1470d763cef2SBarry Smith   PetscFunctionReturn(0);
1471d763cef2SBarry Smith }
1472d763cef2SBarry Smith 
14734a2ae208SSatish Balay #undef __FUNCT__
14744a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1475d763cef2SBarry Smith /*@
1476d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1477d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1478d763cef2SBarry Smith 
14793f9fe445SBarry Smith    Logically Collective on TS
1480d763cef2SBarry Smith 
1481d763cef2SBarry Smith    Input Parameters:
1482d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1483d763cef2SBarry Smith -  time_step - the size of the timestep
1484d763cef2SBarry Smith 
1485d763cef2SBarry Smith    Level: intermediate
1486d763cef2SBarry Smith 
1487d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1488d763cef2SBarry Smith 
1489d763cef2SBarry Smith .keywords: TS, set, timestep
1490d763cef2SBarry Smith @*/
14917087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1492d763cef2SBarry Smith {
1493d763cef2SBarry Smith   PetscFunctionBegin;
14940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1495c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1496d763cef2SBarry Smith   ts->time_step      = time_step;
149731748224SBarry Smith   ts->time_step_orig = time_step;
1498d763cef2SBarry Smith   PetscFunctionReturn(0);
1499d763cef2SBarry Smith }
1500d763cef2SBarry Smith 
15014a2ae208SSatish Balay #undef __FUNCT__
1502a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1503a43b19c4SJed Brown /*@
150449354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
150549354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
150649354f04SShri Abhyankar      or just return at the final time TS computed.
1507a43b19c4SJed Brown 
1508a43b19c4SJed Brown   Logically Collective on TS
1509a43b19c4SJed Brown 
1510a43b19c4SJed Brown    Input Parameter:
1511a43b19c4SJed Brown +   ts - the time-step context
151249354f04SShri Abhyankar -   eftopt - exact final time option
1513a43b19c4SJed Brown 
1514a43b19c4SJed Brown    Level: beginner
1515a43b19c4SJed Brown 
1516a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1517a43b19c4SJed Brown @*/
151849354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1519a43b19c4SJed Brown {
1520a43b19c4SJed Brown   PetscFunctionBegin;
1521a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
152249354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
152349354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1524a43b19c4SJed Brown   PetscFunctionReturn(0);
1525a43b19c4SJed Brown }
1526a43b19c4SJed Brown 
1527a43b19c4SJed Brown #undef __FUNCT__
15284a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1529d763cef2SBarry Smith /*@
1530d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1531d763cef2SBarry Smith 
1532d763cef2SBarry Smith    Not Collective
1533d763cef2SBarry Smith 
1534d763cef2SBarry Smith    Input Parameter:
1535d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1536d763cef2SBarry Smith 
1537d763cef2SBarry Smith    Output Parameter:
1538d763cef2SBarry Smith .  dt - the current timestep size
1539d763cef2SBarry Smith 
1540d763cef2SBarry Smith    Level: intermediate
1541d763cef2SBarry Smith 
1542d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1543d763cef2SBarry Smith 
1544d763cef2SBarry Smith .keywords: TS, get, timestep
1545d763cef2SBarry Smith @*/
15467087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1547d763cef2SBarry Smith {
1548d763cef2SBarry Smith   PetscFunctionBegin;
15490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1550f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1551d763cef2SBarry Smith   *dt = ts->time_step;
1552d763cef2SBarry Smith   PetscFunctionReturn(0);
1553d763cef2SBarry Smith }
1554d763cef2SBarry Smith 
15554a2ae208SSatish Balay #undef __FUNCT__
15564a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1557d8e5e3e6SSatish Balay /*@
1558d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1559d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1560d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1561d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1562d763cef2SBarry Smith 
1563d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1564d763cef2SBarry Smith 
1565d763cef2SBarry Smith    Input Parameter:
1566d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1567d763cef2SBarry Smith 
1568d763cef2SBarry Smith    Output Parameter:
1569d763cef2SBarry Smith .  v - the vector containing the solution
1570d763cef2SBarry Smith 
1571d763cef2SBarry Smith    Level: intermediate
1572d763cef2SBarry Smith 
1573d763cef2SBarry Smith .seealso: TSGetTimeStep()
1574d763cef2SBarry Smith 
1575d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1576d763cef2SBarry Smith @*/
15777087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1578d763cef2SBarry Smith {
1579d763cef2SBarry Smith   PetscFunctionBegin;
15800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15814482741eSBarry Smith   PetscValidPointer(v,2);
15828737fe31SLisandro Dalcin   *v = ts->vec_sol;
1583d763cef2SBarry Smith   PetscFunctionReturn(0);
1584d763cef2SBarry Smith }
1585d763cef2SBarry Smith 
1586bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
15874a2ae208SSatish Balay #undef __FUNCT__
1588bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1589d8e5e3e6SSatish Balay /*@
1590bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1591d763cef2SBarry Smith 
1592bdad233fSMatthew Knepley   Not collective
1593d763cef2SBarry Smith 
1594bdad233fSMatthew Knepley   Input Parameters:
1595bdad233fSMatthew Knepley + ts   - The TS
1596bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1597d763cef2SBarry Smith .vb
15980910c330SBarry Smith          U_t - A U = 0      (linear)
15990910c330SBarry Smith          U_t - A(t) U = 0   (linear)
16000910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1601d763cef2SBarry Smith .ve
1602d763cef2SBarry Smith 
1603d763cef2SBarry Smith    Level: beginner
1604d763cef2SBarry Smith 
1605bdad233fSMatthew Knepley .keywords: TS, problem type
1606bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1607d763cef2SBarry Smith @*/
16087087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1609a7cc72afSBarry Smith {
16109e2a6581SJed Brown   PetscErrorCode ierr;
16119e2a6581SJed Brown 
1612d763cef2SBarry Smith   PetscFunctionBegin;
16130700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1614bdad233fSMatthew Knepley   ts->problem_type = type;
16159e2a6581SJed Brown   if (type == TS_LINEAR) {
16169e2a6581SJed Brown     SNES snes;
16179e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
16189e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
16199e2a6581SJed Brown   }
1620d763cef2SBarry Smith   PetscFunctionReturn(0);
1621d763cef2SBarry Smith }
1622d763cef2SBarry Smith 
1623bdad233fSMatthew Knepley #undef __FUNCT__
1624bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1625bdad233fSMatthew Knepley /*@C
1626bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1627bdad233fSMatthew Knepley 
1628bdad233fSMatthew Knepley   Not collective
1629bdad233fSMatthew Knepley 
1630bdad233fSMatthew Knepley   Input Parameter:
1631bdad233fSMatthew Knepley . ts   - The TS
1632bdad233fSMatthew Knepley 
1633bdad233fSMatthew Knepley   Output Parameter:
1634bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1635bdad233fSMatthew Knepley .vb
1636089b2837SJed Brown          M U_t = A U
1637089b2837SJed Brown          M(t) U_t = A(t) U
1638b5abc632SBarry Smith          F(t,U,U_t)
1639bdad233fSMatthew Knepley .ve
1640bdad233fSMatthew Knepley 
1641bdad233fSMatthew Knepley    Level: beginner
1642bdad233fSMatthew Knepley 
1643bdad233fSMatthew Knepley .keywords: TS, problem type
1644bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1645bdad233fSMatthew Knepley @*/
16467087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1647a7cc72afSBarry Smith {
1648bdad233fSMatthew Knepley   PetscFunctionBegin;
16490700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
16504482741eSBarry Smith   PetscValidIntPointer(type,2);
1651bdad233fSMatthew Knepley   *type = ts->problem_type;
1652bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1653bdad233fSMatthew Knepley }
1654d763cef2SBarry Smith 
16554a2ae208SSatish Balay #undef __FUNCT__
16564a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1657d763cef2SBarry Smith /*@
1658d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1659d763cef2SBarry Smith    of a timestepper.
1660d763cef2SBarry Smith 
1661d763cef2SBarry Smith    Collective on TS
1662d763cef2SBarry Smith 
1663d763cef2SBarry Smith    Input Parameter:
1664d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1665d763cef2SBarry Smith 
1666d763cef2SBarry Smith    Notes:
1667d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1668d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1669d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1670d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1671d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1672d763cef2SBarry Smith 
1673d763cef2SBarry Smith    Level: advanced
1674d763cef2SBarry Smith 
1675d763cef2SBarry Smith .keywords: TS, timestep, setup
1676d763cef2SBarry Smith 
1677d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1678d763cef2SBarry Smith @*/
16797087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1680d763cef2SBarry Smith {
1681dfbe8321SBarry Smith   PetscErrorCode ierr;
16826c6b9e74SPeter Brune   DM             dm;
16836c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
16846c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
16856c6b9e74SPeter Brune   TSIJacobian    ijac;
16866c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1687d763cef2SBarry Smith 
1688d763cef2SBarry Smith   PetscFunctionBegin;
16890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1690277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1691277b19d0SLisandro Dalcin 
16927adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
16939596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1694d763cef2SBarry Smith   }
1695277b19d0SLisandro Dalcin 
1696277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1697277b19d0SLisandro Dalcin 
1698ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr);
16991c3436cfSJed Brown 
1700277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1701000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1702277b19d0SLisandro Dalcin   }
1703277b19d0SLisandro Dalcin 
17046c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
17056c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
17066c6b9e74SPeter Brune    */
17076c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
17080298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
17096c6b9e74SPeter Brune   if (!func) {
17106c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
17116c6b9e74SPeter Brune   }
17126c6b9e74SPeter 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.
17136c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
17146c6b9e74SPeter Brune    */
17150298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
17160298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
17170298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
17186c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
17196c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
17206c6b9e74SPeter Brune   }
1721277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1722277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1723277b19d0SLisandro Dalcin }
1724277b19d0SLisandro Dalcin 
1725277b19d0SLisandro Dalcin #undef __FUNCT__
1726277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1727277b19d0SLisandro Dalcin /*@
1728277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1729277b19d0SLisandro Dalcin 
1730277b19d0SLisandro Dalcin    Collective on TS
1731277b19d0SLisandro Dalcin 
1732277b19d0SLisandro Dalcin    Input Parameter:
1733277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1734277b19d0SLisandro Dalcin 
1735277b19d0SLisandro Dalcin    Level: beginner
1736277b19d0SLisandro Dalcin 
1737277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1738277b19d0SLisandro Dalcin 
1739277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1740277b19d0SLisandro Dalcin @*/
1741277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1742277b19d0SLisandro Dalcin {
1743277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1744277b19d0SLisandro Dalcin 
1745277b19d0SLisandro Dalcin   PetscFunctionBegin;
1746277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1747277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1748277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1749277b19d0SLisandro Dalcin   }
1750277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1751bbd56ea5SKarl Rupp 
17524e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
17534e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1754214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
17556bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1756e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1757e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
175838637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1759bbd56ea5SKarl Rupp 
1760277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1761d763cef2SBarry Smith   PetscFunctionReturn(0);
1762d763cef2SBarry Smith }
1763d763cef2SBarry Smith 
17644a2ae208SSatish Balay #undef __FUNCT__
17654a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1766d8e5e3e6SSatish Balay /*@
1767d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1768d763cef2SBarry Smith    with TSCreate().
1769d763cef2SBarry Smith 
1770d763cef2SBarry Smith    Collective on TS
1771d763cef2SBarry Smith 
1772d763cef2SBarry Smith    Input Parameter:
1773d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1774d763cef2SBarry Smith 
1775d763cef2SBarry Smith    Level: beginner
1776d763cef2SBarry Smith 
1777d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1778d763cef2SBarry Smith 
1779d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1780d763cef2SBarry Smith @*/
17816bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1782d763cef2SBarry Smith {
17836849ba73SBarry Smith   PetscErrorCode ierr;
1784d763cef2SBarry Smith 
1785d763cef2SBarry Smith   PetscFunctionBegin;
17866bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
17876bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
17886bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1789d763cef2SBarry Smith 
17906bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1791277b19d0SLisandro Dalcin 
1792be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
17936bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
17946bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
17956d4c513bSLisandro Dalcin 
179684df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
17976bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
17986bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
17996bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
18006d4c513bSLisandro Dalcin 
1801a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1802d763cef2SBarry Smith   PetscFunctionReturn(0);
1803d763cef2SBarry Smith }
1804d763cef2SBarry Smith 
18054a2ae208SSatish Balay #undef __FUNCT__
18064a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1807d8e5e3e6SSatish Balay /*@
1808d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1809d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1810d763cef2SBarry Smith 
1811d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1812d763cef2SBarry Smith 
1813d763cef2SBarry Smith    Input Parameter:
1814d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1815d763cef2SBarry Smith 
1816d763cef2SBarry Smith    Output Parameter:
1817d763cef2SBarry Smith .  snes - the nonlinear solver context
1818d763cef2SBarry Smith 
1819d763cef2SBarry Smith    Notes:
1820d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1821d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
182294b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1823d763cef2SBarry Smith 
1824d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
18250298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
1826d763cef2SBarry Smith 
1827d763cef2SBarry Smith    Level: beginner
1828d763cef2SBarry Smith 
1829d763cef2SBarry Smith .keywords: timestep, get, SNES
1830d763cef2SBarry Smith @*/
18317087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1832d763cef2SBarry Smith {
1833d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1834d372ba47SLisandro Dalcin 
1835d763cef2SBarry Smith   PetscFunctionBegin;
18360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18374482741eSBarry Smith   PetscValidPointer(snes,2);
1838d372ba47SLisandro Dalcin   if (!ts->snes) {
1839ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
18400298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1841d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1842d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1843496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
18449e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
18459e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
18469e2a6581SJed Brown     }
1847d372ba47SLisandro Dalcin   }
1848d763cef2SBarry Smith   *snes = ts->snes;
1849d763cef2SBarry Smith   PetscFunctionReturn(0);
1850d763cef2SBarry Smith }
1851d763cef2SBarry Smith 
18524a2ae208SSatish Balay #undef __FUNCT__
1853deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1854deb2cd25SJed Brown /*@
1855deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1856deb2cd25SJed Brown 
1857deb2cd25SJed Brown    Collective
1858deb2cd25SJed Brown 
1859deb2cd25SJed Brown    Input Parameter:
1860deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1861deb2cd25SJed Brown -  snes - the nonlinear solver context
1862deb2cd25SJed Brown 
1863deb2cd25SJed Brown    Notes:
1864deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1865deb2cd25SJed Brown 
1866deb2cd25SJed Brown    Level: developer
1867deb2cd25SJed Brown 
1868deb2cd25SJed Brown .keywords: timestep, set, SNES
1869deb2cd25SJed Brown @*/
1870deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1871deb2cd25SJed Brown {
1872deb2cd25SJed Brown   PetscErrorCode ierr;
1873740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1874deb2cd25SJed Brown 
1875deb2cd25SJed Brown   PetscFunctionBegin;
1876deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1877deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1878deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1879deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1880bbd56ea5SKarl Rupp 
1881deb2cd25SJed Brown   ts->snes = snes;
1882bbd56ea5SKarl Rupp 
18830298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
18840298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
1885740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
18860298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1887740132f1SEmil Constantinescu   }
1888deb2cd25SJed Brown   PetscFunctionReturn(0);
1889deb2cd25SJed Brown }
1890deb2cd25SJed Brown 
1891deb2cd25SJed Brown #undef __FUNCT__
189294b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1893d8e5e3e6SSatish Balay /*@
189494b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1895d763cef2SBarry Smith    a TS (timestepper) context.
1896d763cef2SBarry Smith 
189794b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1898d763cef2SBarry Smith 
1899d763cef2SBarry Smith    Input Parameter:
1900d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1901d763cef2SBarry Smith 
1902d763cef2SBarry Smith    Output Parameter:
190394b7f48cSBarry Smith .  ksp - the nonlinear solver context
1904d763cef2SBarry Smith 
1905d763cef2SBarry Smith    Notes:
190694b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1907d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1908d763cef2SBarry Smith    KSP and PC contexts as well.
1909d763cef2SBarry Smith 
191094b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
19110298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
1912d763cef2SBarry Smith 
1913d763cef2SBarry Smith    Level: beginner
1914d763cef2SBarry Smith 
191594b7f48cSBarry Smith .keywords: timestep, get, KSP
1916d763cef2SBarry Smith @*/
19177087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1918d763cef2SBarry Smith {
1919d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1920089b2837SJed Brown   SNES           snes;
1921d372ba47SLisandro Dalcin 
1922d763cef2SBarry Smith   PetscFunctionBegin;
19230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19244482741eSBarry Smith   PetscValidPointer(ksp,2);
192517186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1926e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1927089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1928089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1929d763cef2SBarry Smith   PetscFunctionReturn(0);
1930d763cef2SBarry Smith }
1931d763cef2SBarry Smith 
1932d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1933d763cef2SBarry Smith 
19344a2ae208SSatish Balay #undef __FUNCT__
1935adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1936adb62b0dSMatthew Knepley /*@
1937adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1938adb62b0dSMatthew Knepley    maximum time for iteration.
1939adb62b0dSMatthew Knepley 
19403f9fe445SBarry Smith    Not Collective
1941adb62b0dSMatthew Knepley 
1942adb62b0dSMatthew Knepley    Input Parameters:
1943adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
19440298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
19450298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
1946adb62b0dSMatthew Knepley 
1947adb62b0dSMatthew Knepley    Level: intermediate
1948adb62b0dSMatthew Knepley 
1949adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1950adb62b0dSMatthew Knepley @*/
19517087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1952adb62b0dSMatthew Knepley {
1953adb62b0dSMatthew Knepley   PetscFunctionBegin;
19540700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1955abc0a331SBarry Smith   if (maxsteps) {
19564482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1957adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1958adb62b0dSMatthew Knepley   }
1959abc0a331SBarry Smith   if (maxtime) {
19604482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1961adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
1962adb62b0dSMatthew Knepley   }
1963adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1964adb62b0dSMatthew Knepley }
1965adb62b0dSMatthew Knepley 
1966adb62b0dSMatthew Knepley #undef __FUNCT__
19674a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1968d763cef2SBarry Smith /*@
1969d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1970d763cef2SBarry Smith    maximum time for iteration.
1971d763cef2SBarry Smith 
19723f9fe445SBarry Smith    Logically Collective on TS
1973d763cef2SBarry Smith 
1974d763cef2SBarry Smith    Input Parameters:
1975d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1976d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1977d763cef2SBarry Smith -  maxtime - final time to iterate to
1978d763cef2SBarry Smith 
1979d763cef2SBarry Smith    Options Database Keys:
1980d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
19813bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1982d763cef2SBarry Smith 
1983d763cef2SBarry Smith    Notes:
1984d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1985d763cef2SBarry Smith 
1986d763cef2SBarry Smith    Level: intermediate
1987d763cef2SBarry Smith 
1988d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1989a43b19c4SJed Brown 
1990a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1991d763cef2SBarry Smith @*/
19927087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1993d763cef2SBarry Smith {
1994d763cef2SBarry Smith   PetscFunctionBegin;
19950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1996c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1997c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
199839b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
199939b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2000d763cef2SBarry Smith   PetscFunctionReturn(0);
2001d763cef2SBarry Smith }
2002d763cef2SBarry Smith 
20034a2ae208SSatish Balay #undef __FUNCT__
20044a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2005d763cef2SBarry Smith /*@
2006d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2007d763cef2SBarry Smith    for use by the TS routines.
2008d763cef2SBarry Smith 
20093f9fe445SBarry Smith    Logically Collective on TS and Vec
2010d763cef2SBarry Smith 
2011d763cef2SBarry Smith    Input Parameters:
2012d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
20130910c330SBarry Smith -  u - the solution vector
2014d763cef2SBarry Smith 
2015d763cef2SBarry Smith    Level: beginner
2016d763cef2SBarry Smith 
2017d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2018d763cef2SBarry Smith @*/
20190910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2020d763cef2SBarry Smith {
20218737fe31SLisandro Dalcin   PetscErrorCode ierr;
2022496e6a7aSJed Brown   DM             dm;
20238737fe31SLisandro Dalcin 
2024d763cef2SBarry Smith   PetscFunctionBegin;
20250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20260910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
20270910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
20286bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2029bbd56ea5SKarl Rupp 
20300910c330SBarry Smith   ts->vec_sol = u;
2031bbd56ea5SKarl Rupp 
2032496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
20330910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2034d763cef2SBarry Smith   PetscFunctionReturn(0);
2035d763cef2SBarry Smith }
2036d763cef2SBarry Smith 
2037e74ef692SMatthew Knepley #undef __FUNCT__
2038e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2039ac226902SBarry Smith /*@C
2040000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
20413f2090d5SJed Brown   called once at the beginning of each time step.
2042000e7ae3SMatthew Knepley 
20433f9fe445SBarry Smith   Logically Collective on TS
2044000e7ae3SMatthew Knepley 
2045000e7ae3SMatthew Knepley   Input Parameters:
2046000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2047000e7ae3SMatthew Knepley - func - The function
2048000e7ae3SMatthew Knepley 
2049000e7ae3SMatthew Knepley   Calling sequence of func:
2050000e7ae3SMatthew Knepley . func (TS ts);
2051000e7ae3SMatthew Knepley 
2052000e7ae3SMatthew Knepley   Level: intermediate
2053000e7ae3SMatthew Knepley 
2054b8123daeSJed Brown   Note:
2055b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2056b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2057b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2058b8123daeSJed Brown 
2059000e7ae3SMatthew Knepley .keywords: TS, timestep
2060b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
2061000e7ae3SMatthew Knepley @*/
20627087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2063000e7ae3SMatthew Knepley {
2064000e7ae3SMatthew Knepley   PetscFunctionBegin;
20650700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2066000e7ae3SMatthew Knepley   ts->ops->prestep = func;
2067000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2068000e7ae3SMatthew Knepley }
2069000e7ae3SMatthew Knepley 
2070e74ef692SMatthew Knepley #undef __FUNCT__
20713f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
207209ee8438SJed Brown /*@
20733f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
20743f2090d5SJed Brown 
20753f2090d5SJed Brown   Collective on TS
20763f2090d5SJed Brown 
20773f2090d5SJed Brown   Input Parameters:
20783f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
20793f2090d5SJed Brown 
20803f2090d5SJed Brown   Notes:
20813f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
20823f2090d5SJed Brown   so most users would not generally call this routine themselves.
20833f2090d5SJed Brown 
20843f2090d5SJed Brown   Level: developer
20853f2090d5SJed Brown 
20863f2090d5SJed Brown .keywords: TS, timestep
2087b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
20883f2090d5SJed Brown @*/
20897087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
20903f2090d5SJed Brown {
20913f2090d5SJed Brown   PetscErrorCode ierr;
20923f2090d5SJed Brown 
20933f2090d5SJed Brown   PetscFunctionBegin;
20940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
209572ac3e02SJed Brown   if (ts->ops->prestep) {
20963f2090d5SJed Brown     PetscStackPush("TS PreStep function");
20973f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
20983f2090d5SJed Brown     PetscStackPop;
2099312ce896SJed Brown   }
21003f2090d5SJed Brown   PetscFunctionReturn(0);
21013f2090d5SJed Brown }
21023f2090d5SJed Brown 
21033f2090d5SJed Brown #undef __FUNCT__
2104b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2105b8123daeSJed Brown /*@C
2106b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2107b8123daeSJed Brown   called once at the beginning of each stage.
2108b8123daeSJed Brown 
2109b8123daeSJed Brown   Logically Collective on TS
2110b8123daeSJed Brown 
2111b8123daeSJed Brown   Input Parameters:
2112b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2113b8123daeSJed Brown - func - The function
2114b8123daeSJed Brown 
2115b8123daeSJed Brown   Calling sequence of func:
2116b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2117b8123daeSJed Brown 
2118b8123daeSJed Brown   Level: intermediate
2119b8123daeSJed Brown 
2120b8123daeSJed Brown   Note:
2121b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2122b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2123b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2124b8123daeSJed Brown 
2125b8123daeSJed Brown .keywords: TS, timestep
2126b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2127b8123daeSJed Brown @*/
2128b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2129b8123daeSJed Brown {
2130b8123daeSJed Brown   PetscFunctionBegin;
2131b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2132b8123daeSJed Brown   ts->ops->prestage = func;
2133b8123daeSJed Brown   PetscFunctionReturn(0);
2134b8123daeSJed Brown }
2135b8123daeSJed Brown 
2136b8123daeSJed Brown #undef __FUNCT__
2137b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2138b8123daeSJed Brown /*@
2139b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2140b8123daeSJed Brown 
2141b8123daeSJed Brown   Collective on TS
2142b8123daeSJed Brown 
2143b8123daeSJed Brown   Input Parameters:
2144b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2145b8123daeSJed Brown 
2146b8123daeSJed Brown   Notes:
2147b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2148b8123daeSJed Brown   most users would not generally call this routine themselves.
2149b8123daeSJed Brown 
2150b8123daeSJed Brown   Level: developer
2151b8123daeSJed Brown 
2152b8123daeSJed Brown .keywords: TS, timestep
2153b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2154b8123daeSJed Brown @*/
2155b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2156b8123daeSJed Brown {
2157b8123daeSJed Brown   PetscErrorCode ierr;
2158b8123daeSJed Brown 
2159b8123daeSJed Brown   PetscFunctionBegin;
2160b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2161b8123daeSJed Brown   if (ts->ops->prestage) {
2162b8123daeSJed Brown     PetscStackPush("TS PreStage function");
2163b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
2164b8123daeSJed Brown     PetscStackPop;
2165b8123daeSJed Brown   }
2166b8123daeSJed Brown   PetscFunctionReturn(0);
2167b8123daeSJed Brown }
2168b8123daeSJed Brown 
2169b8123daeSJed Brown #undef __FUNCT__
2170e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2171ac226902SBarry Smith /*@C
2172000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
21733f2090d5SJed Brown   called once at the end of each time step.
2174000e7ae3SMatthew Knepley 
21753f9fe445SBarry Smith   Logically Collective on TS
2176000e7ae3SMatthew Knepley 
2177000e7ae3SMatthew Knepley   Input Parameters:
2178000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2179000e7ae3SMatthew Knepley - func - The function
2180000e7ae3SMatthew Knepley 
2181000e7ae3SMatthew Knepley   Calling sequence of func:
2182b8123daeSJed Brown $ func (TS ts);
2183000e7ae3SMatthew Knepley 
2184000e7ae3SMatthew Knepley   Level: intermediate
2185000e7ae3SMatthew Knepley 
2186000e7ae3SMatthew Knepley .keywords: TS, timestep
2187b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2188000e7ae3SMatthew Knepley @*/
21897087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2190000e7ae3SMatthew Knepley {
2191000e7ae3SMatthew Knepley   PetscFunctionBegin;
21920700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2193000e7ae3SMatthew Knepley   ts->ops->poststep = func;
2194000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2195000e7ae3SMatthew Knepley }
2196000e7ae3SMatthew Knepley 
2197e74ef692SMatthew Knepley #undef __FUNCT__
21983f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
219909ee8438SJed Brown /*@
22003f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
22013f2090d5SJed Brown 
22023f2090d5SJed Brown   Collective on TS
22033f2090d5SJed Brown 
22043f2090d5SJed Brown   Input Parameters:
22053f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
22063f2090d5SJed Brown 
22073f2090d5SJed Brown   Notes:
22083f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
22093f2090d5SJed Brown   so most users would not generally call this routine themselves.
22103f2090d5SJed Brown 
22113f2090d5SJed Brown   Level: developer
22123f2090d5SJed Brown 
22133f2090d5SJed Brown .keywords: TS, timestep
22143f2090d5SJed Brown @*/
22157087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
22163f2090d5SJed Brown {
22173f2090d5SJed Brown   PetscErrorCode ierr;
22183f2090d5SJed Brown 
22193f2090d5SJed Brown   PetscFunctionBegin;
22200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
222172ac3e02SJed Brown   if (ts->ops->poststep) {
22223f2090d5SJed Brown     PetscStackPush("TS PostStep function");
22233f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
22243f2090d5SJed Brown     PetscStackPop;
222572ac3e02SJed Brown   }
22263f2090d5SJed Brown   PetscFunctionReturn(0);
22273f2090d5SJed Brown }
22283f2090d5SJed Brown 
2229d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2230d763cef2SBarry Smith 
22314a2ae208SSatish Balay #undef __FUNCT__
2232a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2233d763cef2SBarry Smith /*@C
2234a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2235d763cef2SBarry Smith    timestep to display the iteration's  progress.
2236d763cef2SBarry Smith 
22373f9fe445SBarry Smith    Logically Collective on TS
2238d763cef2SBarry Smith 
2239d763cef2SBarry Smith    Input Parameters:
2240d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2241e213d8f1SJed Brown .  monitor - monitoring routine
2242329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
22430298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2244b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
22450298fd71SBarry Smith           (may be NULL)
2246d763cef2SBarry Smith 
2247e213d8f1SJed Brown    Calling sequence of monitor:
22480910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2249d763cef2SBarry Smith 
2250d763cef2SBarry Smith +    ts - the TS context
225188c05cc5SBarry 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
225288c05cc5SBarry Smith                                been interpolated to)
22531f06c33eSBarry Smith .    time - current time
22540910c330SBarry Smith .    u - current iterate
2255d763cef2SBarry Smith -    mctx - [optional] monitoring context
2256d763cef2SBarry Smith 
2257d763cef2SBarry Smith    Notes:
2258d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2259d763cef2SBarry Smith    already has been loaded.
2260d763cef2SBarry Smith 
2261025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2262025f1a04SBarry Smith 
2263d763cef2SBarry Smith    Level: intermediate
2264d763cef2SBarry Smith 
2265d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2266d763cef2SBarry Smith 
2267a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2268d763cef2SBarry Smith @*/
2269c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2270d763cef2SBarry Smith {
2271d763cef2SBarry Smith   PetscFunctionBegin;
22720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
227317186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2274d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
22758704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2276d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2277d763cef2SBarry Smith   PetscFunctionReturn(0);
2278d763cef2SBarry Smith }
2279d763cef2SBarry Smith 
22804a2ae208SSatish Balay #undef __FUNCT__
2281a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2282d763cef2SBarry Smith /*@C
2283a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2284d763cef2SBarry Smith 
22853f9fe445SBarry Smith    Logically Collective on TS
2286d763cef2SBarry Smith 
2287d763cef2SBarry Smith    Input Parameters:
2288d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2289d763cef2SBarry Smith 
2290d763cef2SBarry Smith    Notes:
2291d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2292d763cef2SBarry Smith 
2293d763cef2SBarry Smith    Level: intermediate
2294d763cef2SBarry Smith 
2295d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2296d763cef2SBarry Smith 
2297a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2298d763cef2SBarry Smith @*/
22997087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2300d763cef2SBarry Smith {
2301d952e501SBarry Smith   PetscErrorCode ierr;
2302d952e501SBarry Smith   PetscInt       i;
2303d952e501SBarry Smith 
2304d763cef2SBarry Smith   PetscFunctionBegin;
23050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2306d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
23078704b422SBarry Smith     if (ts->monitordestroy[i]) {
23088704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2309d952e501SBarry Smith     }
2310d952e501SBarry Smith   }
2311d763cef2SBarry Smith   ts->numbermonitors = 0;
2312d763cef2SBarry Smith   PetscFunctionReturn(0);
2313d763cef2SBarry Smith }
2314d763cef2SBarry Smith 
23154a2ae208SSatish Balay #undef __FUNCT__
2316a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2317d8e5e3e6SSatish Balay /*@
2318a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
23195516499fSSatish Balay 
23205516499fSSatish Balay    Level: intermediate
232141251cbbSSatish Balay 
23225516499fSSatish Balay .keywords: TS, set, monitor
23235516499fSSatish Balay 
232441251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
232541251cbbSSatish Balay @*/
2326649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2327d763cef2SBarry Smith {
2328dfbe8321SBarry Smith   PetscErrorCode ierr;
2329ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2330d132466eSBarry Smith 
2331d763cef2SBarry Smith   PetscFunctionBegin;
2332649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2333971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2334649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2335d763cef2SBarry Smith   PetscFunctionReturn(0);
2336d763cef2SBarry Smith }
2337d763cef2SBarry Smith 
23384a2ae208SSatish Balay #undef __FUNCT__
2339cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2340cd652676SJed Brown /*@
2341cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2342cd652676SJed Brown 
2343cd652676SJed Brown    Logically Collective on TS
2344cd652676SJed Brown 
2345cd652676SJed Brown    Input Argument:
2346cd652676SJed Brown .  ts - time stepping context
2347cd652676SJed Brown 
2348cd652676SJed Brown    Output Argument:
2349cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2350cd652676SJed Brown 
2351cd652676SJed Brown    Level: intermediate
2352cd652676SJed Brown 
2353cd652676SJed Brown .keywords: TS, set
2354cd652676SJed Brown 
2355cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2356cd652676SJed Brown @*/
2357cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2358cd652676SJed Brown {
2359cd652676SJed Brown   PetscFunctionBegin;
2360cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2361cd652676SJed Brown   ts->retain_stages = flg;
2362cd652676SJed Brown   PetscFunctionReturn(0);
2363cd652676SJed Brown }
2364cd652676SJed Brown 
2365cd652676SJed Brown #undef __FUNCT__
2366cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2367cd652676SJed Brown /*@
2368cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2369cd652676SJed Brown 
2370cd652676SJed Brown    Collective on TS
2371cd652676SJed Brown 
2372cd652676SJed Brown    Input Argument:
2373cd652676SJed Brown +  ts - time stepping context
2374cd652676SJed Brown -  t - time to interpolate to
2375cd652676SJed Brown 
2376cd652676SJed Brown    Output Argument:
23770910c330SBarry Smith .  U - state at given time
2378cd652676SJed Brown 
2379cd652676SJed Brown    Notes:
2380cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2381cd652676SJed Brown 
2382cd652676SJed Brown    Level: intermediate
2383cd652676SJed Brown 
2384cd652676SJed Brown    Developer Notes:
2385cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2386cd652676SJed Brown 
2387cd652676SJed Brown .keywords: TS, set
2388cd652676SJed Brown 
2389cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2390cd652676SJed Brown @*/
23910910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2392cd652676SJed Brown {
2393cd652676SJed Brown   PetscErrorCode ierr;
2394cd652676SJed Brown 
2395cd652676SJed Brown   PetscFunctionBegin;
2396cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2397ce94432eSBarry 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);
2398ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
23990910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2400cd652676SJed Brown   PetscFunctionReturn(0);
2401cd652676SJed Brown }
2402cd652676SJed Brown 
2403cd652676SJed Brown #undef __FUNCT__
24044a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2405d763cef2SBarry Smith /*@
24066d9e5789SSean Farley    TSStep - Steps one time step
2407d763cef2SBarry Smith 
2408d763cef2SBarry Smith    Collective on TS
2409d763cef2SBarry Smith 
2410d763cef2SBarry Smith    Input Parameter:
2411d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2412d763cef2SBarry Smith 
24136d9e5789SSean Farley    Level: intermediate
2414d763cef2SBarry Smith 
2415b8123daeSJed Brown    Notes:
2416b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2417b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2418b8123daeSJed Brown 
241925cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
242025cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
242125cb2221SBarry Smith 
2422d763cef2SBarry Smith .keywords: TS, timestep, solve
2423d763cef2SBarry Smith 
242425cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2425d763cef2SBarry Smith @*/
2426193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2427d763cef2SBarry Smith {
2428362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2429dfbe8321SBarry Smith   PetscErrorCode ierr;
2430d763cef2SBarry Smith 
2431d763cef2SBarry Smith   PetscFunctionBegin;
24320700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2433d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2434d405a339SMatthew Knepley 
2435362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2436362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2437bbd56ea5SKarl Rupp 
2438d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2439193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2440d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2441bbd56ea5SKarl Rupp 
2442362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2443362cd11cSLisandro Dalcin 
2444362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2445cef5090cSJed Brown     if (ts->errorifstepfailed) {
2446cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2447ce94432eSBarry 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]);
2448ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2449cef5090cSJed Brown     }
2450362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2451db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2452db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2453362cd11cSLisandro Dalcin   }
2454d763cef2SBarry Smith   PetscFunctionReturn(0);
2455d763cef2SBarry Smith }
2456d763cef2SBarry Smith 
24574a2ae208SSatish Balay #undef __FUNCT__
245805175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
245905175c85SJed Brown /*@
246005175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
246105175c85SJed Brown 
24621c3436cfSJed Brown    Collective on TS
246305175c85SJed Brown 
246405175c85SJed Brown    Input Arguments:
24651c3436cfSJed Brown +  ts - time stepping context
24661c3436cfSJed Brown .  order - desired order of accuracy
24670298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
246805175c85SJed Brown 
246905175c85SJed Brown    Output Arguments:
24700910c330SBarry Smith .  U - state at the end of the current step
247105175c85SJed Brown 
247205175c85SJed Brown    Level: advanced
247305175c85SJed Brown 
2474108c343cSJed Brown    Notes:
2475108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2476108c343cSJed 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.
2477108c343cSJed Brown 
24781c3436cfSJed Brown .seealso: TSStep(), TSAdapt
247905175c85SJed Brown @*/
24800910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
248105175c85SJed Brown {
248205175c85SJed Brown   PetscErrorCode ierr;
248305175c85SJed Brown 
248405175c85SJed Brown   PetscFunctionBegin;
248505175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
248605175c85SJed Brown   PetscValidType(ts,1);
24870910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2488ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
24890910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
249005175c85SJed Brown   PetscFunctionReturn(0);
249105175c85SJed Brown }
249205175c85SJed Brown 
249305175c85SJed Brown #undef __FUNCT__
24946a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
24956a4d4014SLisandro Dalcin /*@
24966a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
24976a4d4014SLisandro Dalcin 
24986a4d4014SLisandro Dalcin    Collective on TS
24996a4d4014SLisandro Dalcin 
25006a4d4014SLisandro Dalcin    Input Parameter:
25016a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2502cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
25035a3a76d0SJed Brown 
25046a4d4014SLisandro Dalcin    Level: beginner
25056a4d4014SLisandro Dalcin 
25065a3a76d0SJed Brown    Notes:
25075a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
25085a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
25095a3a76d0SJed Brown    stepped over the final time.
25105a3a76d0SJed Brown 
25116a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
25126a4d4014SLisandro Dalcin 
25136a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
25146a4d4014SLisandro Dalcin @*/
2515cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
25166a4d4014SLisandro Dalcin {
25174d7d938eSLisandro Dalcin   PetscBool         flg;
25184d7d938eSLisandro Dalcin   PetscViewer       viewer;
25196a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2520cffb1e40SBarry Smith   PetscViewerFormat format;
2521f22f69f0SBarry Smith 
25226a4d4014SLisandro Dalcin   PetscFunctionBegin;
25230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2524f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
252549354f04SShri 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 */
25260910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
25275a3a76d0SJed Brown       Vec y;
25280910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
25295a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
25305a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
25315a3a76d0SJed Brown     }
2532f2c2a1b9SBarry Smith     if (u) {
25330910c330SBarry Smith       ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2534f2c2a1b9SBarry Smith     }
2535bbd56ea5SKarl Rupp   } else if (u) {
25360910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
25375a3a76d0SJed Brown   }
2538b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
25396a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2540193ac0bcSJed Brown   ts->steps             = 0;
25415ef26d82SJed Brown   ts->ksp_its           = 0;
25425ef26d82SJed Brown   ts->snes_its          = 0;
2543c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2544c610991cSLisandro Dalcin   ts->reject            = 0;
2545193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2546193ac0bcSJed Brown 
2547193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2548193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
25490910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2550bbd56ea5SKarl Rupp 
2551cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2552193ac0bcSJed Brown   } else {
25536a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2554362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2555db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2556db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2557e1a7a14fSJed Brown     while (!ts->reason) {
2558193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2559193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2560193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2561193ac0bcSJed Brown     }
256249354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
25630910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2564bbd56ea5SKarl Rupp 
2565cc708dedSBarry Smith       ts->solvetime = ts->max_time;
25660574a7fbSJed Brown     } else {
2567ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2568cc708dedSBarry Smith       ts->solvetime = ts->ptime;
25690574a7fbSJed Brown     }
2570193ac0bcSJed Brown   }
25713923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2572ce94432eSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
25734d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2574cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
25754d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2576cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2577cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
25782b0a91c0SBarry Smith   }
25796a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
25806a4d4014SLisandro Dalcin }
25816a4d4014SLisandro Dalcin 
25826a4d4014SLisandro Dalcin #undef __FUNCT__
25834a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2584228d79bcSJed Brown /*@
2585228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2586228d79bcSJed Brown 
2587228d79bcSJed Brown    Collective on TS
2588228d79bcSJed Brown 
2589228d79bcSJed Brown    Input Parameters:
2590228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2591228d79bcSJed Brown .  step - step number that has just completed
2592228d79bcSJed Brown .  ptime - model time of the state
25930910c330SBarry Smith -  u - state at the current model time
2594228d79bcSJed Brown 
2595228d79bcSJed Brown    Notes:
2596228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2597228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2598228d79bcSJed Brown 
2599228d79bcSJed Brown    Level: advanced
2600228d79bcSJed Brown 
2601228d79bcSJed Brown .keywords: TS, timestep
2602228d79bcSJed Brown @*/
26030910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2604d763cef2SBarry Smith {
26056849ba73SBarry Smith   PetscErrorCode ierr;
2606a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2607d763cef2SBarry Smith 
2608d763cef2SBarry Smith   PetscFunctionBegin;
2609d763cef2SBarry Smith   for (i=0; i<n; i++) {
26100910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2611d763cef2SBarry Smith   }
2612d763cef2SBarry Smith   PetscFunctionReturn(0);
2613d763cef2SBarry Smith }
2614d763cef2SBarry Smith 
2615d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
26160b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
26170b039ecaSBarry Smith   PetscDrawLG lg;
26180b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2619201da799SBarry Smith   PetscInt    ksp_its,snes_its;
26200b039ecaSBarry Smith };
26210b039ecaSBarry Smith 
2622d763cef2SBarry Smith 
26234a2ae208SSatish Balay #undef __FUNCT__
2624a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2625d763cef2SBarry Smith /*@C
2626a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2627a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2628d763cef2SBarry Smith 
2629d763cef2SBarry Smith    Collective on TS
2630d763cef2SBarry Smith 
2631d763cef2SBarry Smith    Input Parameters:
2632d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2633d763cef2SBarry Smith .  label - the title to put in the title bar
26347c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2635a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2636a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2637d763cef2SBarry Smith 
2638d763cef2SBarry Smith    Output Parameter:
26390b039ecaSBarry Smith .  ctx - the context
2640d763cef2SBarry Smith 
2641d763cef2SBarry Smith    Options Database Key:
26424f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
26434f09c107SBarry Smith .  -ts_monitor_lg_solution -
264499fdda47SBarry Smith .  -ts_monitor_lg_error -
264599fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
264699fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
264799fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2648d763cef2SBarry Smith 
2649d763cef2SBarry Smith    Notes:
2650a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2651d763cef2SBarry Smith 
2652d763cef2SBarry Smith    Level: intermediate
2653d763cef2SBarry Smith 
26547c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2655d763cef2SBarry Smith 
26564f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
26577c922b88SBarry Smith 
2658d763cef2SBarry Smith @*/
2659a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2660d763cef2SBarry Smith {
2661b0a32e0cSBarry Smith   PetscDraw      win;
2662dfbe8321SBarry Smith   PetscErrorCode ierr;
266399fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2664d763cef2SBarry Smith 
2665d763cef2SBarry Smith   PetscFunctionBegin;
2666201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2667a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
26683fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
26690b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
26700298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-lg_indicate_data_points",&flg,NULL);CHKERRQ(ierr);
267199fdda47SBarry Smith   if (flg) {
26720b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
267399fdda47SBarry Smith   }
26740b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2675a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2676d763cef2SBarry Smith   PetscFunctionReturn(0);
2677d763cef2SBarry Smith }
2678d763cef2SBarry Smith 
26794a2ae208SSatish Balay #undef __FUNCT__
26804f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
26814f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2682d763cef2SBarry Smith {
26830b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2684c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2685dfbe8321SBarry Smith   PetscErrorCode ierr;
2686d763cef2SBarry Smith 
2687d763cef2SBarry Smith   PetscFunctionBegin;
2688a9f9c1f6SBarry Smith   if (!n) {
2689a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2690a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2691a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2692a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2693a9f9c1f6SBarry Smith   }
2694c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
26950b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
269699fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))) {
26970b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
26983923b477SBarry Smith   }
2699d763cef2SBarry Smith   PetscFunctionReturn(0);
2700d763cef2SBarry Smith }
2701d763cef2SBarry Smith 
27024a2ae208SSatish Balay #undef __FUNCT__
2703a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2704d763cef2SBarry Smith /*@C
2705a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2706a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2707d763cef2SBarry Smith 
27080b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2709d763cef2SBarry Smith 
2710d763cef2SBarry Smith    Input Parameter:
27110b039ecaSBarry Smith .  ctx - the monitor context
2712d763cef2SBarry Smith 
2713d763cef2SBarry Smith    Level: intermediate
2714d763cef2SBarry Smith 
2715d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2716d763cef2SBarry Smith 
27174f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2718d763cef2SBarry Smith @*/
2719a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2720d763cef2SBarry Smith {
2721b0a32e0cSBarry Smith   PetscDraw      draw;
2722dfbe8321SBarry Smith   PetscErrorCode ierr;
2723d763cef2SBarry Smith 
2724d763cef2SBarry Smith   PetscFunctionBegin;
27250b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
27266bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
27270b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
27280b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2729d763cef2SBarry Smith   PetscFunctionReturn(0);
2730d763cef2SBarry Smith }
2731d763cef2SBarry Smith 
27324a2ae208SSatish Balay #undef __FUNCT__
27334a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2734d763cef2SBarry Smith /*@
2735b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2736d763cef2SBarry Smith 
2737d763cef2SBarry Smith    Not Collective
2738d763cef2SBarry Smith 
2739d763cef2SBarry Smith    Input Parameter:
2740d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2741d763cef2SBarry Smith 
2742d763cef2SBarry Smith    Output Parameter:
2743d763cef2SBarry Smith .  t  - the current time
2744d763cef2SBarry Smith 
2745d763cef2SBarry Smith    Level: beginner
2746d763cef2SBarry Smith 
2747b8123daeSJed Brown    Note:
2748b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2749b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2750b8123daeSJed Brown 
2751d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2752d763cef2SBarry Smith 
2753d763cef2SBarry Smith .keywords: TS, get, time
2754d763cef2SBarry Smith @*/
27557087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2756d763cef2SBarry Smith {
2757d763cef2SBarry Smith   PetscFunctionBegin;
27580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2759f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2760d763cef2SBarry Smith   *t = ts->ptime;
2761d763cef2SBarry Smith   PetscFunctionReturn(0);
2762d763cef2SBarry Smith }
2763d763cef2SBarry Smith 
27644a2ae208SSatish Balay #undef __FUNCT__
27656a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
27666a4d4014SLisandro Dalcin /*@
27676a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
27686a4d4014SLisandro Dalcin 
27693f9fe445SBarry Smith    Logically Collective on TS
27706a4d4014SLisandro Dalcin 
27716a4d4014SLisandro Dalcin    Input Parameters:
27726a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
27736a4d4014SLisandro Dalcin -  time - the time
27746a4d4014SLisandro Dalcin 
27756a4d4014SLisandro Dalcin    Level: intermediate
27766a4d4014SLisandro Dalcin 
27776a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
27786a4d4014SLisandro Dalcin 
27796a4d4014SLisandro Dalcin .keywords: TS, set, time
27806a4d4014SLisandro Dalcin @*/
27817087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
27826a4d4014SLisandro Dalcin {
27836a4d4014SLisandro Dalcin   PetscFunctionBegin;
27840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2785c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
27866a4d4014SLisandro Dalcin   ts->ptime = t;
27876a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
27886a4d4014SLisandro Dalcin }
27896a4d4014SLisandro Dalcin 
27906a4d4014SLisandro Dalcin #undef __FUNCT__
27914a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2792d763cef2SBarry Smith /*@C
2793d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2794d763cef2SBarry Smith    TS options in the database.
2795d763cef2SBarry Smith 
27963f9fe445SBarry Smith    Logically Collective on TS
2797d763cef2SBarry Smith 
2798d763cef2SBarry Smith    Input Parameter:
2799d763cef2SBarry Smith +  ts     - The TS context
2800d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2801d763cef2SBarry Smith 
2802d763cef2SBarry Smith    Notes:
2803d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2804d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2805d763cef2SBarry Smith    hyphen.
2806d763cef2SBarry Smith 
2807d763cef2SBarry Smith    Level: advanced
2808d763cef2SBarry Smith 
2809d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2810d763cef2SBarry Smith 
2811d763cef2SBarry Smith .seealso: TSSetFromOptions()
2812d763cef2SBarry Smith 
2813d763cef2SBarry Smith @*/
28147087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2815d763cef2SBarry Smith {
2816dfbe8321SBarry Smith   PetscErrorCode ierr;
2817089b2837SJed Brown   SNES           snes;
2818d763cef2SBarry Smith 
2819d763cef2SBarry Smith   PetscFunctionBegin;
28200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2821d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2822089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2823089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2824d763cef2SBarry Smith   PetscFunctionReturn(0);
2825d763cef2SBarry Smith }
2826d763cef2SBarry Smith 
2827d763cef2SBarry Smith 
28284a2ae208SSatish Balay #undef __FUNCT__
28294a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2830d763cef2SBarry Smith /*@C
2831d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2832d763cef2SBarry Smith    TS options in the database.
2833d763cef2SBarry Smith 
28343f9fe445SBarry Smith    Logically Collective on TS
2835d763cef2SBarry Smith 
2836d763cef2SBarry Smith    Input Parameter:
2837d763cef2SBarry Smith +  ts     - The TS context
2838d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2839d763cef2SBarry Smith 
2840d763cef2SBarry Smith    Notes:
2841d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2842d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2843d763cef2SBarry Smith    hyphen.
2844d763cef2SBarry Smith 
2845d763cef2SBarry Smith    Level: advanced
2846d763cef2SBarry Smith 
2847d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2848d763cef2SBarry Smith 
2849d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2850d763cef2SBarry Smith 
2851d763cef2SBarry Smith @*/
28527087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2853d763cef2SBarry Smith {
2854dfbe8321SBarry Smith   PetscErrorCode ierr;
2855089b2837SJed Brown   SNES           snes;
2856d763cef2SBarry Smith 
2857d763cef2SBarry Smith   PetscFunctionBegin;
28580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2859d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2860089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2861089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2862d763cef2SBarry Smith   PetscFunctionReturn(0);
2863d763cef2SBarry Smith }
2864d763cef2SBarry Smith 
28654a2ae208SSatish Balay #undef __FUNCT__
28664a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2867d763cef2SBarry Smith /*@C
2868d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2869d763cef2SBarry Smith    TS options in the database.
2870d763cef2SBarry Smith 
2871d763cef2SBarry Smith    Not Collective
2872d763cef2SBarry Smith 
2873d763cef2SBarry Smith    Input Parameter:
2874d763cef2SBarry Smith .  ts - The TS context
2875d763cef2SBarry Smith 
2876d763cef2SBarry Smith    Output Parameter:
2877d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2878d763cef2SBarry Smith 
2879d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2880d763cef2SBarry Smith    sufficient length to hold the prefix.
2881d763cef2SBarry Smith 
2882d763cef2SBarry Smith    Level: intermediate
2883d763cef2SBarry Smith 
2884d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2885d763cef2SBarry Smith 
2886d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2887d763cef2SBarry Smith @*/
28887087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2889d763cef2SBarry Smith {
2890dfbe8321SBarry Smith   PetscErrorCode ierr;
2891d763cef2SBarry Smith 
2892d763cef2SBarry Smith   PetscFunctionBegin;
28930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28944482741eSBarry Smith   PetscValidPointer(prefix,2);
2895d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2896d763cef2SBarry Smith   PetscFunctionReturn(0);
2897d763cef2SBarry Smith }
2898d763cef2SBarry Smith 
28994a2ae208SSatish Balay #undef __FUNCT__
29004a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2901d763cef2SBarry Smith /*@C
2902d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2903d763cef2SBarry Smith 
2904d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2905d763cef2SBarry Smith 
2906d763cef2SBarry Smith    Input Parameter:
2907d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2908d763cef2SBarry Smith 
2909d763cef2SBarry Smith    Output Parameters:
2910b5abc632SBarry Smith +  J   - The Jacobian J of F, where U_t = G(U,t)
2911d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2912089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2913d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2914d763cef2SBarry Smith 
29150298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
2916d763cef2SBarry Smith 
2917d763cef2SBarry Smith    Level: intermediate
2918d763cef2SBarry Smith 
291926d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2920d763cef2SBarry Smith 
2921d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2922d763cef2SBarry Smith @*/
2923089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2924d763cef2SBarry Smith {
2925089b2837SJed Brown   PetscErrorCode ierr;
2926089b2837SJed Brown   SNES           snes;
292724989b8cSPeter Brune   DM             dm;
2928089b2837SJed Brown 
2929d763cef2SBarry Smith   PetscFunctionBegin;
2930089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
29310298fd71SBarry Smith   ierr = SNESGetJacobian(snes,J,M,NULL,NULL);CHKERRQ(ierr);
293224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
293324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2934d763cef2SBarry Smith   PetscFunctionReturn(0);
2935d763cef2SBarry Smith }
2936d763cef2SBarry Smith 
29371713a123SBarry Smith #undef __FUNCT__
29382eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
29392eca1d9cSJed Brown /*@C
29402eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
29412eca1d9cSJed Brown 
29422eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
29432eca1d9cSJed Brown 
29442eca1d9cSJed Brown    Input Parameter:
29452eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
29462eca1d9cSJed Brown 
29472eca1d9cSJed Brown    Output Parameters:
29482eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
29492eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
29502eca1d9cSJed Brown .  f   - The function to compute the matrices
29512eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
29522eca1d9cSJed Brown 
29530298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
29542eca1d9cSJed Brown 
29552eca1d9cSJed Brown    Level: advanced
29562eca1d9cSJed Brown 
29572eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
29582eca1d9cSJed Brown 
29592eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
29602eca1d9cSJed Brown @*/
29617087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
29622eca1d9cSJed Brown {
2963089b2837SJed Brown   PetscErrorCode ierr;
2964089b2837SJed Brown   SNES           snes;
296524989b8cSPeter Brune   DM             dm;
29660910c330SBarry Smith 
29672eca1d9cSJed Brown   PetscFunctionBegin;
2968089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2969f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
29700298fd71SBarry Smith   ierr = SNESGetJacobian(snes,A,B,NULL,NULL);CHKERRQ(ierr);
297124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
297224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
29732eca1d9cSJed Brown   PetscFunctionReturn(0);
29742eca1d9cSJed Brown }
29752eca1d9cSJed Brown 
29766083293cSBarry Smith 
29772eca1d9cSJed Brown #undef __FUNCT__
297883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
29791713a123SBarry Smith /*@C
298083a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
29811713a123SBarry Smith    VecView() for the solution at each timestep
29821713a123SBarry Smith 
29831713a123SBarry Smith    Collective on TS
29841713a123SBarry Smith 
29851713a123SBarry Smith    Input Parameters:
29861713a123SBarry Smith +  ts - the TS context
29871713a123SBarry Smith .  step - current time-step
2988142b95e3SSatish Balay .  ptime - current time
29890298fd71SBarry Smith -  dummy - either a viewer or NULL
29901713a123SBarry Smith 
299199fdda47SBarry Smith    Options Database:
299299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
299399fdda47SBarry Smith 
299499fdda47SBarry Smith    Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
299599fdda47SBarry Smith        will look bad
299699fdda47SBarry Smith 
29971713a123SBarry Smith    Level: intermediate
29981713a123SBarry Smith 
29991713a123SBarry Smith .keywords: TS,  vector, monitor, view
30001713a123SBarry Smith 
3001a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
30021713a123SBarry Smith @*/
30030910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
30041713a123SBarry Smith {
3005dfbe8321SBarry Smith   PetscErrorCode   ierr;
300683a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3007473a3ab2SBarry Smith   PetscDraw        draw;
30081713a123SBarry Smith 
30091713a123SBarry Smith   PetscFunctionBegin;
30106083293cSBarry Smith   if (!step && ictx->showinitial) {
30116083293cSBarry Smith     if (!ictx->initialsolution) {
30120910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
30131713a123SBarry Smith     }
30140910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
30156083293cSBarry Smith   }
30163fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
30170dcf80beSBarry Smith 
30186083293cSBarry Smith   if (ictx->showinitial) {
30196083293cSBarry Smith     PetscReal pause;
30206083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
30216083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
30226083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
30236083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
30246083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
30256083293cSBarry Smith   }
30260910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3027473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3028473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3029473a3ab2SBarry Smith     char      time[32];
3030473a3ab2SBarry Smith     size_t    len;
3031473a3ab2SBarry Smith 
3032473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3033473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3034473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3035473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
30360298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3037473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3038473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3039473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3040473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3041473a3ab2SBarry Smith   }
3042473a3ab2SBarry Smith 
30436083293cSBarry Smith   if (ictx->showinitial) {
30446083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
30456083293cSBarry Smith   }
30461713a123SBarry Smith   PetscFunctionReturn(0);
30471713a123SBarry Smith }
30481713a123SBarry Smith 
30492d5ee99bSBarry Smith #undef __FUNCT__
30502d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
30512d5ee99bSBarry Smith /*@C
30522d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
30532d5ee99bSBarry Smith 
30542d5ee99bSBarry Smith    Collective on TS
30552d5ee99bSBarry Smith 
30562d5ee99bSBarry Smith    Input Parameters:
30572d5ee99bSBarry Smith +  ts - the TS context
30582d5ee99bSBarry Smith .  step - current time-step
30592d5ee99bSBarry Smith .  ptime - current time
30602d5ee99bSBarry Smith -  dummy - either a viewer or NULL
30612d5ee99bSBarry Smith 
30622d5ee99bSBarry Smith    Level: intermediate
30632d5ee99bSBarry Smith 
30642d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
30652d5ee99bSBarry Smith 
30662d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
30672d5ee99bSBarry Smith @*/
30682d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
30692d5ee99bSBarry Smith {
30702d5ee99bSBarry Smith   PetscErrorCode    ierr;
30712d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
30722d5ee99bSBarry Smith   PetscDraw         draw;
30732d5ee99bSBarry Smith   MPI_Comm          comm;
30742d5ee99bSBarry Smith   PetscInt          n;
30752d5ee99bSBarry Smith   PetscMPIInt       size;
30762d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
30772d5ee99bSBarry Smith   char              time[32];
30782d5ee99bSBarry Smith   size_t            len;
30792d5ee99bSBarry Smith   const PetscScalar *U;
30802d5ee99bSBarry Smith 
30812d5ee99bSBarry Smith   PetscFunctionBegin;
30822d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
30832d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
30842d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
30852d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
30862d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
30872d5ee99bSBarry Smith 
30882d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
30892d5ee99bSBarry Smith 
30902d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
3091*4b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3092a4494fc1SBarry Smith   if ((U[0] < xl) || (U[1] < yl) || (U[0] > xr) || (U[1] > yr)) {
3093a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3094a4494fc1SBarry Smith       PetscFunctionReturn(0);
3095a4494fc1SBarry Smith   }
30962d5ee99bSBarry Smith   if (!step) ictx->color++;
30972d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
30982d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
30992d5ee99bSBarry Smith 
31002d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
3101*4b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
31022d5ee99bSBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
31032d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
31042d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
31052d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
31062d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
31072d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
31082d5ee99bSBarry Smith   }
31092d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
31102d5ee99bSBarry Smith   PetscFunctionReturn(0);
31112d5ee99bSBarry Smith }
31122d5ee99bSBarry Smith 
31131713a123SBarry Smith 
31146c699258SBarry Smith #undef __FUNCT__
311583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
31166083293cSBarry Smith /*@C
311783a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
31186083293cSBarry Smith 
31196083293cSBarry Smith    Collective on TS
31206083293cSBarry Smith 
31216083293cSBarry Smith    Input Parameters:
31226083293cSBarry Smith .    ctx - the monitor context
31236083293cSBarry Smith 
31246083293cSBarry Smith    Level: intermediate
31256083293cSBarry Smith 
31266083293cSBarry Smith .keywords: TS,  vector, monitor, view
31276083293cSBarry Smith 
312883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
31296083293cSBarry Smith @*/
313083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
31316083293cSBarry Smith {
31326083293cSBarry Smith   PetscErrorCode ierr;
31336083293cSBarry Smith 
31346083293cSBarry Smith   PetscFunctionBegin;
3135*4b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
313683a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
313783a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
313883a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
31396083293cSBarry Smith   PetscFunctionReturn(0);
31406083293cSBarry Smith }
31416083293cSBarry Smith 
31426083293cSBarry Smith #undef __FUNCT__
314383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
31446083293cSBarry Smith /*@C
314583a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
31466083293cSBarry Smith 
31476083293cSBarry Smith    Collective on TS
31486083293cSBarry Smith 
31496083293cSBarry Smith    Input Parameter:
31506083293cSBarry Smith .    ts - time-step context
31516083293cSBarry Smith 
31526083293cSBarry Smith    Output Patameter:
31536083293cSBarry Smith .    ctx - the monitor context
31546083293cSBarry Smith 
315599fdda47SBarry Smith    Options Database:
315699fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
315799fdda47SBarry Smith 
31586083293cSBarry Smith    Level: intermediate
31596083293cSBarry Smith 
31606083293cSBarry Smith .keywords: TS,  vector, monitor, view
31616083293cSBarry Smith 
316283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
31636083293cSBarry Smith @*/
316483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
31656083293cSBarry Smith {
31666083293cSBarry Smith   PetscErrorCode   ierr;
31676083293cSBarry Smith 
31686083293cSBarry Smith   PetscFunctionBegin;
316983a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
317083a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3171e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3172bbd56ea5SKarl Rupp 
317383a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3174473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
31750298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3176473a3ab2SBarry Smith 
3177473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
31780298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
31792d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
31806083293cSBarry Smith   PetscFunctionReturn(0);
31816083293cSBarry Smith }
31826083293cSBarry Smith 
31836083293cSBarry Smith #undef __FUNCT__
318483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
31853a471f94SBarry Smith /*@C
318683a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
31873a471f94SBarry Smith    VecView() for the error at each timestep
31883a471f94SBarry Smith 
31893a471f94SBarry Smith    Collective on TS
31903a471f94SBarry Smith 
31913a471f94SBarry Smith    Input Parameters:
31923a471f94SBarry Smith +  ts - the TS context
31933a471f94SBarry Smith .  step - current time-step
31943a471f94SBarry Smith .  ptime - current time
31950298fd71SBarry Smith -  dummy - either a viewer or NULL
31963a471f94SBarry Smith 
31973a471f94SBarry Smith    Level: intermediate
31983a471f94SBarry Smith 
31993a471f94SBarry Smith .keywords: TS,  vector, monitor, view
32003a471f94SBarry Smith 
32013a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
32023a471f94SBarry Smith @*/
32030910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
32043a471f94SBarry Smith {
32053a471f94SBarry Smith   PetscErrorCode   ierr;
320683a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
320783a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
32083a471f94SBarry Smith   Vec              work;
32093a471f94SBarry Smith 
32103a471f94SBarry Smith   PetscFunctionBegin;
32113fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
32120910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
32133a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
32140910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
32153a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
32163a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
32173a471f94SBarry Smith   PetscFunctionReturn(0);
32183a471f94SBarry Smith }
32193a471f94SBarry Smith 
32202a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
32213a471f94SBarry Smith #undef __FUNCT__
32226c699258SBarry Smith #define __FUNCT__ "TSSetDM"
32236c699258SBarry Smith /*@
32246c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
32256c699258SBarry Smith 
32263f9fe445SBarry Smith    Logically Collective on TS and DM
32276c699258SBarry Smith 
32286c699258SBarry Smith    Input Parameters:
32296c699258SBarry Smith +  ts - the preconditioner context
32306c699258SBarry Smith -  dm - the dm
32316c699258SBarry Smith 
32326c699258SBarry Smith    Level: intermediate
32336c699258SBarry Smith 
32346c699258SBarry Smith 
32356c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
32366c699258SBarry Smith @*/
32377087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
32386c699258SBarry Smith {
32396c699258SBarry Smith   PetscErrorCode ierr;
3240089b2837SJed Brown   SNES           snes;
3241942e3340SBarry Smith   DMTS           tsdm;
32426c699258SBarry Smith 
32436c699258SBarry Smith   PetscFunctionBegin;
32440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
324570663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3246942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
32472a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3248942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3249942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
325024989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
325124989b8cSPeter Brune         tsdm->originaldm = dm;
325224989b8cSPeter Brune       }
325324989b8cSPeter Brune     }
32546bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
325524989b8cSPeter Brune   }
32566c699258SBarry Smith   ts->dm = dm;
3257bbd56ea5SKarl Rupp 
3258089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3259089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
32606c699258SBarry Smith   PetscFunctionReturn(0);
32616c699258SBarry Smith }
32626c699258SBarry Smith 
32636c699258SBarry Smith #undef __FUNCT__
32646c699258SBarry Smith #define __FUNCT__ "TSGetDM"
32656c699258SBarry Smith /*@
32666c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
32676c699258SBarry Smith 
32683f9fe445SBarry Smith    Not Collective
32696c699258SBarry Smith 
32706c699258SBarry Smith    Input Parameter:
32716c699258SBarry Smith . ts - the preconditioner context
32726c699258SBarry Smith 
32736c699258SBarry Smith    Output Parameter:
32746c699258SBarry Smith .  dm - the dm
32756c699258SBarry Smith 
32766c699258SBarry Smith    Level: intermediate
32776c699258SBarry Smith 
32786c699258SBarry Smith 
32796c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
32806c699258SBarry Smith @*/
32817087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
32826c699258SBarry Smith {
3283496e6a7aSJed Brown   PetscErrorCode ierr;
3284496e6a7aSJed Brown 
32856c699258SBarry Smith   PetscFunctionBegin;
32860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3287496e6a7aSJed Brown   if (!ts->dm) {
3288ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
3289496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3290496e6a7aSJed Brown   }
32916c699258SBarry Smith   *dm = ts->dm;
32926c699258SBarry Smith   PetscFunctionReturn(0);
32936c699258SBarry Smith }
32941713a123SBarry Smith 
32950f5c6efeSJed Brown #undef __FUNCT__
32960f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
32970f5c6efeSJed Brown /*@
32980f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
32990f5c6efeSJed Brown 
33003f9fe445SBarry Smith    Logically Collective on SNES
33010f5c6efeSJed Brown 
33020f5c6efeSJed Brown    Input Parameter:
3303d42a1c89SJed Brown + snes - nonlinear solver
33040910c330SBarry Smith . U - the current state at which to evaluate the residual
3305d42a1c89SJed Brown - ctx - user context, must be a TS
33060f5c6efeSJed Brown 
33070f5c6efeSJed Brown    Output Parameter:
33080f5c6efeSJed Brown . F - the nonlinear residual
33090f5c6efeSJed Brown 
33100f5c6efeSJed Brown    Notes:
33110f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
33120f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
33130f5c6efeSJed Brown 
33140f5c6efeSJed Brown    Level: advanced
33150f5c6efeSJed Brown 
33160f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
33170f5c6efeSJed Brown @*/
33180910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
33190f5c6efeSJed Brown {
33200f5c6efeSJed Brown   TS             ts = (TS)ctx;
33210f5c6efeSJed Brown   PetscErrorCode ierr;
33220f5c6efeSJed Brown 
33230f5c6efeSJed Brown   PetscFunctionBegin;
33240f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33250910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
33260f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
33270f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
33280910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
33290f5c6efeSJed Brown   PetscFunctionReturn(0);
33300f5c6efeSJed Brown }
33310f5c6efeSJed Brown 
33320f5c6efeSJed Brown #undef __FUNCT__
33330f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
33340f5c6efeSJed Brown /*@
33350f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
33360f5c6efeSJed Brown 
33370f5c6efeSJed Brown    Collective on SNES
33380f5c6efeSJed Brown 
33390f5c6efeSJed Brown    Input Parameter:
33400f5c6efeSJed Brown + snes - nonlinear solver
33410910c330SBarry Smith . U - the current state at which to evaluate the residual
33420f5c6efeSJed Brown - ctx - user context, must be a TS
33430f5c6efeSJed Brown 
33440f5c6efeSJed Brown    Output Parameter:
33450f5c6efeSJed Brown + A - the Jacobian
33460f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
33470f5c6efeSJed Brown - flag - indicates any structure change in the matrix
33480f5c6efeSJed Brown 
33490f5c6efeSJed Brown    Notes:
33500f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
33510f5c6efeSJed Brown 
33520f5c6efeSJed Brown    Level: developer
33530f5c6efeSJed Brown 
33540f5c6efeSJed Brown .seealso: SNESSetJacobian()
33550f5c6efeSJed Brown @*/
33560910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
33570f5c6efeSJed Brown {
33580f5c6efeSJed Brown   TS             ts = (TS)ctx;
33590f5c6efeSJed Brown   PetscErrorCode ierr;
33600f5c6efeSJed Brown 
33610f5c6efeSJed Brown   PetscFunctionBegin;
33620f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33630910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
33640f5c6efeSJed Brown   PetscValidPointer(A,3);
33650f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
33660f5c6efeSJed Brown   PetscValidPointer(B,4);
33670f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
33680f5c6efeSJed Brown   PetscValidPointer(flag,5);
33690f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
33700910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
33710f5c6efeSJed Brown   PetscFunctionReturn(0);
33720f5c6efeSJed Brown }
3373325fc9f4SBarry Smith 
33740e4ef248SJed Brown #undef __FUNCT__
33750e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
33760e4ef248SJed Brown /*@C
33770e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
33780e4ef248SJed Brown 
33790e4ef248SJed Brown    Collective on TS
33800e4ef248SJed Brown 
33810e4ef248SJed Brown    Input Arguments:
33820e4ef248SJed Brown +  ts - time stepping context
33830e4ef248SJed Brown .  t - time at which to evaluate
33840910c330SBarry Smith .  U - state at which to evaluate
33850e4ef248SJed Brown -  ctx - context
33860e4ef248SJed Brown 
33870e4ef248SJed Brown    Output Arguments:
33880e4ef248SJed Brown .  F - right hand side
33890e4ef248SJed Brown 
33900e4ef248SJed Brown    Level: intermediate
33910e4ef248SJed Brown 
33920e4ef248SJed Brown    Notes:
33930e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
33940e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
33950e4ef248SJed Brown 
33960e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
33970e4ef248SJed Brown @*/
33980910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
33990e4ef248SJed Brown {
34000e4ef248SJed Brown   PetscErrorCode ierr;
34010e4ef248SJed Brown   Mat            Arhs,Brhs;
34020e4ef248SJed Brown   MatStructure   flg2;
34030e4ef248SJed Brown 
34040e4ef248SJed Brown   PetscFunctionBegin;
34050e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
34060910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
34070910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
34080e4ef248SJed Brown   PetscFunctionReturn(0);
34090e4ef248SJed Brown }
34100e4ef248SJed Brown 
34110e4ef248SJed Brown #undef __FUNCT__
34120e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
34130e4ef248SJed Brown /*@C
34140e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
34150e4ef248SJed Brown 
34160e4ef248SJed Brown    Collective on TS
34170e4ef248SJed Brown 
34180e4ef248SJed Brown    Input Arguments:
34190e4ef248SJed Brown +  ts - time stepping context
34200e4ef248SJed Brown .  t - time at which to evaluate
34210910c330SBarry Smith .  U - state at which to evaluate
34220e4ef248SJed Brown -  ctx - context
34230e4ef248SJed Brown 
34240e4ef248SJed Brown    Output Arguments:
34250e4ef248SJed Brown +  A - pointer to operator
34260e4ef248SJed Brown .  B - pointer to preconditioning matrix
34270e4ef248SJed Brown -  flg - matrix structure flag
34280e4ef248SJed Brown 
34290e4ef248SJed Brown    Level: intermediate
34300e4ef248SJed Brown 
34310e4ef248SJed Brown    Notes:
34320e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
34330e4ef248SJed Brown 
34340e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
34350e4ef248SJed Brown @*/
34360910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
34370e4ef248SJed Brown {
34380e4ef248SJed Brown   PetscFunctionBegin;
34390e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
34400e4ef248SJed Brown   PetscFunctionReturn(0);
34410e4ef248SJed Brown }
34420e4ef248SJed Brown 
34430026cea9SSean Farley #undef __FUNCT__
34440026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
34450026cea9SSean Farley /*@C
34460026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
34470026cea9SSean Farley 
34480026cea9SSean Farley    Collective on TS
34490026cea9SSean Farley 
34500026cea9SSean Farley    Input Arguments:
34510026cea9SSean Farley +  ts - time stepping context
34520026cea9SSean Farley .  t - time at which to evaluate
34530910c330SBarry Smith .  U - state at which to evaluate
34540910c330SBarry Smith .  Udot - time derivative of state vector
34550026cea9SSean Farley -  ctx - context
34560026cea9SSean Farley 
34570026cea9SSean Farley    Output Arguments:
34580026cea9SSean Farley .  F - left hand side
34590026cea9SSean Farley 
34600026cea9SSean Farley    Level: intermediate
34610026cea9SSean Farley 
34620026cea9SSean Farley    Notes:
34630910c330SBarry 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
34640026cea9SSean Farley    user is required to write their own TSComputeIFunction.
34650026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
34660026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
34670026cea9SSean Farley 
34680026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
34690026cea9SSean Farley @*/
34700910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
34710026cea9SSean Farley {
34720026cea9SSean Farley   PetscErrorCode ierr;
34730026cea9SSean Farley   Mat            A,B;
34740026cea9SSean Farley   MatStructure   flg2;
34750026cea9SSean Farley 
34760026cea9SSean Farley   PetscFunctionBegin;
34770298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
34780910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
34790910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
34800026cea9SSean Farley   PetscFunctionReturn(0);
34810026cea9SSean Farley }
34820026cea9SSean Farley 
34830026cea9SSean Farley #undef __FUNCT__
34840026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
34850026cea9SSean Farley /*@C
348624e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
34870026cea9SSean Farley 
34880026cea9SSean Farley    Collective on TS
34890026cea9SSean Farley 
34900026cea9SSean Farley    Input Arguments:
34910026cea9SSean Farley +  ts - time stepping context
34920026cea9SSean Farley .  t - time at which to evaluate
34930910c330SBarry Smith .  U - state at which to evaluate
34940910c330SBarry Smith .  Udot - time derivative of state vector
34950026cea9SSean Farley .  shift - shift to apply
34960026cea9SSean Farley -  ctx - context
34970026cea9SSean Farley 
34980026cea9SSean Farley    Output Arguments:
34990026cea9SSean Farley +  A - pointer to operator
35000026cea9SSean Farley .  B - pointer to preconditioning matrix
35010026cea9SSean Farley -  flg - matrix structure flag
35020026cea9SSean Farley 
35030026cea9SSean Farley    Level: intermediate
35040026cea9SSean Farley 
35050026cea9SSean Farley    Notes:
35060026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
35070026cea9SSean Farley 
35080026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
35090026cea9SSean Farley @*/
35100910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
35110026cea9SSean Farley {
35120026cea9SSean Farley   PetscFunctionBegin;
35130026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
35140026cea9SSean Farley   PetscFunctionReturn(0);
35150026cea9SSean Farley }
3516e817cc15SEmil Constantinescu #undef __FUNCT__
3517e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3518e817cc15SEmil Constantinescu /*@
3519e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3520e817cc15SEmil Constantinescu 
3521e817cc15SEmil Constantinescu    Not Collective
3522e817cc15SEmil Constantinescu 
3523e817cc15SEmil Constantinescu    Input Parameter:
3524e817cc15SEmil Constantinescu .  ts - the TS context
3525e817cc15SEmil Constantinescu 
3526e817cc15SEmil Constantinescu    Output Parameter:
3527e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3528e817cc15SEmil Constantinescu 
3529e817cc15SEmil Constantinescu    Level: beginner
3530e817cc15SEmil Constantinescu 
3531e817cc15SEmil Constantinescu .keywords: TS, equation type
3532e817cc15SEmil Constantinescu 
3533e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3534e817cc15SEmil Constantinescu @*/
3535e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3536e817cc15SEmil Constantinescu {
3537e817cc15SEmil Constantinescu   PetscFunctionBegin;
3538e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3539e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3540e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3541e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3542e817cc15SEmil Constantinescu }
3543e817cc15SEmil Constantinescu 
3544e817cc15SEmil Constantinescu #undef __FUNCT__
3545e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3546e817cc15SEmil Constantinescu /*@
3547e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3548e817cc15SEmil Constantinescu 
3549e817cc15SEmil Constantinescu    Not Collective
3550e817cc15SEmil Constantinescu 
3551e817cc15SEmil Constantinescu    Input Parameter:
3552e817cc15SEmil Constantinescu +  ts - the TS context
3553e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3554e817cc15SEmil Constantinescu 
3555e817cc15SEmil Constantinescu    Level: advanced
3556e817cc15SEmil Constantinescu 
3557e817cc15SEmil Constantinescu .keywords: TS, equation type
3558e817cc15SEmil Constantinescu 
3559e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3560e817cc15SEmil Constantinescu @*/
3561e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3562e817cc15SEmil Constantinescu {
3563e817cc15SEmil Constantinescu   PetscFunctionBegin;
3564e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3565e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3566e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3567e817cc15SEmil Constantinescu }
35680026cea9SSean Farley 
35694af1b03aSJed Brown #undef __FUNCT__
35704af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
35714af1b03aSJed Brown /*@
35724af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
35734af1b03aSJed Brown 
35744af1b03aSJed Brown    Not Collective
35754af1b03aSJed Brown 
35764af1b03aSJed Brown    Input Parameter:
35774af1b03aSJed Brown .  ts - the TS context
35784af1b03aSJed Brown 
35794af1b03aSJed Brown    Output Parameter:
35804af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
35814af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
35824af1b03aSJed Brown 
3583487e0bb9SJed Brown    Level: beginner
35844af1b03aSJed Brown 
3585cd652676SJed Brown    Notes:
3586cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
35874af1b03aSJed Brown 
35884af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
35894af1b03aSJed Brown 
35904af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
35914af1b03aSJed Brown @*/
35924af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
35934af1b03aSJed Brown {
35944af1b03aSJed Brown   PetscFunctionBegin;
35954af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35964af1b03aSJed Brown   PetscValidPointer(reason,2);
35974af1b03aSJed Brown   *reason = ts->reason;
35984af1b03aSJed Brown   PetscFunctionReturn(0);
35994af1b03aSJed Brown }
36004af1b03aSJed Brown 
3601fb1732b5SBarry Smith #undef __FUNCT__
3602d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3603d6ad946cSShri Abhyankar /*@
3604d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3605d6ad946cSShri Abhyankar 
3606d6ad946cSShri Abhyankar    Not Collective
3607d6ad946cSShri Abhyankar 
3608d6ad946cSShri Abhyankar    Input Parameter:
3609d6ad946cSShri Abhyankar +  ts - the TS context
3610d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3611d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3612d6ad946cSShri Abhyankar 
3613f5abba47SShri Abhyankar    Level: advanced
3614d6ad946cSShri Abhyankar 
3615d6ad946cSShri Abhyankar    Notes:
3616d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3617d6ad946cSShri Abhyankar 
3618d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3619d6ad946cSShri Abhyankar 
3620d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3621d6ad946cSShri Abhyankar @*/
3622d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3623d6ad946cSShri Abhyankar {
3624d6ad946cSShri Abhyankar   PetscFunctionBegin;
3625d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3626d6ad946cSShri Abhyankar   ts->reason = reason;
3627d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3628d6ad946cSShri Abhyankar }
3629d6ad946cSShri Abhyankar 
3630d6ad946cSShri Abhyankar #undef __FUNCT__
3631cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3632cc708dedSBarry Smith /*@
3633cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3634cc708dedSBarry Smith 
3635cc708dedSBarry Smith    Not Collective
3636cc708dedSBarry Smith 
3637cc708dedSBarry Smith    Input Parameter:
3638cc708dedSBarry Smith .  ts - the TS context
3639cc708dedSBarry Smith 
3640cc708dedSBarry Smith    Output Parameter:
3641cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3642cc708dedSBarry Smith 
3643487e0bb9SJed Brown    Level: beginner
3644cc708dedSBarry Smith 
3645cc708dedSBarry Smith    Notes:
3646cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3647cc708dedSBarry Smith 
3648cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3649cc708dedSBarry Smith 
3650cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3651cc708dedSBarry Smith @*/
3652cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3653cc708dedSBarry Smith {
3654cc708dedSBarry Smith   PetscFunctionBegin;
3655cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3656cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3657cc708dedSBarry Smith   *ftime = ts->solvetime;
3658cc708dedSBarry Smith   PetscFunctionReturn(0);
3659cc708dedSBarry Smith }
3660cc708dedSBarry Smith 
3661cc708dedSBarry Smith #undef __FUNCT__
36625ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
36639f67acb7SJed Brown /*@
36645ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
36659f67acb7SJed Brown    used by the time integrator.
36669f67acb7SJed Brown 
36679f67acb7SJed Brown    Not Collective
36689f67acb7SJed Brown 
36699f67acb7SJed Brown    Input Parameter:
36709f67acb7SJed Brown .  ts - TS context
36719f67acb7SJed Brown 
36729f67acb7SJed Brown    Output Parameter:
36739f67acb7SJed Brown .  nits - number of nonlinear iterations
36749f67acb7SJed Brown 
36759f67acb7SJed Brown    Notes:
36769f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
36779f67acb7SJed Brown 
36789f67acb7SJed Brown    Level: intermediate
36799f67acb7SJed Brown 
36809f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
36819f67acb7SJed Brown 
36825ef26d82SJed Brown .seealso:  TSGetKSPIterations()
36839f67acb7SJed Brown @*/
36845ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
36859f67acb7SJed Brown {
36869f67acb7SJed Brown   PetscFunctionBegin;
36879f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36889f67acb7SJed Brown   PetscValidIntPointer(nits,2);
36895ef26d82SJed Brown   *nits = ts->snes_its;
36909f67acb7SJed Brown   PetscFunctionReturn(0);
36919f67acb7SJed Brown }
36929f67acb7SJed Brown 
36939f67acb7SJed Brown #undef __FUNCT__
36945ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
36959f67acb7SJed Brown /*@
36965ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
36979f67acb7SJed Brown    used by the time integrator.
36989f67acb7SJed Brown 
36999f67acb7SJed Brown    Not Collective
37009f67acb7SJed Brown 
37019f67acb7SJed Brown    Input Parameter:
37029f67acb7SJed Brown .  ts - TS context
37039f67acb7SJed Brown 
37049f67acb7SJed Brown    Output Parameter:
37059f67acb7SJed Brown .  lits - number of linear iterations
37069f67acb7SJed Brown 
37079f67acb7SJed Brown    Notes:
37089f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
37099f67acb7SJed Brown 
37109f67acb7SJed Brown    Level: intermediate
37119f67acb7SJed Brown 
37129f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
37139f67acb7SJed Brown 
37145ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
37159f67acb7SJed Brown @*/
37165ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
37179f67acb7SJed Brown {
37189f67acb7SJed Brown   PetscFunctionBegin;
37199f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37209f67acb7SJed Brown   PetscValidIntPointer(lits,2);
37215ef26d82SJed Brown   *lits = ts->ksp_its;
37229f67acb7SJed Brown   PetscFunctionReturn(0);
37239f67acb7SJed Brown }
37249f67acb7SJed Brown 
37259f67acb7SJed Brown #undef __FUNCT__
3726cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3727cef5090cSJed Brown /*@
3728cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3729cef5090cSJed Brown 
3730cef5090cSJed Brown    Not Collective
3731cef5090cSJed Brown 
3732cef5090cSJed Brown    Input Parameter:
3733cef5090cSJed Brown .  ts - TS context
3734cef5090cSJed Brown 
3735cef5090cSJed Brown    Output Parameter:
3736cef5090cSJed Brown .  rejects - number of steps rejected
3737cef5090cSJed Brown 
3738cef5090cSJed Brown    Notes:
3739cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3740cef5090cSJed Brown 
3741cef5090cSJed Brown    Level: intermediate
3742cef5090cSJed Brown 
3743cef5090cSJed Brown .keywords: TS, get, number
3744cef5090cSJed Brown 
37455ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3746cef5090cSJed Brown @*/
3747cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3748cef5090cSJed Brown {
3749cef5090cSJed Brown   PetscFunctionBegin;
3750cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3751cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3752cef5090cSJed Brown   *rejects = ts->reject;
3753cef5090cSJed Brown   PetscFunctionReturn(0);
3754cef5090cSJed Brown }
3755cef5090cSJed Brown 
3756cef5090cSJed Brown #undef __FUNCT__
3757cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3758cef5090cSJed Brown /*@
3759cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3760cef5090cSJed Brown 
3761cef5090cSJed Brown    Not Collective
3762cef5090cSJed Brown 
3763cef5090cSJed Brown    Input Parameter:
3764cef5090cSJed Brown .  ts - TS context
3765cef5090cSJed Brown 
3766cef5090cSJed Brown    Output Parameter:
3767cef5090cSJed Brown .  fails - number of failed nonlinear solves
3768cef5090cSJed Brown 
3769cef5090cSJed Brown    Notes:
3770cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3771cef5090cSJed Brown 
3772cef5090cSJed Brown    Level: intermediate
3773cef5090cSJed Brown 
3774cef5090cSJed Brown .keywords: TS, get, number
3775cef5090cSJed Brown 
37765ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3777cef5090cSJed Brown @*/
3778cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3779cef5090cSJed Brown {
3780cef5090cSJed Brown   PetscFunctionBegin;
3781cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3782cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3783cef5090cSJed Brown   *fails = ts->num_snes_failures;
3784cef5090cSJed Brown   PetscFunctionReturn(0);
3785cef5090cSJed Brown }
3786cef5090cSJed Brown 
3787cef5090cSJed Brown #undef __FUNCT__
3788cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3789cef5090cSJed Brown /*@
3790cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3791cef5090cSJed Brown 
3792cef5090cSJed Brown    Not Collective
3793cef5090cSJed Brown 
3794cef5090cSJed Brown    Input Parameter:
3795cef5090cSJed Brown +  ts - TS context
3796cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3797cef5090cSJed Brown 
3798cef5090cSJed Brown    Notes:
3799cef5090cSJed Brown    The counter is reset to zero for each step
3800cef5090cSJed Brown 
3801cef5090cSJed Brown    Options Database Key:
3802cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3803cef5090cSJed Brown 
3804cef5090cSJed Brown    Level: intermediate
3805cef5090cSJed Brown 
3806cef5090cSJed Brown .keywords: TS, set, maximum, number
3807cef5090cSJed Brown 
38085ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3809cef5090cSJed Brown @*/
3810cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3811cef5090cSJed Brown {
3812cef5090cSJed Brown   PetscFunctionBegin;
3813cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3814cef5090cSJed Brown   ts->max_reject = rejects;
3815cef5090cSJed Brown   PetscFunctionReturn(0);
3816cef5090cSJed Brown }
3817cef5090cSJed Brown 
3818cef5090cSJed Brown #undef __FUNCT__
3819cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3820cef5090cSJed Brown /*@
3821cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3822cef5090cSJed Brown 
3823cef5090cSJed Brown    Not Collective
3824cef5090cSJed Brown 
3825cef5090cSJed Brown    Input Parameter:
3826cef5090cSJed Brown +  ts - TS context
3827cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3828cef5090cSJed Brown 
3829cef5090cSJed Brown    Notes:
3830cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3831cef5090cSJed Brown 
3832cef5090cSJed Brown    Options Database Key:
3833cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3834cef5090cSJed Brown 
3835cef5090cSJed Brown    Level: intermediate
3836cef5090cSJed Brown 
3837cef5090cSJed Brown .keywords: TS, set, maximum, number
3838cef5090cSJed Brown 
38395ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3840cef5090cSJed Brown @*/
3841cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3842cef5090cSJed Brown {
3843cef5090cSJed Brown   PetscFunctionBegin;
3844cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3845cef5090cSJed Brown   ts->max_snes_failures = fails;
3846cef5090cSJed Brown   PetscFunctionReturn(0);
3847cef5090cSJed Brown }
3848cef5090cSJed Brown 
3849cef5090cSJed Brown #undef __FUNCT__
3850cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3851cef5090cSJed Brown /*@
3852cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3853cef5090cSJed Brown 
3854cef5090cSJed Brown    Not Collective
3855cef5090cSJed Brown 
3856cef5090cSJed Brown    Input Parameter:
3857cef5090cSJed Brown +  ts - TS context
3858cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3859cef5090cSJed Brown 
3860cef5090cSJed Brown    Options Database Key:
3861cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3862cef5090cSJed Brown 
3863cef5090cSJed Brown    Level: intermediate
3864cef5090cSJed Brown 
3865cef5090cSJed Brown .keywords: TS, set, error
3866cef5090cSJed Brown 
38675ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3868cef5090cSJed Brown @*/
3869cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3870cef5090cSJed Brown {
3871cef5090cSJed Brown   PetscFunctionBegin;
3872cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3873cef5090cSJed Brown   ts->errorifstepfailed = err;
3874cef5090cSJed Brown   PetscFunctionReturn(0);
3875cef5090cSJed Brown }
3876cef5090cSJed Brown 
3877cef5090cSJed Brown #undef __FUNCT__
3878fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3879fb1732b5SBarry Smith /*@C
3880fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3881fb1732b5SBarry Smith 
3882fb1732b5SBarry Smith    Collective on TS
3883fb1732b5SBarry Smith 
3884fb1732b5SBarry Smith    Input Parameters:
3885fb1732b5SBarry Smith +  ts - the TS context
3886fb1732b5SBarry Smith .  step - current time-step
3887fb1732b5SBarry Smith .  ptime - current time
38880910c330SBarry Smith .  u - current state
3889fb1732b5SBarry Smith -  viewer - binary viewer
3890fb1732b5SBarry Smith 
3891fb1732b5SBarry Smith    Level: intermediate
3892fb1732b5SBarry Smith 
3893fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3894fb1732b5SBarry Smith 
3895fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3896fb1732b5SBarry Smith @*/
38970910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3898fb1732b5SBarry Smith {
3899fb1732b5SBarry Smith   PetscErrorCode ierr;
3900ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3901fb1732b5SBarry Smith 
3902fb1732b5SBarry Smith   PetscFunctionBegin;
39030910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3904ed81e22dSJed Brown   PetscFunctionReturn(0);
3905ed81e22dSJed Brown }
3906ed81e22dSJed Brown 
3907ed81e22dSJed Brown #undef __FUNCT__
3908ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3909ed81e22dSJed Brown /*@C
3910ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3911ed81e22dSJed Brown 
3912ed81e22dSJed Brown    Collective on TS
3913ed81e22dSJed Brown 
3914ed81e22dSJed Brown    Input Parameters:
3915ed81e22dSJed Brown +  ts - the TS context
3916ed81e22dSJed Brown .  step - current time-step
3917ed81e22dSJed Brown .  ptime - current time
39180910c330SBarry Smith .  u - current state
3919ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3920ed81e22dSJed Brown 
3921ed81e22dSJed Brown    Level: intermediate
3922ed81e22dSJed Brown 
3923ed81e22dSJed Brown    Notes:
3924ed81e22dSJed 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.
3925ed81e22dSJed Brown    These are named according to the file name template.
3926ed81e22dSJed Brown 
3927ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3928ed81e22dSJed Brown 
3929ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3930ed81e22dSJed Brown 
3931ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3932ed81e22dSJed Brown @*/
39330910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3934ed81e22dSJed Brown {
3935ed81e22dSJed Brown   PetscErrorCode ierr;
3936ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3937ed81e22dSJed Brown   PetscViewer    viewer;
3938ed81e22dSJed Brown 
3939ed81e22dSJed Brown   PetscFunctionBegin;
39408caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3941ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
39420910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3943ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3944ed81e22dSJed Brown   PetscFunctionReturn(0);
3945ed81e22dSJed Brown }
3946ed81e22dSJed Brown 
3947ed81e22dSJed Brown #undef __FUNCT__
3948ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3949ed81e22dSJed Brown /*@C
3950ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3951ed81e22dSJed Brown 
3952ed81e22dSJed Brown    Collective on TS
3953ed81e22dSJed Brown 
3954ed81e22dSJed Brown    Input Parameters:
3955ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3956ed81e22dSJed Brown 
3957ed81e22dSJed Brown    Level: intermediate
3958ed81e22dSJed Brown 
3959ed81e22dSJed Brown    Note:
3960ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3961ed81e22dSJed Brown 
3962ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3963ed81e22dSJed Brown 
3964ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3965ed81e22dSJed Brown @*/
3966ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3967ed81e22dSJed Brown {
3968ed81e22dSJed Brown   PetscErrorCode ierr;
3969ed81e22dSJed Brown 
3970ed81e22dSJed Brown   PetscFunctionBegin;
3971ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3972fb1732b5SBarry Smith   PetscFunctionReturn(0);
3973fb1732b5SBarry Smith }
3974fb1732b5SBarry Smith 
397584df9cb4SJed Brown #undef __FUNCT__
3976ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt"
397784df9cb4SJed Brown /*@
3978ad6bc421SBarry Smith    TSGetTSAdapt - Get the adaptive controller context for the current method
397984df9cb4SJed Brown 
3980ed81e22dSJed Brown    Collective on TS if controller has not been created yet
398184df9cb4SJed Brown 
398284df9cb4SJed Brown    Input Arguments:
3983ed81e22dSJed Brown .  ts - time stepping context
398484df9cb4SJed Brown 
398584df9cb4SJed Brown    Output Arguments:
3986ed81e22dSJed Brown .  adapt - adaptive controller
398784df9cb4SJed Brown 
398884df9cb4SJed Brown    Level: intermediate
398984df9cb4SJed Brown 
3990ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
399184df9cb4SJed Brown @*/
3992ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt)
399384df9cb4SJed Brown {
399484df9cb4SJed Brown   PetscErrorCode ierr;
399584df9cb4SJed Brown 
399684df9cb4SJed Brown   PetscFunctionBegin;
399784df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
399884df9cb4SJed Brown   PetscValidPointer(adapt,2);
399984df9cb4SJed Brown   if (!ts->adapt) {
4000ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
40011c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
40021c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
400384df9cb4SJed Brown   }
400484df9cb4SJed Brown   *adapt = ts->adapt;
400584df9cb4SJed Brown   PetscFunctionReturn(0);
400684df9cb4SJed Brown }
4007d6ebe24aSShri Abhyankar 
4008d6ebe24aSShri Abhyankar #undef __FUNCT__
40091c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
40101c3436cfSJed Brown /*@
40111c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
40121c3436cfSJed Brown 
40131c3436cfSJed Brown    Logically Collective
40141c3436cfSJed Brown 
40151c3436cfSJed Brown    Input Arguments:
40161c3436cfSJed Brown +  ts - time integration context
40171c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
40180298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
40191c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
40200298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
40211c3436cfSJed Brown 
40221c3436cfSJed Brown    Level: beginner
40231c3436cfSJed Brown 
4024c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
40251c3436cfSJed Brown @*/
40261c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
40271c3436cfSJed Brown {
40281c3436cfSJed Brown   PetscErrorCode ierr;
40291c3436cfSJed Brown 
40301c3436cfSJed Brown   PetscFunctionBegin;
4031c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
40321c3436cfSJed Brown   if (vatol) {
40331c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
40341c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4035bbd56ea5SKarl Rupp 
40361c3436cfSJed Brown     ts->vatol = vatol;
40371c3436cfSJed Brown   }
4038c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
40391c3436cfSJed Brown   if (vrtol) {
40401c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
40411c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4042bbd56ea5SKarl Rupp 
40431c3436cfSJed Brown     ts->vrtol = vrtol;
40441c3436cfSJed Brown   }
40451c3436cfSJed Brown   PetscFunctionReturn(0);
40461c3436cfSJed Brown }
40471c3436cfSJed Brown 
40481c3436cfSJed Brown #undef __FUNCT__
4049c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4050c5033834SJed Brown /*@
4051c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4052c5033834SJed Brown 
4053c5033834SJed Brown    Logically Collective
4054c5033834SJed Brown 
4055c5033834SJed Brown    Input Arguments:
4056c5033834SJed Brown .  ts - time integration context
4057c5033834SJed Brown 
4058c5033834SJed Brown    Output Arguments:
40590298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
40600298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
40610298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
40620298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4063c5033834SJed Brown 
4064c5033834SJed Brown    Level: beginner
4065c5033834SJed Brown 
4066c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4067c5033834SJed Brown @*/
4068c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4069c5033834SJed Brown {
4070c5033834SJed Brown   PetscFunctionBegin;
4071c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4072c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4073c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4074c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4075c5033834SJed Brown   PetscFunctionReturn(0);
4076c5033834SJed Brown }
4077c5033834SJed Brown 
4078c5033834SJed Brown #undef __FUNCT__
40791c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
40801c3436cfSJed Brown /*@
40811c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
40821c3436cfSJed Brown 
40831c3436cfSJed Brown    Collective on TS
40841c3436cfSJed Brown 
40851c3436cfSJed Brown    Input Arguments:
40861c3436cfSJed Brown +  ts - time stepping context
40871c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
40881c3436cfSJed Brown 
40891c3436cfSJed Brown    Output Arguments:
40901c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
40911c3436cfSJed Brown 
40921c3436cfSJed Brown    Level: developer
40931c3436cfSJed Brown 
40941c3436cfSJed Brown .seealso: TSSetTolerances()
40951c3436cfSJed Brown @*/
40961c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
40971c3436cfSJed Brown {
40981c3436cfSJed Brown   PetscErrorCode    ierr;
40991c3436cfSJed Brown   PetscInt          i,n,N;
41000910c330SBarry Smith   const PetscScalar *u,*y;
41010910c330SBarry Smith   Vec               U;
41021c3436cfSJed Brown   PetscReal         sum,gsum;
41031c3436cfSJed Brown 
41041c3436cfSJed Brown   PetscFunctionBegin;
41051c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41061c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
41071c3436cfSJed Brown   PetscValidPointer(norm,3);
41080910c330SBarry Smith   U = ts->vec_sol;
41090910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4110ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
41111c3436cfSJed Brown 
41120910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
41130910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
41140910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
41151c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
41161c3436cfSJed Brown   sum  = 0.;
4117ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4118ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4119ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4120ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4121ceefc113SJed Brown     for (i=0; i<n; i++) {
41220910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
41230910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4124ceefc113SJed Brown     }
4125ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4126ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4127ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4128ceefc113SJed Brown     const PetscScalar *atol;
4129ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4130ceefc113SJed Brown     for (i=0; i<n; i++) {
41310910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
41320910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4133ceefc113SJed Brown     }
4134ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4135ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4136ceefc113SJed Brown     const PetscScalar *rtol;
4137ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4138ceefc113SJed Brown     for (i=0; i<n; i++) {
41390910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
41400910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4141ceefc113SJed Brown     }
4142ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4143ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
41441c3436cfSJed Brown     for (i=0; i<n; i++) {
41450910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
41460910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
41471c3436cfSJed Brown     }
4148ceefc113SJed Brown   }
41490910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
41501c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
41511c3436cfSJed Brown 
4152ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
41531c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4154ce94432eSBarry Smith   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
41551c3436cfSJed Brown   PetscFunctionReturn(0);
41561c3436cfSJed Brown }
41571c3436cfSJed Brown 
41581c3436cfSJed Brown #undef __FUNCT__
41598d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
41608d59e960SJed Brown /*@
41618d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
41628d59e960SJed Brown 
41638d59e960SJed Brown    Logically Collective on TS
41648d59e960SJed Brown 
41658d59e960SJed Brown    Input Arguments:
41668d59e960SJed Brown +  ts - time stepping context
41678d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
41688d59e960SJed Brown 
41698d59e960SJed Brown    Note:
41708d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
41718d59e960SJed Brown 
41728d59e960SJed Brown    Level: intermediate
41738d59e960SJed Brown 
41748d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
41758d59e960SJed Brown @*/
41768d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
41778d59e960SJed Brown {
41788d59e960SJed Brown   PetscFunctionBegin;
41798d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41808d59e960SJed Brown   ts->cfltime_local = cfltime;
41818d59e960SJed Brown   ts->cfltime       = -1.;
41828d59e960SJed Brown   PetscFunctionReturn(0);
41838d59e960SJed Brown }
41848d59e960SJed Brown 
41858d59e960SJed Brown #undef __FUNCT__
41868d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
41878d59e960SJed Brown /*@
41888d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
41898d59e960SJed Brown 
41908d59e960SJed Brown    Collective on TS
41918d59e960SJed Brown 
41928d59e960SJed Brown    Input Arguments:
41938d59e960SJed Brown .  ts - time stepping context
41948d59e960SJed Brown 
41958d59e960SJed Brown    Output Arguments:
41968d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
41978d59e960SJed Brown 
41988d59e960SJed Brown    Level: advanced
41998d59e960SJed Brown 
42008d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
42018d59e960SJed Brown @*/
42028d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
42038d59e960SJed Brown {
42048d59e960SJed Brown   PetscErrorCode ierr;
42058d59e960SJed Brown 
42068d59e960SJed Brown   PetscFunctionBegin;
42078d59e960SJed Brown   if (ts->cfltime < 0) {
4208ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
42098d59e960SJed Brown   }
42108d59e960SJed Brown   *cfltime = ts->cfltime;
42118d59e960SJed Brown   PetscFunctionReturn(0);
42128d59e960SJed Brown }
42138d59e960SJed Brown 
42148d59e960SJed Brown #undef __FUNCT__
4215d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4216d6ebe24aSShri Abhyankar /*@
4217d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4218d6ebe24aSShri Abhyankar 
4219d6ebe24aSShri Abhyankar    Input Parameters:
4220d6ebe24aSShri Abhyankar .  ts   - the TS context.
4221d6ebe24aSShri Abhyankar .  xl   - lower bound.
4222d6ebe24aSShri Abhyankar .  xu   - upper bound.
4223d6ebe24aSShri Abhyankar 
4224d6ebe24aSShri Abhyankar    Notes:
4225d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
422633c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4227d6ebe24aSShri Abhyankar 
42282bd2b0e6SSatish Balay    Level: advanced
42292bd2b0e6SSatish Balay 
4230d6ebe24aSShri Abhyankar @*/
4231d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4232d6ebe24aSShri Abhyankar {
4233d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4234d6ebe24aSShri Abhyankar   SNES           snes;
4235d6ebe24aSShri Abhyankar 
4236d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4237d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4238d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4239d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4240d6ebe24aSShri Abhyankar }
4241d6ebe24aSShri Abhyankar 
4242325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4243c6db04a5SJed Brown #include <mex.h>
4244325fc9f4SBarry Smith 
4245325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4246325fc9f4SBarry Smith 
4247325fc9f4SBarry Smith #undef __FUNCT__
4248325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4249325fc9f4SBarry Smith /*
4250325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4251325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4252325fc9f4SBarry Smith 
4253325fc9f4SBarry Smith    Collective on TS
4254325fc9f4SBarry Smith 
4255325fc9f4SBarry Smith    Input Parameters:
4256325fc9f4SBarry Smith +  snes - the TS context
42570910c330SBarry Smith -  u - input vector
4258325fc9f4SBarry Smith 
4259325fc9f4SBarry Smith    Output Parameter:
4260325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4261325fc9f4SBarry Smith 
4262325fc9f4SBarry Smith    Notes:
4263325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4264325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4265325fc9f4SBarry Smith    themselves.
4266325fc9f4SBarry Smith 
4267325fc9f4SBarry Smith    Level: developer
4268325fc9f4SBarry Smith 
4269325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4270325fc9f4SBarry Smith 
4271325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4272325fc9f4SBarry Smith */
42730910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4274325fc9f4SBarry Smith {
4275325fc9f4SBarry Smith   PetscErrorCode  ierr;
4276325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4277325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4278325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4279325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4280325fc9f4SBarry Smith 
4281325fc9f4SBarry Smith   PetscFunctionBegin;
4282325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
42830910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
42840910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4285325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
42860910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4287325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4288325fc9f4SBarry Smith 
4289325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
42900910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
42910910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
42920910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4293bbd56ea5SKarl Rupp 
4294325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4295325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4296325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4297325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4298325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4299325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4300325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4301325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4302325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4303325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4304325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4305325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4306325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4307325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4308325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4309325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4310325fc9f4SBarry Smith   PetscFunctionReturn(0);
4311325fc9f4SBarry Smith }
4312325fc9f4SBarry Smith 
4313325fc9f4SBarry Smith 
4314325fc9f4SBarry Smith #undef __FUNCT__
4315325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4316325fc9f4SBarry Smith /*
4317325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4318325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4319e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4320325fc9f4SBarry Smith 
4321325fc9f4SBarry Smith    Logically Collective on TS
4322325fc9f4SBarry Smith 
4323325fc9f4SBarry Smith    Input Parameters:
4324325fc9f4SBarry Smith +  ts - the TS context
4325325fc9f4SBarry Smith -  func - function evaluation routine
4326325fc9f4SBarry Smith 
4327325fc9f4SBarry Smith    Calling sequence of func:
43280910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4329325fc9f4SBarry Smith 
4330325fc9f4SBarry Smith    Level: beginner
4331325fc9f4SBarry Smith 
4332325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4333325fc9f4SBarry Smith 
4334325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4335325fc9f4SBarry Smith */
4336cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4337325fc9f4SBarry Smith {
4338325fc9f4SBarry Smith   PetscErrorCode  ierr;
4339325fc9f4SBarry Smith   TSMatlabContext *sctx;
4340325fc9f4SBarry Smith 
4341325fc9f4SBarry Smith   PetscFunctionBegin;
4342325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4343325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4344325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4345325fc9f4SBarry Smith   /*
4346325fc9f4SBarry Smith      This should work, but it doesn't
4347325fc9f4SBarry Smith   sctx->ctx = ctx;
4348325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4349325fc9f4SBarry Smith   */
4350325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4351bbd56ea5SKarl Rupp 
43520298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4353325fc9f4SBarry Smith   PetscFunctionReturn(0);
4354325fc9f4SBarry Smith }
4355325fc9f4SBarry Smith 
4356325fc9f4SBarry Smith #undef __FUNCT__
4357325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4358325fc9f4SBarry Smith /*
4359325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4360325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4361325fc9f4SBarry Smith 
4362325fc9f4SBarry Smith    Collective on TS
4363325fc9f4SBarry Smith 
4364325fc9f4SBarry Smith    Input Parameters:
4365cdcf91faSSean Farley +  ts - the TS context
43660910c330SBarry Smith .  u - input vector
4367325fc9f4SBarry Smith .  A, B - the matrices
4368325fc9f4SBarry Smith -  ctx - user context
4369325fc9f4SBarry Smith 
4370325fc9f4SBarry Smith    Output Parameter:
4371325fc9f4SBarry Smith .  flag - structure of the matrix
4372325fc9f4SBarry Smith 
4373325fc9f4SBarry Smith    Level: developer
4374325fc9f4SBarry Smith 
4375325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4376325fc9f4SBarry Smith 
4377325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4378325fc9f4SBarry Smith @*/
43790910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4380325fc9f4SBarry Smith {
4381325fc9f4SBarry Smith   PetscErrorCode  ierr;
4382325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4383325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4384325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4385325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4386325fc9f4SBarry Smith 
4387325fc9f4SBarry Smith   PetscFunctionBegin;
4388cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43890910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4390325fc9f4SBarry Smith 
43910910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4392325fc9f4SBarry Smith 
4393cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
43940910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
43950910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
43960910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
43970910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4398bbd56ea5SKarl Rupp 
4399325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4400325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4401325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4402325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4403325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4404325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4405325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4406325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4407325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4408325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4409325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4410325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4411325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4412325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4413325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4414325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4415325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4416325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4417325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4418325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4419325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4420325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4421325fc9f4SBarry Smith   PetscFunctionReturn(0);
4422325fc9f4SBarry Smith }
4423325fc9f4SBarry Smith 
4424325fc9f4SBarry Smith 
4425325fc9f4SBarry Smith #undef __FUNCT__
4426325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4427325fc9f4SBarry Smith /*
4428325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4429e3c5b3baSBarry 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
4430325fc9f4SBarry Smith 
4431325fc9f4SBarry Smith    Logically Collective on TS
4432325fc9f4SBarry Smith 
4433325fc9f4SBarry Smith    Input Parameters:
4434cdcf91faSSean Farley +  ts - the TS context
4435325fc9f4SBarry Smith .  A,B - Jacobian matrices
4436325fc9f4SBarry Smith .  func - function evaluation routine
4437325fc9f4SBarry Smith -  ctx - user context
4438325fc9f4SBarry Smith 
4439325fc9f4SBarry Smith    Calling sequence of func:
44400910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4441325fc9f4SBarry Smith 
4442325fc9f4SBarry Smith 
4443325fc9f4SBarry Smith    Level: developer
4444325fc9f4SBarry Smith 
4445325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4446325fc9f4SBarry Smith 
4447325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4448325fc9f4SBarry Smith */
4449cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4450325fc9f4SBarry Smith {
4451325fc9f4SBarry Smith   PetscErrorCode  ierr;
4452325fc9f4SBarry Smith   TSMatlabContext *sctx;
4453325fc9f4SBarry Smith 
4454325fc9f4SBarry Smith   PetscFunctionBegin;
4455325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4456325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4457325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4458325fc9f4SBarry Smith   /*
4459325fc9f4SBarry Smith      This should work, but it doesn't
4460325fc9f4SBarry Smith   sctx->ctx = ctx;
4461325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4462325fc9f4SBarry Smith   */
4463325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4464bbd56ea5SKarl Rupp 
4465cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4466325fc9f4SBarry Smith   PetscFunctionReturn(0);
4467325fc9f4SBarry Smith }
4468325fc9f4SBarry Smith 
4469b5b1a830SBarry Smith #undef __FUNCT__
4470b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4471b5b1a830SBarry Smith /*
4472b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4473b5b1a830SBarry Smith 
4474b5b1a830SBarry Smith    Collective on TS
4475b5b1a830SBarry Smith 
4476b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4477b5b1a830SBarry Smith @*/
44780910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4479b5b1a830SBarry Smith {
4480b5b1a830SBarry Smith   PetscErrorCode  ierr;
4481b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4482a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4483b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4484b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4485b5b1a830SBarry Smith 
4486b5b1a830SBarry Smith   PetscFunctionBegin;
4487cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44880910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4489b5b1a830SBarry Smith 
4490cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
44910910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4492bbd56ea5SKarl Rupp 
4493b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4494b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4495b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4496b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4497b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4498b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4499b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4500b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4501b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4502b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4503b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4504b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4505b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4506b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4507b5b1a830SBarry Smith   PetscFunctionReturn(0);
4508b5b1a830SBarry Smith }
4509b5b1a830SBarry Smith 
4510b5b1a830SBarry Smith 
4511b5b1a830SBarry Smith #undef __FUNCT__
4512b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4513b5b1a830SBarry Smith /*
4514b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4515b5b1a830SBarry Smith 
4516b5b1a830SBarry Smith    Level: developer
4517b5b1a830SBarry Smith 
4518b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4519b5b1a830SBarry Smith 
4520b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4521b5b1a830SBarry Smith */
4522cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4523b5b1a830SBarry Smith {
4524b5b1a830SBarry Smith   PetscErrorCode  ierr;
4525b5b1a830SBarry Smith   TSMatlabContext *sctx;
4526b5b1a830SBarry Smith 
4527b5b1a830SBarry Smith   PetscFunctionBegin;
4528b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4529b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4530b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4531b5b1a830SBarry Smith   /*
4532b5b1a830SBarry Smith      This should work, but it doesn't
4533b5b1a830SBarry Smith   sctx->ctx = ctx;
4534b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4535b5b1a830SBarry Smith   */
4536b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4537bbd56ea5SKarl Rupp 
45380298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
4539b5b1a830SBarry Smith   PetscFunctionReturn(0);
4540b5b1a830SBarry Smith }
4541325fc9f4SBarry Smith #endif
4542b3603a34SBarry Smith 
45430b039ecaSBarry Smith 
45440b039ecaSBarry Smith 
4545b3603a34SBarry Smith #undef __FUNCT__
45464f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4547b3603a34SBarry Smith /*@C
45484f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4549b3603a34SBarry Smith        in a time based line graph
4550b3603a34SBarry Smith 
4551b3603a34SBarry Smith    Collective on TS
4552b3603a34SBarry Smith 
4553b3603a34SBarry Smith    Input Parameters:
4554b3603a34SBarry Smith +  ts - the TS context
4555b3603a34SBarry Smith .  step - current time-step
4556b3603a34SBarry Smith .  ptime - current time
4557b3603a34SBarry Smith -  lg - a line graph object
4558b3603a34SBarry Smith 
4559b3603a34SBarry Smith    Level: intermediate
4560b3603a34SBarry Smith 
45610b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4562b3603a34SBarry Smith 
4563b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4564b3603a34SBarry Smith 
4565b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4566b3603a34SBarry Smith @*/
45670910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4568b3603a34SBarry Smith {
4569b3603a34SBarry Smith   PetscErrorCode    ierr;
45700b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4571b3603a34SBarry Smith   const PetscScalar *yy;
457258ff32f7SBarry Smith   PetscInt          dim;
4573b3603a34SBarry Smith 
4574b3603a34SBarry Smith   PetscFunctionBegin;
457558ff32f7SBarry Smith   if (!step) {
4576a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4577a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4578a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
45790910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
45800b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
45810b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
458258ff32f7SBarry Smith   }
45830910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4584e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4585e3efe391SJed Brown   {
4586e3efe391SJed Brown     PetscReal *yreal;
4587e3efe391SJed Brown     PetscInt  i,n;
45880910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4589e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4590e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
45910b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4592e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4593e3efe391SJed Brown   }
4594e3efe391SJed Brown #else
45950b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4596e3efe391SJed Brown #endif
45970910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
45983fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
45990b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
46003923b477SBarry Smith   }
4601b3603a34SBarry Smith   PetscFunctionReturn(0);
4602b3603a34SBarry Smith }
4603b3603a34SBarry Smith 
4604b3603a34SBarry Smith #undef __FUNCT__
46054f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4606ef20d060SBarry Smith /*@C
46074f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4608ef20d060SBarry Smith        in a time based line graph
4609ef20d060SBarry Smith 
4610ef20d060SBarry Smith    Collective on TS
4611ef20d060SBarry Smith 
4612ef20d060SBarry Smith    Input Parameters:
4613ef20d060SBarry Smith +  ts - the TS context
4614ef20d060SBarry Smith .  step - current time-step
4615ef20d060SBarry Smith .  ptime - current time
4616ef20d060SBarry Smith -  lg - a line graph object
4617ef20d060SBarry Smith 
4618ef20d060SBarry Smith    Level: intermediate
4619ef20d060SBarry Smith 
4620abd5a294SJed Brown    Notes:
4621abd5a294SJed Brown    Only for sequential solves.
4622abd5a294SJed Brown 
4623abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4624abd5a294SJed Brown 
4625abd5a294SJed Brown    Options Database Keys:
46264f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4627ef20d060SBarry Smith 
4628ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4629ef20d060SBarry Smith 
4630abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4631ef20d060SBarry Smith @*/
46320910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4633ef20d060SBarry Smith {
4634ef20d060SBarry Smith   PetscErrorCode    ierr;
46350b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4636ef20d060SBarry Smith   const PetscScalar *yy;
4637ef20d060SBarry Smith   Vec               y;
4638a9f9c1f6SBarry Smith   PetscInt          dim;
4639ef20d060SBarry Smith 
4640ef20d060SBarry Smith   PetscFunctionBegin;
4641a9f9c1f6SBarry Smith   if (!step) {
4642a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4643a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
46441ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
46450910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4646a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4647a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4648a9f9c1f6SBarry Smith   }
46490910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4650ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
46510910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4652ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4653e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4654e3efe391SJed Brown   {
4655e3efe391SJed Brown     PetscReal *yreal;
4656e3efe391SJed Brown     PetscInt  i,n;
4657e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4658e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4659e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
46600b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4661e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4662e3efe391SJed Brown   }
4663e3efe391SJed Brown #else
46640b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4665e3efe391SJed Brown #endif
4666ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4667ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
46683fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
46690b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
46703923b477SBarry Smith   }
4671ef20d060SBarry Smith   PetscFunctionReturn(0);
4672ef20d060SBarry Smith }
4673ef20d060SBarry Smith 
4674201da799SBarry Smith #undef __FUNCT__
4675201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4676201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4677201da799SBarry Smith {
4678201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4679201da799SBarry Smith   PetscReal      x   = ptime,y;
4680201da799SBarry Smith   PetscErrorCode ierr;
4681201da799SBarry Smith   PetscInt       its;
4682201da799SBarry Smith 
4683201da799SBarry Smith   PetscFunctionBegin;
4684201da799SBarry Smith   if (!n) {
4685201da799SBarry Smith     PetscDrawAxis axis;
4686bbd56ea5SKarl Rupp 
4687201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4688201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4689201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4690bbd56ea5SKarl Rupp 
4691201da799SBarry Smith     ctx->snes_its = 0;
4692201da799SBarry Smith   }
4693201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4694201da799SBarry Smith   y    = its - ctx->snes_its;
4695201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
46963fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4697201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4698201da799SBarry Smith   }
4699201da799SBarry Smith   ctx->snes_its = its;
4700201da799SBarry Smith   PetscFunctionReturn(0);
4701201da799SBarry Smith }
4702201da799SBarry Smith 
4703201da799SBarry Smith #undef __FUNCT__
4704201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4705201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4706201da799SBarry Smith {
4707201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4708201da799SBarry Smith   PetscReal      x   = ptime,y;
4709201da799SBarry Smith   PetscErrorCode ierr;
4710201da799SBarry Smith   PetscInt       its;
4711201da799SBarry Smith 
4712201da799SBarry Smith   PetscFunctionBegin;
4713201da799SBarry Smith   if (!n) {
4714201da799SBarry Smith     PetscDrawAxis axis;
4715bbd56ea5SKarl Rupp 
4716201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4717201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4718201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4719bbd56ea5SKarl Rupp 
4720201da799SBarry Smith     ctx->ksp_its = 0;
4721201da799SBarry Smith   }
4722201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4723201da799SBarry Smith   y    = its - ctx->ksp_its;
4724201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
472599fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4726201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4727201da799SBarry Smith   }
4728201da799SBarry Smith   ctx->ksp_its = its;
4729201da799SBarry Smith   PetscFunctionReturn(0);
4730201da799SBarry Smith }
4731f9c1d6abSBarry Smith 
4732f9c1d6abSBarry Smith #undef __FUNCT__
4733f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4734f9c1d6abSBarry Smith /*@
4735f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4736f9c1d6abSBarry Smith 
4737f9c1d6abSBarry Smith    Collective on TS and Vec
4738f9c1d6abSBarry Smith 
4739f9c1d6abSBarry Smith    Input Parameters:
4740f9c1d6abSBarry Smith +  ts - the TS context
4741f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4742f9c1d6abSBarry Smith 
4743f9c1d6abSBarry Smith    Output Parameters:
4744f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4745f9c1d6abSBarry Smith 
4746f9c1d6abSBarry Smith    Level: developer
4747f9c1d6abSBarry Smith 
4748f9c1d6abSBarry Smith .keywords: TS, compute
4749f9c1d6abSBarry Smith 
4750f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4751f9c1d6abSBarry Smith @*/
4752f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4753f9c1d6abSBarry Smith {
4754f9c1d6abSBarry Smith   PetscErrorCode ierr;
4755f9c1d6abSBarry Smith 
4756f9c1d6abSBarry Smith   PetscFunctionBegin;
4757f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4758ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4759f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4760f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4761f9c1d6abSBarry Smith }
4762