xref: /petsc/src/ts/interface/ts.c (revision 3fbbecb0fdf35794d246fa5a01e785cb29ea92d8)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
4d763cef2SBarry Smith 
5d5ba7fb7SMatthew Knepley /* Logging support */
67087cfbeSBarry Smith PetscClassId  TS_CLASSID;
7166c7f25SBarry Smith PetscLogEvent  TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
8d405a339SMatthew Knepley 
94a2ae208SSatish Balay #undef __FUNCT__
10bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
11bdad233fSMatthew Knepley /*
12bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
13bdad233fSMatthew Knepley 
14bdad233fSMatthew Knepley   Collective on TS
15bdad233fSMatthew Knepley 
16bdad233fSMatthew Knepley   Input Parameter:
17bdad233fSMatthew Knepley . ts - The ts
18bdad233fSMatthew Knepley 
19bdad233fSMatthew Knepley   Level: intermediate
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
22bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
23bdad233fSMatthew Knepley */
246849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
25bdad233fSMatthew Knepley {
26ace3abfcSBarry Smith   PetscBool      opt;
272fc52814SBarry Smith   const char     *defaultType;
28bdad233fSMatthew Knepley   char           typeName[256];
29dfbe8321SBarry Smith   PetscErrorCode ierr;
30bdad233fSMatthew Knepley 
31bdad233fSMatthew Knepley   PetscFunctionBegin;
327adad957SLisandro Dalcin   if (((PetscObject)ts)->type_name) {
337adad957SLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
34bdad233fSMatthew Knepley   } else {
359596e0b4SJed Brown     defaultType = TSEULER;
36bdad233fSMatthew Knepley   }
37bdad233fSMatthew Knepley 
38cce0b1b2SLisandro Dalcin   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
39bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
40a7cc72afSBarry Smith   if (opt) {
41bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
42bdad233fSMatthew Knepley   } else {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   }
45bdad233fSMatthew Knepley   PetscFunctionReturn(0);
46bdad233fSMatthew Knepley }
47bdad233fSMatthew Knepley 
48bdad233fSMatthew Knepley #undef __FUNCT__
49bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
50bdad233fSMatthew Knepley /*@
51bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
52bdad233fSMatthew Knepley 
53bdad233fSMatthew Knepley    Collective on TS
54bdad233fSMatthew Knepley 
55bdad233fSMatthew Knepley    Input Parameter:
56bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
57bdad233fSMatthew Knepley 
58bdad233fSMatthew Knepley    Options Database Keys:
594d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
60bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
613bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
62bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
63bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
64201da799SBarry Smith .  -ts_monitor_lg_timestep - plot time-step information at each timestep
65201da799SBarry Smith .  -ts_monitor_lg_ksp_iterations - plot the number of linear iterations used at each time-step
66201da799SBarry Smith -  -ts_monitor_lg_snes_iterations - plot the number of nonlinear iterations used at each time-step
67bdad233fSMatthew Knepley 
68bdad233fSMatthew Knepley    Level: beginner
69bdad233fSMatthew Knepley 
70bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
71bdad233fSMatthew Knepley 
72a313700dSBarry Smith .seealso: TSGetType()
73bdad233fSMatthew Knepley @*/
747087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
75bdad233fSMatthew Knepley {
76ace3abfcSBarry Smith   PetscBool      opt,flg;
77dfbe8321SBarry Smith   PetscErrorCode ierr;
78649052a6SBarry Smith   PetscViewer    monviewer;
79eabae89aSBarry Smith   char           monfilename[PETSC_MAX_PATH_LEN];
80089b2837SJed Brown   SNES           snes;
811c3436cfSJed Brown   TSAdapt        adapt;
8231748224SBarry Smith   PetscReal      time_step;
83bdad233fSMatthew Knepley 
84bdad233fSMatthew Knepley   PetscFunctionBegin;
850700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
863194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
87a43b19c4SJed Brown     /* Handle TS type options */
88a43b19c4SJed Brown     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
89bdad233fSMatthew Knepley 
90bdad233fSMatthew Knepley     /* Handle generic TS options */
91bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
923bca7d26SBarry Smith     ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
93d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr);
9431748224SBarry Smith     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
9531748224SBarry Smith     if (flg) {
9631748224SBarry Smith       ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
9731748224SBarry Smith     }
98a43b19c4SJed Brown     opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time;
99a43b19c4SJed Brown     ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr);
100461e00b3SSean Farley     if (flg) {ierr = TSSetExactFinalTime(ts,opt);CHKERRQ(ierr);}
101cef5090cSJed Brown     ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,PETSC_NULL);CHKERRQ(ierr);
102cef5090cSJed Brown     ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr);
103cef5090cSJed Brown     ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
1041c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr);
1051c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr);
106bdad233fSMatthew Knepley 
107bdad233fSMatthew Knepley     /* Monitor options */
108a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
109eabae89aSBarry Smith     if (flg) {
110649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
111649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
112bdad233fSMatthew Knepley     }
1135180491cSLisandro Dalcin     ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1145180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1155180491cSLisandro Dalcin 
1164f09c107SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
117a7cc72afSBarry Smith     if (opt) {
1180b039ecaSBarry Smith       TSMonitorLGCtx ctx;
1193923b477SBarry Smith       PetscInt       howoften = 1;
120a80ad3e0SBarry Smith 
1214f09c107SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
1221d1e9da1SBarry Smith       ierr = TSMonitorLGCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1234f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
124b3603a34SBarry Smith     }
1254f09c107SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
126b3603a34SBarry Smith     if (opt) {
1270b039ecaSBarry Smith       TSMonitorLGCtx ctx;
1283923b477SBarry Smith       PetscInt       howoften = 1;
129b3603a34SBarry Smith 
1304f09c107SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
1313923b477SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
1324f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
133bdad233fSMatthew Knepley     }
1344f09c107SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
135ef20d060SBarry Smith     if (opt) {
1360b039ecaSBarry Smith       TSMonitorLGCtx ctx;
1373923b477SBarry Smith       PetscInt       howoften = 1;
138ef20d060SBarry Smith 
1394f09c107SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
1403923b477SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
1414f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
142ef20d060SBarry Smith     }
143201da799SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
144201da799SBarry Smith     if (opt) {
145201da799SBarry Smith       TSMonitorLGCtx ctx;
146201da799SBarry Smith       PetscInt       howoften = 1;
147201da799SBarry Smith 
148201da799SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
149201da799SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
150201da799SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
151201da799SBarry Smith     }
152201da799SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
153201da799SBarry Smith     if (opt) {
154201da799SBarry Smith       TSMonitorLGCtx ctx;
155201da799SBarry Smith       PetscInt       howoften = 1;
156201da799SBarry Smith 
157201da799SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
158201da799SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
159201da799SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
160201da799SBarry Smith     }
161ef20d060SBarry Smith     opt  = PETSC_FALSE;
1620dcf80beSBarry Smith     ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
163a7cc72afSBarry Smith     if (opt) {
16483a4ac43SBarry Smith       TSMonitorDrawCtx ctx;
16583a4ac43SBarry Smith       PetscInt         howoften = 1;
166a80ad3e0SBarry Smith 
16783a4ac43SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
16883a4ac43SBarry Smith       ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
16983a4ac43SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
170bdad233fSMatthew Knepley     }
171fb1732b5SBarry Smith     opt  = PETSC_FALSE;
1720dcf80beSBarry Smith     ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
1733a471f94SBarry Smith     if (opt) {
17483a4ac43SBarry Smith       TSMonitorDrawCtx ctx;
17583a4ac43SBarry Smith       PetscInt         howoften = 1;
1763a471f94SBarry Smith 
17783a4ac43SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
17883a4ac43SBarry Smith       ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
17983a4ac43SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
1803a471f94SBarry Smith     }
1813a471f94SBarry Smith     opt  = PETSC_FALSE;
182fb1732b5SBarry Smith     ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
183fb1732b5SBarry Smith     if (flg) {
184fb1732b5SBarry Smith       PetscViewer ctx;
185fb1732b5SBarry Smith       if (monfilename[0]) {
186fb1732b5SBarry Smith         ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
187fb1732b5SBarry Smith       } else {
188fb1732b5SBarry Smith         ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
189fb1732b5SBarry Smith       }
190fb1732b5SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
191fb1732b5SBarry Smith     }
192ed81e22dSJed Brown     opt  = PETSC_FALSE;
193ed81e22dSJed Brown     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);
194ed81e22dSJed Brown     if (flg) {
195ed81e22dSJed Brown       const char *ptr,*ptr2;
196ed81e22dSJed Brown       char *filetemplate;
197ed81e22dSJed Brown       if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
198ed81e22dSJed Brown       /* Do some cursory validation of the input. */
199ed81e22dSJed Brown       ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
200ed81e22dSJed Brown       if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
201ed81e22dSJed Brown       for (ptr++ ; ptr && *ptr; ptr++) {
202ed81e22dSJed Brown         ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
203ed81e22dSJed Brown         if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
204ed81e22dSJed Brown         if (ptr2) break;
205ed81e22dSJed Brown       }
206ed81e22dSJed Brown       ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
207ed81e22dSJed Brown       ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
208ed81e22dSJed Brown     }
209bdad233fSMatthew Knepley 
2101c3436cfSJed Brown     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
2111c3436cfSJed Brown     ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2121c3436cfSJed Brown 
213d52bd9f3SBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
214d52bd9f3SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
215d52bd9f3SBarry Smith 
216bdad233fSMatthew Knepley     /* Handle specific TS options */
217abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
218bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
219bdad233fSMatthew Knepley     }
2205d973c19SBarry Smith 
2215d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
2225d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
223bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
224bdad233fSMatthew Knepley   PetscFunctionReturn(0);
225bdad233fSMatthew Knepley }
226bdad233fSMatthew Knepley 
227bdad233fSMatthew Knepley #undef __FUNCT__
228cdcf91faSSean Farley #undef __FUNCT__
2294a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
230a7a1495cSBarry Smith /*@
2318c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
232a7a1495cSBarry Smith       set with TSSetRHSJacobian().
233a7a1495cSBarry Smith 
234a7a1495cSBarry Smith    Collective on TS and Vec
235a7a1495cSBarry Smith 
236a7a1495cSBarry Smith    Input Parameters:
237316643e7SJed Brown +  ts - the TS context
238a7a1495cSBarry Smith .  t - current timestep
239a7a1495cSBarry Smith -  x - input vector
240a7a1495cSBarry Smith 
241a7a1495cSBarry Smith    Output Parameters:
242a7a1495cSBarry Smith +  A - Jacobian matrix
243a7a1495cSBarry Smith .  B - optional preconditioning matrix
244a7a1495cSBarry Smith -  flag - flag indicating matrix structure
245a7a1495cSBarry Smith 
246a7a1495cSBarry Smith    Notes:
247a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
248a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
249a7a1495cSBarry Smith 
25094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
251a7a1495cSBarry Smith    flag parameter.
252a7a1495cSBarry Smith 
253a7a1495cSBarry Smith    Level: developer
254a7a1495cSBarry Smith 
255a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
256a7a1495cSBarry Smith 
25794b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
258a7a1495cSBarry Smith @*/
2597087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
260a7a1495cSBarry Smith {
261dfbe8321SBarry Smith   PetscErrorCode ierr;
2620e4ef248SJed Brown   PetscInt       Xstate;
26324989b8cSPeter Brune   DM             dm;
26424989b8cSPeter Brune   TSDM           tsdm;
26524989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
26624989b8cSPeter Brune   void           *ctx;
26724989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
268a7a1495cSBarry Smith 
269a7a1495cSBarry Smith   PetscFunctionBegin;
2700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2710700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
272c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
27324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
27424989b8cSPeter Brune   ierr = DMTSGetContext(dm,&tsdm);CHKERRQ(ierr);
27524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
27624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
2770e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
2780e4ef248SJed Brown   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) {
2790e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
2800e4ef248SJed Brown     PetscFunctionReturn(0);
281f8ede8e7SJed Brown   }
282d90be118SSean Farley 
28324989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
284d90be118SSean Farley 
28524989b8cSPeter Brune   if (rhsjacobianfunc) {
286d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
287a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
288a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
28924989b8cSPeter Brune     ierr = (*rhsjacobianfunc)(ts,t,X,A,B,flg,ctx);CHKERRQ(ierr);
290a7a1495cSBarry Smith     PetscStackPop;
291d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
292a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
2930700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
2940700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
295ef66eb69SBarry Smith   } else {
296214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
297214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
298214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
299ef66eb69SBarry Smith   }
3000e4ef248SJed Brown   ts->rhsjacobian.time = t;
3010e4ef248SJed Brown   ts->rhsjacobian.X = X;
3020e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3030e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
304a7a1495cSBarry Smith   PetscFunctionReturn(0);
305a7a1495cSBarry Smith }
306a7a1495cSBarry Smith 
3074a2ae208SSatish Balay #undef __FUNCT__
3084a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
309316643e7SJed Brown /*@
310d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
311d763cef2SBarry Smith 
312316643e7SJed Brown    Collective on TS and Vec
313316643e7SJed Brown 
314316643e7SJed Brown    Input Parameters:
315316643e7SJed Brown +  ts - the TS context
316316643e7SJed Brown .  t - current time
317316643e7SJed Brown -  x - state vector
318316643e7SJed Brown 
319316643e7SJed Brown    Output Parameter:
320316643e7SJed Brown .  y - right hand side
321316643e7SJed Brown 
322316643e7SJed Brown    Note:
323316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
324316643e7SJed Brown    is used internally within the nonlinear solvers.
325316643e7SJed Brown 
326316643e7SJed Brown    Level: developer
327316643e7SJed Brown 
328316643e7SJed Brown .keywords: TS, compute
329316643e7SJed Brown 
330316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
331316643e7SJed Brown @*/
332dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
333d763cef2SBarry Smith {
334dfbe8321SBarry Smith   PetscErrorCode ierr;
335d763cef2SBarry Smith 
33624989b8cSPeter Brune   TSRHSFunction rhsfunction;
33724989b8cSPeter Brune   TSIFunction   ifunction;
33824989b8cSPeter Brune   void          *ctx;
33924989b8cSPeter Brune   DM            dm;
34024989b8cSPeter Brune 
341d763cef2SBarry Smith   PetscFunctionBegin;
3420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3430700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3440700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
34524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
34624989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
34724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
348d763cef2SBarry Smith 
34924989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
350d763cef2SBarry Smith 
351d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
35224989b8cSPeter Brune   if (rhsfunction) {
353d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
35424989b8cSPeter Brune     ierr = (*rhsfunction)(ts,t,x,y,ctx);CHKERRQ(ierr);
355d763cef2SBarry Smith     PetscStackPop;
356214bc6a2SJed Brown   } else {
357214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
358b2cd27e8SJed Brown   }
35944a41b28SSean Farley 
360d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
361d763cef2SBarry Smith   PetscFunctionReturn(0);
362d763cef2SBarry Smith }
363d763cef2SBarry Smith 
3644a2ae208SSatish Balay #undef __FUNCT__
365ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
366ef20d060SBarry Smith /*@
367ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
368ef20d060SBarry Smith 
369ef20d060SBarry Smith    Collective on TS and Vec
370ef20d060SBarry Smith 
371ef20d060SBarry Smith    Input Parameters:
372ef20d060SBarry Smith +  ts - the TS context
373ef20d060SBarry Smith -  t - current time
374ef20d060SBarry Smith 
375ef20d060SBarry Smith    Output Parameter:
376ef20d060SBarry Smith .  y - right hand side
377ef20d060SBarry Smith 
378ef20d060SBarry Smith    Note:
379ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
380ef20d060SBarry Smith    is used internally within the nonlinear solvers.
381ef20d060SBarry Smith 
382ef20d060SBarry Smith    Level: developer
383ef20d060SBarry Smith 
384ef20d060SBarry Smith .keywords: TS, compute
385ef20d060SBarry Smith 
386abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
387ef20d060SBarry Smith @*/
388ef20d060SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec x)
389ef20d060SBarry Smith {
390ef20d060SBarry Smith   PetscErrorCode     ierr;
391ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
392ef20d060SBarry Smith   void               *ctx;
393ef20d060SBarry Smith   DM                 dm;
394ef20d060SBarry Smith 
395ef20d060SBarry Smith   PetscFunctionBegin;
396ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
397ef20d060SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
398ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
399ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
400ef20d060SBarry Smith 
401ef20d060SBarry Smith   if (solutionfunction) {
402ef20d060SBarry Smith     PetscStackPush("TS user right-hand-side function");
403ef20d060SBarry Smith     ierr = (*solutionfunction)(ts,t,x,ctx);CHKERRQ(ierr);
404ef20d060SBarry Smith     PetscStackPop;
405ef20d060SBarry Smith   }
406ef20d060SBarry Smith   PetscFunctionReturn(0);
407ef20d060SBarry Smith }
408ef20d060SBarry Smith 
409ef20d060SBarry Smith #undef __FUNCT__
410214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
411214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
412214bc6a2SJed Brown {
4132dd45cf8SJed Brown   Vec            F;
414214bc6a2SJed Brown   PetscErrorCode ierr;
415214bc6a2SJed Brown 
416214bc6a2SJed Brown   PetscFunctionBegin;
4170da9a4f0SJed Brown   *Frhs = PETSC_NULL;
4182dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
419214bc6a2SJed Brown   if (!ts->Frhs) {
4202dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
421214bc6a2SJed Brown   }
422214bc6a2SJed Brown   *Frhs = ts->Frhs;
423214bc6a2SJed Brown   PetscFunctionReturn(0);
424214bc6a2SJed Brown }
425214bc6a2SJed Brown 
426214bc6a2SJed Brown #undef __FUNCT__
427214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
428214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
429214bc6a2SJed Brown {
430214bc6a2SJed Brown   Mat            A,B;
4312dd45cf8SJed Brown   PetscErrorCode ierr;
432214bc6a2SJed Brown 
433214bc6a2SJed Brown   PetscFunctionBegin;
434214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
435214bc6a2SJed Brown   if (Arhs) {
436214bc6a2SJed Brown     if (!ts->Arhs) {
437214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
438214bc6a2SJed Brown     }
439214bc6a2SJed Brown     *Arhs = ts->Arhs;
440214bc6a2SJed Brown   }
441214bc6a2SJed Brown   if (Brhs) {
442214bc6a2SJed Brown     if (!ts->Brhs) {
443214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
444214bc6a2SJed Brown     }
445214bc6a2SJed Brown     *Brhs = ts->Brhs;
446214bc6a2SJed Brown   }
447214bc6a2SJed Brown   PetscFunctionReturn(0);
448214bc6a2SJed Brown }
449214bc6a2SJed Brown 
450214bc6a2SJed Brown #undef __FUNCT__
451316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
452316643e7SJed Brown /*@
453316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
454316643e7SJed Brown 
455316643e7SJed Brown    Collective on TS and Vec
456316643e7SJed Brown 
457316643e7SJed Brown    Input Parameters:
458316643e7SJed Brown +  ts - the TS context
459316643e7SJed Brown .  t - current time
460316643e7SJed Brown .  X - state vector
461214bc6a2SJed Brown .  Xdot - time derivative of state vector
462214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
463316643e7SJed Brown 
464316643e7SJed Brown    Output Parameter:
465316643e7SJed Brown .  Y - right hand side
466316643e7SJed Brown 
467316643e7SJed Brown    Note:
468316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
469316643e7SJed Brown    is used internally within the nonlinear solvers.
470316643e7SJed Brown 
471316643e7SJed Brown    If the user did did not write their equations in implicit form, this
472316643e7SJed Brown    function recasts them in implicit form.
473316643e7SJed Brown 
474316643e7SJed Brown    Level: developer
475316643e7SJed Brown 
476316643e7SJed Brown .keywords: TS, compute
477316643e7SJed Brown 
478316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
479316643e7SJed Brown @*/
480214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex)
481316643e7SJed Brown {
482316643e7SJed Brown   PetscErrorCode ierr;
48324989b8cSPeter Brune   TSIFunction    ifunction;
48424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
48524989b8cSPeter Brune   void           *ctx;
48624989b8cSPeter Brune   DM             dm;
487316643e7SJed Brown 
488316643e7SJed Brown   PetscFunctionBegin;
4890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4900700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4910700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
4920700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
493316643e7SJed Brown 
49424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
49524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
49624989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
49724989b8cSPeter Brune 
49824989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
499d90be118SSean Farley 
500316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
50124989b8cSPeter Brune   if (ifunction) {
502316643e7SJed Brown     PetscStackPush("TS user implicit function");
50324989b8cSPeter Brune     ierr = (*ifunction)(ts,t,X,Xdot,Y,ctx);CHKERRQ(ierr);
504316643e7SJed Brown     PetscStackPop;
505214bc6a2SJed Brown   }
506214bc6a2SJed Brown   if (imex) {
50724989b8cSPeter Brune     if (!ifunction) {
5082dd45cf8SJed Brown       ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);
5092dd45cf8SJed Brown     }
51024989b8cSPeter Brune   } else if (rhsfunction) {
51124989b8cSPeter Brune     if (ifunction) {
512214bc6a2SJed Brown       Vec Frhs;
513214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
514214bc6a2SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr);
515214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
5162dd45cf8SJed Brown     } else {
5172dd45cf8SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
5182dd45cf8SJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
519316643e7SJed Brown     }
5204a6899ffSJed Brown   }
521316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
522316643e7SJed Brown   PetscFunctionReturn(0);
523316643e7SJed Brown }
524316643e7SJed Brown 
525316643e7SJed Brown #undef __FUNCT__
526316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
527316643e7SJed Brown /*@
528316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
529316643e7SJed Brown 
530316643e7SJed Brown    Collective on TS and Vec
531316643e7SJed Brown 
532316643e7SJed Brown    Input
533316643e7SJed Brown       Input Parameters:
534316643e7SJed Brown +  ts - the TS context
535316643e7SJed Brown .  t - current timestep
536316643e7SJed Brown .  X - state vector
537316643e7SJed Brown .  Xdot - time derivative of state vector
538214bc6a2SJed Brown .  shift - shift to apply, see note below
539214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
540316643e7SJed Brown 
541316643e7SJed Brown    Output Parameters:
542316643e7SJed Brown +  A - Jacobian matrix
543316643e7SJed Brown .  B - optional preconditioning matrix
544316643e7SJed Brown -  flag - flag indicating matrix structure
545316643e7SJed Brown 
546316643e7SJed Brown    Notes:
547316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
548316643e7SJed Brown 
549316643e7SJed Brown    dF/dX + shift*dF/dXdot
550316643e7SJed Brown 
551316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
552316643e7SJed Brown    is used internally within the nonlinear solvers.
553316643e7SJed Brown 
554316643e7SJed Brown    Level: developer
555316643e7SJed Brown 
556316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
557316643e7SJed Brown 
558316643e7SJed Brown .seealso:  TSSetIJacobian()
55963495f91SJed Brown @*/
560214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
561316643e7SJed Brown {
5620026cea9SSean Farley   PetscInt Xstate, Xdotstate;
563316643e7SJed Brown   PetscErrorCode ierr;
56424989b8cSPeter Brune   TSIJacobian    ijacobian;
56524989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
56624989b8cSPeter Brune   DM             dm;
56724989b8cSPeter Brune   void           *ctx;
568316643e7SJed Brown 
569316643e7SJed Brown   PetscFunctionBegin;
57024989b8cSPeter Brune 
5710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5720700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
5730700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
574316643e7SJed Brown   PetscValidPointer(A,6);
5750700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
576316643e7SJed Brown   PetscValidPointer(B,7);
5770700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
578316643e7SJed Brown   PetscValidPointer(flg,8);
57924989b8cSPeter Brune 
58024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
58124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
58224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
58324989b8cSPeter Brune 
5840026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
5850026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr);
5860026cea9SSean Farley   if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == X && ts->ijacobian.Xstate == Xstate && ts->ijacobian.Xdot == Xdot && ts->ijacobian.Xdotstate == Xdotstate && ts->ijacobian.imex == imex))) {
5870026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
5880026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5890026cea9SSean Farley     PetscFunctionReturn(0);
5900026cea9SSean Farley   }
591316643e7SJed Brown 
59224989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
593316643e7SJed Brown 
5944e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
595316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
59624989b8cSPeter Brune   if (ijacobian) {
5972dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
598316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
59924989b8cSPeter Brune     ierr = (*ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ctx);CHKERRQ(ierr);
600316643e7SJed Brown     PetscStackPop;
601214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
602214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
603214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
6044a6899ffSJed Brown   }
605214bc6a2SJed Brown   if (imex) {
60624989b8cSPeter Brune     if (!ijacobian) {  /* system was written as Xdot = F(t,X) */
607214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
6082dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
609214bc6a2SJed Brown       if (*A != *B) {
610214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
611214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
612214bc6a2SJed Brown       }
613214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
614214bc6a2SJed Brown     }
615214bc6a2SJed Brown   } else {
61624989b8cSPeter Brune     if (!ijacobian) {
617214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
618214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
619214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
620316643e7SJed Brown       if (*A != *B) {
621316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
622316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
623316643e7SJed Brown       }
62424989b8cSPeter Brune     } else if (rhsjacobian) {
625214bc6a2SJed Brown       Mat Arhs,Brhs;
626214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
627214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
628214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
629214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
630214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
631214bc6a2SJed Brown       if (*A != *B) {
632214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
633214bc6a2SJed Brown       }
634214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
635214bc6a2SJed Brown     }
636316643e7SJed Brown   }
6370026cea9SSean Farley 
6380026cea9SSean Farley   ts->ijacobian.time = t;
6390026cea9SSean Farley   ts->ijacobian.X = X;
6400026cea9SSean Farley   ts->ijacobian.Xdot = Xdot;
6410026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr);
6420026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
6430026cea9SSean Farley   ts->ijacobian.shift = shift;
6440026cea9SSean Farley   ts->ijacobian.imex = imex;
6450026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
646316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
647316643e7SJed Brown   PetscFunctionReturn(0);
648316643e7SJed Brown }
649316643e7SJed Brown 
650316643e7SJed Brown #undef __FUNCT__
6514a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
652d763cef2SBarry Smith /*@C
653d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
654d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
655d763cef2SBarry Smith 
6563f9fe445SBarry Smith     Logically Collective on TS
657d763cef2SBarry Smith 
658d763cef2SBarry Smith     Input Parameters:
659d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
660ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
661d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
662d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
663d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
664d763cef2SBarry Smith 
665d763cef2SBarry Smith     Calling sequence of func:
66687828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
667d763cef2SBarry Smith 
668d763cef2SBarry Smith +   t - current timestep
669d763cef2SBarry Smith .   u - input vector
670d763cef2SBarry Smith .   F - function vector
671d763cef2SBarry Smith -   ctx - [optional] user-defined function context
672d763cef2SBarry Smith 
673d763cef2SBarry Smith     Level: beginner
674d763cef2SBarry Smith 
675d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
676d763cef2SBarry Smith 
677d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
678d763cef2SBarry Smith @*/
679089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
680d763cef2SBarry Smith {
681089b2837SJed Brown   PetscErrorCode ierr;
682089b2837SJed Brown   SNES           snes;
683e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
68424989b8cSPeter Brune   DM             dm;
685d763cef2SBarry Smith 
686089b2837SJed Brown   PetscFunctionBegin;
6870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
688ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
68924989b8cSPeter Brune 
69024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
69124989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
692089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
693e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
694e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
695e856ceecSJed Brown     r = ralloc;
696e856ceecSJed Brown   }
697089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
698e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
699d763cef2SBarry Smith   PetscFunctionReturn(0);
700d763cef2SBarry Smith }
701d763cef2SBarry Smith 
7024a2ae208SSatish Balay #undef __FUNCT__
703ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
704ef20d060SBarry Smith /*@C
705abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
706ef20d060SBarry Smith 
707ef20d060SBarry Smith     Logically Collective on TS
708ef20d060SBarry Smith 
709ef20d060SBarry Smith     Input Parameters:
710ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
711ef20d060SBarry Smith .   f - routine for evaluating the solution
712ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
713ef20d060SBarry Smith           function evaluation routine (may be PETSC_NULL)
714ef20d060SBarry Smith 
715ef20d060SBarry Smith     Calling sequence of func:
716ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
717ef20d060SBarry Smith 
718ef20d060SBarry Smith +   t - current timestep
719ef20d060SBarry Smith .   u - output vector
720ef20d060SBarry Smith -   ctx - [optional] user-defined function context
721ef20d060SBarry Smith 
722abd5a294SJed Brown     Notes:
723abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
724abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
725abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
726abd5a294SJed Brown 
7274f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
728abd5a294SJed Brown 
729ef20d060SBarry Smith     Level: beginner
730ef20d060SBarry Smith 
731ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
732ef20d060SBarry Smith 
733abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction()
734ef20d060SBarry Smith @*/
735ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
736ef20d060SBarry Smith {
737ef20d060SBarry Smith   PetscErrorCode ierr;
738ef20d060SBarry Smith   DM             dm;
739ef20d060SBarry Smith 
740ef20d060SBarry Smith   PetscFunctionBegin;
741ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
742ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
743ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
744ef20d060SBarry Smith   PetscFunctionReturn(0);
745ef20d060SBarry Smith }
746ef20d060SBarry Smith 
747ef20d060SBarry Smith #undef __FUNCT__
7484a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
749d763cef2SBarry Smith /*@C
750d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
751d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
752d763cef2SBarry Smith 
7533f9fe445SBarry Smith    Logically Collective on TS
754d763cef2SBarry Smith 
755d763cef2SBarry Smith    Input Parameters:
756d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
757d763cef2SBarry Smith .  A   - Jacobian matrix
758d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
759d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
760d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
761d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
762d763cef2SBarry Smith 
763d763cef2SBarry Smith    Calling sequence of func:
76487828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
765d763cef2SBarry Smith 
766d763cef2SBarry Smith +  t - current timestep
767d763cef2SBarry Smith .  u - input vector
768d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
769d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
770d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
77194b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
772d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
773d763cef2SBarry Smith 
774d763cef2SBarry Smith    Notes:
77594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
776d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
777d763cef2SBarry Smith 
778d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
779d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
78056335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
781d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
782d763cef2SBarry Smith    throughout the global iterations.
783d763cef2SBarry Smith 
784d763cef2SBarry Smith    Level: beginner
785d763cef2SBarry Smith 
786d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
787d763cef2SBarry Smith 
788ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
789d763cef2SBarry Smith 
790d763cef2SBarry Smith @*/
791089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
792d763cef2SBarry Smith {
793277b19d0SLisandro Dalcin   PetscErrorCode ierr;
794089b2837SJed Brown   SNES           snes;
79524989b8cSPeter Brune   DM             dm;
79624989b8cSPeter Brune   TSIJacobian    ijacobian;
797277b19d0SLisandro Dalcin 
798d763cef2SBarry Smith   PetscFunctionBegin;
7990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
800277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
801277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
802277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
803277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
804d763cef2SBarry Smith 
80524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
80624989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
80724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
80824989b8cSPeter Brune 
809089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
8105f659677SPeter Brune   if (!ijacobian) {
811089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
8120e4ef248SJed Brown   }
8130e4ef248SJed Brown   if (A) {
8140e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8150e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
8160e4ef248SJed Brown     ts->Arhs = A;
8170e4ef248SJed Brown   }
8180e4ef248SJed Brown   if (B) {
8190e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8200e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
8210e4ef248SJed Brown     ts->Brhs = B;
8220e4ef248SJed Brown   }
823d763cef2SBarry Smith   PetscFunctionReturn(0);
824d763cef2SBarry Smith }
825d763cef2SBarry Smith 
826316643e7SJed Brown 
827316643e7SJed Brown #undef __FUNCT__
828316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
829316643e7SJed Brown /*@C
830316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
831316643e7SJed Brown 
8323f9fe445SBarry Smith    Logically Collective on TS
833316643e7SJed Brown 
834316643e7SJed Brown    Input Parameters:
835316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
836ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
837316643e7SJed Brown .  f   - the function evaluation routine
838316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
839316643e7SJed Brown 
840316643e7SJed Brown    Calling sequence of f:
841316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
842316643e7SJed Brown 
843316643e7SJed Brown +  t   - time at step/stage being solved
844316643e7SJed Brown .  u   - state vector
845316643e7SJed Brown .  u_t - time derivative of state vector
846316643e7SJed Brown .  F   - function vector
847316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
848316643e7SJed Brown 
849316643e7SJed Brown    Important:
850d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
851316643e7SJed Brown 
852316643e7SJed Brown    Level: beginner
853316643e7SJed Brown 
854316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
855316643e7SJed Brown 
856d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
857316643e7SJed Brown @*/
858089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
859316643e7SJed Brown {
860089b2837SJed Brown   PetscErrorCode ierr;
861089b2837SJed Brown   SNES           snes;
862e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
86324989b8cSPeter Brune   DM             dm;
864316643e7SJed Brown 
865316643e7SJed Brown   PetscFunctionBegin;
8660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
867ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
86824989b8cSPeter Brune 
86924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
87024989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
87124989b8cSPeter Brune 
872089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
873e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
874e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
875e856ceecSJed Brown     res = resalloc;
876e856ceecSJed Brown   }
877089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
878e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
87924989b8cSPeter Brune 
880089b2837SJed Brown   PetscFunctionReturn(0);
881089b2837SJed Brown }
882089b2837SJed Brown 
883089b2837SJed Brown #undef __FUNCT__
884089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
885089b2837SJed Brown /*@C
886089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
887089b2837SJed Brown 
888089b2837SJed Brown    Not Collective
889089b2837SJed Brown 
890089b2837SJed Brown    Input Parameter:
891089b2837SJed Brown .  ts - the TS context
892089b2837SJed Brown 
893089b2837SJed Brown    Output Parameter:
894089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
895089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
896089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
897089b2837SJed Brown 
898089b2837SJed Brown    Level: advanced
899089b2837SJed Brown 
900089b2837SJed Brown .keywords: TS, nonlinear, get, function
901089b2837SJed Brown 
902089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
903089b2837SJed Brown @*/
904089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
905089b2837SJed Brown {
906089b2837SJed Brown   PetscErrorCode ierr;
907089b2837SJed Brown   SNES snes;
90824989b8cSPeter Brune   DM   dm;
909089b2837SJed Brown 
910089b2837SJed Brown   PetscFunctionBegin;
911089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
912089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
913089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
91424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
91524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
916089b2837SJed Brown   PetscFunctionReturn(0);
917089b2837SJed Brown }
918089b2837SJed Brown 
919089b2837SJed Brown #undef __FUNCT__
920089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
921089b2837SJed Brown /*@C
922089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
923089b2837SJed Brown 
924089b2837SJed Brown    Not Collective
925089b2837SJed Brown 
926089b2837SJed Brown    Input Parameter:
927089b2837SJed Brown .  ts - the TS context
928089b2837SJed Brown 
929089b2837SJed Brown    Output Parameter:
930089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
931089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
932089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
933089b2837SJed Brown 
934089b2837SJed Brown    Level: advanced
935089b2837SJed Brown 
936089b2837SJed Brown .keywords: TS, nonlinear, get, function
937089b2837SJed Brown 
938089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
939089b2837SJed Brown @*/
940089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
941089b2837SJed Brown {
942089b2837SJed Brown   PetscErrorCode ierr;
943089b2837SJed Brown   SNES snes;
94424989b8cSPeter Brune   DM   dm;
945089b2837SJed Brown 
946089b2837SJed Brown   PetscFunctionBegin;
947089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
948089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
949089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
95024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
95124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
952316643e7SJed Brown   PetscFunctionReturn(0);
953316643e7SJed Brown }
954316643e7SJed Brown 
955316643e7SJed Brown #undef __FUNCT__
956316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
957316643e7SJed Brown /*@C
958a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
959a4f0a591SBarry Smith         you provided with TSSetIFunction().
960316643e7SJed Brown 
9613f9fe445SBarry Smith    Logically Collective on TS
962316643e7SJed Brown 
963316643e7SJed Brown    Input Parameters:
964316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
965316643e7SJed Brown .  A   - Jacobian matrix
966316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
967316643e7SJed Brown .  f   - the Jacobian evaluation routine
968316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
969316643e7SJed Brown 
970316643e7SJed Brown    Calling sequence of f:
9711b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
972316643e7SJed Brown 
973316643e7SJed Brown +  t    - time at step/stage being solved
9741b4a444bSJed Brown .  U    - state vector
9751b4a444bSJed Brown .  U_t  - time derivative of state vector
976316643e7SJed Brown .  a    - shift
9771b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
978316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
979316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
980316643e7SJed Brown           structure (same as flag in KSPSetOperators())
981316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
982316643e7SJed Brown 
983316643e7SJed Brown    Notes:
984316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
985316643e7SJed Brown 
986a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
987a4f0a591SBarry Smith    the Jacobian of G(U) = F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
988a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
989a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
990a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
991a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
992a4f0a591SBarry Smith 
993316643e7SJed Brown    Level: beginner
994316643e7SJed Brown 
995316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
996316643e7SJed Brown 
997ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
998316643e7SJed Brown 
999316643e7SJed Brown @*/
10007087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1001316643e7SJed Brown {
1002316643e7SJed Brown   PetscErrorCode ierr;
1003089b2837SJed Brown   SNES           snes;
100424989b8cSPeter Brune   DM             dm;
1005316643e7SJed Brown 
1006316643e7SJed Brown   PetscFunctionBegin;
10070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10080700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
10090700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1010316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1011316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
101224989b8cSPeter Brune 
101324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
101424989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
101524989b8cSPeter Brune 
1016089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1017089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1018316643e7SJed Brown   PetscFunctionReturn(0);
1019316643e7SJed Brown }
1020316643e7SJed Brown 
10214a2ae208SSatish Balay #undef __FUNCT__
10224a2ae208SSatish Balay #define __FUNCT__ "TSView"
10237e2c5f70SBarry Smith /*@C
1024d763cef2SBarry Smith     TSView - Prints the TS data structure.
1025d763cef2SBarry Smith 
10264c49b128SBarry Smith     Collective on TS
1027d763cef2SBarry Smith 
1028d763cef2SBarry Smith     Input Parameters:
1029d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1030d763cef2SBarry Smith -   viewer - visualization context
1031d763cef2SBarry Smith 
1032d763cef2SBarry Smith     Options Database Key:
1033d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1034d763cef2SBarry Smith 
1035d763cef2SBarry Smith     Notes:
1036d763cef2SBarry Smith     The available visualization contexts include
1037b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1038b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1039d763cef2SBarry Smith          output where only the first processor opens
1040d763cef2SBarry Smith          the file.  All other processors send their
1041d763cef2SBarry Smith          data to the first processor to print.
1042d763cef2SBarry Smith 
1043d763cef2SBarry Smith     The user can open an alternative visualization context with
1044b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1045d763cef2SBarry Smith 
1046d763cef2SBarry Smith     Level: beginner
1047d763cef2SBarry Smith 
1048d763cef2SBarry Smith .keywords: TS, timestep, view
1049d763cef2SBarry Smith 
1050b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1051d763cef2SBarry Smith @*/
10527087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1053d763cef2SBarry Smith {
1054dfbe8321SBarry Smith   PetscErrorCode ierr;
105519fd82e9SBarry Smith   TSType         type;
1056089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
1057d763cef2SBarry Smith 
1058d763cef2SBarry Smith   PetscFunctionBegin;
10590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10603050cee2SBarry Smith   if (!viewer) {
10617adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
10623050cee2SBarry Smith   }
10630700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1064c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1065fd16b177SBarry Smith 
1066251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1067251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
106832077d6dSBarry Smith   if (iascii) {
1069317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
107077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1071a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1072d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
10735ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1074c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1075d763cef2SBarry Smith     }
10765ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1077193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1078d52bd9f3SBarry Smith     if (ts->ops->view) {
1079d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1080d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1081d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1082d52bd9f3SBarry Smith     }
10830f5bd95cSBarry Smith   } else if (isstring) {
1084a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1085b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
1086d763cef2SBarry Smith   }
1087b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1088251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1089b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1090d763cef2SBarry Smith   PetscFunctionReturn(0);
1091d763cef2SBarry Smith }
1092d763cef2SBarry Smith 
1093d763cef2SBarry Smith 
10944a2ae208SSatish Balay #undef __FUNCT__
10954a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1096b07ff414SBarry Smith /*@
1097d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1098d763cef2SBarry Smith    the timesteppers.
1099d763cef2SBarry Smith 
11003f9fe445SBarry Smith    Logically Collective on TS
1101d763cef2SBarry Smith 
1102d763cef2SBarry Smith    Input Parameters:
1103d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1104d763cef2SBarry Smith -  usrP - optional user context
1105d763cef2SBarry Smith 
1106d763cef2SBarry Smith    Level: intermediate
1107d763cef2SBarry Smith 
1108d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1109d763cef2SBarry Smith 
1110d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1111d763cef2SBarry Smith @*/
11127087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1113d763cef2SBarry Smith {
1114d763cef2SBarry Smith   PetscFunctionBegin;
11150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1116d763cef2SBarry Smith   ts->user = usrP;
1117d763cef2SBarry Smith   PetscFunctionReturn(0);
1118d763cef2SBarry Smith }
1119d763cef2SBarry Smith 
11204a2ae208SSatish Balay #undef __FUNCT__
11214a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1122b07ff414SBarry Smith /*@
1123d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1124d763cef2SBarry Smith     timestepper.
1125d763cef2SBarry Smith 
1126d763cef2SBarry Smith     Not Collective
1127d763cef2SBarry Smith 
1128d763cef2SBarry Smith     Input Parameter:
1129d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1130d763cef2SBarry Smith 
1131d763cef2SBarry Smith     Output Parameter:
1132d763cef2SBarry Smith .   usrP - user context
1133d763cef2SBarry Smith 
1134d763cef2SBarry Smith     Level: intermediate
1135d763cef2SBarry Smith 
1136d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1137d763cef2SBarry Smith 
1138d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1139d763cef2SBarry Smith @*/
1140e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1141d763cef2SBarry Smith {
1142d763cef2SBarry Smith   PetscFunctionBegin;
11430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1144e71120c6SJed Brown   *(void**)usrP = ts->user;
1145d763cef2SBarry Smith   PetscFunctionReturn(0);
1146d763cef2SBarry Smith }
1147d763cef2SBarry Smith 
11484a2ae208SSatish Balay #undef __FUNCT__
11494a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1150d763cef2SBarry Smith /*@
1151b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1152d763cef2SBarry Smith 
1153d763cef2SBarry Smith    Not Collective
1154d763cef2SBarry Smith 
1155d763cef2SBarry Smith    Input Parameter:
1156d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1157d763cef2SBarry Smith 
1158d763cef2SBarry Smith    Output Parameter:
1159b8123daeSJed Brown .  iter - number of steps completed so far
1160d763cef2SBarry Smith 
1161d763cef2SBarry Smith    Level: intermediate
1162d763cef2SBarry Smith 
1163d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1164b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1165d763cef2SBarry Smith @*/
11667087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
1167d763cef2SBarry Smith {
1168d763cef2SBarry Smith   PetscFunctionBegin;
11690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11704482741eSBarry Smith   PetscValidIntPointer(iter,2);
1171d763cef2SBarry Smith   *iter = ts->steps;
1172d763cef2SBarry Smith   PetscFunctionReturn(0);
1173d763cef2SBarry Smith }
1174d763cef2SBarry Smith 
11754a2ae208SSatish Balay #undef __FUNCT__
11764a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1177d763cef2SBarry Smith /*@
1178d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1179d763cef2SBarry Smith    as well as the initial time.
1180d763cef2SBarry Smith 
11813f9fe445SBarry Smith    Logically Collective on TS
1182d763cef2SBarry Smith 
1183d763cef2SBarry Smith    Input Parameters:
1184d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1185d763cef2SBarry Smith .  initial_time - the initial time
1186d763cef2SBarry Smith -  time_step - the size of the timestep
1187d763cef2SBarry Smith 
1188d763cef2SBarry Smith    Level: intermediate
1189d763cef2SBarry Smith 
1190d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1191d763cef2SBarry Smith 
1192d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1193d763cef2SBarry Smith @*/
11947087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1195d763cef2SBarry Smith {
1196e144a568SJed Brown   PetscErrorCode ierr;
1197e144a568SJed Brown 
1198d763cef2SBarry Smith   PetscFunctionBegin;
11990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1200e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1201d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1202d763cef2SBarry Smith   PetscFunctionReturn(0);
1203d763cef2SBarry Smith }
1204d763cef2SBarry Smith 
12054a2ae208SSatish Balay #undef __FUNCT__
12064a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1207d763cef2SBarry Smith /*@
1208d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1209d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1210d763cef2SBarry Smith 
12113f9fe445SBarry Smith    Logically Collective on TS
1212d763cef2SBarry Smith 
1213d763cef2SBarry Smith    Input Parameters:
1214d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1215d763cef2SBarry Smith -  time_step - the size of the timestep
1216d763cef2SBarry Smith 
1217d763cef2SBarry Smith    Level: intermediate
1218d763cef2SBarry Smith 
1219d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1220d763cef2SBarry Smith 
1221d763cef2SBarry Smith .keywords: TS, set, timestep
1222d763cef2SBarry Smith @*/
12237087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1224d763cef2SBarry Smith {
1225d763cef2SBarry Smith   PetscFunctionBegin;
12260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1227c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1228d763cef2SBarry Smith   ts->time_step = time_step;
122931748224SBarry Smith   ts->time_step_orig = time_step;
1230d763cef2SBarry Smith   PetscFunctionReturn(0);
1231d763cef2SBarry Smith }
1232d763cef2SBarry Smith 
12334a2ae208SSatish Balay #undef __FUNCT__
1234a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1235a43b19c4SJed Brown /*@
1236a43b19c4SJed Brown    TSSetExactFinalTime - Determines whether to interpolate solution to the
1237a43b19c4SJed Brown       exact final time requested by the user or just returns it at the final time
1238a43b19c4SJed Brown       it computed.
1239a43b19c4SJed Brown 
1240a43b19c4SJed Brown   Logically Collective on TS
1241a43b19c4SJed Brown 
1242a43b19c4SJed Brown    Input Parameter:
1243a43b19c4SJed Brown +   ts - the time-step context
1244a43b19c4SJed Brown -   ft - PETSC_TRUE if interpolates, else PETSC_FALSE
1245a43b19c4SJed Brown 
1246a43b19c4SJed Brown    Level: beginner
1247a43b19c4SJed Brown 
1248a43b19c4SJed Brown .seealso: TSSetDuration()
1249a43b19c4SJed Brown @*/
1250a43b19c4SJed Brown PetscErrorCode  TSSetExactFinalTime(TS ts,PetscBool flg)
1251a43b19c4SJed Brown {
1252a43b19c4SJed Brown 
1253a43b19c4SJed Brown   PetscFunctionBegin;
1254a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1255d8cd7023SBarry Smith   PetscValidLogicalCollectiveBool(ts,flg,2);
1256a43b19c4SJed Brown   ts->exact_final_time = flg;
1257a43b19c4SJed Brown   PetscFunctionReturn(0);
1258a43b19c4SJed Brown }
1259a43b19c4SJed Brown 
1260a43b19c4SJed Brown #undef __FUNCT__
12614a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1262d763cef2SBarry Smith /*@
1263d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1264d763cef2SBarry Smith 
1265d763cef2SBarry Smith    Not Collective
1266d763cef2SBarry Smith 
1267d763cef2SBarry Smith    Input Parameter:
1268d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1269d763cef2SBarry Smith 
1270d763cef2SBarry Smith    Output Parameter:
1271d763cef2SBarry Smith .  dt - the current timestep size
1272d763cef2SBarry Smith 
1273d763cef2SBarry Smith    Level: intermediate
1274d763cef2SBarry Smith 
1275d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1276d763cef2SBarry Smith 
1277d763cef2SBarry Smith .keywords: TS, get, timestep
1278d763cef2SBarry Smith @*/
12797087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1280d763cef2SBarry Smith {
1281d763cef2SBarry Smith   PetscFunctionBegin;
12820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1283f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1284d763cef2SBarry Smith   *dt = ts->time_step;
1285d763cef2SBarry Smith   PetscFunctionReturn(0);
1286d763cef2SBarry Smith }
1287d763cef2SBarry Smith 
12884a2ae208SSatish Balay #undef __FUNCT__
12894a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1290d8e5e3e6SSatish Balay /*@
1291d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1292d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1293d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1294d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1295d763cef2SBarry Smith 
1296d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1297d763cef2SBarry Smith 
1298d763cef2SBarry Smith    Input Parameter:
1299d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1300d763cef2SBarry Smith 
1301d763cef2SBarry Smith    Output Parameter:
1302d763cef2SBarry Smith .  v - the vector containing the solution
1303d763cef2SBarry Smith 
1304d763cef2SBarry Smith    Level: intermediate
1305d763cef2SBarry Smith 
1306d763cef2SBarry Smith .seealso: TSGetTimeStep()
1307d763cef2SBarry Smith 
1308d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1309d763cef2SBarry Smith @*/
13107087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1311d763cef2SBarry Smith {
1312d763cef2SBarry Smith   PetscFunctionBegin;
13130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13144482741eSBarry Smith   PetscValidPointer(v,2);
13158737fe31SLisandro Dalcin   *v = ts->vec_sol;
1316d763cef2SBarry Smith   PetscFunctionReturn(0);
1317d763cef2SBarry Smith }
1318d763cef2SBarry Smith 
1319bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
13204a2ae208SSatish Balay #undef __FUNCT__
1321bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1322d8e5e3e6SSatish Balay /*@
1323bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1324d763cef2SBarry Smith 
1325bdad233fSMatthew Knepley   Not collective
1326d763cef2SBarry Smith 
1327bdad233fSMatthew Knepley   Input Parameters:
1328bdad233fSMatthew Knepley + ts   - The TS
1329bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1330d763cef2SBarry Smith .vb
1331d763cef2SBarry Smith          U_t = A U
1332d763cef2SBarry Smith          U_t = A(t) U
1333d763cef2SBarry Smith          U_t = F(t,U)
1334d763cef2SBarry Smith .ve
1335d763cef2SBarry Smith 
1336d763cef2SBarry Smith    Level: beginner
1337d763cef2SBarry Smith 
1338bdad233fSMatthew Knepley .keywords: TS, problem type
1339bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1340d763cef2SBarry Smith @*/
13417087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1342a7cc72afSBarry Smith {
13439e2a6581SJed Brown   PetscErrorCode ierr;
13449e2a6581SJed Brown 
1345d763cef2SBarry Smith   PetscFunctionBegin;
13460700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1347bdad233fSMatthew Knepley   ts->problem_type = type;
13489e2a6581SJed Brown   if (type == TS_LINEAR) {
13499e2a6581SJed Brown     SNES snes;
13509e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13519e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
13529e2a6581SJed Brown   }
1353d763cef2SBarry Smith   PetscFunctionReturn(0);
1354d763cef2SBarry Smith }
1355d763cef2SBarry Smith 
1356bdad233fSMatthew Knepley #undef __FUNCT__
1357bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1358bdad233fSMatthew Knepley /*@C
1359bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1360bdad233fSMatthew Knepley 
1361bdad233fSMatthew Knepley   Not collective
1362bdad233fSMatthew Knepley 
1363bdad233fSMatthew Knepley   Input Parameter:
1364bdad233fSMatthew Knepley . ts   - The TS
1365bdad233fSMatthew Knepley 
1366bdad233fSMatthew Knepley   Output Parameter:
1367bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1368bdad233fSMatthew Knepley .vb
1369089b2837SJed Brown          M U_t = A U
1370089b2837SJed Brown          M(t) U_t = A(t) U
1371bdad233fSMatthew Knepley          U_t = F(t,U)
1372bdad233fSMatthew Knepley .ve
1373bdad233fSMatthew Knepley 
1374bdad233fSMatthew Knepley    Level: beginner
1375bdad233fSMatthew Knepley 
1376bdad233fSMatthew Knepley .keywords: TS, problem type
1377bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1378bdad233fSMatthew Knepley @*/
13797087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1380a7cc72afSBarry Smith {
1381bdad233fSMatthew Knepley   PetscFunctionBegin;
13820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
13834482741eSBarry Smith   PetscValidIntPointer(type,2);
1384bdad233fSMatthew Knepley   *type = ts->problem_type;
1385bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1386bdad233fSMatthew Knepley }
1387d763cef2SBarry Smith 
13884a2ae208SSatish Balay #undef __FUNCT__
13894a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1390d763cef2SBarry Smith /*@
1391d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1392d763cef2SBarry Smith    of a timestepper.
1393d763cef2SBarry Smith 
1394d763cef2SBarry Smith    Collective on TS
1395d763cef2SBarry Smith 
1396d763cef2SBarry Smith    Input Parameter:
1397d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1398d763cef2SBarry Smith 
1399d763cef2SBarry Smith    Notes:
1400d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1401d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1402d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1403d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1404d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1405d763cef2SBarry Smith 
1406d763cef2SBarry Smith    Level: advanced
1407d763cef2SBarry Smith 
1408d763cef2SBarry Smith .keywords: TS, timestep, setup
1409d763cef2SBarry Smith 
1410d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1411d763cef2SBarry Smith @*/
14127087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1413d763cef2SBarry Smith {
1414dfbe8321SBarry Smith   PetscErrorCode ierr;
14156c6b9e74SPeter Brune   DM             dm;
14166c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
14176c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
14186c6b9e74SPeter Brune   TSIJacobian    ijac;
14196c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1420d763cef2SBarry Smith 
1421d763cef2SBarry Smith   PetscFunctionBegin;
14220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1423277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1424277b19d0SLisandro Dalcin 
14257adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
14269596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1427d763cef2SBarry Smith   }
1428a43b19c4SJed Brown   if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE;
1429277b19d0SLisandro Dalcin 
1430277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1431277b19d0SLisandro Dalcin 
14321c3436cfSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
14331c3436cfSJed Brown 
1434277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1435000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1436277b19d0SLisandro Dalcin   }
1437277b19d0SLisandro Dalcin 
14386c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
14396c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
14406c6b9e74SPeter Brune    */
14416c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
14426c6b9e74SPeter Brune   ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr);
14436c6b9e74SPeter Brune   if (!func) {
14446c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
14456c6b9e74SPeter Brune   }
14466c6b9e74SPeter 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.
14476c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
14486c6b9e74SPeter Brune    */
14496c6b9e74SPeter Brune   ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr);
14506c6b9e74SPeter Brune   ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr);
14516c6b9e74SPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr);
14526c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
14536c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
14546c6b9e74SPeter Brune   }
1455277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1456277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1457277b19d0SLisandro Dalcin }
1458277b19d0SLisandro Dalcin 
1459277b19d0SLisandro Dalcin #undef __FUNCT__
1460277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1461277b19d0SLisandro Dalcin /*@
1462277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1463277b19d0SLisandro Dalcin 
1464277b19d0SLisandro Dalcin    Collective on TS
1465277b19d0SLisandro Dalcin 
1466277b19d0SLisandro Dalcin    Input Parameter:
1467277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1468277b19d0SLisandro Dalcin 
1469277b19d0SLisandro Dalcin    Level: beginner
1470277b19d0SLisandro Dalcin 
1471277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1472277b19d0SLisandro Dalcin 
1473277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1474277b19d0SLisandro Dalcin @*/
1475277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1476277b19d0SLisandro Dalcin {
1477277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1478277b19d0SLisandro Dalcin 
1479277b19d0SLisandro Dalcin   PetscFunctionBegin;
1480277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1481277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1482277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1483277b19d0SLisandro Dalcin   }
1484277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
14854e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
14864e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1487214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
14886bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1489e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1490e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
149138637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1492277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1493d763cef2SBarry Smith   PetscFunctionReturn(0);
1494d763cef2SBarry Smith }
1495d763cef2SBarry Smith 
14964a2ae208SSatish Balay #undef __FUNCT__
14974a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1498d8e5e3e6SSatish Balay /*@
1499d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1500d763cef2SBarry Smith    with TSCreate().
1501d763cef2SBarry Smith 
1502d763cef2SBarry Smith    Collective on TS
1503d763cef2SBarry Smith 
1504d763cef2SBarry Smith    Input Parameter:
1505d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1506d763cef2SBarry Smith 
1507d763cef2SBarry Smith    Level: beginner
1508d763cef2SBarry Smith 
1509d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1510d763cef2SBarry Smith 
1511d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1512d763cef2SBarry Smith @*/
15136bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1514d763cef2SBarry Smith {
15156849ba73SBarry Smith   PetscErrorCode ierr;
1516d763cef2SBarry Smith 
1517d763cef2SBarry Smith   PetscFunctionBegin;
15186bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
15196bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
15206bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1521d763cef2SBarry Smith 
15226bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1523277b19d0SLisandro Dalcin 
1524be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
15256bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
15266bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
15276d4c513bSLisandro Dalcin 
152884df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
15296bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
15306bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
15316bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
15326d4c513bSLisandro Dalcin 
1533a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1534d763cef2SBarry Smith   PetscFunctionReturn(0);
1535d763cef2SBarry Smith }
1536d763cef2SBarry Smith 
15374a2ae208SSatish Balay #undef __FUNCT__
15384a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1539d8e5e3e6SSatish Balay /*@
1540d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1541d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1544d763cef2SBarry Smith 
1545d763cef2SBarry Smith    Input Parameter:
1546d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1547d763cef2SBarry Smith 
1548d763cef2SBarry Smith    Output Parameter:
1549d763cef2SBarry Smith .  snes - the nonlinear solver context
1550d763cef2SBarry Smith 
1551d763cef2SBarry Smith    Notes:
1552d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1553d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
155494b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1555d763cef2SBarry Smith 
1556d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1557d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1558d763cef2SBarry Smith 
1559d763cef2SBarry Smith    Level: beginner
1560d763cef2SBarry Smith 
1561d763cef2SBarry Smith .keywords: timestep, get, SNES
1562d763cef2SBarry Smith @*/
15637087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1564d763cef2SBarry Smith {
1565d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1566d372ba47SLisandro Dalcin 
1567d763cef2SBarry Smith   PetscFunctionBegin;
15680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15694482741eSBarry Smith   PetscValidPointer(snes,2);
1570d372ba47SLisandro Dalcin   if (!ts->snes) {
1571d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
15726c6b9e74SPeter Brune     ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1573d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1574d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1575496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
15769e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
15779e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
15789e2a6581SJed Brown     }
1579d372ba47SLisandro Dalcin   }
1580d763cef2SBarry Smith   *snes = ts->snes;
1581d763cef2SBarry Smith   PetscFunctionReturn(0);
1582d763cef2SBarry Smith }
1583d763cef2SBarry Smith 
15844a2ae208SSatish Balay #undef __FUNCT__
158594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1586d8e5e3e6SSatish Balay /*@
158794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1588d763cef2SBarry Smith    a TS (timestepper) context.
1589d763cef2SBarry Smith 
159094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1591d763cef2SBarry Smith 
1592d763cef2SBarry Smith    Input Parameter:
1593d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1594d763cef2SBarry Smith 
1595d763cef2SBarry Smith    Output Parameter:
159694b7f48cSBarry Smith .  ksp - the nonlinear solver context
1597d763cef2SBarry Smith 
1598d763cef2SBarry Smith    Notes:
159994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1600d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1601d763cef2SBarry Smith    KSP and PC contexts as well.
1602d763cef2SBarry Smith 
160394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
160494b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1605d763cef2SBarry Smith 
1606d763cef2SBarry Smith    Level: beginner
1607d763cef2SBarry Smith 
160894b7f48cSBarry Smith .keywords: timestep, get, KSP
1609d763cef2SBarry Smith @*/
16107087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1611d763cef2SBarry Smith {
1612d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1613089b2837SJed Brown   SNES           snes;
1614d372ba47SLisandro Dalcin 
1615d763cef2SBarry Smith   PetscFunctionBegin;
16160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16174482741eSBarry Smith   PetscValidPointer(ksp,2);
161817186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1619e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1620089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1621089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1622d763cef2SBarry Smith   PetscFunctionReturn(0);
1623d763cef2SBarry Smith }
1624d763cef2SBarry Smith 
1625d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1626d763cef2SBarry Smith 
16274a2ae208SSatish Balay #undef __FUNCT__
1628adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1629adb62b0dSMatthew Knepley /*@
1630adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1631adb62b0dSMatthew Knepley    maximum time for iteration.
1632adb62b0dSMatthew Knepley 
16333f9fe445SBarry Smith    Not Collective
1634adb62b0dSMatthew Knepley 
1635adb62b0dSMatthew Knepley    Input Parameters:
1636adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1637adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1638adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1639adb62b0dSMatthew Knepley 
1640adb62b0dSMatthew Knepley    Level: intermediate
1641adb62b0dSMatthew Knepley 
1642adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1643adb62b0dSMatthew Knepley @*/
16447087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1645adb62b0dSMatthew Knepley {
1646adb62b0dSMatthew Knepley   PetscFunctionBegin;
16470700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1648abc0a331SBarry Smith   if (maxsteps) {
16494482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1650adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1651adb62b0dSMatthew Knepley   }
1652abc0a331SBarry Smith   if (maxtime) {
16534482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1654adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1655adb62b0dSMatthew Knepley   }
1656adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1657adb62b0dSMatthew Knepley }
1658adb62b0dSMatthew Knepley 
1659adb62b0dSMatthew Knepley #undef __FUNCT__
16604a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1661d763cef2SBarry Smith /*@
1662d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1663d763cef2SBarry Smith    maximum time for iteration.
1664d763cef2SBarry Smith 
16653f9fe445SBarry Smith    Logically Collective on TS
1666d763cef2SBarry Smith 
1667d763cef2SBarry Smith    Input Parameters:
1668d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1669d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1670d763cef2SBarry Smith -  maxtime - final time to iterate to
1671d763cef2SBarry Smith 
1672d763cef2SBarry Smith    Options Database Keys:
1673d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
16743bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1675d763cef2SBarry Smith 
1676d763cef2SBarry Smith    Notes:
1677d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1678d763cef2SBarry Smith 
1679d763cef2SBarry Smith    Level: intermediate
1680d763cef2SBarry Smith 
1681d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1682a43b19c4SJed Brown 
1683a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1684d763cef2SBarry Smith @*/
16857087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1686d763cef2SBarry Smith {
1687d763cef2SBarry Smith   PetscFunctionBegin;
16880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1689c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1690c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
169139b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
169239b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1693d763cef2SBarry Smith   PetscFunctionReturn(0);
1694d763cef2SBarry Smith }
1695d763cef2SBarry Smith 
16964a2ae208SSatish Balay #undef __FUNCT__
16974a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1698d763cef2SBarry Smith /*@
1699d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1700d763cef2SBarry Smith    for use by the TS routines.
1701d763cef2SBarry Smith 
17023f9fe445SBarry Smith    Logically Collective on TS and Vec
1703d763cef2SBarry Smith 
1704d763cef2SBarry Smith    Input Parameters:
1705d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1706d763cef2SBarry Smith -  x - the solution vector
1707d763cef2SBarry Smith 
1708d763cef2SBarry Smith    Level: beginner
1709d763cef2SBarry Smith 
1710d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1711d763cef2SBarry Smith @*/
17127087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1713d763cef2SBarry Smith {
17148737fe31SLisandro Dalcin   PetscErrorCode ierr;
1715496e6a7aSJed Brown   DM             dm;
17168737fe31SLisandro Dalcin 
1717d763cef2SBarry Smith   PetscFunctionBegin;
17180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17190700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
17208737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
17216bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
17228737fe31SLisandro Dalcin   ts->vec_sol = x;
1723496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1724496e6a7aSJed Brown   ierr = DMShellSetGlobalVector(dm,x);CHKERRQ(ierr);
1725d763cef2SBarry Smith   PetscFunctionReturn(0);
1726d763cef2SBarry Smith }
1727d763cef2SBarry Smith 
1728e74ef692SMatthew Knepley #undef __FUNCT__
1729e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1730ac226902SBarry Smith /*@C
1731000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
17323f2090d5SJed Brown   called once at the beginning of each time step.
1733000e7ae3SMatthew Knepley 
17343f9fe445SBarry Smith   Logically Collective on TS
1735000e7ae3SMatthew Knepley 
1736000e7ae3SMatthew Knepley   Input Parameters:
1737000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1738000e7ae3SMatthew Knepley - func - The function
1739000e7ae3SMatthew Knepley 
1740000e7ae3SMatthew Knepley   Calling sequence of func:
1741000e7ae3SMatthew Knepley . func (TS ts);
1742000e7ae3SMatthew Knepley 
1743000e7ae3SMatthew Knepley   Level: intermediate
1744000e7ae3SMatthew Knepley 
1745b8123daeSJed Brown   Note:
1746b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1747b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1748b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1749b8123daeSJed Brown 
1750000e7ae3SMatthew Knepley .keywords: TS, timestep
1751b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1752000e7ae3SMatthew Knepley @*/
17537087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1754000e7ae3SMatthew Knepley {
1755000e7ae3SMatthew Knepley   PetscFunctionBegin;
17560700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1757000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1758000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1759000e7ae3SMatthew Knepley }
1760000e7ae3SMatthew Knepley 
1761e74ef692SMatthew Knepley #undef __FUNCT__
17623f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
176309ee8438SJed Brown /*@
17643f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
17653f2090d5SJed Brown 
17663f2090d5SJed Brown   Collective on TS
17673f2090d5SJed Brown 
17683f2090d5SJed Brown   Input Parameters:
17693f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
17703f2090d5SJed Brown 
17713f2090d5SJed Brown   Notes:
17723f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
17733f2090d5SJed Brown   so most users would not generally call this routine themselves.
17743f2090d5SJed Brown 
17753f2090d5SJed Brown   Level: developer
17763f2090d5SJed Brown 
17773f2090d5SJed Brown .keywords: TS, timestep
1778b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
17793f2090d5SJed Brown @*/
17807087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
17813f2090d5SJed Brown {
17823f2090d5SJed Brown   PetscErrorCode ierr;
17833f2090d5SJed Brown 
17843f2090d5SJed Brown   PetscFunctionBegin;
17850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
178672ac3e02SJed Brown   if (ts->ops->prestep) {
17873f2090d5SJed Brown     PetscStackPush("TS PreStep function");
17883f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
17893f2090d5SJed Brown     PetscStackPop;
1790312ce896SJed Brown   }
17913f2090d5SJed Brown   PetscFunctionReturn(0);
17923f2090d5SJed Brown }
17933f2090d5SJed Brown 
17943f2090d5SJed Brown #undef __FUNCT__
1795b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1796b8123daeSJed Brown /*@C
1797b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1798b8123daeSJed Brown   called once at the beginning of each stage.
1799b8123daeSJed Brown 
1800b8123daeSJed Brown   Logically Collective on TS
1801b8123daeSJed Brown 
1802b8123daeSJed Brown   Input Parameters:
1803b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1804b8123daeSJed Brown - func - The function
1805b8123daeSJed Brown 
1806b8123daeSJed Brown   Calling sequence of func:
1807b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1808b8123daeSJed Brown 
1809b8123daeSJed Brown   Level: intermediate
1810b8123daeSJed Brown 
1811b8123daeSJed Brown   Note:
1812b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
1813b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
1814b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
1815b8123daeSJed Brown 
1816b8123daeSJed Brown .keywords: TS, timestep
1817b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
1818b8123daeSJed Brown @*/
1819b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
1820b8123daeSJed Brown {
1821b8123daeSJed Brown   PetscFunctionBegin;
1822b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1823b8123daeSJed Brown   ts->ops->prestage = func;
1824b8123daeSJed Brown   PetscFunctionReturn(0);
1825b8123daeSJed Brown }
1826b8123daeSJed Brown 
1827b8123daeSJed Brown #undef __FUNCT__
1828b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
1829b8123daeSJed Brown /*@
1830b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
1831b8123daeSJed Brown 
1832b8123daeSJed Brown   Collective on TS
1833b8123daeSJed Brown 
1834b8123daeSJed Brown   Input Parameters:
1835b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
1836b8123daeSJed Brown 
1837b8123daeSJed Brown   Notes:
1838b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
1839b8123daeSJed Brown   most users would not generally call this routine themselves.
1840b8123daeSJed Brown 
1841b8123daeSJed Brown   Level: developer
1842b8123daeSJed Brown 
1843b8123daeSJed Brown .keywords: TS, timestep
1844b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
1845b8123daeSJed Brown @*/
1846b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
1847b8123daeSJed Brown {
1848b8123daeSJed Brown   PetscErrorCode ierr;
1849b8123daeSJed Brown 
1850b8123daeSJed Brown   PetscFunctionBegin;
1851b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1852b8123daeSJed Brown   if (ts->ops->prestage) {
1853b8123daeSJed Brown     PetscStackPush("TS PreStage function");
1854b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
1855b8123daeSJed Brown     PetscStackPop;
1856b8123daeSJed Brown   }
1857b8123daeSJed Brown   PetscFunctionReturn(0);
1858b8123daeSJed Brown }
1859b8123daeSJed Brown 
1860b8123daeSJed Brown #undef __FUNCT__
1861e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1862ac226902SBarry Smith /*@C
1863000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
18643f2090d5SJed Brown   called once at the end of each time step.
1865000e7ae3SMatthew Knepley 
18663f9fe445SBarry Smith   Logically Collective on TS
1867000e7ae3SMatthew Knepley 
1868000e7ae3SMatthew Knepley   Input Parameters:
1869000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1870000e7ae3SMatthew Knepley - func - The function
1871000e7ae3SMatthew Knepley 
1872000e7ae3SMatthew Knepley   Calling sequence of func:
1873b8123daeSJed Brown $ func (TS ts);
1874000e7ae3SMatthew Knepley 
1875000e7ae3SMatthew Knepley   Level: intermediate
1876000e7ae3SMatthew Knepley 
1877000e7ae3SMatthew Knepley .keywords: TS, timestep
1878b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
1879000e7ae3SMatthew Knepley @*/
18807087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1881000e7ae3SMatthew Knepley {
1882000e7ae3SMatthew Knepley   PetscFunctionBegin;
18830700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1884000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1885000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1886000e7ae3SMatthew Knepley }
1887000e7ae3SMatthew Knepley 
1888e74ef692SMatthew Knepley #undef __FUNCT__
18893f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
189009ee8438SJed Brown /*@
18913f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
18923f2090d5SJed Brown 
18933f2090d5SJed Brown   Collective on TS
18943f2090d5SJed Brown 
18953f2090d5SJed Brown   Input Parameters:
18963f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
18973f2090d5SJed Brown 
18983f2090d5SJed Brown   Notes:
18993f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
19003f2090d5SJed Brown   so most users would not generally call this routine themselves.
19013f2090d5SJed Brown 
19023f2090d5SJed Brown   Level: developer
19033f2090d5SJed Brown 
19043f2090d5SJed Brown .keywords: TS, timestep
19053f2090d5SJed Brown @*/
19067087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
19073f2090d5SJed Brown {
19083f2090d5SJed Brown   PetscErrorCode ierr;
19093f2090d5SJed Brown 
19103f2090d5SJed Brown   PetscFunctionBegin;
19110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
191272ac3e02SJed Brown   if (ts->ops->poststep) {
19133f2090d5SJed Brown     PetscStackPush("TS PostStep function");
19143f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
19153f2090d5SJed Brown     PetscStackPop;
191672ac3e02SJed Brown   }
19173f2090d5SJed Brown   PetscFunctionReturn(0);
19183f2090d5SJed Brown }
19193f2090d5SJed Brown 
1920d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1921d763cef2SBarry Smith 
19224a2ae208SSatish Balay #undef __FUNCT__
1923a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1924d763cef2SBarry Smith /*@C
1925a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1926d763cef2SBarry Smith    timestep to display the iteration's  progress.
1927d763cef2SBarry Smith 
19283f9fe445SBarry Smith    Logically Collective on TS
1929d763cef2SBarry Smith 
1930d763cef2SBarry Smith    Input Parameters:
1931d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1932e213d8f1SJed Brown .  monitor - monitoring routine
1933329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1934b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1935b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1936b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1937d763cef2SBarry Smith 
1938e213d8f1SJed Brown    Calling sequence of monitor:
1939e213d8f1SJed Brown $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1940d763cef2SBarry Smith 
1941d763cef2SBarry Smith +    ts - the TS context
1942d763cef2SBarry Smith .    steps - iteration number
19431f06c33eSBarry Smith .    time - current time
1944d763cef2SBarry Smith .    x - current iterate
1945d763cef2SBarry Smith -    mctx - [optional] monitoring context
1946d763cef2SBarry Smith 
1947d763cef2SBarry Smith    Notes:
1948d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1949d763cef2SBarry Smith    already has been loaded.
1950d763cef2SBarry Smith 
1951025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1952025f1a04SBarry Smith 
1953d763cef2SBarry Smith    Level: intermediate
1954d763cef2SBarry Smith 
1955d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1956d763cef2SBarry Smith 
1957a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1958d763cef2SBarry Smith @*/
1959c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1960d763cef2SBarry Smith {
1961d763cef2SBarry Smith   PetscFunctionBegin;
19620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
196317186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1964d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
19658704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]    = mdestroy;
1966d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1967d763cef2SBarry Smith   PetscFunctionReturn(0);
1968d763cef2SBarry Smith }
1969d763cef2SBarry Smith 
19704a2ae208SSatish Balay #undef __FUNCT__
1971a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1972d763cef2SBarry Smith /*@C
1973a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1974d763cef2SBarry Smith 
19753f9fe445SBarry Smith    Logically Collective on TS
1976d763cef2SBarry Smith 
1977d763cef2SBarry Smith    Input Parameters:
1978d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1979d763cef2SBarry Smith 
1980d763cef2SBarry Smith    Notes:
1981d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1982d763cef2SBarry Smith 
1983d763cef2SBarry Smith    Level: intermediate
1984d763cef2SBarry Smith 
1985d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1986d763cef2SBarry Smith 
1987a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1988d763cef2SBarry Smith @*/
19897087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1990d763cef2SBarry Smith {
1991d952e501SBarry Smith   PetscErrorCode ierr;
1992d952e501SBarry Smith   PetscInt       i;
1993d952e501SBarry Smith 
1994d763cef2SBarry Smith   PetscFunctionBegin;
19950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1996d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
19978704b422SBarry Smith     if (ts->monitordestroy[i]) {
19988704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1999d952e501SBarry Smith     }
2000d952e501SBarry Smith   }
2001d763cef2SBarry Smith   ts->numbermonitors = 0;
2002d763cef2SBarry Smith   PetscFunctionReturn(0);
2003d763cef2SBarry Smith }
2004d763cef2SBarry Smith 
20054a2ae208SSatish Balay #undef __FUNCT__
2006a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2007d8e5e3e6SSatish Balay /*@
2008a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
20095516499fSSatish Balay 
20105516499fSSatish Balay    Level: intermediate
201141251cbbSSatish Balay 
20125516499fSSatish Balay .keywords: TS, set, monitor
20135516499fSSatish Balay 
201441251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
201541251cbbSSatish Balay @*/
2016649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2017d763cef2SBarry Smith {
2018dfbe8321SBarry Smith   PetscErrorCode ierr;
2019649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
2020d132466eSBarry Smith 
2021d763cef2SBarry Smith   PetscFunctionBegin;
2022649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2023971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2024649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2025d763cef2SBarry Smith   PetscFunctionReturn(0);
2026d763cef2SBarry Smith }
2027d763cef2SBarry Smith 
20284a2ae208SSatish Balay #undef __FUNCT__
2029cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2030cd652676SJed Brown /*@
2031cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2032cd652676SJed Brown 
2033cd652676SJed Brown    Logically Collective on TS
2034cd652676SJed Brown 
2035cd652676SJed Brown    Input Argument:
2036cd652676SJed Brown .  ts - time stepping context
2037cd652676SJed Brown 
2038cd652676SJed Brown    Output Argument:
2039cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2040cd652676SJed Brown 
2041cd652676SJed Brown    Level: intermediate
2042cd652676SJed Brown 
2043cd652676SJed Brown .keywords: TS, set
2044cd652676SJed Brown 
2045cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2046cd652676SJed Brown @*/
2047cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2048cd652676SJed Brown {
2049cd652676SJed Brown 
2050cd652676SJed Brown   PetscFunctionBegin;
2051cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2052cd652676SJed Brown   ts->retain_stages = flg;
2053cd652676SJed Brown   PetscFunctionReturn(0);
2054cd652676SJed Brown }
2055cd652676SJed Brown 
2056cd652676SJed Brown #undef __FUNCT__
2057cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2058cd652676SJed Brown /*@
2059cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2060cd652676SJed Brown 
2061cd652676SJed Brown    Collective on TS
2062cd652676SJed Brown 
2063cd652676SJed Brown    Input Argument:
2064cd652676SJed Brown +  ts - time stepping context
2065cd652676SJed Brown -  t - time to interpolate to
2066cd652676SJed Brown 
2067cd652676SJed Brown    Output Argument:
2068cd652676SJed Brown .  X - state at given time
2069cd652676SJed Brown 
2070cd652676SJed Brown    Notes:
2071cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2072cd652676SJed Brown 
2073cd652676SJed Brown    Level: intermediate
2074cd652676SJed Brown 
2075cd652676SJed Brown    Developer Notes:
2076cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2077cd652676SJed Brown 
2078cd652676SJed Brown .keywords: TS, set
2079cd652676SJed Brown 
2080cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2081cd652676SJed Brown @*/
2082cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X)
2083cd652676SJed Brown {
2084cd652676SJed Brown   PetscErrorCode ierr;
2085cd652676SJed Brown 
2086cd652676SJed Brown   PetscFunctionBegin;
2087cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2088d8cd7023SBarry Smith   if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime);
2089cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
2090cd652676SJed Brown   ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr);
2091cd652676SJed Brown   PetscFunctionReturn(0);
2092cd652676SJed Brown }
2093cd652676SJed Brown 
2094cd652676SJed Brown #undef __FUNCT__
20954a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2096d763cef2SBarry Smith /*@
20976d9e5789SSean Farley    TSStep - Steps one time step
2098d763cef2SBarry Smith 
2099d763cef2SBarry Smith    Collective on TS
2100d763cef2SBarry Smith 
2101d763cef2SBarry Smith    Input Parameter:
2102d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2103d763cef2SBarry Smith 
21046d9e5789SSean Farley    Level: intermediate
2105d763cef2SBarry Smith 
2106b8123daeSJed Brown    Notes:
2107b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2108b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2109b8123daeSJed Brown 
2110d763cef2SBarry Smith .keywords: TS, timestep, solve
2111d763cef2SBarry Smith 
2112b8123daeSJed Brown .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage()
2113d763cef2SBarry Smith @*/
2114193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2115d763cef2SBarry Smith {
2116362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2117dfbe8321SBarry Smith   PetscErrorCode ierr;
2118d763cef2SBarry Smith 
2119d763cef2SBarry Smith   PetscFunctionBegin;
21200700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2121d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2122d405a339SMatthew Knepley 
2123362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2124362cd11cSLisandro Dalcin 
2125362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2126d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2127193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2128d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2129362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2130362cd11cSLisandro Dalcin 
2131362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2132cef5090cSJed Brown     if (ts->errorifstepfailed) {
2133cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2134cef5090cSJed Brown         SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
2135cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2136cef5090cSJed Brown     }
2137362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2138362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2139362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2140362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2141362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2142362cd11cSLisandro Dalcin   }
2143362cd11cSLisandro Dalcin 
2144d763cef2SBarry Smith   PetscFunctionReturn(0);
2145d763cef2SBarry Smith }
2146d763cef2SBarry Smith 
21474a2ae208SSatish Balay #undef __FUNCT__
214805175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
214905175c85SJed Brown /*@
215005175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
215105175c85SJed Brown 
21521c3436cfSJed Brown    Collective on TS
215305175c85SJed Brown 
215405175c85SJed Brown    Input Arguments:
21551c3436cfSJed Brown +  ts - time stepping context
21561c3436cfSJed Brown .  order - desired order of accuracy
21571c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
215805175c85SJed Brown 
215905175c85SJed Brown    Output Arguments:
21601c3436cfSJed Brown .  X - state at the end of the current step
216105175c85SJed Brown 
216205175c85SJed Brown    Level: advanced
216305175c85SJed Brown 
2164108c343cSJed Brown    Notes:
2165108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2166108c343cSJed 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.
2167108c343cSJed Brown 
21681c3436cfSJed Brown .seealso: TSStep(), TSAdapt
216905175c85SJed Brown @*/
21701c3436cfSJed Brown PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec X,PetscBool *done)
217105175c85SJed Brown {
217205175c85SJed Brown   PetscErrorCode ierr;
217305175c85SJed Brown 
217405175c85SJed Brown   PetscFunctionBegin;
217505175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
217605175c85SJed Brown   PetscValidType(ts,1);
217705175c85SJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
21781c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
21791c3436cfSJed Brown   ierr = (*ts->ops->evaluatestep)(ts,order,X,done);CHKERRQ(ierr);
218005175c85SJed Brown   PetscFunctionReturn(0);
218105175c85SJed Brown }
218205175c85SJed Brown 
218305175c85SJed Brown #undef __FUNCT__
21846a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
21856a4d4014SLisandro Dalcin /*@
21866a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
21876a4d4014SLisandro Dalcin 
21886a4d4014SLisandro Dalcin    Collective on TS
21896a4d4014SLisandro Dalcin 
21906a4d4014SLisandro Dalcin    Input Parameter:
21916a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2192362cd11cSLisandro Dalcin -  x - the solution vector
21936a4d4014SLisandro Dalcin 
21945a3a76d0SJed Brown    Output Parameter:
21955a3a76d0SJed Brown .  ftime - time of the state vector x upon completion
21965a3a76d0SJed Brown 
21976a4d4014SLisandro Dalcin    Level: beginner
21986a4d4014SLisandro Dalcin 
21995a3a76d0SJed Brown    Notes:
22005a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
22015a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
22025a3a76d0SJed Brown    stepped over the final time.
22035a3a76d0SJed Brown 
22046a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
22056a4d4014SLisandro Dalcin 
22066a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
22076a4d4014SLisandro Dalcin @*/
22085a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime)
22096a4d4014SLisandro Dalcin {
22104d7d938eSLisandro Dalcin   PetscBool      flg;
22114d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
22124d7d938eSLisandro Dalcin   PetscViewer    viewer;
22136a4d4014SLisandro Dalcin   PetscErrorCode ierr;
2214f22f69f0SBarry Smith 
22156a4d4014SLisandro Dalcin   PetscFunctionBegin;
22160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2217193ac0bcSJed Brown   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
22185a3a76d0SJed Brown   if (ts->exact_final_time) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
22195a3a76d0SJed Brown     if (!ts->vec_sol || x == ts->vec_sol) {
22205a3a76d0SJed Brown       Vec y;
22215a3a76d0SJed Brown       ierr = VecDuplicate(x,&y);CHKERRQ(ierr);
22225a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
22235a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
22245a3a76d0SJed Brown     }
2225362cd11cSLisandro Dalcin     ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr);
22265a3a76d0SJed Brown   } else {
2227193ac0bcSJed Brown     ierr = TSSetSolution(ts,x);CHKERRQ(ierr);
22285a3a76d0SJed Brown   }
2229b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
22306a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2231193ac0bcSJed Brown   ts->steps = 0;
22325ef26d82SJed Brown   ts->ksp_its = 0;
22335ef26d82SJed Brown   ts->snes_its = 0;
2234c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2235c610991cSLisandro Dalcin   ts->reject = 0;
2236193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
2237193ac0bcSJed Brown 
2238193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2239193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
22405a3a76d0SJed Brown     ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
2241a5b82c69SSean Farley     if (ftime) *ftime = ts->ptime;
2242193ac0bcSJed Brown   } else {
22436a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2244362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2245362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2246362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2247362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2248362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2249e1a7a14fSJed Brown     while (!ts->reason) {
2250193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2251193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2252193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2253193ac0bcSJed Brown     }
2254362cd11cSLisandro Dalcin     if (ts->exact_final_time && ts->ptime > ts->max_time) {
2255a43b19c4SJed Brown       ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr);
22565a3a76d0SJed Brown       if (ftime) *ftime = ts->max_time;
22570574a7fbSJed Brown     } else {
22580574a7fbSJed Brown       ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
22590574a7fbSJed Brown       if (ftime) *ftime = ts->ptime;
22600574a7fbSJed Brown     }
2261193ac0bcSJed Brown   }
22623923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
22634d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
22644d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
22654d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
22664d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
22674d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2268193ac0bcSJed Brown   }
22696a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
22706a4d4014SLisandro Dalcin }
22716a4d4014SLisandro Dalcin 
22726a4d4014SLisandro Dalcin #undef __FUNCT__
22734a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2274228d79bcSJed Brown /*@
2275228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2276228d79bcSJed Brown 
2277228d79bcSJed Brown    Collective on TS
2278228d79bcSJed Brown 
2279228d79bcSJed Brown    Input Parameters:
2280228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2281228d79bcSJed Brown .  step - step number that has just completed
2282228d79bcSJed Brown .  ptime - model time of the state
2283228d79bcSJed Brown -  x - state at the current model time
2284228d79bcSJed Brown 
2285228d79bcSJed Brown    Notes:
2286228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2287228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2288228d79bcSJed Brown 
2289228d79bcSJed Brown    Level: advanced
2290228d79bcSJed Brown 
2291228d79bcSJed Brown .keywords: TS, timestep
2292228d79bcSJed Brown @*/
2293a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
2294d763cef2SBarry Smith {
22956849ba73SBarry Smith   PetscErrorCode ierr;
2296a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2297d763cef2SBarry Smith 
2298d763cef2SBarry Smith   PetscFunctionBegin;
2299d763cef2SBarry Smith   for (i=0; i<n; i++) {
2300142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
2301d763cef2SBarry Smith   }
2302d763cef2SBarry Smith   PetscFunctionReturn(0);
2303d763cef2SBarry Smith }
2304d763cef2SBarry Smith 
2305d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
23060b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
23070b039ecaSBarry Smith   PetscDrawLG lg;
23080b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2309201da799SBarry Smith   PetscInt    ksp_its,snes_its;
23100b039ecaSBarry Smith };
23110b039ecaSBarry Smith 
2312d763cef2SBarry Smith 
23134a2ae208SSatish Balay #undef __FUNCT__
2314a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2315d763cef2SBarry Smith /*@C
2316a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2317a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2318d763cef2SBarry Smith 
2319d763cef2SBarry Smith    Collective on TS
2320d763cef2SBarry Smith 
2321d763cef2SBarry Smith    Input Parameters:
2322d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2323d763cef2SBarry Smith .  label - the title to put in the title bar
23247c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2325a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2326a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2327d763cef2SBarry Smith 
2328d763cef2SBarry Smith    Output Parameter:
23290b039ecaSBarry Smith .  ctx - the context
2330d763cef2SBarry Smith 
2331d763cef2SBarry Smith    Options Database Key:
23324f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
23334f09c107SBarry Smith .  -ts_monitor_lg_solution -
23344f09c107SBarry Smith -  -ts_monitor_lg_error -
2335d763cef2SBarry Smith 
2336d763cef2SBarry Smith    Notes:
2337a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2338d763cef2SBarry Smith 
2339d763cef2SBarry Smith    Level: intermediate
2340d763cef2SBarry Smith 
23417c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2342d763cef2SBarry Smith 
23434f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
23447c922b88SBarry Smith 
2345d763cef2SBarry Smith @*/
2346a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2347d763cef2SBarry Smith {
2348b0a32e0cSBarry Smith   PetscDraw      win;
2349dfbe8321SBarry Smith   PetscErrorCode ierr;
2350d763cef2SBarry Smith 
2351d763cef2SBarry Smith   PetscFunctionBegin;
2352201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2353a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
2354*3fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
23550b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
23560b039ecaSBarry Smith   ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
23570b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2358a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2359d763cef2SBarry Smith   PetscFunctionReturn(0);
2360d763cef2SBarry Smith }
2361d763cef2SBarry Smith 
23624a2ae208SSatish Balay #undef __FUNCT__
23634f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
23644f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2365d763cef2SBarry Smith {
23660b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2367c32365f1SBarry Smith   PetscReal      x = ptime,y;
2368dfbe8321SBarry Smith   PetscErrorCode ierr;
2369d763cef2SBarry Smith 
2370d763cef2SBarry Smith   PetscFunctionBegin;
2371a9f9c1f6SBarry Smith   if (!n) {
2372a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
2373a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2374a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2375a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2376a9f9c1f6SBarry Smith   }
2377c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
23780b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
2379*3fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) || ((ctx->howoften == -1) && (n == -1)))){
23800b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
23813923b477SBarry Smith   }
2382d763cef2SBarry Smith   PetscFunctionReturn(0);
2383d763cef2SBarry Smith }
2384d763cef2SBarry Smith 
23854a2ae208SSatish Balay #undef __FUNCT__
2386a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2387d763cef2SBarry Smith /*@C
2388a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2389a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2390d763cef2SBarry Smith 
23910b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2392d763cef2SBarry Smith 
2393d763cef2SBarry Smith    Input Parameter:
23940b039ecaSBarry Smith .  ctx - the monitor context
2395d763cef2SBarry Smith 
2396d763cef2SBarry Smith    Level: intermediate
2397d763cef2SBarry Smith 
2398d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2399d763cef2SBarry Smith 
24004f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2401d763cef2SBarry Smith @*/
2402a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2403d763cef2SBarry Smith {
2404b0a32e0cSBarry Smith   PetscDraw      draw;
2405dfbe8321SBarry Smith   PetscErrorCode ierr;
2406d763cef2SBarry Smith 
2407d763cef2SBarry Smith   PetscFunctionBegin;
24080b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
24096bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
24100b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
24110b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2412d763cef2SBarry Smith   PetscFunctionReturn(0);
2413d763cef2SBarry Smith }
2414d763cef2SBarry Smith 
24154a2ae208SSatish Balay #undef __FUNCT__
24164a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2417d763cef2SBarry Smith /*@
2418b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2419d763cef2SBarry Smith 
2420d763cef2SBarry Smith    Not Collective
2421d763cef2SBarry Smith 
2422d763cef2SBarry Smith    Input Parameter:
2423d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2424d763cef2SBarry Smith 
2425d763cef2SBarry Smith    Output Parameter:
2426d763cef2SBarry Smith .  t  - the current time
2427d763cef2SBarry Smith 
2428d763cef2SBarry Smith    Level: beginner
2429d763cef2SBarry Smith 
2430b8123daeSJed Brown    Note:
2431b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2432b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2433b8123daeSJed Brown 
2434d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2435d763cef2SBarry Smith 
2436d763cef2SBarry Smith .keywords: TS, get, time
2437d763cef2SBarry Smith @*/
24387087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2439d763cef2SBarry Smith {
2440d763cef2SBarry Smith   PetscFunctionBegin;
24410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2442f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2443d763cef2SBarry Smith   *t = ts->ptime;
2444d763cef2SBarry Smith   PetscFunctionReturn(0);
2445d763cef2SBarry Smith }
2446d763cef2SBarry Smith 
24474a2ae208SSatish Balay #undef __FUNCT__
24486a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
24496a4d4014SLisandro Dalcin /*@
24506a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
24516a4d4014SLisandro Dalcin 
24523f9fe445SBarry Smith    Logically Collective on TS
24536a4d4014SLisandro Dalcin 
24546a4d4014SLisandro Dalcin    Input Parameters:
24556a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
24566a4d4014SLisandro Dalcin -  time - the time
24576a4d4014SLisandro Dalcin 
24586a4d4014SLisandro Dalcin    Level: intermediate
24596a4d4014SLisandro Dalcin 
24606a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
24616a4d4014SLisandro Dalcin 
24626a4d4014SLisandro Dalcin .keywords: TS, set, time
24636a4d4014SLisandro Dalcin @*/
24647087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
24656a4d4014SLisandro Dalcin {
24666a4d4014SLisandro Dalcin   PetscFunctionBegin;
24670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2468c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
24696a4d4014SLisandro Dalcin   ts->ptime = t;
24706a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
24716a4d4014SLisandro Dalcin }
24726a4d4014SLisandro Dalcin 
24736a4d4014SLisandro Dalcin #undef __FUNCT__
24744a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2475d763cef2SBarry Smith /*@C
2476d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2477d763cef2SBarry Smith    TS options in the database.
2478d763cef2SBarry Smith 
24793f9fe445SBarry Smith    Logically Collective on TS
2480d763cef2SBarry Smith 
2481d763cef2SBarry Smith    Input Parameter:
2482d763cef2SBarry Smith +  ts     - The TS context
2483d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2484d763cef2SBarry Smith 
2485d763cef2SBarry Smith    Notes:
2486d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2487d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2488d763cef2SBarry Smith    hyphen.
2489d763cef2SBarry Smith 
2490d763cef2SBarry Smith    Level: advanced
2491d763cef2SBarry Smith 
2492d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2493d763cef2SBarry Smith 
2494d763cef2SBarry Smith .seealso: TSSetFromOptions()
2495d763cef2SBarry Smith 
2496d763cef2SBarry Smith @*/
24977087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2498d763cef2SBarry Smith {
2499dfbe8321SBarry Smith   PetscErrorCode ierr;
2500089b2837SJed Brown   SNES           snes;
2501d763cef2SBarry Smith 
2502d763cef2SBarry Smith   PetscFunctionBegin;
25030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2504d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2505089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2506089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2507d763cef2SBarry Smith   PetscFunctionReturn(0);
2508d763cef2SBarry Smith }
2509d763cef2SBarry Smith 
2510d763cef2SBarry Smith 
25114a2ae208SSatish Balay #undef __FUNCT__
25124a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2513d763cef2SBarry Smith /*@C
2514d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2515d763cef2SBarry Smith    TS options in the database.
2516d763cef2SBarry Smith 
25173f9fe445SBarry Smith    Logically Collective on TS
2518d763cef2SBarry Smith 
2519d763cef2SBarry Smith    Input Parameter:
2520d763cef2SBarry Smith +  ts     - The TS context
2521d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2522d763cef2SBarry Smith 
2523d763cef2SBarry Smith    Notes:
2524d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2525d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2526d763cef2SBarry Smith    hyphen.
2527d763cef2SBarry Smith 
2528d763cef2SBarry Smith    Level: advanced
2529d763cef2SBarry Smith 
2530d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2531d763cef2SBarry Smith 
2532d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2533d763cef2SBarry Smith 
2534d763cef2SBarry Smith @*/
25357087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2536d763cef2SBarry Smith {
2537dfbe8321SBarry Smith   PetscErrorCode ierr;
2538089b2837SJed Brown   SNES           snes;
2539d763cef2SBarry Smith 
2540d763cef2SBarry Smith   PetscFunctionBegin;
25410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2542d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2543089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2544089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2545d763cef2SBarry Smith   PetscFunctionReturn(0);
2546d763cef2SBarry Smith }
2547d763cef2SBarry Smith 
25484a2ae208SSatish Balay #undef __FUNCT__
25494a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2550d763cef2SBarry Smith /*@C
2551d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2552d763cef2SBarry Smith    TS options in the database.
2553d763cef2SBarry Smith 
2554d763cef2SBarry Smith    Not Collective
2555d763cef2SBarry Smith 
2556d763cef2SBarry Smith    Input Parameter:
2557d763cef2SBarry Smith .  ts - The TS context
2558d763cef2SBarry Smith 
2559d763cef2SBarry Smith    Output Parameter:
2560d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2561d763cef2SBarry Smith 
2562d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2563d763cef2SBarry Smith    sufficient length to hold the prefix.
2564d763cef2SBarry Smith 
2565d763cef2SBarry Smith    Level: intermediate
2566d763cef2SBarry Smith 
2567d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2568d763cef2SBarry Smith 
2569d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2570d763cef2SBarry Smith @*/
25717087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2572d763cef2SBarry Smith {
2573dfbe8321SBarry Smith   PetscErrorCode ierr;
2574d763cef2SBarry Smith 
2575d763cef2SBarry Smith   PetscFunctionBegin;
25760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25774482741eSBarry Smith   PetscValidPointer(prefix,2);
2578d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2579d763cef2SBarry Smith   PetscFunctionReturn(0);
2580d763cef2SBarry Smith }
2581d763cef2SBarry Smith 
25824a2ae208SSatish Balay #undef __FUNCT__
25834a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2584d763cef2SBarry Smith /*@C
2585d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2586d763cef2SBarry Smith 
2587d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2588d763cef2SBarry Smith 
2589d763cef2SBarry Smith    Input Parameter:
2590d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2591d763cef2SBarry Smith 
2592d763cef2SBarry Smith    Output Parameters:
2593d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2594d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2595089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2596d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2597d763cef2SBarry Smith 
2598d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2599d763cef2SBarry Smith 
2600d763cef2SBarry Smith    Level: intermediate
2601d763cef2SBarry Smith 
260226d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2603d763cef2SBarry Smith 
2604d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2605d763cef2SBarry Smith @*/
2606089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2607d763cef2SBarry Smith {
2608089b2837SJed Brown   PetscErrorCode ierr;
2609089b2837SJed Brown   SNES           snes;
261024989b8cSPeter Brune   DM             dm;
2611089b2837SJed Brown 
2612d763cef2SBarry Smith   PetscFunctionBegin;
2613089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2614089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
261524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
261624989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2617d763cef2SBarry Smith   PetscFunctionReturn(0);
2618d763cef2SBarry Smith }
2619d763cef2SBarry Smith 
26201713a123SBarry Smith #undef __FUNCT__
26212eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
26222eca1d9cSJed Brown /*@C
26232eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
26242eca1d9cSJed Brown 
26252eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
26262eca1d9cSJed Brown 
26272eca1d9cSJed Brown    Input Parameter:
26282eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
26292eca1d9cSJed Brown 
26302eca1d9cSJed Brown    Output Parameters:
26312eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
26322eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
26332eca1d9cSJed Brown .  f   - The function to compute the matrices
26342eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
26352eca1d9cSJed Brown 
26362eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
26372eca1d9cSJed Brown 
26382eca1d9cSJed Brown    Level: advanced
26392eca1d9cSJed Brown 
26402eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
26412eca1d9cSJed Brown 
26422eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
26432eca1d9cSJed Brown @*/
26447087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
26452eca1d9cSJed Brown {
2646089b2837SJed Brown   PetscErrorCode ierr;
2647089b2837SJed Brown   SNES           snes;
264824989b8cSPeter Brune   DM             dm;
26492eca1d9cSJed Brown   PetscFunctionBegin;
2650089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2651089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
265224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
265324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
26542eca1d9cSJed Brown   PetscFunctionReturn(0);
26552eca1d9cSJed Brown }
26562eca1d9cSJed Brown 
265783a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
26586083293cSBarry Smith   PetscViewer viewer;
26596083293cSBarry Smith   Vec         initialsolution;
26606083293cSBarry Smith   PetscBool   showinitial;
266183a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
266283a4ac43SBarry Smith };
26636083293cSBarry Smith 
26642eca1d9cSJed Brown #undef __FUNCT__
266583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
26661713a123SBarry Smith /*@C
266783a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
26681713a123SBarry Smith    VecView() for the solution at each timestep
26691713a123SBarry Smith 
26701713a123SBarry Smith    Collective on TS
26711713a123SBarry Smith 
26721713a123SBarry Smith    Input Parameters:
26731713a123SBarry Smith +  ts - the TS context
26741713a123SBarry Smith .  step - current time-step
2675142b95e3SSatish Balay .  ptime - current time
26761713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
26771713a123SBarry Smith 
26781713a123SBarry Smith    Level: intermediate
26791713a123SBarry Smith 
26801713a123SBarry Smith .keywords: TS,  vector, monitor, view
26811713a123SBarry Smith 
2682a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
26831713a123SBarry Smith @*/
268483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
26851713a123SBarry Smith {
2686dfbe8321SBarry Smith   PetscErrorCode   ierr;
268783a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
26881713a123SBarry Smith 
26891713a123SBarry Smith   PetscFunctionBegin;
26906083293cSBarry Smith   if (!step && ictx->showinitial) {
26916083293cSBarry Smith     if (!ictx->initialsolution) {
26926083293cSBarry Smith       ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr);
26931713a123SBarry Smith     }
26946083293cSBarry Smith     ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr);
26956083293cSBarry Smith   }
2696*3fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
26970dcf80beSBarry Smith 
26986083293cSBarry Smith   if (ictx->showinitial) {
26996083293cSBarry Smith     PetscReal pause;
27006083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
27016083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
27026083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
27036083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
27046083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
27056083293cSBarry Smith   }
27066083293cSBarry Smith   ierr = VecView(x,ictx->viewer);CHKERRQ(ierr);
27076083293cSBarry Smith   if (ictx->showinitial) {
27086083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
27096083293cSBarry Smith   }
27101713a123SBarry Smith   PetscFunctionReturn(0);
27111713a123SBarry Smith }
27121713a123SBarry Smith 
27131713a123SBarry Smith 
27146c699258SBarry Smith #undef __FUNCT__
271583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
27166083293cSBarry Smith /*@C
271783a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
27186083293cSBarry Smith 
27196083293cSBarry Smith    Collective on TS
27206083293cSBarry Smith 
27216083293cSBarry Smith    Input Parameters:
27226083293cSBarry Smith .    ctx - the monitor context
27236083293cSBarry Smith 
27246083293cSBarry Smith    Level: intermediate
27256083293cSBarry Smith 
27266083293cSBarry Smith .keywords: TS,  vector, monitor, view
27276083293cSBarry Smith 
272883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
27296083293cSBarry Smith @*/
273083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
27316083293cSBarry Smith {
27326083293cSBarry Smith   PetscErrorCode       ierr;
27336083293cSBarry Smith 
27346083293cSBarry Smith   PetscFunctionBegin;
273583a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
273683a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
273783a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
27386083293cSBarry Smith   PetscFunctionReturn(0);
27396083293cSBarry Smith }
27406083293cSBarry Smith 
27416083293cSBarry Smith #undef __FUNCT__
274283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
27436083293cSBarry Smith /*@C
274483a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
27456083293cSBarry Smith 
27466083293cSBarry Smith    Collective on TS
27476083293cSBarry Smith 
27486083293cSBarry Smith    Input Parameter:
27496083293cSBarry Smith .    ts - time-step context
27506083293cSBarry Smith 
27516083293cSBarry Smith    Output Patameter:
27526083293cSBarry Smith .    ctx - the monitor context
27536083293cSBarry Smith 
27546083293cSBarry Smith    Level: intermediate
27556083293cSBarry Smith 
27566083293cSBarry Smith .keywords: TS,  vector, monitor, view
27576083293cSBarry Smith 
275883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
27596083293cSBarry Smith @*/
276083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
27616083293cSBarry Smith {
27626083293cSBarry Smith   PetscErrorCode   ierr;
27636083293cSBarry Smith 
27646083293cSBarry Smith   PetscFunctionBegin;
276583a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
276683a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
276783a4ac43SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
276883a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
276983a4ac43SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr);
27706083293cSBarry Smith   PetscFunctionReturn(0);
27716083293cSBarry Smith }
27726083293cSBarry Smith 
27736083293cSBarry Smith #undef __FUNCT__
277483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
27753a471f94SBarry Smith /*@C
277683a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
27773a471f94SBarry Smith    VecView() for the error at each timestep
27783a471f94SBarry Smith 
27793a471f94SBarry Smith    Collective on TS
27803a471f94SBarry Smith 
27813a471f94SBarry Smith    Input Parameters:
27823a471f94SBarry Smith +  ts - the TS context
27833a471f94SBarry Smith .  step - current time-step
27843a471f94SBarry Smith .  ptime - current time
27853a471f94SBarry Smith -  dummy - either a viewer or PETSC_NULL
27863a471f94SBarry Smith 
27873a471f94SBarry Smith    Level: intermediate
27883a471f94SBarry Smith 
27893a471f94SBarry Smith .keywords: TS,  vector, monitor, view
27903a471f94SBarry Smith 
27913a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
27923a471f94SBarry Smith @*/
279383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
27943a471f94SBarry Smith {
27953a471f94SBarry Smith   PetscErrorCode   ierr;
279683a4ac43SBarry Smith   TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy;
279783a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
27983a471f94SBarry Smith   Vec              work;
27993a471f94SBarry Smith 
28003a471f94SBarry Smith   PetscFunctionBegin;
2801*3fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
28023a471f94SBarry Smith   ierr = VecDuplicate(x,&work);CHKERRQ(ierr);
28033a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
28043a471f94SBarry Smith   ierr = VecAXPY(work,-1.0,x);CHKERRQ(ierr);
28053a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
28063a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
28073a471f94SBarry Smith   PetscFunctionReturn(0);
28083a471f94SBarry Smith }
28093a471f94SBarry Smith 
28103a471f94SBarry Smith #undef __FUNCT__
28116c699258SBarry Smith #define __FUNCT__ "TSSetDM"
28126c699258SBarry Smith /*@
28136c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
28146c699258SBarry Smith 
28153f9fe445SBarry Smith    Logically Collective on TS and DM
28166c699258SBarry Smith 
28176c699258SBarry Smith    Input Parameters:
28186c699258SBarry Smith +  ts - the preconditioner context
28196c699258SBarry Smith -  dm - the dm
28206c699258SBarry Smith 
28216c699258SBarry Smith    Level: intermediate
28226c699258SBarry Smith 
28236c699258SBarry Smith 
28246c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
28256c699258SBarry Smith @*/
28267087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
28276c699258SBarry Smith {
28286c699258SBarry Smith   PetscErrorCode ierr;
2829089b2837SJed Brown   SNES           snes;
283024989b8cSPeter Brune   TSDM           tsdm;
28316c699258SBarry Smith 
28326c699258SBarry Smith   PetscFunctionBegin;
28330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
283470663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
283524989b8cSPeter Brune   if (ts->dm) {               /* Move the TSDM context over to the new DM unless the new DM already has one */
283624989b8cSPeter Brune     PetscContainer oldcontainer,container;
283724989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr);
283824989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr);
283924989b8cSPeter Brune     if (oldcontainer && !container) {
284024989b8cSPeter Brune       ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr);
284124989b8cSPeter Brune       ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr);
284224989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
284324989b8cSPeter Brune         tsdm->originaldm = dm;
284424989b8cSPeter Brune       }
284524989b8cSPeter Brune     }
28466bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
284724989b8cSPeter Brune   }
28486c699258SBarry Smith   ts->dm = dm;
2849089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2850089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
28516c699258SBarry Smith   PetscFunctionReturn(0);
28526c699258SBarry Smith }
28536c699258SBarry Smith 
28546c699258SBarry Smith #undef __FUNCT__
28556c699258SBarry Smith #define __FUNCT__ "TSGetDM"
28566c699258SBarry Smith /*@
28576c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
28586c699258SBarry Smith 
28593f9fe445SBarry Smith    Not Collective
28606c699258SBarry Smith 
28616c699258SBarry Smith    Input Parameter:
28626c699258SBarry Smith . ts - the preconditioner context
28636c699258SBarry Smith 
28646c699258SBarry Smith    Output Parameter:
28656c699258SBarry Smith .  dm - the dm
28666c699258SBarry Smith 
28676c699258SBarry Smith    Level: intermediate
28686c699258SBarry Smith 
28696c699258SBarry Smith 
28706c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
28716c699258SBarry Smith @*/
28727087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
28736c699258SBarry Smith {
2874496e6a7aSJed Brown   PetscErrorCode ierr;
2875496e6a7aSJed Brown 
28766c699258SBarry Smith   PetscFunctionBegin;
28770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2878496e6a7aSJed Brown   if (!ts->dm) {
2879496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
2880496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
2881496e6a7aSJed Brown   }
28826c699258SBarry Smith   *dm = ts->dm;
28836c699258SBarry Smith   PetscFunctionReturn(0);
28846c699258SBarry Smith }
28851713a123SBarry Smith 
28860f5c6efeSJed Brown #undef __FUNCT__
28870f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
28880f5c6efeSJed Brown /*@
28890f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
28900f5c6efeSJed Brown 
28913f9fe445SBarry Smith    Logically Collective on SNES
28920f5c6efeSJed Brown 
28930f5c6efeSJed Brown    Input Parameter:
2894d42a1c89SJed Brown + snes - nonlinear solver
28950f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2896d42a1c89SJed Brown - ctx - user context, must be a TS
28970f5c6efeSJed Brown 
28980f5c6efeSJed Brown    Output Parameter:
28990f5c6efeSJed Brown . F - the nonlinear residual
29000f5c6efeSJed Brown 
29010f5c6efeSJed Brown    Notes:
29020f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
29030f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
29040f5c6efeSJed Brown 
29050f5c6efeSJed Brown    Level: advanced
29060f5c6efeSJed Brown 
29070f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
29080f5c6efeSJed Brown @*/
29097087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
29100f5c6efeSJed Brown {
29110f5c6efeSJed Brown   TS ts = (TS)ctx;
29120f5c6efeSJed Brown   PetscErrorCode ierr;
29130f5c6efeSJed Brown 
29140f5c6efeSJed Brown   PetscFunctionBegin;
29150f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
29160f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
29170f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
29180f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
29190f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
29200f5c6efeSJed Brown   PetscFunctionReturn(0);
29210f5c6efeSJed Brown }
29220f5c6efeSJed Brown 
29230f5c6efeSJed Brown #undef __FUNCT__
29240f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
29250f5c6efeSJed Brown /*@
29260f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
29270f5c6efeSJed Brown 
29280f5c6efeSJed Brown    Collective on SNES
29290f5c6efeSJed Brown 
29300f5c6efeSJed Brown    Input Parameter:
29310f5c6efeSJed Brown + snes - nonlinear solver
29320f5c6efeSJed Brown . X - the current state at which to evaluate the residual
29330f5c6efeSJed Brown - ctx - user context, must be a TS
29340f5c6efeSJed Brown 
29350f5c6efeSJed Brown    Output Parameter:
29360f5c6efeSJed Brown + A - the Jacobian
29370f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
29380f5c6efeSJed Brown - flag - indicates any structure change in the matrix
29390f5c6efeSJed Brown 
29400f5c6efeSJed Brown    Notes:
29410f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
29420f5c6efeSJed Brown 
29430f5c6efeSJed Brown    Level: developer
29440f5c6efeSJed Brown 
29450f5c6efeSJed Brown .seealso: SNESSetJacobian()
29460f5c6efeSJed Brown @*/
29477087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
29480f5c6efeSJed Brown {
29490f5c6efeSJed Brown   TS ts = (TS)ctx;
29500f5c6efeSJed Brown   PetscErrorCode ierr;
29510f5c6efeSJed Brown 
29520f5c6efeSJed Brown   PetscFunctionBegin;
29530f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
29540f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
29550f5c6efeSJed Brown   PetscValidPointer(A,3);
29560f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
29570f5c6efeSJed Brown   PetscValidPointer(B,4);
29580f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
29590f5c6efeSJed Brown   PetscValidPointer(flag,5);
29600f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
29610f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
29620f5c6efeSJed Brown   PetscFunctionReturn(0);
29630f5c6efeSJed Brown }
2964325fc9f4SBarry Smith 
29650e4ef248SJed Brown #undef __FUNCT__
29660e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
29670e4ef248SJed Brown /*@C
29680e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
29690e4ef248SJed Brown 
29700e4ef248SJed Brown    Collective on TS
29710e4ef248SJed Brown 
29720e4ef248SJed Brown    Input Arguments:
29730e4ef248SJed Brown +  ts - time stepping context
29740e4ef248SJed Brown .  t - time at which to evaluate
29750e4ef248SJed Brown .  X - state at which to evaluate
29760e4ef248SJed Brown -  ctx - context
29770e4ef248SJed Brown 
29780e4ef248SJed Brown    Output Arguments:
29790e4ef248SJed Brown .  F - right hand side
29800e4ef248SJed Brown 
29810e4ef248SJed Brown    Level: intermediate
29820e4ef248SJed Brown 
29830e4ef248SJed Brown    Notes:
29840e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
29850e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
29860e4ef248SJed Brown 
29870e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
29880e4ef248SJed Brown @*/
29890e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx)
29900e4ef248SJed Brown {
29910e4ef248SJed Brown   PetscErrorCode ierr;
29920e4ef248SJed Brown   Mat Arhs,Brhs;
29930e4ef248SJed Brown   MatStructure flg2;
29940e4ef248SJed Brown 
29950e4ef248SJed Brown   PetscFunctionBegin;
29960e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
29970e4ef248SJed Brown   ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
29980e4ef248SJed Brown   ierr = MatMult(Arhs,X,F);CHKERRQ(ierr);
29990e4ef248SJed Brown   PetscFunctionReturn(0);
30000e4ef248SJed Brown }
30010e4ef248SJed Brown 
30020e4ef248SJed Brown #undef __FUNCT__
30030e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
30040e4ef248SJed Brown /*@C
30050e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
30060e4ef248SJed Brown 
30070e4ef248SJed Brown    Collective on TS
30080e4ef248SJed Brown 
30090e4ef248SJed Brown    Input Arguments:
30100e4ef248SJed Brown +  ts - time stepping context
30110e4ef248SJed Brown .  t - time at which to evaluate
30120e4ef248SJed Brown .  X - state at which to evaluate
30130e4ef248SJed Brown -  ctx - context
30140e4ef248SJed Brown 
30150e4ef248SJed Brown    Output Arguments:
30160e4ef248SJed Brown +  A - pointer to operator
30170e4ef248SJed Brown .  B - pointer to preconditioning matrix
30180e4ef248SJed Brown -  flg - matrix structure flag
30190e4ef248SJed Brown 
30200e4ef248SJed Brown    Level: intermediate
30210e4ef248SJed Brown 
30220e4ef248SJed Brown    Notes:
30230e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
30240e4ef248SJed Brown 
30250e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
30260e4ef248SJed Brown @*/
30270e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx)
30280e4ef248SJed Brown {
30290e4ef248SJed Brown 
30300e4ef248SJed Brown   PetscFunctionBegin;
30310e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
30320e4ef248SJed Brown   PetscFunctionReturn(0);
30330e4ef248SJed Brown }
30340e4ef248SJed Brown 
30350026cea9SSean Farley #undef __FUNCT__
30360026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
30370026cea9SSean Farley /*@C
30380026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
30390026cea9SSean Farley 
30400026cea9SSean Farley    Collective on TS
30410026cea9SSean Farley 
30420026cea9SSean Farley    Input Arguments:
30430026cea9SSean Farley +  ts - time stepping context
30440026cea9SSean Farley .  t - time at which to evaluate
30450026cea9SSean Farley .  X - state at which to evaluate
30460026cea9SSean Farley .  Xdot - time derivative of state vector
30470026cea9SSean Farley -  ctx - context
30480026cea9SSean Farley 
30490026cea9SSean Farley    Output Arguments:
30500026cea9SSean Farley .  F - left hand side
30510026cea9SSean Farley 
30520026cea9SSean Farley    Level: intermediate
30530026cea9SSean Farley 
30540026cea9SSean Farley    Notes:
30550026cea9SSean Farley    The assumption here is that the left hand side is of the form A*Xdot (and not A*Xdot + B*X). For other cases, the
30560026cea9SSean Farley    user is required to write their own TSComputeIFunction.
30570026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
30580026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
30590026cea9SSean Farley 
30600026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
30610026cea9SSean Farley @*/
30620026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx)
30630026cea9SSean Farley {
30640026cea9SSean Farley   PetscErrorCode ierr;
30650026cea9SSean Farley   Mat A,B;
30660026cea9SSean Farley   MatStructure flg2;
30670026cea9SSean Farley 
30680026cea9SSean Farley   PetscFunctionBegin;
30690026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
30700026cea9SSean Farley   ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
30710026cea9SSean Farley   ierr = MatMult(A,Xdot,F);CHKERRQ(ierr);
30720026cea9SSean Farley   PetscFunctionReturn(0);
30730026cea9SSean Farley }
30740026cea9SSean Farley 
30750026cea9SSean Farley #undef __FUNCT__
30760026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
30770026cea9SSean Farley /*@C
307824e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
30790026cea9SSean Farley 
30800026cea9SSean Farley    Collective on TS
30810026cea9SSean Farley 
30820026cea9SSean Farley    Input Arguments:
30830026cea9SSean Farley +  ts - time stepping context
30840026cea9SSean Farley .  t - time at which to evaluate
30850026cea9SSean Farley .  X - state at which to evaluate
30860026cea9SSean Farley .  Xdot - time derivative of state vector
30870026cea9SSean Farley .  shift - shift to apply
30880026cea9SSean Farley -  ctx - context
30890026cea9SSean Farley 
30900026cea9SSean Farley    Output Arguments:
30910026cea9SSean Farley +  A - pointer to operator
30920026cea9SSean Farley .  B - pointer to preconditioning matrix
30930026cea9SSean Farley -  flg - matrix structure flag
30940026cea9SSean Farley 
30950026cea9SSean Farley    Level: intermediate
30960026cea9SSean Farley 
30970026cea9SSean Farley    Notes:
30980026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
30990026cea9SSean Farley 
31000026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
31010026cea9SSean Farley @*/
31020026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
31030026cea9SSean Farley {
31040026cea9SSean Farley 
31050026cea9SSean Farley   PetscFunctionBegin;
31060026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
31070026cea9SSean Farley   PetscFunctionReturn(0);
31080026cea9SSean Farley }
31090026cea9SSean Farley 
31104af1b03aSJed Brown 
31114af1b03aSJed Brown #undef __FUNCT__
31124af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
31134af1b03aSJed Brown /*@
31144af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
31154af1b03aSJed Brown 
31164af1b03aSJed Brown    Not Collective
31174af1b03aSJed Brown 
31184af1b03aSJed Brown    Input Parameter:
31194af1b03aSJed Brown .  ts - the TS context
31204af1b03aSJed Brown 
31214af1b03aSJed Brown    Output Parameter:
31224af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
31234af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
31244af1b03aSJed Brown 
31254af1b03aSJed Brown    Level: intermediate
31264af1b03aSJed Brown 
3127cd652676SJed Brown    Notes:
3128cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
31294af1b03aSJed Brown 
31304af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
31314af1b03aSJed Brown 
31324af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
31334af1b03aSJed Brown @*/
31344af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
31354af1b03aSJed Brown {
31364af1b03aSJed Brown   PetscFunctionBegin;
31374af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31384af1b03aSJed Brown   PetscValidPointer(reason,2);
31394af1b03aSJed Brown   *reason = ts->reason;
31404af1b03aSJed Brown   PetscFunctionReturn(0);
31414af1b03aSJed Brown }
31424af1b03aSJed Brown 
3143fb1732b5SBarry Smith #undef __FUNCT__
31445ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
31459f67acb7SJed Brown /*@
31465ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
31479f67acb7SJed Brown    used by the time integrator.
31489f67acb7SJed Brown 
31499f67acb7SJed Brown    Not Collective
31509f67acb7SJed Brown 
31519f67acb7SJed Brown    Input Parameter:
31529f67acb7SJed Brown .  ts - TS context
31539f67acb7SJed Brown 
31549f67acb7SJed Brown    Output Parameter:
31559f67acb7SJed Brown .  nits - number of nonlinear iterations
31569f67acb7SJed Brown 
31579f67acb7SJed Brown    Notes:
31589f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
31599f67acb7SJed Brown 
31609f67acb7SJed Brown    Level: intermediate
31619f67acb7SJed Brown 
31629f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
31639f67acb7SJed Brown 
31645ef26d82SJed Brown .seealso:  TSGetKSPIterations()
31659f67acb7SJed Brown @*/
31665ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
31679f67acb7SJed Brown {
31689f67acb7SJed Brown   PetscFunctionBegin;
31699f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31709f67acb7SJed Brown   PetscValidIntPointer(nits,2);
31715ef26d82SJed Brown   *nits = ts->snes_its;
31729f67acb7SJed Brown   PetscFunctionReturn(0);
31739f67acb7SJed Brown }
31749f67acb7SJed Brown 
31759f67acb7SJed Brown #undef __FUNCT__
31765ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
31779f67acb7SJed Brown /*@
31785ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
31799f67acb7SJed Brown    used by the time integrator.
31809f67acb7SJed Brown 
31819f67acb7SJed Brown    Not Collective
31829f67acb7SJed Brown 
31839f67acb7SJed Brown    Input Parameter:
31849f67acb7SJed Brown .  ts - TS context
31859f67acb7SJed Brown 
31869f67acb7SJed Brown    Output Parameter:
31879f67acb7SJed Brown .  lits - number of linear iterations
31889f67acb7SJed Brown 
31899f67acb7SJed Brown    Notes:
31909f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
31919f67acb7SJed Brown 
31929f67acb7SJed Brown    Level: intermediate
31939f67acb7SJed Brown 
31949f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
31959f67acb7SJed Brown 
31965ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
31979f67acb7SJed Brown @*/
31985ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
31999f67acb7SJed Brown {
32009f67acb7SJed Brown   PetscFunctionBegin;
32019f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32029f67acb7SJed Brown   PetscValidIntPointer(lits,2);
32035ef26d82SJed Brown   *lits = ts->ksp_its;
32049f67acb7SJed Brown   PetscFunctionReturn(0);
32059f67acb7SJed Brown }
32069f67acb7SJed Brown 
32079f67acb7SJed Brown #undef __FUNCT__
3208cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3209cef5090cSJed Brown /*@
3210cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3211cef5090cSJed Brown 
3212cef5090cSJed Brown    Not Collective
3213cef5090cSJed Brown 
3214cef5090cSJed Brown    Input Parameter:
3215cef5090cSJed Brown .  ts - TS context
3216cef5090cSJed Brown 
3217cef5090cSJed Brown    Output Parameter:
3218cef5090cSJed Brown .  rejects - number of steps rejected
3219cef5090cSJed Brown 
3220cef5090cSJed Brown    Notes:
3221cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3222cef5090cSJed Brown 
3223cef5090cSJed Brown    Level: intermediate
3224cef5090cSJed Brown 
3225cef5090cSJed Brown .keywords: TS, get, number
3226cef5090cSJed Brown 
32275ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3228cef5090cSJed Brown @*/
3229cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3230cef5090cSJed Brown {
3231cef5090cSJed Brown   PetscFunctionBegin;
3232cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3233cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3234cef5090cSJed Brown   *rejects = ts->reject;
3235cef5090cSJed Brown   PetscFunctionReturn(0);
3236cef5090cSJed Brown }
3237cef5090cSJed Brown 
3238cef5090cSJed Brown #undef __FUNCT__
3239cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3240cef5090cSJed Brown /*@
3241cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3242cef5090cSJed Brown 
3243cef5090cSJed Brown    Not Collective
3244cef5090cSJed Brown 
3245cef5090cSJed Brown    Input Parameter:
3246cef5090cSJed Brown .  ts - TS context
3247cef5090cSJed Brown 
3248cef5090cSJed Brown    Output Parameter:
3249cef5090cSJed Brown .  fails - number of failed nonlinear solves
3250cef5090cSJed Brown 
3251cef5090cSJed Brown    Notes:
3252cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3253cef5090cSJed Brown 
3254cef5090cSJed Brown    Level: intermediate
3255cef5090cSJed Brown 
3256cef5090cSJed Brown .keywords: TS, get, number
3257cef5090cSJed Brown 
32585ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3259cef5090cSJed Brown @*/
3260cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3261cef5090cSJed Brown {
3262cef5090cSJed Brown   PetscFunctionBegin;
3263cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3264cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3265cef5090cSJed Brown   *fails = ts->num_snes_failures;
3266cef5090cSJed Brown   PetscFunctionReturn(0);
3267cef5090cSJed Brown }
3268cef5090cSJed Brown 
3269cef5090cSJed Brown #undef __FUNCT__
3270cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3271cef5090cSJed Brown /*@
3272cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3273cef5090cSJed Brown 
3274cef5090cSJed Brown    Not Collective
3275cef5090cSJed Brown 
3276cef5090cSJed Brown    Input Parameter:
3277cef5090cSJed Brown +  ts - TS context
3278cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3279cef5090cSJed Brown 
3280cef5090cSJed Brown    Notes:
3281cef5090cSJed Brown    The counter is reset to zero for each step
3282cef5090cSJed Brown 
3283cef5090cSJed Brown    Options Database Key:
3284cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3285cef5090cSJed Brown 
3286cef5090cSJed Brown    Level: intermediate
3287cef5090cSJed Brown 
3288cef5090cSJed Brown .keywords: TS, set, maximum, number
3289cef5090cSJed Brown 
32905ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3291cef5090cSJed Brown @*/
3292cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3293cef5090cSJed Brown {
3294cef5090cSJed Brown   PetscFunctionBegin;
3295cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3296cef5090cSJed Brown   ts->max_reject = rejects;
3297cef5090cSJed Brown   PetscFunctionReturn(0);
3298cef5090cSJed Brown }
3299cef5090cSJed Brown 
3300cef5090cSJed Brown #undef __FUNCT__
3301cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3302cef5090cSJed Brown /*@
3303cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3304cef5090cSJed Brown 
3305cef5090cSJed Brown    Not Collective
3306cef5090cSJed Brown 
3307cef5090cSJed Brown    Input Parameter:
3308cef5090cSJed Brown +  ts - TS context
3309cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3310cef5090cSJed Brown 
3311cef5090cSJed Brown    Notes:
3312cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3313cef5090cSJed Brown 
3314cef5090cSJed Brown    Options Database Key:
3315cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3316cef5090cSJed Brown 
3317cef5090cSJed Brown    Level: intermediate
3318cef5090cSJed Brown 
3319cef5090cSJed Brown .keywords: TS, set, maximum, number
3320cef5090cSJed Brown 
33215ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3322cef5090cSJed Brown @*/
3323cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3324cef5090cSJed Brown {
3325cef5090cSJed Brown   PetscFunctionBegin;
3326cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3327cef5090cSJed Brown   ts->max_snes_failures = fails;
3328cef5090cSJed Brown   PetscFunctionReturn(0);
3329cef5090cSJed Brown }
3330cef5090cSJed Brown 
3331cef5090cSJed Brown #undef __FUNCT__
3332cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3333cef5090cSJed Brown /*@
3334cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3335cef5090cSJed Brown 
3336cef5090cSJed Brown    Not Collective
3337cef5090cSJed Brown 
3338cef5090cSJed Brown    Input Parameter:
3339cef5090cSJed Brown +  ts - TS context
3340cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3341cef5090cSJed Brown 
3342cef5090cSJed Brown    Options Database Key:
3343cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3344cef5090cSJed Brown 
3345cef5090cSJed Brown    Level: intermediate
3346cef5090cSJed Brown 
3347cef5090cSJed Brown .keywords: TS, set, error
3348cef5090cSJed Brown 
33495ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3350cef5090cSJed Brown @*/
3351cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3352cef5090cSJed Brown {
3353cef5090cSJed Brown   PetscFunctionBegin;
3354cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3355cef5090cSJed Brown   ts->errorifstepfailed = err;
3356cef5090cSJed Brown   PetscFunctionReturn(0);
3357cef5090cSJed Brown }
3358cef5090cSJed Brown 
3359cef5090cSJed Brown #undef __FUNCT__
3360fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3361fb1732b5SBarry Smith /*@C
3362fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3363fb1732b5SBarry Smith 
3364fb1732b5SBarry Smith    Collective on TS
3365fb1732b5SBarry Smith 
3366fb1732b5SBarry Smith    Input Parameters:
3367fb1732b5SBarry Smith +  ts - the TS context
3368fb1732b5SBarry Smith .  step - current time-step
3369fb1732b5SBarry Smith .  ptime - current time
3370ed81e22dSJed Brown .  x - current state
3371fb1732b5SBarry Smith -  viewer - binary viewer
3372fb1732b5SBarry Smith 
3373fb1732b5SBarry Smith    Level: intermediate
3374fb1732b5SBarry Smith 
3375fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3376fb1732b5SBarry Smith 
3377fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3378fb1732b5SBarry Smith @*/
3379ed81e22dSJed Brown PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec x,void *viewer)
3380fb1732b5SBarry Smith {
3381fb1732b5SBarry Smith   PetscErrorCode       ierr;
3382ed81e22dSJed Brown   PetscViewer          v = (PetscViewer)viewer;
3383fb1732b5SBarry Smith 
3384fb1732b5SBarry Smith   PetscFunctionBegin;
3385ed81e22dSJed Brown   ierr = VecView(x,v);CHKERRQ(ierr);
3386ed81e22dSJed Brown   PetscFunctionReturn(0);
3387ed81e22dSJed Brown }
3388ed81e22dSJed Brown 
3389ed81e22dSJed Brown #undef __FUNCT__
3390ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3391ed81e22dSJed Brown /*@C
3392ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3393ed81e22dSJed Brown 
3394ed81e22dSJed Brown    Collective on TS
3395ed81e22dSJed Brown 
3396ed81e22dSJed Brown    Input Parameters:
3397ed81e22dSJed Brown +  ts - the TS context
3398ed81e22dSJed Brown .  step - current time-step
3399ed81e22dSJed Brown .  ptime - current time
3400ed81e22dSJed Brown .  x - current state
3401ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3402ed81e22dSJed Brown 
3403ed81e22dSJed Brown    Level: intermediate
3404ed81e22dSJed Brown 
3405ed81e22dSJed Brown    Notes:
3406ed81e22dSJed 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.
3407ed81e22dSJed Brown    These are named according to the file name template.
3408ed81e22dSJed Brown 
3409ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3410ed81e22dSJed Brown 
3411ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3412ed81e22dSJed Brown 
3413ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3414ed81e22dSJed Brown @*/
3415ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec x,void *filenametemplate)
3416ed81e22dSJed Brown {
3417ed81e22dSJed Brown   PetscErrorCode ierr;
3418ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3419ed81e22dSJed Brown   PetscViewer    viewer;
3420ed81e22dSJed Brown 
3421ed81e22dSJed Brown   PetscFunctionBegin;
34228caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3423ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
3424fb1732b5SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
3425ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3426ed81e22dSJed Brown   PetscFunctionReturn(0);
3427ed81e22dSJed Brown }
3428ed81e22dSJed Brown 
3429ed81e22dSJed Brown #undef __FUNCT__
3430ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3431ed81e22dSJed Brown /*@C
3432ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3433ed81e22dSJed Brown 
3434ed81e22dSJed Brown    Collective on TS
3435ed81e22dSJed Brown 
3436ed81e22dSJed Brown    Input Parameters:
3437ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3438ed81e22dSJed Brown 
3439ed81e22dSJed Brown    Level: intermediate
3440ed81e22dSJed Brown 
3441ed81e22dSJed Brown    Note:
3442ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3443ed81e22dSJed Brown 
3444ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3445ed81e22dSJed Brown 
3446ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3447ed81e22dSJed Brown @*/
3448ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3449ed81e22dSJed Brown {
3450ed81e22dSJed Brown   PetscErrorCode ierr;
3451ed81e22dSJed Brown 
3452ed81e22dSJed Brown   PetscFunctionBegin;
3453ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3454fb1732b5SBarry Smith   PetscFunctionReturn(0);
3455fb1732b5SBarry Smith }
3456fb1732b5SBarry Smith 
345784df9cb4SJed Brown #undef __FUNCT__
345884df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt"
345984df9cb4SJed Brown /*@
346084df9cb4SJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
346184df9cb4SJed Brown 
3462ed81e22dSJed Brown    Collective on TS if controller has not been created yet
346384df9cb4SJed Brown 
346484df9cb4SJed Brown    Input Arguments:
3465ed81e22dSJed Brown .  ts - time stepping context
346684df9cb4SJed Brown 
346784df9cb4SJed Brown    Output Arguments:
3468ed81e22dSJed Brown .  adapt - adaptive controller
346984df9cb4SJed Brown 
347084df9cb4SJed Brown    Level: intermediate
347184df9cb4SJed Brown 
3472ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
347384df9cb4SJed Brown @*/
347484df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
347584df9cb4SJed Brown {
347684df9cb4SJed Brown   PetscErrorCode ierr;
347784df9cb4SJed Brown 
347884df9cb4SJed Brown   PetscFunctionBegin;
347984df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
348084df9cb4SJed Brown   PetscValidPointer(adapt,2);
348184df9cb4SJed Brown   if (!ts->adapt) {
348284df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
34831c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
34841c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
348584df9cb4SJed Brown   }
348684df9cb4SJed Brown   *adapt = ts->adapt;
348784df9cb4SJed Brown   PetscFunctionReturn(0);
348884df9cb4SJed Brown }
3489d6ebe24aSShri Abhyankar 
3490d6ebe24aSShri Abhyankar #undef __FUNCT__
34911c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
34921c3436cfSJed Brown /*@
34931c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
34941c3436cfSJed Brown 
34951c3436cfSJed Brown    Logically Collective
34961c3436cfSJed Brown 
34971c3436cfSJed Brown    Input Arguments:
34981c3436cfSJed Brown +  ts - time integration context
34991c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
35001c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
35011c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
35021c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
35031c3436cfSJed Brown 
35041c3436cfSJed Brown    Level: beginner
35051c3436cfSJed Brown 
3506c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
35071c3436cfSJed Brown @*/
35081c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
35091c3436cfSJed Brown {
35101c3436cfSJed Brown   PetscErrorCode ierr;
35111c3436cfSJed Brown 
35121c3436cfSJed Brown   PetscFunctionBegin;
3513c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
35141c3436cfSJed Brown   if (vatol) {
35151c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
35161c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
35171c3436cfSJed Brown     ts->vatol = vatol;
35181c3436cfSJed Brown   }
3519c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
35201c3436cfSJed Brown   if (vrtol) {
35211c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
35221c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
35231c3436cfSJed Brown     ts->vrtol = vrtol;
35241c3436cfSJed Brown   }
35251c3436cfSJed Brown   PetscFunctionReturn(0);
35261c3436cfSJed Brown }
35271c3436cfSJed Brown 
35281c3436cfSJed Brown #undef __FUNCT__
3529c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3530c5033834SJed Brown /*@
3531c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3532c5033834SJed Brown 
3533c5033834SJed Brown    Logically Collective
3534c5033834SJed Brown 
3535c5033834SJed Brown    Input Arguments:
3536c5033834SJed Brown .  ts - time integration context
3537c5033834SJed Brown 
3538c5033834SJed Brown    Output Arguments:
3539c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3540c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3541c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3542c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3543c5033834SJed Brown 
3544c5033834SJed Brown    Level: beginner
3545c5033834SJed Brown 
3546c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3547c5033834SJed Brown @*/
3548c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3549c5033834SJed Brown {
3550c5033834SJed Brown 
3551c5033834SJed Brown   PetscFunctionBegin;
3552c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3553c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3554c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3555c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3556c5033834SJed Brown   PetscFunctionReturn(0);
3557c5033834SJed Brown }
3558c5033834SJed Brown 
3559c5033834SJed Brown #undef __FUNCT__
35601c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
35611c3436cfSJed Brown /*@
35621c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
35631c3436cfSJed Brown 
35641c3436cfSJed Brown    Collective on TS
35651c3436cfSJed Brown 
35661c3436cfSJed Brown    Input Arguments:
35671c3436cfSJed Brown +  ts - time stepping context
35681c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
35691c3436cfSJed Brown 
35701c3436cfSJed Brown    Output Arguments:
35711c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
35721c3436cfSJed Brown 
35731c3436cfSJed Brown    Level: developer
35741c3436cfSJed Brown 
35751c3436cfSJed Brown .seealso: TSSetTolerances()
35761c3436cfSJed Brown @*/
35771c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
35781c3436cfSJed Brown {
35791c3436cfSJed Brown   PetscErrorCode ierr;
35801c3436cfSJed Brown   PetscInt i,n,N;
35811c3436cfSJed Brown   const PetscScalar *x,*y;
35821c3436cfSJed Brown   Vec X;
35831c3436cfSJed Brown   PetscReal sum,gsum;
35841c3436cfSJed Brown 
35851c3436cfSJed Brown   PetscFunctionBegin;
35861c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35871c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
35881c3436cfSJed Brown   PetscValidPointer(norm,3);
35891c3436cfSJed Brown   X = ts->vec_sol;
35901c3436cfSJed Brown   PetscCheckSameTypeAndComm(X,1,Y,2);
35911c3436cfSJed Brown   if (X == Y) SETERRQ(((PetscObject)X)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
35921c3436cfSJed Brown 
35931c3436cfSJed Brown   ierr = VecGetSize(X,&N);CHKERRQ(ierr);
35941c3436cfSJed Brown   ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr);
35951c3436cfSJed Brown   ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr);
35961c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
35971c3436cfSJed Brown   sum = 0.;
3598ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3599ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3600ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3601ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3602ceefc113SJed Brown     for (i=0; i<n; i++) {
3603ceefc113SJed Brown       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3604ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3605ceefc113SJed Brown     }
3606ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3607ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3608ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3609ceefc113SJed Brown     const PetscScalar *atol;
3610ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3611ceefc113SJed Brown     for (i=0; i<n; i++) {
3612ceefc113SJed Brown       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3613ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3614ceefc113SJed Brown     }
3615ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3616ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3617ceefc113SJed Brown     const PetscScalar *rtol;
3618ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3619ceefc113SJed Brown     for (i=0; i<n; i++) {
3620ceefc113SJed Brown       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3621ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3622ceefc113SJed Brown     }
3623ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3624ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
36251c3436cfSJed Brown     for (i=0; i<n; i++) {
36261c3436cfSJed Brown       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
36271c3436cfSJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
36281c3436cfSJed Brown     }
3629ceefc113SJed Brown   }
36301c3436cfSJed Brown   ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr);
36311c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
36321c3436cfSJed Brown 
36331c3436cfSJed Brown   ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
36341c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
36351c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
36361c3436cfSJed Brown   PetscFunctionReturn(0);
36371c3436cfSJed Brown }
36381c3436cfSJed Brown 
36391c3436cfSJed Brown #undef __FUNCT__
36408d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
36418d59e960SJed Brown /*@
36428d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
36438d59e960SJed Brown 
36448d59e960SJed Brown    Logically Collective on TS
36458d59e960SJed Brown 
36468d59e960SJed Brown    Input Arguments:
36478d59e960SJed Brown +  ts - time stepping context
36488d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
36498d59e960SJed Brown 
36508d59e960SJed Brown    Note:
36518d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
36528d59e960SJed Brown 
36538d59e960SJed Brown    Level: intermediate
36548d59e960SJed Brown 
36558d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
36568d59e960SJed Brown @*/
36578d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
36588d59e960SJed Brown {
36598d59e960SJed Brown 
36608d59e960SJed Brown   PetscFunctionBegin;
36618d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36628d59e960SJed Brown   ts->cfltime_local = cfltime;
36638d59e960SJed Brown   ts->cfltime = -1.;
36648d59e960SJed Brown   PetscFunctionReturn(0);
36658d59e960SJed Brown }
36668d59e960SJed Brown 
36678d59e960SJed Brown #undef __FUNCT__
36688d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
36698d59e960SJed Brown /*@
36708d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
36718d59e960SJed Brown 
36728d59e960SJed Brown    Collective on TS
36738d59e960SJed Brown 
36748d59e960SJed Brown    Input Arguments:
36758d59e960SJed Brown .  ts - time stepping context
36768d59e960SJed Brown 
36778d59e960SJed Brown    Output Arguments:
36788d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
36798d59e960SJed Brown 
36808d59e960SJed Brown    Level: advanced
36818d59e960SJed Brown 
36828d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
36838d59e960SJed Brown @*/
36848d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
36858d59e960SJed Brown {
36868d59e960SJed Brown   PetscErrorCode ierr;
36878d59e960SJed Brown 
36888d59e960SJed Brown   PetscFunctionBegin;
36898d59e960SJed Brown   if (ts->cfltime < 0) {
36908d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
36918d59e960SJed Brown   }
36928d59e960SJed Brown   *cfltime = ts->cfltime;
36938d59e960SJed Brown   PetscFunctionReturn(0);
36948d59e960SJed Brown }
36958d59e960SJed Brown 
36968d59e960SJed Brown #undef __FUNCT__
3697d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
3698d6ebe24aSShri Abhyankar /*@
3699d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
3700d6ebe24aSShri Abhyankar 
3701d6ebe24aSShri Abhyankar    Input Parameters:
3702d6ebe24aSShri Abhyankar .  ts   - the TS context.
3703d6ebe24aSShri Abhyankar .  xl   - lower bound.
3704d6ebe24aSShri Abhyankar .  xu   - upper bound.
3705d6ebe24aSShri Abhyankar 
3706d6ebe24aSShri Abhyankar    Notes:
3707d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
370833c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
3709d6ebe24aSShri Abhyankar 
37102bd2b0e6SSatish Balay    Level: advanced
37112bd2b0e6SSatish Balay 
3712d6ebe24aSShri Abhyankar @*/
3713d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
3714d6ebe24aSShri Abhyankar {
3715d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
3716d6ebe24aSShri Abhyankar   SNES           snes;
3717d6ebe24aSShri Abhyankar 
3718d6ebe24aSShri Abhyankar   PetscFunctionBegin;
3719d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3720d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
3721d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
3722d6ebe24aSShri Abhyankar }
3723d6ebe24aSShri Abhyankar 
3724325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3725c6db04a5SJed Brown #include <mex.h>
3726325fc9f4SBarry Smith 
3727325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
3728325fc9f4SBarry Smith 
3729325fc9f4SBarry Smith #undef __FUNCT__
3730325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
3731325fc9f4SBarry Smith /*
3732325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
3733325fc9f4SBarry Smith                          TSSetFunctionMatlab().
3734325fc9f4SBarry Smith 
3735325fc9f4SBarry Smith    Collective on TS
3736325fc9f4SBarry Smith 
3737325fc9f4SBarry Smith    Input Parameters:
3738325fc9f4SBarry Smith +  snes - the TS context
3739325fc9f4SBarry Smith -  x - input vector
3740325fc9f4SBarry Smith 
3741325fc9f4SBarry Smith    Output Parameter:
3742325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
3743325fc9f4SBarry Smith 
3744325fc9f4SBarry Smith    Notes:
3745325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
3746325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
3747325fc9f4SBarry Smith    themselves.
3748325fc9f4SBarry Smith 
3749325fc9f4SBarry Smith    Level: developer
3750325fc9f4SBarry Smith 
3751325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3752325fc9f4SBarry Smith 
3753325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3754325fc9f4SBarry Smith */
37557087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
3756325fc9f4SBarry Smith {
3757325fc9f4SBarry Smith   PetscErrorCode   ierr;
3758325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3759325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
3760325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
3761325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
3762325fc9f4SBarry Smith 
3763325fc9f4SBarry Smith   PetscFunctionBegin;
3764325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
3765325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3766325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
3767325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
3768325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
3769325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
3770325fc9f4SBarry Smith 
3771325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3772325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3773880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
3774325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
3775325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3776325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
3777325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3778325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3779325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
3780325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
3781325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
3782325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
3783325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3784325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3785325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3786325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3787325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3788325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3789325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3790325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3791325fc9f4SBarry Smith   PetscFunctionReturn(0);
3792325fc9f4SBarry Smith }
3793325fc9f4SBarry Smith 
3794325fc9f4SBarry Smith 
3795325fc9f4SBarry Smith #undef __FUNCT__
3796325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
3797325fc9f4SBarry Smith /*
3798325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
3799325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
3800e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
3801325fc9f4SBarry Smith 
3802325fc9f4SBarry Smith    Logically Collective on TS
3803325fc9f4SBarry Smith 
3804325fc9f4SBarry Smith    Input Parameters:
3805325fc9f4SBarry Smith +  ts - the TS context
3806325fc9f4SBarry Smith -  func - function evaluation routine
3807325fc9f4SBarry Smith 
3808325fc9f4SBarry Smith    Calling sequence of func:
3809325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
3810325fc9f4SBarry Smith 
3811325fc9f4SBarry Smith    Level: beginner
3812325fc9f4SBarry Smith 
3813325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3814325fc9f4SBarry Smith 
3815325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3816325fc9f4SBarry Smith */
3817cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
3818325fc9f4SBarry Smith {
3819325fc9f4SBarry Smith   PetscErrorCode  ierr;
3820325fc9f4SBarry Smith   TSMatlabContext *sctx;
3821325fc9f4SBarry Smith 
3822325fc9f4SBarry Smith   PetscFunctionBegin;
3823325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3824325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3825325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3826325fc9f4SBarry Smith   /*
3827325fc9f4SBarry Smith      This should work, but it doesn't
3828325fc9f4SBarry Smith   sctx->ctx = ctx;
3829325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3830325fc9f4SBarry Smith   */
3831325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3832cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
3833325fc9f4SBarry Smith   PetscFunctionReturn(0);
3834325fc9f4SBarry Smith }
3835325fc9f4SBarry Smith 
3836325fc9f4SBarry Smith #undef __FUNCT__
3837325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
3838325fc9f4SBarry Smith /*
3839325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
3840325fc9f4SBarry Smith                          TSSetJacobianMatlab().
3841325fc9f4SBarry Smith 
3842325fc9f4SBarry Smith    Collective on TS
3843325fc9f4SBarry Smith 
3844325fc9f4SBarry Smith    Input Parameters:
3845cdcf91faSSean Farley +  ts - the TS context
3846325fc9f4SBarry Smith .  x - input vector
3847325fc9f4SBarry Smith .  A, B - the matrices
3848325fc9f4SBarry Smith -  ctx - user context
3849325fc9f4SBarry Smith 
3850325fc9f4SBarry Smith    Output Parameter:
3851325fc9f4SBarry Smith .  flag - structure of the matrix
3852325fc9f4SBarry Smith 
3853325fc9f4SBarry Smith    Level: developer
3854325fc9f4SBarry Smith 
3855325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3856325fc9f4SBarry Smith 
3857325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3858325fc9f4SBarry Smith @*/
3859cdcf91faSSean Farley PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
3860325fc9f4SBarry Smith {
3861325fc9f4SBarry Smith   PetscErrorCode  ierr;
3862325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3863325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
3864325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
3865325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
3866325fc9f4SBarry Smith 
3867325fc9f4SBarry Smith   PetscFunctionBegin;
3868cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3869325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3870325fc9f4SBarry Smith 
3871325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
3872325fc9f4SBarry Smith 
3873cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3874325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3875325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
3876325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
3877325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
3878325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3879325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
3880325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3881325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3882325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
3883325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
3884325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
3885325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
3886325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
3887325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
3888325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3889325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
3890325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3891325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3892325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3893325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3894325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3895325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3896325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
3897325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
3898325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3899325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
3900325fc9f4SBarry Smith   PetscFunctionReturn(0);
3901325fc9f4SBarry Smith }
3902325fc9f4SBarry Smith 
3903325fc9f4SBarry Smith 
3904325fc9f4SBarry Smith #undef __FUNCT__
3905325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
3906325fc9f4SBarry Smith /*
3907325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
3908e3c5b3baSBarry 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
3909325fc9f4SBarry Smith 
3910325fc9f4SBarry Smith    Logically Collective on TS
3911325fc9f4SBarry Smith 
3912325fc9f4SBarry Smith    Input Parameters:
3913cdcf91faSSean Farley +  ts - the TS context
3914325fc9f4SBarry Smith .  A,B - Jacobian matrices
3915325fc9f4SBarry Smith .  func - function evaluation routine
3916325fc9f4SBarry Smith -  ctx - user context
3917325fc9f4SBarry Smith 
3918325fc9f4SBarry Smith    Calling sequence of func:
3919cdcf91faSSean Farley $    flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
3920325fc9f4SBarry Smith 
3921325fc9f4SBarry Smith 
3922325fc9f4SBarry Smith    Level: developer
3923325fc9f4SBarry Smith 
3924325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3925325fc9f4SBarry Smith 
3926325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3927325fc9f4SBarry Smith */
3928cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
3929325fc9f4SBarry Smith {
3930325fc9f4SBarry Smith   PetscErrorCode    ierr;
3931325fc9f4SBarry Smith   TSMatlabContext *sctx;
3932325fc9f4SBarry Smith 
3933325fc9f4SBarry Smith   PetscFunctionBegin;
3934325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3935325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3936325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3937325fc9f4SBarry Smith   /*
3938325fc9f4SBarry Smith      This should work, but it doesn't
3939325fc9f4SBarry Smith   sctx->ctx = ctx;
3940325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3941325fc9f4SBarry Smith   */
3942325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3943cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
3944325fc9f4SBarry Smith   PetscFunctionReturn(0);
3945325fc9f4SBarry Smith }
3946325fc9f4SBarry Smith 
3947b5b1a830SBarry Smith #undef __FUNCT__
3948b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
3949b5b1a830SBarry Smith /*
3950b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
3951b5b1a830SBarry Smith 
3952b5b1a830SBarry Smith    Collective on TS
3953b5b1a830SBarry Smith 
3954b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3955b5b1a830SBarry Smith @*/
3956cdcf91faSSean Farley PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx)
3957b5b1a830SBarry Smith {
3958b5b1a830SBarry Smith   PetscErrorCode  ierr;
3959b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3960a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
3961b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
3962b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
3963b5b1a830SBarry Smith 
3964b5b1a830SBarry Smith   PetscFunctionBegin;
3965cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3966b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
3967b5b1a830SBarry Smith 
3968cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3969b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3970b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3971b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
3972b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
3973b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
3974b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
3975b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
3976b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
3977b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3978b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
3979b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
3980b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
3981b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
3982b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
3983b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
3984b5b1a830SBarry Smith   PetscFunctionReturn(0);
3985b5b1a830SBarry Smith }
3986b5b1a830SBarry Smith 
3987b5b1a830SBarry Smith 
3988b5b1a830SBarry Smith #undef __FUNCT__
3989b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
3990b5b1a830SBarry Smith /*
3991b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
3992b5b1a830SBarry Smith 
3993b5b1a830SBarry Smith    Level: developer
3994b5b1a830SBarry Smith 
3995b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
3996b5b1a830SBarry Smith 
3997b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3998b5b1a830SBarry Smith */
3999cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4000b5b1a830SBarry Smith {
4001b5b1a830SBarry Smith   PetscErrorCode    ierr;
4002b5b1a830SBarry Smith   TSMatlabContext *sctx;
4003b5b1a830SBarry Smith 
4004b5b1a830SBarry Smith   PetscFunctionBegin;
4005b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4006b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4007b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4008b5b1a830SBarry Smith   /*
4009b5b1a830SBarry Smith      This should work, but it doesn't
4010b5b1a830SBarry Smith   sctx->ctx = ctx;
4011b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4012b5b1a830SBarry Smith   */
4013b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4014cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
4015b5b1a830SBarry Smith   PetscFunctionReturn(0);
4016b5b1a830SBarry Smith }
4017325fc9f4SBarry Smith #endif
4018b3603a34SBarry Smith 
40190b039ecaSBarry Smith 
40200b039ecaSBarry Smith 
4021b3603a34SBarry Smith #undef __FUNCT__
40224f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4023b3603a34SBarry Smith /*@C
40244f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4025b3603a34SBarry Smith        in a time based line graph
4026b3603a34SBarry Smith 
4027b3603a34SBarry Smith    Collective on TS
4028b3603a34SBarry Smith 
4029b3603a34SBarry Smith    Input Parameters:
4030b3603a34SBarry Smith +  ts - the TS context
4031b3603a34SBarry Smith .  step - current time-step
4032b3603a34SBarry Smith .  ptime - current time
4033b3603a34SBarry Smith -  lg - a line graph object
4034b3603a34SBarry Smith 
4035b3603a34SBarry Smith    Level: intermediate
4036b3603a34SBarry Smith 
40370b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4038b3603a34SBarry Smith 
4039b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4040b3603a34SBarry Smith 
4041b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4042b3603a34SBarry Smith @*/
40434f09c107SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
4044b3603a34SBarry Smith {
4045b3603a34SBarry Smith   PetscErrorCode    ierr;
40460b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4047b3603a34SBarry Smith   const PetscScalar *yy;
404858ff32f7SBarry Smith   PetscInt          dim;
4049b3603a34SBarry Smith 
4050b3603a34SBarry Smith   PetscFunctionBegin;
405158ff32f7SBarry Smith   if (!step) {
4052a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4053a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4054a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
405558ff32f7SBarry Smith     ierr = VecGetLocalSize(x,&dim);CHKERRQ(ierr);
40560b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
40570b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
405858ff32f7SBarry Smith   }
4059b3603a34SBarry Smith   ierr = VecGetArrayRead(x,&yy);CHKERRQ(ierr);
4060e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4061e3efe391SJed Brown   {
4062e3efe391SJed Brown     PetscReal *yreal;
4063e3efe391SJed Brown     PetscInt i,n;
4064e3efe391SJed Brown     ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
4065e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4066e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
40670b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4068e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4069e3efe391SJed Brown   }
4070e3efe391SJed Brown #else
40710b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4072e3efe391SJed Brown #endif
4073b3603a34SBarry Smith   ierr = VecRestoreArrayRead(x,&yy);CHKERRQ(ierr);
4074*3fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
40750b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
40763923b477SBarry Smith   }
4077b3603a34SBarry Smith   PetscFunctionReturn(0);
4078b3603a34SBarry Smith }
4079b3603a34SBarry Smith 
4080b3603a34SBarry Smith #undef __FUNCT__
40814f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4082ef20d060SBarry Smith /*@C
40834f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4084ef20d060SBarry Smith        in a time based line graph
4085ef20d060SBarry Smith 
4086ef20d060SBarry Smith    Collective on TS
4087ef20d060SBarry Smith 
4088ef20d060SBarry Smith    Input Parameters:
4089ef20d060SBarry Smith +  ts - the TS context
4090ef20d060SBarry Smith .  step - current time-step
4091ef20d060SBarry Smith .  ptime - current time
4092ef20d060SBarry Smith -  lg - a line graph object
4093ef20d060SBarry Smith 
4094ef20d060SBarry Smith    Level: intermediate
4095ef20d060SBarry Smith 
4096abd5a294SJed Brown    Notes:
4097abd5a294SJed Brown    Only for sequential solves.
4098abd5a294SJed Brown 
4099abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4100abd5a294SJed Brown 
4101abd5a294SJed Brown    Options Database Keys:
41024f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4103ef20d060SBarry Smith 
4104ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4105ef20d060SBarry Smith 
4106abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4107ef20d060SBarry Smith @*/
41084f09c107SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
4109ef20d060SBarry Smith {
4110ef20d060SBarry Smith   PetscErrorCode    ierr;
41110b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4112ef20d060SBarry Smith   const PetscScalar *yy;
4113ef20d060SBarry Smith   Vec               y;
4114a9f9c1f6SBarry Smith   PetscInt          dim;
4115ef20d060SBarry Smith 
4116ef20d060SBarry Smith   PetscFunctionBegin;
4117a9f9c1f6SBarry Smith   if (!step) {
4118a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4119a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4120a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
4121a9f9c1f6SBarry Smith     ierr = VecGetLocalSize(x,&dim);CHKERRQ(ierr);
4122a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4123a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4124a9f9c1f6SBarry Smith   }
4125ef20d060SBarry Smith   ierr = VecDuplicate(x,&y);CHKERRQ(ierr);
4126ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
4127ef20d060SBarry Smith   ierr = VecAXPY(y,-1.0,x);CHKERRQ(ierr);
4128ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4129e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4130e3efe391SJed Brown   {
4131e3efe391SJed Brown     PetscReal *yreal;
4132e3efe391SJed Brown     PetscInt  i,n;
4133e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4134e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4135e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
41360b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4137e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4138e3efe391SJed Brown   }
4139e3efe391SJed Brown #else
41400b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4141e3efe391SJed Brown #endif
4142ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4143ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
4144*3fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
41450b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
41463923b477SBarry Smith   }
4147ef20d060SBarry Smith   PetscFunctionReturn(0);
4148ef20d060SBarry Smith }
4149ef20d060SBarry Smith 
4150201da799SBarry Smith #undef __FUNCT__
4151201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4152201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4153201da799SBarry Smith {
4154201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4155201da799SBarry Smith   PetscReal      x = ptime,y;
4156201da799SBarry Smith   PetscErrorCode ierr;
4157201da799SBarry Smith   PetscInt       its;
4158201da799SBarry Smith 
4159201da799SBarry Smith   PetscFunctionBegin;
4160201da799SBarry Smith   if (!n) {
4161201da799SBarry Smith     PetscDrawAxis  axis;
4162201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4163201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4164201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4165201da799SBarry Smith     ctx->snes_its  = 0;
4166201da799SBarry Smith   }
4167201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4168201da799SBarry Smith   y    = its - ctx->snes_its;
4169201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4170*3fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){
4171201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4172201da799SBarry Smith   }
4173201da799SBarry Smith   ctx->snes_its = its;
4174201da799SBarry Smith   PetscFunctionReturn(0);
4175201da799SBarry Smith }
4176201da799SBarry Smith 
4177201da799SBarry Smith #undef __FUNCT__
4178201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4179201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4180201da799SBarry Smith {
4181201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4182201da799SBarry Smith   PetscReal      x = ptime,y;
4183201da799SBarry Smith   PetscErrorCode ierr;
4184201da799SBarry Smith   PetscInt       its;
4185201da799SBarry Smith 
4186201da799SBarry Smith   PetscFunctionBegin;
4187201da799SBarry Smith   if (!n) {
4188201da799SBarry Smith     PetscDrawAxis  axis;
4189201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4190201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4191201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4192201da799SBarry Smith     ctx->ksp_its = 0;
4193201da799SBarry Smith   }
4194201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4195201da799SBarry Smith   y    = its - ctx->ksp_its;
4196201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4197*3fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1) || ((ctx->howoften == -1) && (n == -1)))){
4198201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4199201da799SBarry Smith   }
4200201da799SBarry Smith   ctx->ksp_its = its;
4201201da799SBarry Smith   PetscFunctionReturn(0);
4202201da799SBarry Smith }
4203