xref: /petsc/src/ts/interface/ts.c (revision 3a471f94986bfde4ed7e275bcd16bbb6c3e18864)
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;
162acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
163a7cc72afSBarry Smith     if (opt) {
1646083293cSBarry Smith       void        *ctx;
165a80ad3e0SBarry Smith       PetscViewer viewer;
166a80ad3e0SBarry Smith 
167ab352700SBarry Smith       ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,&viewer);
168a80ad3e0SBarry Smith       ierr = TSMonitorSolutionCreate(ts,viewer,&ctx);CHKERRQ(ierr);
169a80ad3e0SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1706083293cSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,ctx,TSMonitorSolutionDestroy);CHKERRQ(ierr);
171bdad233fSMatthew Knepley     }
172fb1732b5SBarry Smith     opt  = PETSC_FALSE;
173*3a471f94SBarry Smith     ierr = PetscOptionsBool("-ts_monitor_error","Monitor error graphically","TSMonitorError",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
174*3a471f94SBarry Smith     if (opt) {
175*3a471f94SBarry Smith       PetscViewer viewer;
176*3a471f94SBarry Smith 
177*3a471f94SBarry Smith       ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,&viewer);
178*3a471f94SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorError,viewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
179*3a471f94SBarry Smith     }
180*3a471f94SBarry Smith     opt  = PETSC_FALSE;
181fb1732b5SBarry Smith     ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
182fb1732b5SBarry Smith     if (flg) {
183fb1732b5SBarry Smith       PetscViewer ctx;
184fb1732b5SBarry Smith       if (monfilename[0]) {
185fb1732b5SBarry Smith         ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
186fb1732b5SBarry Smith       } else {
187fb1732b5SBarry Smith         ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
188fb1732b5SBarry Smith       }
189fb1732b5SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
190fb1732b5SBarry Smith     }
191ed81e22dSJed Brown     opt  = PETSC_FALSE;
192ed81e22dSJed 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);
193ed81e22dSJed Brown     if (flg) {
194ed81e22dSJed Brown       const char *ptr,*ptr2;
195ed81e22dSJed Brown       char *filetemplate;
196ed81e22dSJed Brown       if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
197ed81e22dSJed Brown       /* Do some cursory validation of the input. */
198ed81e22dSJed Brown       ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
199ed81e22dSJed Brown       if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
200ed81e22dSJed Brown       for (ptr++ ; ptr && *ptr; ptr++) {
201ed81e22dSJed Brown         ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
202ed81e22dSJed 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");
203ed81e22dSJed Brown         if (ptr2) break;
204ed81e22dSJed Brown       }
205ed81e22dSJed Brown       ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
206ed81e22dSJed Brown       ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
207ed81e22dSJed Brown     }
208bdad233fSMatthew Knepley 
2091c3436cfSJed Brown     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
2101c3436cfSJed Brown     ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2111c3436cfSJed Brown 
212d52bd9f3SBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
213d52bd9f3SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
214d52bd9f3SBarry Smith 
215bdad233fSMatthew Knepley     /* Handle specific TS options */
216abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
217bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
218bdad233fSMatthew Knepley     }
2195d973c19SBarry Smith 
2205d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
2215d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
222bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
223bdad233fSMatthew Knepley   PetscFunctionReturn(0);
224bdad233fSMatthew Knepley }
225bdad233fSMatthew Knepley 
226bdad233fSMatthew Knepley #undef __FUNCT__
227cdcf91faSSean Farley #undef __FUNCT__
2284a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
229a7a1495cSBarry Smith /*@
2308c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
231a7a1495cSBarry Smith       set with TSSetRHSJacobian().
232a7a1495cSBarry Smith 
233a7a1495cSBarry Smith    Collective on TS and Vec
234a7a1495cSBarry Smith 
235a7a1495cSBarry Smith    Input Parameters:
236316643e7SJed Brown +  ts - the TS context
237a7a1495cSBarry Smith .  t - current timestep
238a7a1495cSBarry Smith -  x - input vector
239a7a1495cSBarry Smith 
240a7a1495cSBarry Smith    Output Parameters:
241a7a1495cSBarry Smith +  A - Jacobian matrix
242a7a1495cSBarry Smith .  B - optional preconditioning matrix
243a7a1495cSBarry Smith -  flag - flag indicating matrix structure
244a7a1495cSBarry Smith 
245a7a1495cSBarry Smith    Notes:
246a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
247a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
248a7a1495cSBarry Smith 
24994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
250a7a1495cSBarry Smith    flag parameter.
251a7a1495cSBarry Smith 
252a7a1495cSBarry Smith    Level: developer
253a7a1495cSBarry Smith 
254a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
255a7a1495cSBarry Smith 
25694b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
257a7a1495cSBarry Smith @*/
2587087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
259a7a1495cSBarry Smith {
260dfbe8321SBarry Smith   PetscErrorCode ierr;
2610e4ef248SJed Brown   PetscInt       Xstate;
26224989b8cSPeter Brune   DM             dm;
26324989b8cSPeter Brune   TSDM           tsdm;
26424989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
26524989b8cSPeter Brune   void           *ctx;
26624989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
267a7a1495cSBarry Smith 
268a7a1495cSBarry Smith   PetscFunctionBegin;
2690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2700700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
271c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
27224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
27324989b8cSPeter Brune   ierr = DMTSGetContext(dm,&tsdm);CHKERRQ(ierr);
27424989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
27524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
2760e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
2770e4ef248SJed Brown   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) {
2780e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
2790e4ef248SJed Brown     PetscFunctionReturn(0);
280f8ede8e7SJed Brown   }
281d90be118SSean Farley 
28224989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
283d90be118SSean Farley 
28424989b8cSPeter Brune   if (rhsjacobianfunc) {
285d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
286a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
287a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
28824989b8cSPeter Brune     ierr = (*rhsjacobianfunc)(ts,t,X,A,B,flg,ctx);CHKERRQ(ierr);
289a7a1495cSBarry Smith     PetscStackPop;
290d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
291a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
2920700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
2930700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
294ef66eb69SBarry Smith   } else {
295214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
296214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
297214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
298ef66eb69SBarry Smith   }
2990e4ef248SJed Brown   ts->rhsjacobian.time = t;
3000e4ef248SJed Brown   ts->rhsjacobian.X = X;
3010e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3020e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
303a7a1495cSBarry Smith   PetscFunctionReturn(0);
304a7a1495cSBarry Smith }
305a7a1495cSBarry Smith 
3064a2ae208SSatish Balay #undef __FUNCT__
3074a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
308316643e7SJed Brown /*@
309d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
310d763cef2SBarry Smith 
311316643e7SJed Brown    Collective on TS and Vec
312316643e7SJed Brown 
313316643e7SJed Brown    Input Parameters:
314316643e7SJed Brown +  ts - the TS context
315316643e7SJed Brown .  t - current time
316316643e7SJed Brown -  x - state vector
317316643e7SJed Brown 
318316643e7SJed Brown    Output Parameter:
319316643e7SJed Brown .  y - right hand side
320316643e7SJed Brown 
321316643e7SJed Brown    Note:
322316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
323316643e7SJed Brown    is used internally within the nonlinear solvers.
324316643e7SJed Brown 
325316643e7SJed Brown    Level: developer
326316643e7SJed Brown 
327316643e7SJed Brown .keywords: TS, compute
328316643e7SJed Brown 
329316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
330316643e7SJed Brown @*/
331dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
332d763cef2SBarry Smith {
333dfbe8321SBarry Smith   PetscErrorCode ierr;
334d763cef2SBarry Smith 
33524989b8cSPeter Brune   TSRHSFunction rhsfunction;
33624989b8cSPeter Brune   TSIFunction   ifunction;
33724989b8cSPeter Brune   void          *ctx;
33824989b8cSPeter Brune   DM            dm;
33924989b8cSPeter Brune 
340d763cef2SBarry Smith   PetscFunctionBegin;
3410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3420700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3430700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
34424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
34524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
34624989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
347d763cef2SBarry Smith 
34824989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
349d763cef2SBarry Smith 
350d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
35124989b8cSPeter Brune   if (rhsfunction) {
352d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
35324989b8cSPeter Brune     ierr = (*rhsfunction)(ts,t,x,y,ctx);CHKERRQ(ierr);
354d763cef2SBarry Smith     PetscStackPop;
355214bc6a2SJed Brown   } else {
356214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
357b2cd27e8SJed Brown   }
35844a41b28SSean Farley 
359d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
360d763cef2SBarry Smith   PetscFunctionReturn(0);
361d763cef2SBarry Smith }
362d763cef2SBarry Smith 
3634a2ae208SSatish Balay #undef __FUNCT__
364ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
365ef20d060SBarry Smith /*@
366ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
367ef20d060SBarry Smith 
368ef20d060SBarry Smith    Collective on TS and Vec
369ef20d060SBarry Smith 
370ef20d060SBarry Smith    Input Parameters:
371ef20d060SBarry Smith +  ts - the TS context
372ef20d060SBarry Smith -  t - current time
373ef20d060SBarry Smith 
374ef20d060SBarry Smith    Output Parameter:
375ef20d060SBarry Smith .  y - right hand side
376ef20d060SBarry Smith 
377ef20d060SBarry Smith    Note:
378ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
379ef20d060SBarry Smith    is used internally within the nonlinear solvers.
380ef20d060SBarry Smith 
381ef20d060SBarry Smith    Level: developer
382ef20d060SBarry Smith 
383ef20d060SBarry Smith .keywords: TS, compute
384ef20d060SBarry Smith 
385abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
386ef20d060SBarry Smith @*/
387ef20d060SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec x)
388ef20d060SBarry Smith {
389ef20d060SBarry Smith   PetscErrorCode     ierr;
390ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
391ef20d060SBarry Smith   void               *ctx;
392ef20d060SBarry Smith   DM                 dm;
393ef20d060SBarry Smith 
394ef20d060SBarry Smith   PetscFunctionBegin;
395ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
396ef20d060SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
397ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
398ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
399ef20d060SBarry Smith 
400ef20d060SBarry Smith   if (solutionfunction) {
401ef20d060SBarry Smith     PetscStackPush("TS user right-hand-side function");
402ef20d060SBarry Smith     ierr = (*solutionfunction)(ts,t,x,ctx);CHKERRQ(ierr);
403ef20d060SBarry Smith     PetscStackPop;
404ef20d060SBarry Smith   }
405ef20d060SBarry Smith   PetscFunctionReturn(0);
406ef20d060SBarry Smith }
407ef20d060SBarry Smith 
408ef20d060SBarry Smith #undef __FUNCT__
409214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
410214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
411214bc6a2SJed Brown {
4122dd45cf8SJed Brown   Vec            F;
413214bc6a2SJed Brown   PetscErrorCode ierr;
414214bc6a2SJed Brown 
415214bc6a2SJed Brown   PetscFunctionBegin;
4160da9a4f0SJed Brown   *Frhs = PETSC_NULL;
4172dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
418214bc6a2SJed Brown   if (!ts->Frhs) {
4192dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
420214bc6a2SJed Brown   }
421214bc6a2SJed Brown   *Frhs = ts->Frhs;
422214bc6a2SJed Brown   PetscFunctionReturn(0);
423214bc6a2SJed Brown }
424214bc6a2SJed Brown 
425214bc6a2SJed Brown #undef __FUNCT__
426214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
427214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
428214bc6a2SJed Brown {
429214bc6a2SJed Brown   Mat            A,B;
4302dd45cf8SJed Brown   PetscErrorCode ierr;
431214bc6a2SJed Brown 
432214bc6a2SJed Brown   PetscFunctionBegin;
433214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
434214bc6a2SJed Brown   if (Arhs) {
435214bc6a2SJed Brown     if (!ts->Arhs) {
436214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
437214bc6a2SJed Brown     }
438214bc6a2SJed Brown     *Arhs = ts->Arhs;
439214bc6a2SJed Brown   }
440214bc6a2SJed Brown   if (Brhs) {
441214bc6a2SJed Brown     if (!ts->Brhs) {
442214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
443214bc6a2SJed Brown     }
444214bc6a2SJed Brown     *Brhs = ts->Brhs;
445214bc6a2SJed Brown   }
446214bc6a2SJed Brown   PetscFunctionReturn(0);
447214bc6a2SJed Brown }
448214bc6a2SJed Brown 
449214bc6a2SJed Brown #undef __FUNCT__
450316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
451316643e7SJed Brown /*@
452316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
453316643e7SJed Brown 
454316643e7SJed Brown    Collective on TS and Vec
455316643e7SJed Brown 
456316643e7SJed Brown    Input Parameters:
457316643e7SJed Brown +  ts - the TS context
458316643e7SJed Brown .  t - current time
459316643e7SJed Brown .  X - state vector
460214bc6a2SJed Brown .  Xdot - time derivative of state vector
461214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
462316643e7SJed Brown 
463316643e7SJed Brown    Output Parameter:
464316643e7SJed Brown .  Y - right hand side
465316643e7SJed Brown 
466316643e7SJed Brown    Note:
467316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
468316643e7SJed Brown    is used internally within the nonlinear solvers.
469316643e7SJed Brown 
470316643e7SJed Brown    If the user did did not write their equations in implicit form, this
471316643e7SJed Brown    function recasts them in implicit form.
472316643e7SJed Brown 
473316643e7SJed Brown    Level: developer
474316643e7SJed Brown 
475316643e7SJed Brown .keywords: TS, compute
476316643e7SJed Brown 
477316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
478316643e7SJed Brown @*/
479214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex)
480316643e7SJed Brown {
481316643e7SJed Brown   PetscErrorCode ierr;
48224989b8cSPeter Brune   TSIFunction    ifunction;
48324989b8cSPeter Brune   TSRHSFunction  rhsfunction;
48424989b8cSPeter Brune   void           *ctx;
48524989b8cSPeter Brune   DM             dm;
486316643e7SJed Brown 
487316643e7SJed Brown   PetscFunctionBegin;
4880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4890700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4900700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
4910700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
492316643e7SJed Brown 
49324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
49424989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
49524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
49624989b8cSPeter Brune 
49724989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
498d90be118SSean Farley 
499316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
50024989b8cSPeter Brune   if (ifunction) {
501316643e7SJed Brown     PetscStackPush("TS user implicit function");
50224989b8cSPeter Brune     ierr = (*ifunction)(ts,t,X,Xdot,Y,ctx);CHKERRQ(ierr);
503316643e7SJed Brown     PetscStackPop;
504214bc6a2SJed Brown   }
505214bc6a2SJed Brown   if (imex) {
50624989b8cSPeter Brune     if (!ifunction) {
5072dd45cf8SJed Brown       ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);
5082dd45cf8SJed Brown     }
50924989b8cSPeter Brune   } else if (rhsfunction) {
51024989b8cSPeter Brune     if (ifunction) {
511214bc6a2SJed Brown       Vec Frhs;
512214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
513214bc6a2SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr);
514214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
5152dd45cf8SJed Brown     } else {
5162dd45cf8SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
5172dd45cf8SJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
518316643e7SJed Brown     }
5194a6899ffSJed Brown   }
520316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
521316643e7SJed Brown   PetscFunctionReturn(0);
522316643e7SJed Brown }
523316643e7SJed Brown 
524316643e7SJed Brown #undef __FUNCT__
525316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
526316643e7SJed Brown /*@
527316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
528316643e7SJed Brown 
529316643e7SJed Brown    Collective on TS and Vec
530316643e7SJed Brown 
531316643e7SJed Brown    Input
532316643e7SJed Brown       Input Parameters:
533316643e7SJed Brown +  ts - the TS context
534316643e7SJed Brown .  t - current timestep
535316643e7SJed Brown .  X - state vector
536316643e7SJed Brown .  Xdot - time derivative of state vector
537214bc6a2SJed Brown .  shift - shift to apply, see note below
538214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
539316643e7SJed Brown 
540316643e7SJed Brown    Output Parameters:
541316643e7SJed Brown +  A - Jacobian matrix
542316643e7SJed Brown .  B - optional preconditioning matrix
543316643e7SJed Brown -  flag - flag indicating matrix structure
544316643e7SJed Brown 
545316643e7SJed Brown    Notes:
546316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
547316643e7SJed Brown 
548316643e7SJed Brown    dF/dX + shift*dF/dXdot
549316643e7SJed Brown 
550316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
551316643e7SJed Brown    is used internally within the nonlinear solvers.
552316643e7SJed Brown 
553316643e7SJed Brown    Level: developer
554316643e7SJed Brown 
555316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
556316643e7SJed Brown 
557316643e7SJed Brown .seealso:  TSSetIJacobian()
55863495f91SJed Brown @*/
559214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
560316643e7SJed Brown {
5610026cea9SSean Farley   PetscInt Xstate, Xdotstate;
562316643e7SJed Brown   PetscErrorCode ierr;
56324989b8cSPeter Brune   TSIJacobian    ijacobian;
56424989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
56524989b8cSPeter Brune   DM             dm;
56624989b8cSPeter Brune   void           *ctx;
567316643e7SJed Brown 
568316643e7SJed Brown   PetscFunctionBegin;
56924989b8cSPeter Brune 
5700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5710700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
5720700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
573316643e7SJed Brown   PetscValidPointer(A,6);
5740700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
575316643e7SJed Brown   PetscValidPointer(B,7);
5760700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
577316643e7SJed Brown   PetscValidPointer(flg,8);
57824989b8cSPeter Brune 
57924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
58024989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
58124989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
58224989b8cSPeter Brune 
5830026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
5840026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr);
5850026cea9SSean 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))) {
5860026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
5870026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5880026cea9SSean Farley     PetscFunctionReturn(0);
5890026cea9SSean Farley   }
590316643e7SJed Brown 
59124989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
592316643e7SJed Brown 
5934e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
594316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
59524989b8cSPeter Brune   if (ijacobian) {
5962dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
597316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
59824989b8cSPeter Brune     ierr = (*ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ctx);CHKERRQ(ierr);
599316643e7SJed Brown     PetscStackPop;
600214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
601214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
602214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
6034a6899ffSJed Brown   }
604214bc6a2SJed Brown   if (imex) {
60524989b8cSPeter Brune     if (!ijacobian) {  /* system was written as Xdot = F(t,X) */
606214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
6072dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
608214bc6a2SJed Brown       if (*A != *B) {
609214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
610214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
611214bc6a2SJed Brown       }
612214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
613214bc6a2SJed Brown     }
614214bc6a2SJed Brown   } else {
61524989b8cSPeter Brune     if (!ijacobian) {
616214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
617214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
618214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
619316643e7SJed Brown       if (*A != *B) {
620316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
621316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
622316643e7SJed Brown       }
62324989b8cSPeter Brune     } else if (rhsjacobian) {
624214bc6a2SJed Brown       Mat Arhs,Brhs;
625214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
626214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
627214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
628214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
629214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
630214bc6a2SJed Brown       if (*A != *B) {
631214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
632214bc6a2SJed Brown       }
633214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
634214bc6a2SJed Brown     }
635316643e7SJed Brown   }
6360026cea9SSean Farley 
6370026cea9SSean Farley   ts->ijacobian.time = t;
6380026cea9SSean Farley   ts->ijacobian.X = X;
6390026cea9SSean Farley   ts->ijacobian.Xdot = Xdot;
6400026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr);
6410026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
6420026cea9SSean Farley   ts->ijacobian.shift = shift;
6430026cea9SSean Farley   ts->ijacobian.imex = imex;
6440026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
645316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
646316643e7SJed Brown   PetscFunctionReturn(0);
647316643e7SJed Brown }
648316643e7SJed Brown 
649316643e7SJed Brown #undef __FUNCT__
6504a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
651d763cef2SBarry Smith /*@C
652d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
653d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
654d763cef2SBarry Smith 
6553f9fe445SBarry Smith     Logically Collective on TS
656d763cef2SBarry Smith 
657d763cef2SBarry Smith     Input Parameters:
658d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
659ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
660d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
661d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
662d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
663d763cef2SBarry Smith 
664d763cef2SBarry Smith     Calling sequence of func:
66587828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
666d763cef2SBarry Smith 
667d763cef2SBarry Smith +   t - current timestep
668d763cef2SBarry Smith .   u - input vector
669d763cef2SBarry Smith .   F - function vector
670d763cef2SBarry Smith -   ctx - [optional] user-defined function context
671d763cef2SBarry Smith 
672d763cef2SBarry Smith     Level: beginner
673d763cef2SBarry Smith 
674d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
675d763cef2SBarry Smith 
676d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
677d763cef2SBarry Smith @*/
678089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
679d763cef2SBarry Smith {
680089b2837SJed Brown   PetscErrorCode ierr;
681089b2837SJed Brown   SNES           snes;
682e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
68324989b8cSPeter Brune   DM             dm;
684d763cef2SBarry Smith 
685089b2837SJed Brown   PetscFunctionBegin;
6860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
687ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
68824989b8cSPeter Brune 
68924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
69024989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
691089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
692e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
693e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
694e856ceecSJed Brown     r = ralloc;
695e856ceecSJed Brown   }
696089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
697e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
698d763cef2SBarry Smith   PetscFunctionReturn(0);
699d763cef2SBarry Smith }
700d763cef2SBarry Smith 
7014a2ae208SSatish Balay #undef __FUNCT__
702ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
703ef20d060SBarry Smith /*@C
704abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
705ef20d060SBarry Smith 
706ef20d060SBarry Smith     Logically Collective on TS
707ef20d060SBarry Smith 
708ef20d060SBarry Smith     Input Parameters:
709ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
710ef20d060SBarry Smith .   f - routine for evaluating the solution
711ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
712ef20d060SBarry Smith           function evaluation routine (may be PETSC_NULL)
713ef20d060SBarry Smith 
714ef20d060SBarry Smith     Calling sequence of func:
715ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
716ef20d060SBarry Smith 
717ef20d060SBarry Smith +   t - current timestep
718ef20d060SBarry Smith .   u - output vector
719ef20d060SBarry Smith -   ctx - [optional] user-defined function context
720ef20d060SBarry Smith 
721abd5a294SJed Brown     Notes:
722abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
723abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
724abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
725abd5a294SJed Brown 
7264f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
727abd5a294SJed Brown 
728ef20d060SBarry Smith     Level: beginner
729ef20d060SBarry Smith 
730ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
731ef20d060SBarry Smith 
732abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction()
733ef20d060SBarry Smith @*/
734ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
735ef20d060SBarry Smith {
736ef20d060SBarry Smith   PetscErrorCode ierr;
737ef20d060SBarry Smith   DM             dm;
738ef20d060SBarry Smith 
739ef20d060SBarry Smith   PetscFunctionBegin;
740ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
741ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
742ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
743ef20d060SBarry Smith   PetscFunctionReturn(0);
744ef20d060SBarry Smith }
745ef20d060SBarry Smith 
746ef20d060SBarry Smith #undef __FUNCT__
7474a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
748d763cef2SBarry Smith /*@C
749d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
750d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
751d763cef2SBarry Smith 
7523f9fe445SBarry Smith    Logically Collective on TS
753d763cef2SBarry Smith 
754d763cef2SBarry Smith    Input Parameters:
755d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
756d763cef2SBarry Smith .  A   - Jacobian matrix
757d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
758d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
759d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
760d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
761d763cef2SBarry Smith 
762d763cef2SBarry Smith    Calling sequence of func:
76387828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
764d763cef2SBarry Smith 
765d763cef2SBarry Smith +  t - current timestep
766d763cef2SBarry Smith .  u - input vector
767d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
768d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
769d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
77094b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
771d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
772d763cef2SBarry Smith 
773d763cef2SBarry Smith    Notes:
77494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
775d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
776d763cef2SBarry Smith 
777d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
778d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
77956335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
780d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
781d763cef2SBarry Smith    throughout the global iterations.
782d763cef2SBarry Smith 
783d763cef2SBarry Smith    Level: beginner
784d763cef2SBarry Smith 
785d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
786d763cef2SBarry Smith 
787ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
788d763cef2SBarry Smith 
789d763cef2SBarry Smith @*/
790089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
791d763cef2SBarry Smith {
792277b19d0SLisandro Dalcin   PetscErrorCode ierr;
793089b2837SJed Brown   SNES           snes;
79424989b8cSPeter Brune   DM             dm;
79524989b8cSPeter Brune   TSIJacobian    ijacobian;
796277b19d0SLisandro Dalcin 
797d763cef2SBarry Smith   PetscFunctionBegin;
7980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
799277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
800277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
801277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
802277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
803d763cef2SBarry Smith 
80424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
80524989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
80624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
80724989b8cSPeter Brune 
808089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
8095f659677SPeter Brune   if (!ijacobian) {
810089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
8110e4ef248SJed Brown   }
8120e4ef248SJed Brown   if (A) {
8130e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8140e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
8150e4ef248SJed Brown     ts->Arhs = A;
8160e4ef248SJed Brown   }
8170e4ef248SJed Brown   if (B) {
8180e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8190e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
8200e4ef248SJed Brown     ts->Brhs = B;
8210e4ef248SJed Brown   }
822d763cef2SBarry Smith   PetscFunctionReturn(0);
823d763cef2SBarry Smith }
824d763cef2SBarry Smith 
825316643e7SJed Brown 
826316643e7SJed Brown #undef __FUNCT__
827316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
828316643e7SJed Brown /*@C
829316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
830316643e7SJed Brown 
8313f9fe445SBarry Smith    Logically Collective on TS
832316643e7SJed Brown 
833316643e7SJed Brown    Input Parameters:
834316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
835ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
836316643e7SJed Brown .  f   - the function evaluation routine
837316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
838316643e7SJed Brown 
839316643e7SJed Brown    Calling sequence of f:
840316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
841316643e7SJed Brown 
842316643e7SJed Brown +  t   - time at step/stage being solved
843316643e7SJed Brown .  u   - state vector
844316643e7SJed Brown .  u_t - time derivative of state vector
845316643e7SJed Brown .  F   - function vector
846316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
847316643e7SJed Brown 
848316643e7SJed Brown    Important:
849d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
850316643e7SJed Brown 
851316643e7SJed Brown    Level: beginner
852316643e7SJed Brown 
853316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
854316643e7SJed Brown 
855d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
856316643e7SJed Brown @*/
857089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
858316643e7SJed Brown {
859089b2837SJed Brown   PetscErrorCode ierr;
860089b2837SJed Brown   SNES           snes;
861e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
86224989b8cSPeter Brune   DM             dm;
863316643e7SJed Brown 
864316643e7SJed Brown   PetscFunctionBegin;
8650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
866ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
86724989b8cSPeter Brune 
86824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
86924989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
87024989b8cSPeter Brune 
871089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
872e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
873e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
874e856ceecSJed Brown     res = resalloc;
875e856ceecSJed Brown   }
876089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
877e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
87824989b8cSPeter Brune 
879089b2837SJed Brown   PetscFunctionReturn(0);
880089b2837SJed Brown }
881089b2837SJed Brown 
882089b2837SJed Brown #undef __FUNCT__
883089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
884089b2837SJed Brown /*@C
885089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
886089b2837SJed Brown 
887089b2837SJed Brown    Not Collective
888089b2837SJed Brown 
889089b2837SJed Brown    Input Parameter:
890089b2837SJed Brown .  ts - the TS context
891089b2837SJed Brown 
892089b2837SJed Brown    Output Parameter:
893089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
894089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
895089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
896089b2837SJed Brown 
897089b2837SJed Brown    Level: advanced
898089b2837SJed Brown 
899089b2837SJed Brown .keywords: TS, nonlinear, get, function
900089b2837SJed Brown 
901089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
902089b2837SJed Brown @*/
903089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
904089b2837SJed Brown {
905089b2837SJed Brown   PetscErrorCode ierr;
906089b2837SJed Brown   SNES snes;
90724989b8cSPeter Brune   DM   dm;
908089b2837SJed Brown 
909089b2837SJed Brown   PetscFunctionBegin;
910089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
911089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
912089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
91324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
91424989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
915089b2837SJed Brown   PetscFunctionReturn(0);
916089b2837SJed Brown }
917089b2837SJed Brown 
918089b2837SJed Brown #undef __FUNCT__
919089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
920089b2837SJed Brown /*@C
921089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
922089b2837SJed Brown 
923089b2837SJed Brown    Not Collective
924089b2837SJed Brown 
925089b2837SJed Brown    Input Parameter:
926089b2837SJed Brown .  ts - the TS context
927089b2837SJed Brown 
928089b2837SJed Brown    Output Parameter:
929089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
930089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
931089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
932089b2837SJed Brown 
933089b2837SJed Brown    Level: advanced
934089b2837SJed Brown 
935089b2837SJed Brown .keywords: TS, nonlinear, get, function
936089b2837SJed Brown 
937089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
938089b2837SJed Brown @*/
939089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
940089b2837SJed Brown {
941089b2837SJed Brown   PetscErrorCode ierr;
942089b2837SJed Brown   SNES snes;
94324989b8cSPeter Brune   DM   dm;
944089b2837SJed Brown 
945089b2837SJed Brown   PetscFunctionBegin;
946089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
947089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
948089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
94924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
95024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
951316643e7SJed Brown   PetscFunctionReturn(0);
952316643e7SJed Brown }
953316643e7SJed Brown 
954316643e7SJed Brown #undef __FUNCT__
955316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
956316643e7SJed Brown /*@C
957a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
958a4f0a591SBarry Smith         you provided with TSSetIFunction().
959316643e7SJed Brown 
9603f9fe445SBarry Smith    Logically Collective on TS
961316643e7SJed Brown 
962316643e7SJed Brown    Input Parameters:
963316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
964316643e7SJed Brown .  A   - Jacobian matrix
965316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
966316643e7SJed Brown .  f   - the Jacobian evaluation routine
967316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
968316643e7SJed Brown 
969316643e7SJed Brown    Calling sequence of f:
9701b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
971316643e7SJed Brown 
972316643e7SJed Brown +  t    - time at step/stage being solved
9731b4a444bSJed Brown .  U    - state vector
9741b4a444bSJed Brown .  U_t  - time derivative of state vector
975316643e7SJed Brown .  a    - shift
9761b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
977316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
978316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
979316643e7SJed Brown           structure (same as flag in KSPSetOperators())
980316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
981316643e7SJed Brown 
982316643e7SJed Brown    Notes:
983316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
984316643e7SJed Brown 
985a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
986a4f0a591SBarry 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.
987a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
988a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
989a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
990a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
991a4f0a591SBarry Smith 
992316643e7SJed Brown    Level: beginner
993316643e7SJed Brown 
994316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
995316643e7SJed Brown 
996ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
997316643e7SJed Brown 
998316643e7SJed Brown @*/
9997087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1000316643e7SJed Brown {
1001316643e7SJed Brown   PetscErrorCode ierr;
1002089b2837SJed Brown   SNES           snes;
100324989b8cSPeter Brune   DM             dm;
1004316643e7SJed Brown 
1005316643e7SJed Brown   PetscFunctionBegin;
10060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10070700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
10080700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1009316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1010316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
101124989b8cSPeter Brune 
101224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
101324989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
101424989b8cSPeter Brune 
1015089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1016089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1017316643e7SJed Brown   PetscFunctionReturn(0);
1018316643e7SJed Brown }
1019316643e7SJed Brown 
10204a2ae208SSatish Balay #undef __FUNCT__
10214a2ae208SSatish Balay #define __FUNCT__ "TSView"
10227e2c5f70SBarry Smith /*@C
1023d763cef2SBarry Smith     TSView - Prints the TS data structure.
1024d763cef2SBarry Smith 
10254c49b128SBarry Smith     Collective on TS
1026d763cef2SBarry Smith 
1027d763cef2SBarry Smith     Input Parameters:
1028d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1029d763cef2SBarry Smith -   viewer - visualization context
1030d763cef2SBarry Smith 
1031d763cef2SBarry Smith     Options Database Key:
1032d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1033d763cef2SBarry Smith 
1034d763cef2SBarry Smith     Notes:
1035d763cef2SBarry Smith     The available visualization contexts include
1036b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1037b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1038d763cef2SBarry Smith          output where only the first processor opens
1039d763cef2SBarry Smith          the file.  All other processors send their
1040d763cef2SBarry Smith          data to the first processor to print.
1041d763cef2SBarry Smith 
1042d763cef2SBarry Smith     The user can open an alternative visualization context with
1043b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1044d763cef2SBarry Smith 
1045d763cef2SBarry Smith     Level: beginner
1046d763cef2SBarry Smith 
1047d763cef2SBarry Smith .keywords: TS, timestep, view
1048d763cef2SBarry Smith 
1049b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1050d763cef2SBarry Smith @*/
10517087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1052d763cef2SBarry Smith {
1053dfbe8321SBarry Smith   PetscErrorCode ierr;
105419fd82e9SBarry Smith   TSType         type;
1055089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
1056d763cef2SBarry Smith 
1057d763cef2SBarry Smith   PetscFunctionBegin;
10580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10593050cee2SBarry Smith   if (!viewer) {
10607adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
10613050cee2SBarry Smith   }
10620700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1063c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1064fd16b177SBarry Smith 
1065251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1066251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
106732077d6dSBarry Smith   if (iascii) {
1068317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
106977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1070a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1071d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
10725ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1073c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1074d763cef2SBarry Smith     }
10755ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1076193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1077d52bd9f3SBarry Smith     if (ts->ops->view) {
1078d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1079d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1080d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1081d52bd9f3SBarry Smith     }
10820f5bd95cSBarry Smith   } else if (isstring) {
1083a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1084b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
1085d763cef2SBarry Smith   }
1086b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1087251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1088b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1089d763cef2SBarry Smith   PetscFunctionReturn(0);
1090d763cef2SBarry Smith }
1091d763cef2SBarry Smith 
1092d763cef2SBarry Smith 
10934a2ae208SSatish Balay #undef __FUNCT__
10944a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1095b07ff414SBarry Smith /*@
1096d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1097d763cef2SBarry Smith    the timesteppers.
1098d763cef2SBarry Smith 
10993f9fe445SBarry Smith    Logically Collective on TS
1100d763cef2SBarry Smith 
1101d763cef2SBarry Smith    Input Parameters:
1102d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1103d763cef2SBarry Smith -  usrP - optional user context
1104d763cef2SBarry Smith 
1105d763cef2SBarry Smith    Level: intermediate
1106d763cef2SBarry Smith 
1107d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1108d763cef2SBarry Smith 
1109d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1110d763cef2SBarry Smith @*/
11117087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1112d763cef2SBarry Smith {
1113d763cef2SBarry Smith   PetscFunctionBegin;
11140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1115d763cef2SBarry Smith   ts->user = usrP;
1116d763cef2SBarry Smith   PetscFunctionReturn(0);
1117d763cef2SBarry Smith }
1118d763cef2SBarry Smith 
11194a2ae208SSatish Balay #undef __FUNCT__
11204a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1121b07ff414SBarry Smith /*@
1122d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1123d763cef2SBarry Smith     timestepper.
1124d763cef2SBarry Smith 
1125d763cef2SBarry Smith     Not Collective
1126d763cef2SBarry Smith 
1127d763cef2SBarry Smith     Input Parameter:
1128d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1129d763cef2SBarry Smith 
1130d763cef2SBarry Smith     Output Parameter:
1131d763cef2SBarry Smith .   usrP - user context
1132d763cef2SBarry Smith 
1133d763cef2SBarry Smith     Level: intermediate
1134d763cef2SBarry Smith 
1135d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1136d763cef2SBarry Smith 
1137d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1138d763cef2SBarry Smith @*/
1139e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1140d763cef2SBarry Smith {
1141d763cef2SBarry Smith   PetscFunctionBegin;
11420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1143e71120c6SJed Brown   *(void**)usrP = ts->user;
1144d763cef2SBarry Smith   PetscFunctionReturn(0);
1145d763cef2SBarry Smith }
1146d763cef2SBarry Smith 
11474a2ae208SSatish Balay #undef __FUNCT__
11484a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1149d763cef2SBarry Smith /*@
1150b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1151d763cef2SBarry Smith 
1152d763cef2SBarry Smith    Not Collective
1153d763cef2SBarry Smith 
1154d763cef2SBarry Smith    Input Parameter:
1155d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1156d763cef2SBarry Smith 
1157d763cef2SBarry Smith    Output Parameter:
1158b8123daeSJed Brown .  iter - number of steps completed so far
1159d763cef2SBarry Smith 
1160d763cef2SBarry Smith    Level: intermediate
1161d763cef2SBarry Smith 
1162d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1163b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1164d763cef2SBarry Smith @*/
11657087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
1166d763cef2SBarry Smith {
1167d763cef2SBarry Smith   PetscFunctionBegin;
11680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11694482741eSBarry Smith   PetscValidIntPointer(iter,2);
1170d763cef2SBarry Smith   *iter = ts->steps;
1171d763cef2SBarry Smith   PetscFunctionReturn(0);
1172d763cef2SBarry Smith }
1173d763cef2SBarry Smith 
11744a2ae208SSatish Balay #undef __FUNCT__
11754a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1176d763cef2SBarry Smith /*@
1177d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1178d763cef2SBarry Smith    as well as the initial time.
1179d763cef2SBarry Smith 
11803f9fe445SBarry Smith    Logically Collective on TS
1181d763cef2SBarry Smith 
1182d763cef2SBarry Smith    Input Parameters:
1183d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1184d763cef2SBarry Smith .  initial_time - the initial time
1185d763cef2SBarry Smith -  time_step - the size of the timestep
1186d763cef2SBarry Smith 
1187d763cef2SBarry Smith    Level: intermediate
1188d763cef2SBarry Smith 
1189d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1190d763cef2SBarry Smith 
1191d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1192d763cef2SBarry Smith @*/
11937087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1194d763cef2SBarry Smith {
1195e144a568SJed Brown   PetscErrorCode ierr;
1196e144a568SJed Brown 
1197d763cef2SBarry Smith   PetscFunctionBegin;
11980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1199e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1200d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1201d763cef2SBarry Smith   PetscFunctionReturn(0);
1202d763cef2SBarry Smith }
1203d763cef2SBarry Smith 
12044a2ae208SSatish Balay #undef __FUNCT__
12054a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1206d763cef2SBarry Smith /*@
1207d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1208d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1209d763cef2SBarry Smith 
12103f9fe445SBarry Smith    Logically Collective on TS
1211d763cef2SBarry Smith 
1212d763cef2SBarry Smith    Input Parameters:
1213d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1214d763cef2SBarry Smith -  time_step - the size of the timestep
1215d763cef2SBarry Smith 
1216d763cef2SBarry Smith    Level: intermediate
1217d763cef2SBarry Smith 
1218d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1219d763cef2SBarry Smith 
1220d763cef2SBarry Smith .keywords: TS, set, timestep
1221d763cef2SBarry Smith @*/
12227087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1223d763cef2SBarry Smith {
1224d763cef2SBarry Smith   PetscFunctionBegin;
12250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1226c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1227d763cef2SBarry Smith   ts->time_step = time_step;
122831748224SBarry Smith   ts->time_step_orig = time_step;
1229d763cef2SBarry Smith   PetscFunctionReturn(0);
1230d763cef2SBarry Smith }
1231d763cef2SBarry Smith 
12324a2ae208SSatish Balay #undef __FUNCT__
1233a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1234a43b19c4SJed Brown /*@
1235a43b19c4SJed Brown    TSSetExactFinalTime - Determines whether to interpolate solution to the
1236a43b19c4SJed Brown       exact final time requested by the user or just returns it at the final time
1237a43b19c4SJed Brown       it computed.
1238a43b19c4SJed Brown 
1239a43b19c4SJed Brown   Logically Collective on TS
1240a43b19c4SJed Brown 
1241a43b19c4SJed Brown    Input Parameter:
1242a43b19c4SJed Brown +   ts - the time-step context
1243a43b19c4SJed Brown -   ft - PETSC_TRUE if interpolates, else PETSC_FALSE
1244a43b19c4SJed Brown 
1245a43b19c4SJed Brown    Level: beginner
1246a43b19c4SJed Brown 
1247a43b19c4SJed Brown .seealso: TSSetDuration()
1248a43b19c4SJed Brown @*/
1249a43b19c4SJed Brown PetscErrorCode  TSSetExactFinalTime(TS ts,PetscBool flg)
1250a43b19c4SJed Brown {
1251a43b19c4SJed Brown 
1252a43b19c4SJed Brown   PetscFunctionBegin;
1253a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1254d8cd7023SBarry Smith   PetscValidLogicalCollectiveBool(ts,flg,2);
1255a43b19c4SJed Brown   ts->exact_final_time = flg;
1256a43b19c4SJed Brown   PetscFunctionReturn(0);
1257a43b19c4SJed Brown }
1258a43b19c4SJed Brown 
1259a43b19c4SJed Brown #undef __FUNCT__
12604a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1261d763cef2SBarry Smith /*@
1262d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1263d763cef2SBarry Smith 
1264d763cef2SBarry Smith    Not Collective
1265d763cef2SBarry Smith 
1266d763cef2SBarry Smith    Input Parameter:
1267d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1268d763cef2SBarry Smith 
1269d763cef2SBarry Smith    Output Parameter:
1270d763cef2SBarry Smith .  dt - the current timestep size
1271d763cef2SBarry Smith 
1272d763cef2SBarry Smith    Level: intermediate
1273d763cef2SBarry Smith 
1274d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1275d763cef2SBarry Smith 
1276d763cef2SBarry Smith .keywords: TS, get, timestep
1277d763cef2SBarry Smith @*/
12787087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1279d763cef2SBarry Smith {
1280d763cef2SBarry Smith   PetscFunctionBegin;
12810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1282f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1283d763cef2SBarry Smith   *dt = ts->time_step;
1284d763cef2SBarry Smith   PetscFunctionReturn(0);
1285d763cef2SBarry Smith }
1286d763cef2SBarry Smith 
12874a2ae208SSatish Balay #undef __FUNCT__
12884a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1289d8e5e3e6SSatish Balay /*@
1290d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1291d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1292d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1293d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1294d763cef2SBarry Smith 
1295d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1296d763cef2SBarry Smith 
1297d763cef2SBarry Smith    Input Parameter:
1298d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1299d763cef2SBarry Smith 
1300d763cef2SBarry Smith    Output Parameter:
1301d763cef2SBarry Smith .  v - the vector containing the solution
1302d763cef2SBarry Smith 
1303d763cef2SBarry Smith    Level: intermediate
1304d763cef2SBarry Smith 
1305d763cef2SBarry Smith .seealso: TSGetTimeStep()
1306d763cef2SBarry Smith 
1307d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1308d763cef2SBarry Smith @*/
13097087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1310d763cef2SBarry Smith {
1311d763cef2SBarry Smith   PetscFunctionBegin;
13120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13134482741eSBarry Smith   PetscValidPointer(v,2);
13148737fe31SLisandro Dalcin   *v = ts->vec_sol;
1315d763cef2SBarry Smith   PetscFunctionReturn(0);
1316d763cef2SBarry Smith }
1317d763cef2SBarry Smith 
1318bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
13194a2ae208SSatish Balay #undef __FUNCT__
1320bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1321d8e5e3e6SSatish Balay /*@
1322bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1323d763cef2SBarry Smith 
1324bdad233fSMatthew Knepley   Not collective
1325d763cef2SBarry Smith 
1326bdad233fSMatthew Knepley   Input Parameters:
1327bdad233fSMatthew Knepley + ts   - The TS
1328bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1329d763cef2SBarry Smith .vb
1330d763cef2SBarry Smith          U_t = A U
1331d763cef2SBarry Smith          U_t = A(t) U
1332d763cef2SBarry Smith          U_t = F(t,U)
1333d763cef2SBarry Smith .ve
1334d763cef2SBarry Smith 
1335d763cef2SBarry Smith    Level: beginner
1336d763cef2SBarry Smith 
1337bdad233fSMatthew Knepley .keywords: TS, problem type
1338bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1339d763cef2SBarry Smith @*/
13407087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1341a7cc72afSBarry Smith {
13429e2a6581SJed Brown   PetscErrorCode ierr;
13439e2a6581SJed Brown 
1344d763cef2SBarry Smith   PetscFunctionBegin;
13450700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1346bdad233fSMatthew Knepley   ts->problem_type = type;
13479e2a6581SJed Brown   if (type == TS_LINEAR) {
13489e2a6581SJed Brown     SNES snes;
13499e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13509e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
13519e2a6581SJed Brown   }
1352d763cef2SBarry Smith   PetscFunctionReturn(0);
1353d763cef2SBarry Smith }
1354d763cef2SBarry Smith 
1355bdad233fSMatthew Knepley #undef __FUNCT__
1356bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1357bdad233fSMatthew Knepley /*@C
1358bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1359bdad233fSMatthew Knepley 
1360bdad233fSMatthew Knepley   Not collective
1361bdad233fSMatthew Knepley 
1362bdad233fSMatthew Knepley   Input Parameter:
1363bdad233fSMatthew Knepley . ts   - The TS
1364bdad233fSMatthew Knepley 
1365bdad233fSMatthew Knepley   Output Parameter:
1366bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1367bdad233fSMatthew Knepley .vb
1368089b2837SJed Brown          M U_t = A U
1369089b2837SJed Brown          M(t) U_t = A(t) U
1370bdad233fSMatthew Knepley          U_t = F(t,U)
1371bdad233fSMatthew Knepley .ve
1372bdad233fSMatthew Knepley 
1373bdad233fSMatthew Knepley    Level: beginner
1374bdad233fSMatthew Knepley 
1375bdad233fSMatthew Knepley .keywords: TS, problem type
1376bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1377bdad233fSMatthew Knepley @*/
13787087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1379a7cc72afSBarry Smith {
1380bdad233fSMatthew Knepley   PetscFunctionBegin;
13810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
13824482741eSBarry Smith   PetscValidIntPointer(type,2);
1383bdad233fSMatthew Knepley   *type = ts->problem_type;
1384bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1385bdad233fSMatthew Knepley }
1386d763cef2SBarry Smith 
13874a2ae208SSatish Balay #undef __FUNCT__
13884a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1389d763cef2SBarry Smith /*@
1390d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1391d763cef2SBarry Smith    of a timestepper.
1392d763cef2SBarry Smith 
1393d763cef2SBarry Smith    Collective on TS
1394d763cef2SBarry Smith 
1395d763cef2SBarry Smith    Input Parameter:
1396d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1397d763cef2SBarry Smith 
1398d763cef2SBarry Smith    Notes:
1399d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1400d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1401d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1402d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1403d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1404d763cef2SBarry Smith 
1405d763cef2SBarry Smith    Level: advanced
1406d763cef2SBarry Smith 
1407d763cef2SBarry Smith .keywords: TS, timestep, setup
1408d763cef2SBarry Smith 
1409d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1410d763cef2SBarry Smith @*/
14117087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1412d763cef2SBarry Smith {
1413dfbe8321SBarry Smith   PetscErrorCode ierr;
14146c6b9e74SPeter Brune   DM             dm;
14156c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
14166c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
14176c6b9e74SPeter Brune   TSIJacobian    ijac;
14186c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1419d763cef2SBarry Smith 
1420d763cef2SBarry Smith   PetscFunctionBegin;
14210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1422277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1423277b19d0SLisandro Dalcin 
14247adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
14259596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1426d763cef2SBarry Smith   }
1427a43b19c4SJed Brown   if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE;
1428277b19d0SLisandro Dalcin 
1429277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1430277b19d0SLisandro Dalcin 
14311c3436cfSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
14321c3436cfSJed Brown 
1433277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1434000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1435277b19d0SLisandro Dalcin   }
1436277b19d0SLisandro Dalcin 
14376c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
14386c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
14396c6b9e74SPeter Brune    */
14406c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
14416c6b9e74SPeter Brune   ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr);
14426c6b9e74SPeter Brune   if (!func) {
14436c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
14446c6b9e74SPeter Brune   }
14456c6b9e74SPeter 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.
14466c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
14476c6b9e74SPeter Brune    */
14486c6b9e74SPeter Brune   ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr);
14496c6b9e74SPeter Brune   ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr);
14506c6b9e74SPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr);
14516c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
14526c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
14536c6b9e74SPeter Brune   }
1454277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1455277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1456277b19d0SLisandro Dalcin }
1457277b19d0SLisandro Dalcin 
1458277b19d0SLisandro Dalcin #undef __FUNCT__
1459277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1460277b19d0SLisandro Dalcin /*@
1461277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1462277b19d0SLisandro Dalcin 
1463277b19d0SLisandro Dalcin    Collective on TS
1464277b19d0SLisandro Dalcin 
1465277b19d0SLisandro Dalcin    Input Parameter:
1466277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1467277b19d0SLisandro Dalcin 
1468277b19d0SLisandro Dalcin    Level: beginner
1469277b19d0SLisandro Dalcin 
1470277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1471277b19d0SLisandro Dalcin 
1472277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1473277b19d0SLisandro Dalcin @*/
1474277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1475277b19d0SLisandro Dalcin {
1476277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1477277b19d0SLisandro Dalcin 
1478277b19d0SLisandro Dalcin   PetscFunctionBegin;
1479277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1480277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1481277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1482277b19d0SLisandro Dalcin   }
1483277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
14844e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
14854e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1486214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
14876bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1488e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1489e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
149038637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1491277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1492d763cef2SBarry Smith   PetscFunctionReturn(0);
1493d763cef2SBarry Smith }
1494d763cef2SBarry Smith 
14954a2ae208SSatish Balay #undef __FUNCT__
14964a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1497d8e5e3e6SSatish Balay /*@
1498d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1499d763cef2SBarry Smith    with TSCreate().
1500d763cef2SBarry Smith 
1501d763cef2SBarry Smith    Collective on TS
1502d763cef2SBarry Smith 
1503d763cef2SBarry Smith    Input Parameter:
1504d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1505d763cef2SBarry Smith 
1506d763cef2SBarry Smith    Level: beginner
1507d763cef2SBarry Smith 
1508d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1509d763cef2SBarry Smith 
1510d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1511d763cef2SBarry Smith @*/
15126bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1513d763cef2SBarry Smith {
15146849ba73SBarry Smith   PetscErrorCode ierr;
1515d763cef2SBarry Smith 
1516d763cef2SBarry Smith   PetscFunctionBegin;
15176bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
15186bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
15196bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1520d763cef2SBarry Smith 
15216bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1522277b19d0SLisandro Dalcin 
1523be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
15246bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
15256bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
15266d4c513bSLisandro Dalcin 
152784df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
15286bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
15296bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
15306bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
15316d4c513bSLisandro Dalcin 
1532a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1533d763cef2SBarry Smith   PetscFunctionReturn(0);
1534d763cef2SBarry Smith }
1535d763cef2SBarry Smith 
15364a2ae208SSatish Balay #undef __FUNCT__
15374a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1538d8e5e3e6SSatish Balay /*@
1539d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1540d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1541d763cef2SBarry Smith 
1542d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1543d763cef2SBarry Smith 
1544d763cef2SBarry Smith    Input Parameter:
1545d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1546d763cef2SBarry Smith 
1547d763cef2SBarry Smith    Output Parameter:
1548d763cef2SBarry Smith .  snes - the nonlinear solver context
1549d763cef2SBarry Smith 
1550d763cef2SBarry Smith    Notes:
1551d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1552d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
155394b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1554d763cef2SBarry Smith 
1555d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1556d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1557d763cef2SBarry Smith 
1558d763cef2SBarry Smith    Level: beginner
1559d763cef2SBarry Smith 
1560d763cef2SBarry Smith .keywords: timestep, get, SNES
1561d763cef2SBarry Smith @*/
15627087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1563d763cef2SBarry Smith {
1564d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1565d372ba47SLisandro Dalcin 
1566d763cef2SBarry Smith   PetscFunctionBegin;
15670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15684482741eSBarry Smith   PetscValidPointer(snes,2);
1569d372ba47SLisandro Dalcin   if (!ts->snes) {
1570d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
15716c6b9e74SPeter Brune     ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1572d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1573d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1574496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
15759e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
15769e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
15779e2a6581SJed Brown     }
1578d372ba47SLisandro Dalcin   }
1579d763cef2SBarry Smith   *snes = ts->snes;
1580d763cef2SBarry Smith   PetscFunctionReturn(0);
1581d763cef2SBarry Smith }
1582d763cef2SBarry Smith 
15834a2ae208SSatish Balay #undef __FUNCT__
158494b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1585d8e5e3e6SSatish Balay /*@
158694b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1587d763cef2SBarry Smith    a TS (timestepper) context.
1588d763cef2SBarry Smith 
158994b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1590d763cef2SBarry Smith 
1591d763cef2SBarry Smith    Input Parameter:
1592d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1593d763cef2SBarry Smith 
1594d763cef2SBarry Smith    Output Parameter:
159594b7f48cSBarry Smith .  ksp - the nonlinear solver context
1596d763cef2SBarry Smith 
1597d763cef2SBarry Smith    Notes:
159894b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1599d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1600d763cef2SBarry Smith    KSP and PC contexts as well.
1601d763cef2SBarry Smith 
160294b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
160394b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1604d763cef2SBarry Smith 
1605d763cef2SBarry Smith    Level: beginner
1606d763cef2SBarry Smith 
160794b7f48cSBarry Smith .keywords: timestep, get, KSP
1608d763cef2SBarry Smith @*/
16097087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1610d763cef2SBarry Smith {
1611d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1612089b2837SJed Brown   SNES           snes;
1613d372ba47SLisandro Dalcin 
1614d763cef2SBarry Smith   PetscFunctionBegin;
16150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16164482741eSBarry Smith   PetscValidPointer(ksp,2);
161717186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1618e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1619089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1620089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1621d763cef2SBarry Smith   PetscFunctionReturn(0);
1622d763cef2SBarry Smith }
1623d763cef2SBarry Smith 
1624d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1625d763cef2SBarry Smith 
16264a2ae208SSatish Balay #undef __FUNCT__
1627adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1628adb62b0dSMatthew Knepley /*@
1629adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1630adb62b0dSMatthew Knepley    maximum time for iteration.
1631adb62b0dSMatthew Knepley 
16323f9fe445SBarry Smith    Not Collective
1633adb62b0dSMatthew Knepley 
1634adb62b0dSMatthew Knepley    Input Parameters:
1635adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1636adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1637adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1638adb62b0dSMatthew Knepley 
1639adb62b0dSMatthew Knepley    Level: intermediate
1640adb62b0dSMatthew Knepley 
1641adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1642adb62b0dSMatthew Knepley @*/
16437087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1644adb62b0dSMatthew Knepley {
1645adb62b0dSMatthew Knepley   PetscFunctionBegin;
16460700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1647abc0a331SBarry Smith   if (maxsteps) {
16484482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1649adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1650adb62b0dSMatthew Knepley   }
1651abc0a331SBarry Smith   if (maxtime) {
16524482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1653adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1654adb62b0dSMatthew Knepley   }
1655adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1656adb62b0dSMatthew Knepley }
1657adb62b0dSMatthew Knepley 
1658adb62b0dSMatthew Knepley #undef __FUNCT__
16594a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1660d763cef2SBarry Smith /*@
1661d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1662d763cef2SBarry Smith    maximum time for iteration.
1663d763cef2SBarry Smith 
16643f9fe445SBarry Smith    Logically Collective on TS
1665d763cef2SBarry Smith 
1666d763cef2SBarry Smith    Input Parameters:
1667d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1668d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1669d763cef2SBarry Smith -  maxtime - final time to iterate to
1670d763cef2SBarry Smith 
1671d763cef2SBarry Smith    Options Database Keys:
1672d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
16733bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1674d763cef2SBarry Smith 
1675d763cef2SBarry Smith    Notes:
1676d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1677d763cef2SBarry Smith 
1678d763cef2SBarry Smith    Level: intermediate
1679d763cef2SBarry Smith 
1680d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1681a43b19c4SJed Brown 
1682a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1683d763cef2SBarry Smith @*/
16847087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1685d763cef2SBarry Smith {
1686d763cef2SBarry Smith   PetscFunctionBegin;
16870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1688c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1689c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
169039b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
169139b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1692d763cef2SBarry Smith   PetscFunctionReturn(0);
1693d763cef2SBarry Smith }
1694d763cef2SBarry Smith 
16954a2ae208SSatish Balay #undef __FUNCT__
16964a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1697d763cef2SBarry Smith /*@
1698d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1699d763cef2SBarry Smith    for use by the TS routines.
1700d763cef2SBarry Smith 
17013f9fe445SBarry Smith    Logically Collective on TS and Vec
1702d763cef2SBarry Smith 
1703d763cef2SBarry Smith    Input Parameters:
1704d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1705d763cef2SBarry Smith -  x - the solution vector
1706d763cef2SBarry Smith 
1707d763cef2SBarry Smith    Level: beginner
1708d763cef2SBarry Smith 
1709d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1710d763cef2SBarry Smith @*/
17117087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1712d763cef2SBarry Smith {
17138737fe31SLisandro Dalcin   PetscErrorCode ierr;
1714496e6a7aSJed Brown   DM             dm;
17158737fe31SLisandro Dalcin 
1716d763cef2SBarry Smith   PetscFunctionBegin;
17170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17180700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
17198737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
17206bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
17218737fe31SLisandro Dalcin   ts->vec_sol = x;
1722496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1723496e6a7aSJed Brown   ierr = DMShellSetGlobalVector(dm,x);CHKERRQ(ierr);
1724d763cef2SBarry Smith   PetscFunctionReturn(0);
1725d763cef2SBarry Smith }
1726d763cef2SBarry Smith 
1727e74ef692SMatthew Knepley #undef __FUNCT__
1728e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1729ac226902SBarry Smith /*@C
1730000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
17313f2090d5SJed Brown   called once at the beginning of each time step.
1732000e7ae3SMatthew Knepley 
17333f9fe445SBarry Smith   Logically Collective on TS
1734000e7ae3SMatthew Knepley 
1735000e7ae3SMatthew Knepley   Input Parameters:
1736000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1737000e7ae3SMatthew Knepley - func - The function
1738000e7ae3SMatthew Knepley 
1739000e7ae3SMatthew Knepley   Calling sequence of func:
1740000e7ae3SMatthew Knepley . func (TS ts);
1741000e7ae3SMatthew Knepley 
1742000e7ae3SMatthew Knepley   Level: intermediate
1743000e7ae3SMatthew Knepley 
1744b8123daeSJed Brown   Note:
1745b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1746b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1747b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1748b8123daeSJed Brown 
1749000e7ae3SMatthew Knepley .keywords: TS, timestep
1750b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1751000e7ae3SMatthew Knepley @*/
17527087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1753000e7ae3SMatthew Knepley {
1754000e7ae3SMatthew Knepley   PetscFunctionBegin;
17550700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1756000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1757000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1758000e7ae3SMatthew Knepley }
1759000e7ae3SMatthew Knepley 
1760e74ef692SMatthew Knepley #undef __FUNCT__
17613f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
176209ee8438SJed Brown /*@
17633f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
17643f2090d5SJed Brown 
17653f2090d5SJed Brown   Collective on TS
17663f2090d5SJed Brown 
17673f2090d5SJed Brown   Input Parameters:
17683f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
17693f2090d5SJed Brown 
17703f2090d5SJed Brown   Notes:
17713f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
17723f2090d5SJed Brown   so most users would not generally call this routine themselves.
17733f2090d5SJed Brown 
17743f2090d5SJed Brown   Level: developer
17753f2090d5SJed Brown 
17763f2090d5SJed Brown .keywords: TS, timestep
1777b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
17783f2090d5SJed Brown @*/
17797087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
17803f2090d5SJed Brown {
17813f2090d5SJed Brown   PetscErrorCode ierr;
17823f2090d5SJed Brown 
17833f2090d5SJed Brown   PetscFunctionBegin;
17840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
178572ac3e02SJed Brown   if (ts->ops->prestep) {
17863f2090d5SJed Brown     PetscStackPush("TS PreStep function");
17873f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
17883f2090d5SJed Brown     PetscStackPop;
1789312ce896SJed Brown   }
17903f2090d5SJed Brown   PetscFunctionReturn(0);
17913f2090d5SJed Brown }
17923f2090d5SJed Brown 
17933f2090d5SJed Brown #undef __FUNCT__
1794b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1795b8123daeSJed Brown /*@C
1796b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1797b8123daeSJed Brown   called once at the beginning of each stage.
1798b8123daeSJed Brown 
1799b8123daeSJed Brown   Logically Collective on TS
1800b8123daeSJed Brown 
1801b8123daeSJed Brown   Input Parameters:
1802b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1803b8123daeSJed Brown - func - The function
1804b8123daeSJed Brown 
1805b8123daeSJed Brown   Calling sequence of func:
1806b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1807b8123daeSJed Brown 
1808b8123daeSJed Brown   Level: intermediate
1809b8123daeSJed Brown 
1810b8123daeSJed Brown   Note:
1811b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
1812b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
1813b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
1814b8123daeSJed Brown 
1815b8123daeSJed Brown .keywords: TS, timestep
1816b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
1817b8123daeSJed Brown @*/
1818b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
1819b8123daeSJed Brown {
1820b8123daeSJed Brown   PetscFunctionBegin;
1821b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1822b8123daeSJed Brown   ts->ops->prestage = func;
1823b8123daeSJed Brown   PetscFunctionReturn(0);
1824b8123daeSJed Brown }
1825b8123daeSJed Brown 
1826b8123daeSJed Brown #undef __FUNCT__
1827b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
1828b8123daeSJed Brown /*@
1829b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
1830b8123daeSJed Brown 
1831b8123daeSJed Brown   Collective on TS
1832b8123daeSJed Brown 
1833b8123daeSJed Brown   Input Parameters:
1834b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
1835b8123daeSJed Brown 
1836b8123daeSJed Brown   Notes:
1837b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
1838b8123daeSJed Brown   most users would not generally call this routine themselves.
1839b8123daeSJed Brown 
1840b8123daeSJed Brown   Level: developer
1841b8123daeSJed Brown 
1842b8123daeSJed Brown .keywords: TS, timestep
1843b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
1844b8123daeSJed Brown @*/
1845b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
1846b8123daeSJed Brown {
1847b8123daeSJed Brown   PetscErrorCode ierr;
1848b8123daeSJed Brown 
1849b8123daeSJed Brown   PetscFunctionBegin;
1850b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1851b8123daeSJed Brown   if (ts->ops->prestage) {
1852b8123daeSJed Brown     PetscStackPush("TS PreStage function");
1853b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
1854b8123daeSJed Brown     PetscStackPop;
1855b8123daeSJed Brown   }
1856b8123daeSJed Brown   PetscFunctionReturn(0);
1857b8123daeSJed Brown }
1858b8123daeSJed Brown 
1859b8123daeSJed Brown #undef __FUNCT__
1860e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1861ac226902SBarry Smith /*@C
1862000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
18633f2090d5SJed Brown   called once at the end of each time step.
1864000e7ae3SMatthew Knepley 
18653f9fe445SBarry Smith   Logically Collective on TS
1866000e7ae3SMatthew Knepley 
1867000e7ae3SMatthew Knepley   Input Parameters:
1868000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1869000e7ae3SMatthew Knepley - func - The function
1870000e7ae3SMatthew Knepley 
1871000e7ae3SMatthew Knepley   Calling sequence of func:
1872b8123daeSJed Brown $ func (TS ts);
1873000e7ae3SMatthew Knepley 
1874000e7ae3SMatthew Knepley   Level: intermediate
1875000e7ae3SMatthew Knepley 
1876000e7ae3SMatthew Knepley .keywords: TS, timestep
1877b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
1878000e7ae3SMatthew Knepley @*/
18797087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1880000e7ae3SMatthew Knepley {
1881000e7ae3SMatthew Knepley   PetscFunctionBegin;
18820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1883000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1884000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1885000e7ae3SMatthew Knepley }
1886000e7ae3SMatthew Knepley 
1887e74ef692SMatthew Knepley #undef __FUNCT__
18883f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
188909ee8438SJed Brown /*@
18903f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
18913f2090d5SJed Brown 
18923f2090d5SJed Brown   Collective on TS
18933f2090d5SJed Brown 
18943f2090d5SJed Brown   Input Parameters:
18953f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
18963f2090d5SJed Brown 
18973f2090d5SJed Brown   Notes:
18983f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
18993f2090d5SJed Brown   so most users would not generally call this routine themselves.
19003f2090d5SJed Brown 
19013f2090d5SJed Brown   Level: developer
19023f2090d5SJed Brown 
19033f2090d5SJed Brown .keywords: TS, timestep
19043f2090d5SJed Brown @*/
19057087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
19063f2090d5SJed Brown {
19073f2090d5SJed Brown   PetscErrorCode ierr;
19083f2090d5SJed Brown 
19093f2090d5SJed Brown   PetscFunctionBegin;
19100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
191172ac3e02SJed Brown   if (ts->ops->poststep) {
19123f2090d5SJed Brown     PetscStackPush("TS PostStep function");
19133f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
19143f2090d5SJed Brown     PetscStackPop;
191572ac3e02SJed Brown   }
19163f2090d5SJed Brown   PetscFunctionReturn(0);
19173f2090d5SJed Brown }
19183f2090d5SJed Brown 
1919d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1920d763cef2SBarry Smith 
19214a2ae208SSatish Balay #undef __FUNCT__
1922a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1923d763cef2SBarry Smith /*@C
1924a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1925d763cef2SBarry Smith    timestep to display the iteration's  progress.
1926d763cef2SBarry Smith 
19273f9fe445SBarry Smith    Logically Collective on TS
1928d763cef2SBarry Smith 
1929d763cef2SBarry Smith    Input Parameters:
1930d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1931e213d8f1SJed Brown .  monitor - monitoring routine
1932329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1933b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1934b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1935b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1936d763cef2SBarry Smith 
1937e213d8f1SJed Brown    Calling sequence of monitor:
1938e213d8f1SJed Brown $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1939d763cef2SBarry Smith 
1940d763cef2SBarry Smith +    ts - the TS context
1941d763cef2SBarry Smith .    steps - iteration number
19421f06c33eSBarry Smith .    time - current time
1943d763cef2SBarry Smith .    x - current iterate
1944d763cef2SBarry Smith -    mctx - [optional] monitoring context
1945d763cef2SBarry Smith 
1946d763cef2SBarry Smith    Notes:
1947d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1948d763cef2SBarry Smith    already has been loaded.
1949d763cef2SBarry Smith 
1950025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1951025f1a04SBarry Smith 
1952d763cef2SBarry Smith    Level: intermediate
1953d763cef2SBarry Smith 
1954d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1955d763cef2SBarry Smith 
1956a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1957d763cef2SBarry Smith @*/
1958c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1959d763cef2SBarry Smith {
1960d763cef2SBarry Smith   PetscFunctionBegin;
19610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
196217186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1963d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
19648704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]    = mdestroy;
1965d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1966d763cef2SBarry Smith   PetscFunctionReturn(0);
1967d763cef2SBarry Smith }
1968d763cef2SBarry Smith 
19694a2ae208SSatish Balay #undef __FUNCT__
1970a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1971d763cef2SBarry Smith /*@C
1972a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1973d763cef2SBarry Smith 
19743f9fe445SBarry Smith    Logically Collective on TS
1975d763cef2SBarry Smith 
1976d763cef2SBarry Smith    Input Parameters:
1977d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1978d763cef2SBarry Smith 
1979d763cef2SBarry Smith    Notes:
1980d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1981d763cef2SBarry Smith 
1982d763cef2SBarry Smith    Level: intermediate
1983d763cef2SBarry Smith 
1984d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1985d763cef2SBarry Smith 
1986a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1987d763cef2SBarry Smith @*/
19887087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1989d763cef2SBarry Smith {
1990d952e501SBarry Smith   PetscErrorCode ierr;
1991d952e501SBarry Smith   PetscInt       i;
1992d952e501SBarry Smith 
1993d763cef2SBarry Smith   PetscFunctionBegin;
19940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1995d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
19968704b422SBarry Smith     if (ts->monitordestroy[i]) {
19978704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1998d952e501SBarry Smith     }
1999d952e501SBarry Smith   }
2000d763cef2SBarry Smith   ts->numbermonitors = 0;
2001d763cef2SBarry Smith   PetscFunctionReturn(0);
2002d763cef2SBarry Smith }
2003d763cef2SBarry Smith 
20044a2ae208SSatish Balay #undef __FUNCT__
2005a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2006d8e5e3e6SSatish Balay /*@
2007a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
20085516499fSSatish Balay 
20095516499fSSatish Balay    Level: intermediate
201041251cbbSSatish Balay 
20115516499fSSatish Balay .keywords: TS, set, monitor
20125516499fSSatish Balay 
201341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
201441251cbbSSatish Balay @*/
2015649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2016d763cef2SBarry Smith {
2017dfbe8321SBarry Smith   PetscErrorCode ierr;
2018649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
2019d132466eSBarry Smith 
2020d763cef2SBarry Smith   PetscFunctionBegin;
2021649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2022971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2023649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2024d763cef2SBarry Smith   PetscFunctionReturn(0);
2025d763cef2SBarry Smith }
2026d763cef2SBarry Smith 
20274a2ae208SSatish Balay #undef __FUNCT__
2028cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2029cd652676SJed Brown /*@
2030cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2031cd652676SJed Brown 
2032cd652676SJed Brown    Logically Collective on TS
2033cd652676SJed Brown 
2034cd652676SJed Brown    Input Argument:
2035cd652676SJed Brown .  ts - time stepping context
2036cd652676SJed Brown 
2037cd652676SJed Brown    Output Argument:
2038cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2039cd652676SJed Brown 
2040cd652676SJed Brown    Level: intermediate
2041cd652676SJed Brown 
2042cd652676SJed Brown .keywords: TS, set
2043cd652676SJed Brown 
2044cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2045cd652676SJed Brown @*/
2046cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2047cd652676SJed Brown {
2048cd652676SJed Brown 
2049cd652676SJed Brown   PetscFunctionBegin;
2050cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2051cd652676SJed Brown   ts->retain_stages = flg;
2052cd652676SJed Brown   PetscFunctionReturn(0);
2053cd652676SJed Brown }
2054cd652676SJed Brown 
2055cd652676SJed Brown #undef __FUNCT__
2056cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2057cd652676SJed Brown /*@
2058cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2059cd652676SJed Brown 
2060cd652676SJed Brown    Collective on TS
2061cd652676SJed Brown 
2062cd652676SJed Brown    Input Argument:
2063cd652676SJed Brown +  ts - time stepping context
2064cd652676SJed Brown -  t - time to interpolate to
2065cd652676SJed Brown 
2066cd652676SJed Brown    Output Argument:
2067cd652676SJed Brown .  X - state at given time
2068cd652676SJed Brown 
2069cd652676SJed Brown    Notes:
2070cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2071cd652676SJed Brown 
2072cd652676SJed Brown    Level: intermediate
2073cd652676SJed Brown 
2074cd652676SJed Brown    Developer Notes:
2075cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2076cd652676SJed Brown 
2077cd652676SJed Brown .keywords: TS, set
2078cd652676SJed Brown 
2079cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2080cd652676SJed Brown @*/
2081cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X)
2082cd652676SJed Brown {
2083cd652676SJed Brown   PetscErrorCode ierr;
2084cd652676SJed Brown 
2085cd652676SJed Brown   PetscFunctionBegin;
2086cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2087d8cd7023SBarry 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);
2088cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
2089cd652676SJed Brown   ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr);
2090cd652676SJed Brown   PetscFunctionReturn(0);
2091cd652676SJed Brown }
2092cd652676SJed Brown 
2093cd652676SJed Brown #undef __FUNCT__
20944a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2095d763cef2SBarry Smith /*@
20966d9e5789SSean Farley    TSStep - Steps one time step
2097d763cef2SBarry Smith 
2098d763cef2SBarry Smith    Collective on TS
2099d763cef2SBarry Smith 
2100d763cef2SBarry Smith    Input Parameter:
2101d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2102d763cef2SBarry Smith 
21036d9e5789SSean Farley    Level: intermediate
2104d763cef2SBarry Smith 
2105b8123daeSJed Brown    Notes:
2106b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2107b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2108b8123daeSJed Brown 
2109d763cef2SBarry Smith .keywords: TS, timestep, solve
2110d763cef2SBarry Smith 
2111b8123daeSJed Brown .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage()
2112d763cef2SBarry Smith @*/
2113193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2114d763cef2SBarry Smith {
2115362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2116dfbe8321SBarry Smith   PetscErrorCode ierr;
2117d763cef2SBarry Smith 
2118d763cef2SBarry Smith   PetscFunctionBegin;
21190700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2120d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2121d405a339SMatthew Knepley 
2122362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2123362cd11cSLisandro Dalcin 
2124362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2125d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2126193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2127d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2128362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2129362cd11cSLisandro Dalcin 
2130362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2131cef5090cSJed Brown     if (ts->errorifstepfailed) {
2132cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2133cef5090cSJed 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]);
2134cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2135cef5090cSJed Brown     }
2136362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2137362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2138362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2139362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2140362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2141362cd11cSLisandro Dalcin   }
2142362cd11cSLisandro Dalcin 
2143d763cef2SBarry Smith   PetscFunctionReturn(0);
2144d763cef2SBarry Smith }
2145d763cef2SBarry Smith 
21464a2ae208SSatish Balay #undef __FUNCT__
214705175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
214805175c85SJed Brown /*@
214905175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
215005175c85SJed Brown 
21511c3436cfSJed Brown    Collective on TS
215205175c85SJed Brown 
215305175c85SJed Brown    Input Arguments:
21541c3436cfSJed Brown +  ts - time stepping context
21551c3436cfSJed Brown .  order - desired order of accuracy
21561c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
215705175c85SJed Brown 
215805175c85SJed Brown    Output Arguments:
21591c3436cfSJed Brown .  X - state at the end of the current step
216005175c85SJed Brown 
216105175c85SJed Brown    Level: advanced
216205175c85SJed Brown 
2163108c343cSJed Brown    Notes:
2164108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2165108c343cSJed 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.
2166108c343cSJed Brown 
21671c3436cfSJed Brown .seealso: TSStep(), TSAdapt
216805175c85SJed Brown @*/
21691c3436cfSJed Brown PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec X,PetscBool *done)
217005175c85SJed Brown {
217105175c85SJed Brown   PetscErrorCode ierr;
217205175c85SJed Brown 
217305175c85SJed Brown   PetscFunctionBegin;
217405175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
217505175c85SJed Brown   PetscValidType(ts,1);
217605175c85SJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
21771c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
21781c3436cfSJed Brown   ierr = (*ts->ops->evaluatestep)(ts,order,X,done);CHKERRQ(ierr);
217905175c85SJed Brown   PetscFunctionReturn(0);
218005175c85SJed Brown }
218105175c85SJed Brown 
218205175c85SJed Brown #undef __FUNCT__
21836a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
21846a4d4014SLisandro Dalcin /*@
21856a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
21866a4d4014SLisandro Dalcin 
21876a4d4014SLisandro Dalcin    Collective on TS
21886a4d4014SLisandro Dalcin 
21896a4d4014SLisandro Dalcin    Input Parameter:
21906a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2191362cd11cSLisandro Dalcin -  x - the solution vector
21926a4d4014SLisandro Dalcin 
21935a3a76d0SJed Brown    Output Parameter:
21945a3a76d0SJed Brown .  ftime - time of the state vector x upon completion
21955a3a76d0SJed Brown 
21966a4d4014SLisandro Dalcin    Level: beginner
21976a4d4014SLisandro Dalcin 
21985a3a76d0SJed Brown    Notes:
21995a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
22005a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
22015a3a76d0SJed Brown    stepped over the final time.
22025a3a76d0SJed Brown 
22036a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
22046a4d4014SLisandro Dalcin 
22056a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
22066a4d4014SLisandro Dalcin @*/
22075a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime)
22086a4d4014SLisandro Dalcin {
22094d7d938eSLisandro Dalcin   PetscBool      flg;
22104d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
22114d7d938eSLisandro Dalcin   PetscViewer    viewer;
22126a4d4014SLisandro Dalcin   PetscErrorCode ierr;
2213f22f69f0SBarry Smith 
22146a4d4014SLisandro Dalcin   PetscFunctionBegin;
22150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2216193ac0bcSJed Brown   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
22175a3a76d0SJed Brown   if (ts->exact_final_time) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
22185a3a76d0SJed Brown     if (!ts->vec_sol || x == ts->vec_sol) {
22195a3a76d0SJed Brown       Vec y;
22205a3a76d0SJed Brown       ierr = VecDuplicate(x,&y);CHKERRQ(ierr);
22215a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
22225a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
22235a3a76d0SJed Brown     }
2224362cd11cSLisandro Dalcin     ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr);
22255a3a76d0SJed Brown   } else {
2226193ac0bcSJed Brown     ierr = TSSetSolution(ts,x);CHKERRQ(ierr);
22275a3a76d0SJed Brown   }
2228b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
22296a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2230193ac0bcSJed Brown   ts->steps = 0;
22315ef26d82SJed Brown   ts->ksp_its = 0;
22325ef26d82SJed Brown   ts->snes_its = 0;
2233c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2234c610991cSLisandro Dalcin   ts->reject = 0;
2235193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
2236193ac0bcSJed Brown 
2237193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2238193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
22395a3a76d0SJed Brown     ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
2240a5b82c69SSean Farley     if (ftime) *ftime = ts->ptime;
2241193ac0bcSJed Brown   } else {
22426a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2243362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2244362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2245362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2246362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2247362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2248e1a7a14fSJed Brown     while (!ts->reason) {
2249193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2250193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2251193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2252193ac0bcSJed Brown     }
2253362cd11cSLisandro Dalcin     if (ts->exact_final_time && ts->ptime > ts->max_time) {
2254a43b19c4SJed Brown       ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr);
22555a3a76d0SJed Brown       if (ftime) *ftime = ts->max_time;
22560574a7fbSJed Brown     } else {
22570574a7fbSJed Brown       ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
22580574a7fbSJed Brown       if (ftime) *ftime = ts->ptime;
22590574a7fbSJed Brown     }
2260193ac0bcSJed Brown   }
22613923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
22624d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
22634d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
22644d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
22654d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
22664d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2267193ac0bcSJed Brown   }
22686a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
22696a4d4014SLisandro Dalcin }
22706a4d4014SLisandro Dalcin 
22716a4d4014SLisandro Dalcin #undef __FUNCT__
22724a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2273228d79bcSJed Brown /*@
2274228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2275228d79bcSJed Brown 
2276228d79bcSJed Brown    Collective on TS
2277228d79bcSJed Brown 
2278228d79bcSJed Brown    Input Parameters:
2279228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2280228d79bcSJed Brown .  step - step number that has just completed
2281228d79bcSJed Brown .  ptime - model time of the state
2282228d79bcSJed Brown -  x - state at the current model time
2283228d79bcSJed Brown 
2284228d79bcSJed Brown    Notes:
2285228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2286228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2287228d79bcSJed Brown 
2288228d79bcSJed Brown    Level: advanced
2289228d79bcSJed Brown 
2290228d79bcSJed Brown .keywords: TS, timestep
2291228d79bcSJed Brown @*/
2292a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
2293d763cef2SBarry Smith {
22946849ba73SBarry Smith   PetscErrorCode ierr;
2295a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2296d763cef2SBarry Smith 
2297d763cef2SBarry Smith   PetscFunctionBegin;
2298d763cef2SBarry Smith   for (i=0; i<n; i++) {
2299142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
2300d763cef2SBarry Smith   }
2301d763cef2SBarry Smith   PetscFunctionReturn(0);
2302d763cef2SBarry Smith }
2303d763cef2SBarry Smith 
2304d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
23050b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
23060b039ecaSBarry Smith   PetscDrawLG lg;
23070b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2308201da799SBarry Smith   PetscInt    ksp_its,snes_its;
23090b039ecaSBarry Smith };
23100b039ecaSBarry Smith 
2311d763cef2SBarry Smith 
23124a2ae208SSatish Balay #undef __FUNCT__
2313a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2314d763cef2SBarry Smith /*@C
2315a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2316a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2317d763cef2SBarry Smith 
2318d763cef2SBarry Smith    Collective on TS
2319d763cef2SBarry Smith 
2320d763cef2SBarry Smith    Input Parameters:
2321d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2322d763cef2SBarry Smith .  label - the title to put in the title bar
23237c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2324a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2325a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2326d763cef2SBarry Smith 
2327d763cef2SBarry Smith    Output Parameter:
23280b039ecaSBarry Smith .  ctx - the context
2329d763cef2SBarry Smith 
2330d763cef2SBarry Smith    Options Database Key:
23314f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
23324f09c107SBarry Smith .  -ts_monitor_lg_solution -
23334f09c107SBarry Smith -  -ts_monitor_lg_error -
2334d763cef2SBarry Smith 
2335d763cef2SBarry Smith    Notes:
2336a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2337d763cef2SBarry Smith 
2338d763cef2SBarry Smith    Level: intermediate
2339d763cef2SBarry Smith 
23407c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2341d763cef2SBarry Smith 
23424f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
23437c922b88SBarry Smith 
2344d763cef2SBarry Smith @*/
2345a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2346d763cef2SBarry Smith {
2347b0a32e0cSBarry Smith   PetscDraw      win;
2348dfbe8321SBarry Smith   PetscErrorCode ierr;
2349d763cef2SBarry Smith 
2350d763cef2SBarry Smith   PetscFunctionBegin;
2351201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2352a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
2353b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
23540b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
23550b039ecaSBarry Smith   ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
23560b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2357a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2358d763cef2SBarry Smith   PetscFunctionReturn(0);
2359d763cef2SBarry Smith }
2360d763cef2SBarry Smith 
23614a2ae208SSatish Balay #undef __FUNCT__
23624f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
23634f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2364d763cef2SBarry Smith {
23650b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2366c32365f1SBarry Smith   PetscReal      x = ptime,y;
2367dfbe8321SBarry Smith   PetscErrorCode ierr;
2368d763cef2SBarry Smith 
2369d763cef2SBarry Smith   PetscFunctionBegin;
2370a9f9c1f6SBarry Smith   if (!n) {
2371a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
2372a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2373a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2374a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2375a9f9c1f6SBarry Smith   }
2376c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
23770b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
23783923b477SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){
23790b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
23803923b477SBarry Smith   }
2381d763cef2SBarry Smith   PetscFunctionReturn(0);
2382d763cef2SBarry Smith }
2383d763cef2SBarry Smith 
23844a2ae208SSatish Balay #undef __FUNCT__
2385a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2386d763cef2SBarry Smith /*@C
2387a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2388a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2389d763cef2SBarry Smith 
23900b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2391d763cef2SBarry Smith 
2392d763cef2SBarry Smith    Input Parameter:
23930b039ecaSBarry Smith .  ctx - the monitor context
2394d763cef2SBarry Smith 
2395d763cef2SBarry Smith    Level: intermediate
2396d763cef2SBarry Smith 
2397d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2398d763cef2SBarry Smith 
23994f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2400d763cef2SBarry Smith @*/
2401a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2402d763cef2SBarry Smith {
2403b0a32e0cSBarry Smith   PetscDraw      draw;
2404dfbe8321SBarry Smith   PetscErrorCode ierr;
2405d763cef2SBarry Smith 
2406d763cef2SBarry Smith   PetscFunctionBegin;
24070b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
24086bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
24090b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
24100b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2411d763cef2SBarry Smith   PetscFunctionReturn(0);
2412d763cef2SBarry Smith }
2413d763cef2SBarry Smith 
24144a2ae208SSatish Balay #undef __FUNCT__
24154a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2416d763cef2SBarry Smith /*@
2417b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2418d763cef2SBarry Smith 
2419d763cef2SBarry Smith    Not Collective
2420d763cef2SBarry Smith 
2421d763cef2SBarry Smith    Input Parameter:
2422d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2423d763cef2SBarry Smith 
2424d763cef2SBarry Smith    Output Parameter:
2425d763cef2SBarry Smith .  t  - the current time
2426d763cef2SBarry Smith 
2427d763cef2SBarry Smith    Level: beginner
2428d763cef2SBarry Smith 
2429b8123daeSJed Brown    Note:
2430b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2431b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2432b8123daeSJed Brown 
2433d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2434d763cef2SBarry Smith 
2435d763cef2SBarry Smith .keywords: TS, get, time
2436d763cef2SBarry Smith @*/
24377087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2438d763cef2SBarry Smith {
2439d763cef2SBarry Smith   PetscFunctionBegin;
24400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2441f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2442d763cef2SBarry Smith   *t = ts->ptime;
2443d763cef2SBarry Smith   PetscFunctionReturn(0);
2444d763cef2SBarry Smith }
2445d763cef2SBarry Smith 
24464a2ae208SSatish Balay #undef __FUNCT__
24476a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
24486a4d4014SLisandro Dalcin /*@
24496a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
24506a4d4014SLisandro Dalcin 
24513f9fe445SBarry Smith    Logically Collective on TS
24526a4d4014SLisandro Dalcin 
24536a4d4014SLisandro Dalcin    Input Parameters:
24546a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
24556a4d4014SLisandro Dalcin -  time - the time
24566a4d4014SLisandro Dalcin 
24576a4d4014SLisandro Dalcin    Level: intermediate
24586a4d4014SLisandro Dalcin 
24596a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
24606a4d4014SLisandro Dalcin 
24616a4d4014SLisandro Dalcin .keywords: TS, set, time
24626a4d4014SLisandro Dalcin @*/
24637087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
24646a4d4014SLisandro Dalcin {
24656a4d4014SLisandro Dalcin   PetscFunctionBegin;
24660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2467c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
24686a4d4014SLisandro Dalcin   ts->ptime = t;
24696a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
24706a4d4014SLisandro Dalcin }
24716a4d4014SLisandro Dalcin 
24726a4d4014SLisandro Dalcin #undef __FUNCT__
24734a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2474d763cef2SBarry Smith /*@C
2475d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2476d763cef2SBarry Smith    TS options in the database.
2477d763cef2SBarry Smith 
24783f9fe445SBarry Smith    Logically Collective on TS
2479d763cef2SBarry Smith 
2480d763cef2SBarry Smith    Input Parameter:
2481d763cef2SBarry Smith +  ts     - The TS context
2482d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2483d763cef2SBarry Smith 
2484d763cef2SBarry Smith    Notes:
2485d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2486d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2487d763cef2SBarry Smith    hyphen.
2488d763cef2SBarry Smith 
2489d763cef2SBarry Smith    Level: advanced
2490d763cef2SBarry Smith 
2491d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2492d763cef2SBarry Smith 
2493d763cef2SBarry Smith .seealso: TSSetFromOptions()
2494d763cef2SBarry Smith 
2495d763cef2SBarry Smith @*/
24967087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2497d763cef2SBarry Smith {
2498dfbe8321SBarry Smith   PetscErrorCode ierr;
2499089b2837SJed Brown   SNES           snes;
2500d763cef2SBarry Smith 
2501d763cef2SBarry Smith   PetscFunctionBegin;
25020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2503d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2504089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2505089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2506d763cef2SBarry Smith   PetscFunctionReturn(0);
2507d763cef2SBarry Smith }
2508d763cef2SBarry Smith 
2509d763cef2SBarry Smith 
25104a2ae208SSatish Balay #undef __FUNCT__
25114a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2512d763cef2SBarry Smith /*@C
2513d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2514d763cef2SBarry Smith    TS options in the database.
2515d763cef2SBarry Smith 
25163f9fe445SBarry Smith    Logically Collective on TS
2517d763cef2SBarry Smith 
2518d763cef2SBarry Smith    Input Parameter:
2519d763cef2SBarry Smith +  ts     - The TS context
2520d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2521d763cef2SBarry Smith 
2522d763cef2SBarry Smith    Notes:
2523d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2524d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2525d763cef2SBarry Smith    hyphen.
2526d763cef2SBarry Smith 
2527d763cef2SBarry Smith    Level: advanced
2528d763cef2SBarry Smith 
2529d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2530d763cef2SBarry Smith 
2531d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2532d763cef2SBarry Smith 
2533d763cef2SBarry Smith @*/
25347087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2535d763cef2SBarry Smith {
2536dfbe8321SBarry Smith   PetscErrorCode ierr;
2537089b2837SJed Brown   SNES           snes;
2538d763cef2SBarry Smith 
2539d763cef2SBarry Smith   PetscFunctionBegin;
25400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2541d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2542089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2543089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2544d763cef2SBarry Smith   PetscFunctionReturn(0);
2545d763cef2SBarry Smith }
2546d763cef2SBarry Smith 
25474a2ae208SSatish Balay #undef __FUNCT__
25484a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2549d763cef2SBarry Smith /*@C
2550d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2551d763cef2SBarry Smith    TS options in the database.
2552d763cef2SBarry Smith 
2553d763cef2SBarry Smith    Not Collective
2554d763cef2SBarry Smith 
2555d763cef2SBarry Smith    Input Parameter:
2556d763cef2SBarry Smith .  ts - The TS context
2557d763cef2SBarry Smith 
2558d763cef2SBarry Smith    Output Parameter:
2559d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2560d763cef2SBarry Smith 
2561d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2562d763cef2SBarry Smith    sufficient length to hold the prefix.
2563d763cef2SBarry Smith 
2564d763cef2SBarry Smith    Level: intermediate
2565d763cef2SBarry Smith 
2566d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2567d763cef2SBarry Smith 
2568d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2569d763cef2SBarry Smith @*/
25707087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2571d763cef2SBarry Smith {
2572dfbe8321SBarry Smith   PetscErrorCode ierr;
2573d763cef2SBarry Smith 
2574d763cef2SBarry Smith   PetscFunctionBegin;
25750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25764482741eSBarry Smith   PetscValidPointer(prefix,2);
2577d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2578d763cef2SBarry Smith   PetscFunctionReturn(0);
2579d763cef2SBarry Smith }
2580d763cef2SBarry Smith 
25814a2ae208SSatish Balay #undef __FUNCT__
25824a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2583d763cef2SBarry Smith /*@C
2584d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2585d763cef2SBarry Smith 
2586d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2587d763cef2SBarry Smith 
2588d763cef2SBarry Smith    Input Parameter:
2589d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2590d763cef2SBarry Smith 
2591d763cef2SBarry Smith    Output Parameters:
2592d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2593d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2594089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2595d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2596d763cef2SBarry Smith 
2597d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2598d763cef2SBarry Smith 
2599d763cef2SBarry Smith    Level: intermediate
2600d763cef2SBarry Smith 
260126d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2602d763cef2SBarry Smith 
2603d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2604d763cef2SBarry Smith @*/
2605089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2606d763cef2SBarry Smith {
2607089b2837SJed Brown   PetscErrorCode ierr;
2608089b2837SJed Brown   SNES           snes;
260924989b8cSPeter Brune   DM             dm;
2610089b2837SJed Brown 
2611d763cef2SBarry Smith   PetscFunctionBegin;
2612089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2613089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
261424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
261524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2616d763cef2SBarry Smith   PetscFunctionReturn(0);
2617d763cef2SBarry Smith }
2618d763cef2SBarry Smith 
26191713a123SBarry Smith #undef __FUNCT__
26202eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
26212eca1d9cSJed Brown /*@C
26222eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
26232eca1d9cSJed Brown 
26242eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
26252eca1d9cSJed Brown 
26262eca1d9cSJed Brown    Input Parameter:
26272eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
26282eca1d9cSJed Brown 
26292eca1d9cSJed Brown    Output Parameters:
26302eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
26312eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
26322eca1d9cSJed Brown .  f   - The function to compute the matrices
26332eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
26342eca1d9cSJed Brown 
26352eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
26362eca1d9cSJed Brown 
26372eca1d9cSJed Brown    Level: advanced
26382eca1d9cSJed Brown 
26392eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
26402eca1d9cSJed Brown 
26412eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
26422eca1d9cSJed Brown @*/
26437087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
26442eca1d9cSJed Brown {
2645089b2837SJed Brown   PetscErrorCode ierr;
2646089b2837SJed Brown   SNES           snes;
264724989b8cSPeter Brune   DM             dm;
26482eca1d9cSJed Brown   PetscFunctionBegin;
2649089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2650089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
265124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
265224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
26532eca1d9cSJed Brown   PetscFunctionReturn(0);
26542eca1d9cSJed Brown }
26552eca1d9cSJed Brown 
26566083293cSBarry Smith typedef struct {
26576083293cSBarry Smith   PetscViewer viewer;
26586083293cSBarry Smith   Vec         initialsolution;
26596083293cSBarry Smith   PetscBool   showinitial;
26606083293cSBarry Smith } TSMonitorSolutionCtx;
26616083293cSBarry Smith 
26622eca1d9cSJed Brown #undef __FUNCT__
2663a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
26641713a123SBarry Smith /*@C
2665a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
26661713a123SBarry Smith    VecView() for the solution at each timestep
26671713a123SBarry Smith 
26681713a123SBarry Smith    Collective on TS
26691713a123SBarry Smith 
26701713a123SBarry Smith    Input Parameters:
26711713a123SBarry Smith +  ts - the TS context
26721713a123SBarry Smith .  step - current time-step
2673142b95e3SSatish Balay .  ptime - current time
26741713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
26751713a123SBarry Smith 
26761713a123SBarry Smith    Level: intermediate
26771713a123SBarry Smith 
26781713a123SBarry Smith .keywords: TS,  vector, monitor, view
26791713a123SBarry Smith 
2680a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
26811713a123SBarry Smith @*/
26827087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
26831713a123SBarry Smith {
2684dfbe8321SBarry Smith   PetscErrorCode       ierr;
26856083293cSBarry Smith   TSMonitorSolutionCtx *ictx = (TSMonitorSolutionCtx*)dummy;
26861713a123SBarry Smith 
26871713a123SBarry Smith   PetscFunctionBegin;
26886083293cSBarry Smith   if (!step && ictx->showinitial) {
26896083293cSBarry Smith     if (!ictx->initialsolution) {
26906083293cSBarry Smith       ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr);
26911713a123SBarry Smith     }
26926083293cSBarry Smith     ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr);
26936083293cSBarry Smith   }
26946083293cSBarry Smith   if (ictx->showinitial) {
26956083293cSBarry Smith     PetscReal pause;
26966083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
26976083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
26986083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
26996083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
27006083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
27016083293cSBarry Smith   }
27026083293cSBarry Smith   ierr = VecView(x,ictx->viewer);CHKERRQ(ierr);
27036083293cSBarry Smith   if (ictx->showinitial) {
27046083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
27056083293cSBarry Smith   }
27061713a123SBarry Smith   PetscFunctionReturn(0);
27071713a123SBarry Smith }
27081713a123SBarry Smith 
27091713a123SBarry Smith 
27106c699258SBarry Smith #undef __FUNCT__
27116083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionDestroy"
27126083293cSBarry Smith /*@C
27136083293cSBarry Smith    TSMonitorSolutionDestroy - Destroys the monitor context for TSMonitorSolution
27146083293cSBarry Smith 
27156083293cSBarry Smith    Collective on TS
27166083293cSBarry Smith 
27176083293cSBarry Smith    Input Parameters:
27186083293cSBarry Smith .    ctx - the monitor context
27196083293cSBarry Smith 
27206083293cSBarry Smith    Level: intermediate
27216083293cSBarry Smith 
27226083293cSBarry Smith .keywords: TS,  vector, monitor, view
27236083293cSBarry Smith 
27246083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
27256083293cSBarry Smith @*/
27266083293cSBarry Smith PetscErrorCode  TSMonitorSolutionDestroy(void **ctx)
27276083293cSBarry Smith {
27286083293cSBarry Smith   PetscErrorCode       ierr;
27296083293cSBarry Smith   TSMonitorSolutionCtx *ictx = *(TSMonitorSolutionCtx**)ctx;
27306083293cSBarry Smith 
27316083293cSBarry Smith   PetscFunctionBegin;
27326083293cSBarry Smith   ierr = PetscViewerDestroy(&ictx->viewer);CHKERRQ(ierr);
27336083293cSBarry Smith   ierr = VecDestroy(&ictx->initialsolution);CHKERRQ(ierr);
27346083293cSBarry Smith   ierr = PetscFree(ictx);CHKERRQ(ierr);
27356083293cSBarry Smith   PetscFunctionReturn(0);
27366083293cSBarry Smith }
27376083293cSBarry Smith 
27386083293cSBarry Smith #undef __FUNCT__
27396083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionCreate"
27406083293cSBarry Smith /*@C
27416083293cSBarry Smith    TSMonitorSolutionCreate - Creates the monitor context for TSMonitorSolution
27426083293cSBarry Smith 
27436083293cSBarry Smith    Collective on TS
27446083293cSBarry Smith 
27456083293cSBarry Smith    Input Parameter:
27466083293cSBarry Smith .    ts - time-step context
27476083293cSBarry Smith 
27486083293cSBarry Smith    Output Patameter:
27496083293cSBarry Smith .    ctx - the monitor context
27506083293cSBarry Smith 
27516083293cSBarry Smith    Level: intermediate
27526083293cSBarry Smith 
27536083293cSBarry Smith .keywords: TS,  vector, monitor, view
27546083293cSBarry Smith 
27556083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
27566083293cSBarry Smith @*/
27576083293cSBarry Smith PetscErrorCode  TSMonitorSolutionCreate(TS ts,PetscViewer viewer,void **ctx)
27586083293cSBarry Smith {
27596083293cSBarry Smith   PetscErrorCode       ierr;
27606083293cSBarry Smith   TSMonitorSolutionCtx *ictx;
27616083293cSBarry Smith 
27626083293cSBarry Smith   PetscFunctionBegin;
27636083293cSBarry Smith   ierr = PetscNew(TSMonitorSolutionCtx,&ictx);CHKERRQ(ierr);
27646083293cSBarry Smith   *ctx = (void*)ictx;
27656083293cSBarry Smith   if (!viewer) {
27666083293cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
27676083293cSBarry Smith   }
27686083293cSBarry Smith   ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr);
27696083293cSBarry Smith   ictx->viewer      = viewer;
27706083293cSBarry Smith   ictx->showinitial = PETSC_FALSE;
27716083293cSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix,"-ts_monitor_solution_initial",&ictx->showinitial,PETSC_NULL);CHKERRQ(ierr);
27726083293cSBarry Smith   PetscFunctionReturn(0);
27736083293cSBarry Smith }
27746083293cSBarry Smith 
27756083293cSBarry Smith #undef __FUNCT__
2776*3a471f94SBarry Smith #define __FUNCT__ "TSMonitorError"
2777*3a471f94SBarry Smith /*@C
2778*3a471f94SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by calling
2779*3a471f94SBarry Smith    VecView() for the error at each timestep
2780*3a471f94SBarry Smith 
2781*3a471f94SBarry Smith    Collective on TS
2782*3a471f94SBarry Smith 
2783*3a471f94SBarry Smith    Input Parameters:
2784*3a471f94SBarry Smith +  ts - the TS context
2785*3a471f94SBarry Smith .  step - current time-step
2786*3a471f94SBarry Smith .  ptime - current time
2787*3a471f94SBarry Smith -  dummy - either a viewer or PETSC_NULL
2788*3a471f94SBarry Smith 
2789*3a471f94SBarry Smith    Level: intermediate
2790*3a471f94SBarry Smith 
2791*3a471f94SBarry Smith .keywords: TS,  vector, monitor, view
2792*3a471f94SBarry Smith 
2793*3a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
2794*3a471f94SBarry Smith @*/
2795*3a471f94SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
2796*3a471f94SBarry Smith {
2797*3a471f94SBarry Smith   PetscErrorCode ierr;
2798*3a471f94SBarry Smith   PetscViewer    viewer = (PetscViewer)dummy;
2799*3a471f94SBarry Smith   Vec            work;
2800*3a471f94SBarry Smith 
2801*3a471f94SBarry Smith   PetscFunctionBegin;
2802*3a471f94SBarry Smith   ierr = VecDuplicate(x,&work);CHKERRQ(ierr);
2803*3a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
2804*3a471f94SBarry Smith   ierr = VecAXPY(work,-1.0,x);CHKERRQ(ierr);
2805*3a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
2806*3a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
2807*3a471f94SBarry Smith   PetscFunctionReturn(0);
2808*3a471f94SBarry Smith }
2809*3a471f94SBarry Smith 
2810*3a471f94SBarry 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);
40743923b477SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((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);
41443923b477SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((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);
4170201da799SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((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);
4197201da799SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((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