xref: /petsc/src/ts/interface/ts.c (revision 0910c3306e5dc43317db86cbd626dc4b7f402a86)
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     }
1618189c53fSBarry Smith     ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1628189c53fSBarry Smith     if (opt) {
1638189c53fSBarry Smith       TSMonitorSPEigCtx ctx;
1648189c53fSBarry Smith       PetscInt          howoften = 1;
1658189c53fSBarry Smith 
1668189c53fSBarry Smith       ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
1678189c53fSBarry Smith       ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
1688189c53fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
1698189c53fSBarry Smith     }
170ef20d060SBarry Smith     opt  = PETSC_FALSE;
1710dcf80beSBarry Smith     ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
172a7cc72afSBarry Smith     if (opt) {
17383a4ac43SBarry Smith       TSMonitorDrawCtx ctx;
17483a4ac43SBarry Smith       PetscInt         howoften = 1;
175a80ad3e0SBarry Smith 
17683a4ac43SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
17783a4ac43SBarry Smith       ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
17883a4ac43SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
179bdad233fSMatthew Knepley     }
180fb1732b5SBarry Smith     opt  = PETSC_FALSE;
1810dcf80beSBarry Smith     ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
1823a471f94SBarry Smith     if (opt) {
18383a4ac43SBarry Smith       TSMonitorDrawCtx ctx;
18483a4ac43SBarry Smith       PetscInt         howoften = 1;
1853a471f94SBarry Smith 
18683a4ac43SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
18783a4ac43SBarry Smith       ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
18883a4ac43SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
1893a471f94SBarry Smith     }
1903a471f94SBarry Smith     opt  = PETSC_FALSE;
191fb1732b5SBarry Smith     ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
192fb1732b5SBarry Smith     if (flg) {
193fb1732b5SBarry Smith       PetscViewer ctx;
194fb1732b5SBarry Smith       if (monfilename[0]) {
195fb1732b5SBarry Smith         ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
196fb1732b5SBarry Smith       } else {
197fb1732b5SBarry Smith         ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
198fb1732b5SBarry Smith       }
199fb1732b5SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
200fb1732b5SBarry Smith     }
201ed81e22dSJed Brown     opt  = PETSC_FALSE;
202ed81e22dSJed 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);
203ed81e22dSJed Brown     if (flg) {
204ed81e22dSJed Brown       const char *ptr,*ptr2;
205ed81e22dSJed Brown       char *filetemplate;
206ed81e22dSJed Brown       if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
207ed81e22dSJed Brown       /* Do some cursory validation of the input. */
208ed81e22dSJed Brown       ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
209ed81e22dSJed Brown       if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
210ed81e22dSJed Brown       for (ptr++ ; ptr && *ptr; ptr++) {
211ed81e22dSJed Brown         ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
212ed81e22dSJed 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");
213ed81e22dSJed Brown         if (ptr2) break;
214ed81e22dSJed Brown       }
215ed81e22dSJed Brown       ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
216ed81e22dSJed Brown       ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
217ed81e22dSJed Brown     }
218bdad233fSMatthew Knepley 
2191c3436cfSJed Brown     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
2201c3436cfSJed Brown     ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2211c3436cfSJed Brown 
222d52bd9f3SBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
223d52bd9f3SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
224d52bd9f3SBarry Smith 
225bdad233fSMatthew Knepley     /* Handle specific TS options */
226abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
227bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
228bdad233fSMatthew Knepley     }
2295d973c19SBarry Smith 
2305d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
2315d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
232bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
233bdad233fSMatthew Knepley   PetscFunctionReturn(0);
234bdad233fSMatthew Knepley }
235bdad233fSMatthew Knepley 
236bdad233fSMatthew Knepley #undef __FUNCT__
237cdcf91faSSean Farley #undef __FUNCT__
2384a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
239a7a1495cSBarry Smith /*@
2408c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
241a7a1495cSBarry Smith       set with TSSetRHSJacobian().
242a7a1495cSBarry Smith 
243a7a1495cSBarry Smith    Collective on TS and Vec
244a7a1495cSBarry Smith 
245a7a1495cSBarry Smith    Input Parameters:
246316643e7SJed Brown +  ts - the TS context
247a7a1495cSBarry Smith .  t - current timestep
248*0910c330SBarry Smith -  U - input vector
249a7a1495cSBarry Smith 
250a7a1495cSBarry Smith    Output Parameters:
251a7a1495cSBarry Smith +  A - Jacobian matrix
252a7a1495cSBarry Smith .  B - optional preconditioning matrix
253a7a1495cSBarry Smith -  flag - flag indicating matrix structure
254a7a1495cSBarry Smith 
255a7a1495cSBarry Smith    Notes:
256a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
257a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
258a7a1495cSBarry Smith 
25994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
260a7a1495cSBarry Smith    flag parameter.
261a7a1495cSBarry Smith 
262a7a1495cSBarry Smith    Level: developer
263a7a1495cSBarry Smith 
264a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
265a7a1495cSBarry Smith 
26694b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
267a7a1495cSBarry Smith @*/
268*0910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
269a7a1495cSBarry Smith {
270dfbe8321SBarry Smith   PetscErrorCode ierr;
271*0910c330SBarry Smith   PetscInt       Ustate;
27224989b8cSPeter Brune   DM             dm;
27324989b8cSPeter Brune   TSDM           tsdm;
27424989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
27524989b8cSPeter Brune   void           *ctx;
27624989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
277a7a1495cSBarry Smith 
278a7a1495cSBarry Smith   PetscFunctionBegin;
2790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
280*0910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
281*0910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
28224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
28324989b8cSPeter Brune   ierr = DMTSGetContext(dm,&tsdm);CHKERRQ(ierr);
28424989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
28524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
286*0910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
287*0910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
2880e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
2890e4ef248SJed Brown     PetscFunctionReturn(0);
290f8ede8e7SJed Brown   }
291d90be118SSean Farley 
29224989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
293d90be118SSean Farley 
29424989b8cSPeter Brune   if (rhsjacobianfunc) {
295*0910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
296a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
297a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
298*0910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
299a7a1495cSBarry Smith     PetscStackPop;
300*0910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
301a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3020700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3030700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
304ef66eb69SBarry Smith   } else {
305214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
306214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
307214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
308ef66eb69SBarry Smith   }
3090e4ef248SJed Brown   ts->rhsjacobian.time = t;
310*0910c330SBarry Smith   ts->rhsjacobian.X = U;
311*0910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3120e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
313a7a1495cSBarry Smith   PetscFunctionReturn(0);
314a7a1495cSBarry Smith }
315a7a1495cSBarry Smith 
3164a2ae208SSatish Balay #undef __FUNCT__
3174a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
318316643e7SJed Brown /*@
319d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
320d763cef2SBarry Smith 
321316643e7SJed Brown    Collective on TS and Vec
322316643e7SJed Brown 
323316643e7SJed Brown    Input Parameters:
324316643e7SJed Brown +  ts - the TS context
325316643e7SJed Brown .  t - current time
326*0910c330SBarry Smith -  U - state vector
327316643e7SJed Brown 
328316643e7SJed Brown    Output Parameter:
329316643e7SJed Brown .  y - right hand side
330316643e7SJed Brown 
331316643e7SJed Brown    Note:
332316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
333316643e7SJed Brown    is used internally within the nonlinear solvers.
334316643e7SJed Brown 
335316643e7SJed Brown    Level: developer
336316643e7SJed Brown 
337316643e7SJed Brown .keywords: TS, compute
338316643e7SJed Brown 
339316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
340316643e7SJed Brown @*/
341*0910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
342d763cef2SBarry Smith {
343dfbe8321SBarry Smith   PetscErrorCode ierr;
34424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
34524989b8cSPeter Brune   TSIFunction    ifunction;
34624989b8cSPeter Brune   void           *ctx;
34724989b8cSPeter Brune   DM             dm;
34824989b8cSPeter Brune 
349d763cef2SBarry Smith   PetscFunctionBegin;
3500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
351*0910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3520700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
35324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
35424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
35524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
356d763cef2SBarry Smith 
35724989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
358d763cef2SBarry Smith 
359*0910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
36024989b8cSPeter Brune   if (rhsfunction) {
361d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
362*0910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
363d763cef2SBarry Smith     PetscStackPop;
364214bc6a2SJed Brown   } else {
365214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
366b2cd27e8SJed Brown   }
36744a41b28SSean Farley 
368*0910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
369d763cef2SBarry Smith   PetscFunctionReturn(0);
370d763cef2SBarry Smith }
371d763cef2SBarry Smith 
3724a2ae208SSatish Balay #undef __FUNCT__
373ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
374ef20d060SBarry Smith /*@
375ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
376ef20d060SBarry Smith 
377ef20d060SBarry Smith    Collective on TS and Vec
378ef20d060SBarry Smith 
379ef20d060SBarry Smith    Input Parameters:
380ef20d060SBarry Smith +  ts - the TS context
381ef20d060SBarry Smith -  t - current time
382ef20d060SBarry Smith 
383ef20d060SBarry Smith    Output Parameter:
384*0910c330SBarry Smith .  U - the solution
385ef20d060SBarry Smith 
386ef20d060SBarry Smith    Note:
387ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
388ef20d060SBarry Smith    is used internally within the nonlinear solvers.
389ef20d060SBarry Smith 
390ef20d060SBarry Smith    Level: developer
391ef20d060SBarry Smith 
392ef20d060SBarry Smith .keywords: TS, compute
393ef20d060SBarry Smith 
394abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
395ef20d060SBarry Smith @*/
396*0910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
397ef20d060SBarry Smith {
398ef20d060SBarry Smith   PetscErrorCode     ierr;
399ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
400ef20d060SBarry Smith   void               *ctx;
401ef20d060SBarry Smith   DM                 dm;
402ef20d060SBarry Smith 
403ef20d060SBarry Smith   PetscFunctionBegin;
404ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
405*0910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
406ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
407ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
408ef20d060SBarry Smith 
409ef20d060SBarry Smith   if (solutionfunction) {
410ef20d060SBarry Smith     PetscStackPush("TS user right-hand-side function");
411*0910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
412ef20d060SBarry Smith     PetscStackPop;
413ef20d060SBarry Smith   }
414ef20d060SBarry Smith   PetscFunctionReturn(0);
415ef20d060SBarry Smith }
416ef20d060SBarry Smith 
417ef20d060SBarry Smith #undef __FUNCT__
418214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
419214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
420214bc6a2SJed Brown {
4212dd45cf8SJed Brown   Vec            F;
422214bc6a2SJed Brown   PetscErrorCode ierr;
423214bc6a2SJed Brown 
424214bc6a2SJed Brown   PetscFunctionBegin;
4250da9a4f0SJed Brown   *Frhs = PETSC_NULL;
4262dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
427214bc6a2SJed Brown   if (!ts->Frhs) {
4282dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
429214bc6a2SJed Brown   }
430214bc6a2SJed Brown   *Frhs = ts->Frhs;
431214bc6a2SJed Brown   PetscFunctionReturn(0);
432214bc6a2SJed Brown }
433214bc6a2SJed Brown 
434214bc6a2SJed Brown #undef __FUNCT__
435214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
436214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
437214bc6a2SJed Brown {
438214bc6a2SJed Brown   Mat            A,B;
4392dd45cf8SJed Brown   PetscErrorCode ierr;
440214bc6a2SJed Brown 
441214bc6a2SJed Brown   PetscFunctionBegin;
442214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
443214bc6a2SJed Brown   if (Arhs) {
444214bc6a2SJed Brown     if (!ts->Arhs) {
445214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
446214bc6a2SJed Brown     }
447214bc6a2SJed Brown     *Arhs = ts->Arhs;
448214bc6a2SJed Brown   }
449214bc6a2SJed Brown   if (Brhs) {
450214bc6a2SJed Brown     if (!ts->Brhs) {
451214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
452214bc6a2SJed Brown     }
453214bc6a2SJed Brown     *Brhs = ts->Brhs;
454214bc6a2SJed Brown   }
455214bc6a2SJed Brown   PetscFunctionReturn(0);
456214bc6a2SJed Brown }
457214bc6a2SJed Brown 
458214bc6a2SJed Brown #undef __FUNCT__
459316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
460316643e7SJed Brown /*@
461*0910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
462316643e7SJed Brown 
463316643e7SJed Brown    Collective on TS and Vec
464316643e7SJed Brown 
465316643e7SJed Brown    Input Parameters:
466316643e7SJed Brown +  ts - the TS context
467316643e7SJed Brown .  t - current time
468*0910c330SBarry Smith .  U - state vector
469*0910c330SBarry Smith .  Udot - time derivative of state vector
470214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
471316643e7SJed Brown 
472316643e7SJed Brown    Output Parameter:
473316643e7SJed Brown .  Y - right hand side
474316643e7SJed Brown 
475316643e7SJed Brown    Note:
476316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
477316643e7SJed Brown    is used internally within the nonlinear solvers.
478316643e7SJed Brown 
479316643e7SJed Brown    If the user did did not write their equations in implicit form, this
480316643e7SJed Brown    function recasts them in implicit form.
481316643e7SJed Brown 
482316643e7SJed Brown    Level: developer
483316643e7SJed Brown 
484316643e7SJed Brown .keywords: TS, compute
485316643e7SJed Brown 
486316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
487316643e7SJed Brown @*/
488*0910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
489316643e7SJed Brown {
490316643e7SJed Brown   PetscErrorCode ierr;
49124989b8cSPeter Brune   TSIFunction    ifunction;
49224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
49324989b8cSPeter Brune   void           *ctx;
49424989b8cSPeter Brune   DM             dm;
495316643e7SJed Brown 
496316643e7SJed Brown   PetscFunctionBegin;
4970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
498*0910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
499*0910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
5000700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
501316643e7SJed Brown 
50224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
50324989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
50424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
50524989b8cSPeter Brune 
50624989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
507d90be118SSean Farley 
508*0910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
50924989b8cSPeter Brune   if (ifunction) {
510316643e7SJed Brown     PetscStackPush("TS user implicit function");
511*0910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
512316643e7SJed Brown     PetscStackPop;
513214bc6a2SJed Brown   }
514214bc6a2SJed Brown   if (imex) {
51524989b8cSPeter Brune     if (!ifunction) {
516*0910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
5172dd45cf8SJed Brown     }
51824989b8cSPeter Brune   } else if (rhsfunction) {
51924989b8cSPeter Brune     if (ifunction) {
520214bc6a2SJed Brown       Vec Frhs;
521214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
522*0910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
523214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
5242dd45cf8SJed Brown     } else {
525*0910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
526*0910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
527316643e7SJed Brown     }
5284a6899ffSJed Brown   }
529*0910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
530316643e7SJed Brown   PetscFunctionReturn(0);
531316643e7SJed Brown }
532316643e7SJed Brown 
533316643e7SJed Brown #undef __FUNCT__
534316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
535316643e7SJed Brown /*@
536316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
537316643e7SJed Brown 
538316643e7SJed Brown    Collective on TS and Vec
539316643e7SJed Brown 
540316643e7SJed Brown    Input
541316643e7SJed Brown       Input Parameters:
542316643e7SJed Brown +  ts - the TS context
543316643e7SJed Brown .  t - current timestep
544*0910c330SBarry Smith .  U - state vector
545*0910c330SBarry Smith .  Udot - time derivative of state vector
546214bc6a2SJed Brown .  shift - shift to apply, see note below
547214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
548316643e7SJed Brown 
549316643e7SJed Brown    Output Parameters:
550316643e7SJed Brown +  A - Jacobian matrix
551316643e7SJed Brown .  B - optional preconditioning matrix
552316643e7SJed Brown -  flag - flag indicating matrix structure
553316643e7SJed Brown 
554316643e7SJed Brown    Notes:
555*0910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
556316643e7SJed Brown 
557*0910c330SBarry Smith    dF/dU + shift*dF/dUdot
558316643e7SJed Brown 
559316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
560316643e7SJed Brown    is used internally within the nonlinear solvers.
561316643e7SJed Brown 
562316643e7SJed Brown    Level: developer
563316643e7SJed Brown 
564316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
565316643e7SJed Brown 
566316643e7SJed Brown .seealso:  TSSetIJacobian()
56763495f91SJed Brown @*/
568*0910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
569316643e7SJed Brown {
570*0910c330SBarry Smith   PetscInt       Ustate, Udotstate;
571316643e7SJed Brown   PetscErrorCode ierr;
57224989b8cSPeter Brune   TSIJacobian    ijacobian;
57324989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
57424989b8cSPeter Brune   DM             dm;
57524989b8cSPeter Brune   void           *ctx;
576316643e7SJed Brown 
577316643e7SJed Brown   PetscFunctionBegin;
57824989b8cSPeter Brune 
5790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
580*0910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
581*0910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
582316643e7SJed Brown   PetscValidPointer(A,6);
5830700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
584316643e7SJed Brown   PetscValidPointer(B,7);
5850700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
586316643e7SJed Brown   PetscValidPointer(flg,8);
58724989b8cSPeter Brune 
58824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
58924989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
59024989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
59124989b8cSPeter Brune 
592*0910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
593*0910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
594*0910c330SBarry Smith   if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == U && ts->ijacobian.Xstate == Ustate && ts->ijacobian.Xdot == Udot && ts->ijacobian.Xdotstate == Udotstate && ts->ijacobian.imex == imex))) {
5950026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
5960026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5970026cea9SSean Farley     PetscFunctionReturn(0);
5980026cea9SSean Farley   }
599316643e7SJed Brown 
60024989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
601316643e7SJed Brown 
6024e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
603*0910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
60424989b8cSPeter Brune   if (ijacobian) {
6052dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
606316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
607*0910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
608316643e7SJed Brown     PetscStackPop;
609214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
610214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
611214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
6124a6899ffSJed Brown   }
613214bc6a2SJed Brown   if (imex) {
614*0910c330SBarry Smith     if (!ijacobian) {  /* system was written as Udot = F(t,U) */
615214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
6162dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
617214bc6a2SJed Brown       if (*A != *B) {
618214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
619214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
620214bc6a2SJed Brown       }
621214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
622214bc6a2SJed Brown     }
623214bc6a2SJed Brown   } else {
62424989b8cSPeter Brune     if (!ijacobian) {
625*0910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr);
626214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
627214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
628316643e7SJed Brown       if (*A != *B) {
629316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
630316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
631316643e7SJed Brown       }
63224989b8cSPeter Brune     } else if (rhsjacobian) {
633214bc6a2SJed Brown       Mat Arhs,Brhs;
634214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
635214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
636*0910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
637214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
638214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
639214bc6a2SJed Brown       if (*A != *B) {
640214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
641214bc6a2SJed Brown       }
642214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
643214bc6a2SJed Brown     }
644316643e7SJed Brown   }
6450026cea9SSean Farley 
6460026cea9SSean Farley   ts->ijacobian.time = t;
647*0910c330SBarry Smith   ts->ijacobian.X = U;
648*0910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
649*0910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
650*0910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
6510026cea9SSean Farley   ts->ijacobian.shift = shift;
6520026cea9SSean Farley   ts->ijacobian.imex = imex;
6530026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
654*0910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
655316643e7SJed Brown   PetscFunctionReturn(0);
656316643e7SJed Brown }
657316643e7SJed Brown 
658316643e7SJed Brown #undef __FUNCT__
6594a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
660d763cef2SBarry Smith /*@C
661d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
662d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
663d763cef2SBarry Smith 
6643f9fe445SBarry Smith     Logically Collective on TS
665d763cef2SBarry Smith 
666d763cef2SBarry Smith     Input Parameters:
667d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
668ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
669d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
670d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
671d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
672d763cef2SBarry Smith 
673d763cef2SBarry Smith     Calling sequence of func:
67487828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
675d763cef2SBarry Smith 
676d763cef2SBarry Smith +   t - current timestep
677d763cef2SBarry Smith .   u - input vector
678d763cef2SBarry Smith .   F - function vector
679d763cef2SBarry Smith -   ctx - [optional] user-defined function context
680d763cef2SBarry Smith 
681d763cef2SBarry Smith     Level: beginner
682d763cef2SBarry Smith 
683d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
684d763cef2SBarry Smith 
685d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
686d763cef2SBarry Smith @*/
687089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
688d763cef2SBarry Smith {
689089b2837SJed Brown   PetscErrorCode ierr;
690089b2837SJed Brown   SNES           snes;
691e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
69224989b8cSPeter Brune   DM             dm;
693d763cef2SBarry Smith 
694089b2837SJed Brown   PetscFunctionBegin;
6950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
696ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
69724989b8cSPeter Brune 
69824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
69924989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
700089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
701e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
702e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
703e856ceecSJed Brown     r = ralloc;
704e856ceecSJed Brown   }
705089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
706e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
707d763cef2SBarry Smith   PetscFunctionReturn(0);
708d763cef2SBarry Smith }
709d763cef2SBarry Smith 
7104a2ae208SSatish Balay #undef __FUNCT__
711ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
712ef20d060SBarry Smith /*@C
713abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
714ef20d060SBarry Smith 
715ef20d060SBarry Smith     Logically Collective on TS
716ef20d060SBarry Smith 
717ef20d060SBarry Smith     Input Parameters:
718ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
719ef20d060SBarry Smith .   f - routine for evaluating the solution
720ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
721ef20d060SBarry Smith           function evaluation routine (may be PETSC_NULL)
722ef20d060SBarry Smith 
723ef20d060SBarry Smith     Calling sequence of func:
724ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
725ef20d060SBarry Smith 
726ef20d060SBarry Smith +   t - current timestep
727ef20d060SBarry Smith .   u - output vector
728ef20d060SBarry Smith -   ctx - [optional] user-defined function context
729ef20d060SBarry Smith 
730abd5a294SJed Brown     Notes:
731abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
732abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
733abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
734abd5a294SJed Brown 
7354f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
736abd5a294SJed Brown 
737ef20d060SBarry Smith     Level: beginner
738ef20d060SBarry Smith 
739ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
740ef20d060SBarry Smith 
741abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction()
742ef20d060SBarry Smith @*/
743ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
744ef20d060SBarry Smith {
745ef20d060SBarry Smith   PetscErrorCode ierr;
746ef20d060SBarry Smith   DM             dm;
747ef20d060SBarry Smith 
748ef20d060SBarry Smith   PetscFunctionBegin;
749ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
750ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
751ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
752ef20d060SBarry Smith   PetscFunctionReturn(0);
753ef20d060SBarry Smith }
754ef20d060SBarry Smith 
755ef20d060SBarry Smith #undef __FUNCT__
7564a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
757d763cef2SBarry Smith /*@C
758d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
759d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
760d763cef2SBarry Smith 
7613f9fe445SBarry Smith    Logically Collective on TS
762d763cef2SBarry Smith 
763d763cef2SBarry Smith    Input Parameters:
764d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
765d763cef2SBarry Smith .  A   - Jacobian matrix
766d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
767d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
768d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
769d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
770d763cef2SBarry Smith 
771d763cef2SBarry Smith    Calling sequence of func:
77287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
773d763cef2SBarry Smith 
774d763cef2SBarry Smith +  t - current timestep
775d763cef2SBarry Smith .  u - input vector
776d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
777d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
778d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
77994b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
780d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
781d763cef2SBarry Smith 
782d763cef2SBarry Smith    Notes:
78394b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
784d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
785d763cef2SBarry Smith 
786d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
787d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
78856335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
789d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
790d763cef2SBarry Smith    throughout the global iterations.
791d763cef2SBarry Smith 
792d763cef2SBarry Smith    Level: beginner
793d763cef2SBarry Smith 
794d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
795d763cef2SBarry Smith 
796ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
797d763cef2SBarry Smith 
798d763cef2SBarry Smith @*/
799089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
800d763cef2SBarry Smith {
801277b19d0SLisandro Dalcin   PetscErrorCode ierr;
802089b2837SJed Brown   SNES           snes;
80324989b8cSPeter Brune   DM             dm;
80424989b8cSPeter Brune   TSIJacobian    ijacobian;
805277b19d0SLisandro Dalcin 
806d763cef2SBarry Smith   PetscFunctionBegin;
8070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
808277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
809277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
810277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
811277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
812d763cef2SBarry Smith 
81324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
81424989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
81524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
81624989b8cSPeter Brune 
817089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
8185f659677SPeter Brune   if (!ijacobian) {
819089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
8200e4ef248SJed Brown   }
8210e4ef248SJed Brown   if (A) {
8220e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8230e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
8240e4ef248SJed Brown     ts->Arhs = A;
8250e4ef248SJed Brown   }
8260e4ef248SJed Brown   if (B) {
8270e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8280e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
8290e4ef248SJed Brown     ts->Brhs = B;
8300e4ef248SJed Brown   }
831d763cef2SBarry Smith   PetscFunctionReturn(0);
832d763cef2SBarry Smith }
833d763cef2SBarry Smith 
834316643e7SJed Brown 
835316643e7SJed Brown #undef __FUNCT__
836316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
837316643e7SJed Brown /*@C
838316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
839316643e7SJed Brown 
8403f9fe445SBarry Smith    Logically Collective on TS
841316643e7SJed Brown 
842316643e7SJed Brown    Input Parameters:
843316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
844ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
845316643e7SJed Brown .  f   - the function evaluation routine
846316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
847316643e7SJed Brown 
848316643e7SJed Brown    Calling sequence of f:
849316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
850316643e7SJed Brown 
851316643e7SJed Brown +  t   - time at step/stage being solved
852316643e7SJed Brown .  u   - state vector
853316643e7SJed Brown .  u_t - time derivative of state vector
854316643e7SJed Brown .  F   - function vector
855316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
856316643e7SJed Brown 
857316643e7SJed Brown    Important:
858d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
859316643e7SJed Brown 
860316643e7SJed Brown    Level: beginner
861316643e7SJed Brown 
862316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
863316643e7SJed Brown 
864d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
865316643e7SJed Brown @*/
866089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
867316643e7SJed Brown {
868089b2837SJed Brown   PetscErrorCode ierr;
869089b2837SJed Brown   SNES           snes;
870e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
87124989b8cSPeter Brune   DM             dm;
872316643e7SJed Brown 
873316643e7SJed Brown   PetscFunctionBegin;
8740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
875ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
87624989b8cSPeter Brune 
87724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
87824989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
87924989b8cSPeter Brune 
880089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
881e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
882e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
883e856ceecSJed Brown     res = resalloc;
884e856ceecSJed Brown   }
885089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
886e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
88724989b8cSPeter Brune 
888089b2837SJed Brown   PetscFunctionReturn(0);
889089b2837SJed Brown }
890089b2837SJed Brown 
891089b2837SJed Brown #undef __FUNCT__
892089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
893089b2837SJed Brown /*@C
894089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
895089b2837SJed Brown 
896089b2837SJed Brown    Not Collective
897089b2837SJed Brown 
898089b2837SJed Brown    Input Parameter:
899089b2837SJed Brown .  ts - the TS context
900089b2837SJed Brown 
901089b2837SJed Brown    Output Parameter:
902089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
903089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
904089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
905089b2837SJed Brown 
906089b2837SJed Brown    Level: advanced
907089b2837SJed Brown 
908089b2837SJed Brown .keywords: TS, nonlinear, get, function
909089b2837SJed Brown 
910089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
911089b2837SJed Brown @*/
912089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
913089b2837SJed Brown {
914089b2837SJed Brown   PetscErrorCode ierr;
915089b2837SJed Brown   SNES           snes;
91624989b8cSPeter Brune   DM             dm;
917089b2837SJed Brown 
918089b2837SJed Brown   PetscFunctionBegin;
919089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
920089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
921089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
92224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
92324989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
924089b2837SJed Brown   PetscFunctionReturn(0);
925089b2837SJed Brown }
926089b2837SJed Brown 
927089b2837SJed Brown #undef __FUNCT__
928089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
929089b2837SJed Brown /*@C
930089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
931089b2837SJed Brown 
932089b2837SJed Brown    Not Collective
933089b2837SJed Brown 
934089b2837SJed Brown    Input Parameter:
935089b2837SJed Brown .  ts - the TS context
936089b2837SJed Brown 
937089b2837SJed Brown    Output Parameter:
938089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
939089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
940089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
941089b2837SJed Brown 
942089b2837SJed Brown    Level: advanced
943089b2837SJed Brown 
944089b2837SJed Brown .keywords: TS, nonlinear, get, function
945089b2837SJed Brown 
946089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
947089b2837SJed Brown @*/
948089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
949089b2837SJed Brown {
950089b2837SJed Brown   PetscErrorCode ierr;
951089b2837SJed Brown   SNES           snes;
95224989b8cSPeter Brune   DM             dm;
953089b2837SJed Brown 
954089b2837SJed Brown   PetscFunctionBegin;
955089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
956089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
957089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
95824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
95924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
960316643e7SJed Brown   PetscFunctionReturn(0);
961316643e7SJed Brown }
962316643e7SJed Brown 
963316643e7SJed Brown #undef __FUNCT__
964316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
965316643e7SJed Brown /*@C
966a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
967a4f0a591SBarry Smith         you provided with TSSetIFunction().
968316643e7SJed Brown 
9693f9fe445SBarry Smith    Logically Collective on TS
970316643e7SJed Brown 
971316643e7SJed Brown    Input Parameters:
972316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
973316643e7SJed Brown .  A   - Jacobian matrix
974316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
975316643e7SJed Brown .  f   - the Jacobian evaluation routine
976316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
977316643e7SJed Brown 
978316643e7SJed Brown    Calling sequence of f:
9791b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
980316643e7SJed Brown 
981316643e7SJed Brown +  t    - time at step/stage being solved
9821b4a444bSJed Brown .  U    - state vector
9831b4a444bSJed Brown .  U_t  - time derivative of state vector
984316643e7SJed Brown .  a    - shift
9851b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
986316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
987316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
988316643e7SJed Brown           structure (same as flag in KSPSetOperators())
989316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
990316643e7SJed Brown 
991316643e7SJed Brown    Notes:
992316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
993316643e7SJed Brown 
994a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
995a4f0a591SBarry 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.
996a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
997a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
998a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
999a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1000a4f0a591SBarry Smith 
1001316643e7SJed Brown    Level: beginner
1002316643e7SJed Brown 
1003316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1004316643e7SJed Brown 
1005ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
1006316643e7SJed Brown 
1007316643e7SJed Brown @*/
10087087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1009316643e7SJed Brown {
1010316643e7SJed Brown   PetscErrorCode ierr;
1011089b2837SJed Brown   SNES           snes;
101224989b8cSPeter Brune   DM             dm;
1013316643e7SJed Brown 
1014316643e7SJed Brown   PetscFunctionBegin;
10150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10160700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
10170700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1018316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1019316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
102024989b8cSPeter Brune 
102124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
102224989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
102324989b8cSPeter Brune 
1024089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1025089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1026316643e7SJed Brown   PetscFunctionReturn(0);
1027316643e7SJed Brown }
1028316643e7SJed Brown 
10294a2ae208SSatish Balay #undef __FUNCT__
10304a2ae208SSatish Balay #define __FUNCT__ "TSView"
10317e2c5f70SBarry Smith /*@C
1032d763cef2SBarry Smith     TSView - Prints the TS data structure.
1033d763cef2SBarry Smith 
10344c49b128SBarry Smith     Collective on TS
1035d763cef2SBarry Smith 
1036d763cef2SBarry Smith     Input Parameters:
1037d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1038d763cef2SBarry Smith -   viewer - visualization context
1039d763cef2SBarry Smith 
1040d763cef2SBarry Smith     Options Database Key:
1041d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1042d763cef2SBarry Smith 
1043d763cef2SBarry Smith     Notes:
1044d763cef2SBarry Smith     The available visualization contexts include
1045b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1046b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1047d763cef2SBarry Smith          output where only the first processor opens
1048d763cef2SBarry Smith          the file.  All other processors send their
1049d763cef2SBarry Smith          data to the first processor to print.
1050d763cef2SBarry Smith 
1051d763cef2SBarry Smith     The user can open an alternative visualization context with
1052b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1053d763cef2SBarry Smith 
1054d763cef2SBarry Smith     Level: beginner
1055d763cef2SBarry Smith 
1056d763cef2SBarry Smith .keywords: TS, timestep, view
1057d763cef2SBarry Smith 
1058b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1059d763cef2SBarry Smith @*/
10607087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1061d763cef2SBarry Smith {
1062dfbe8321SBarry Smith   PetscErrorCode ierr;
106319fd82e9SBarry Smith   TSType         type;
1064089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
1065d763cef2SBarry Smith 
1066d763cef2SBarry Smith   PetscFunctionBegin;
10670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10683050cee2SBarry Smith   if (!viewer) {
10697adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
10703050cee2SBarry Smith   }
10710700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1072c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1073fd16b177SBarry Smith 
1074251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1075251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
107632077d6dSBarry Smith   if (iascii) {
1077317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
107877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1079a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1080d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
10815ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1082c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1083d763cef2SBarry Smith     }
10845ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1085193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1086d52bd9f3SBarry Smith     if (ts->ops->view) {
1087d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1088d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1089d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1090d52bd9f3SBarry Smith     }
10910f5bd95cSBarry Smith   } else if (isstring) {
1092a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1093b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
1094d763cef2SBarry Smith   }
1095b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1096251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1097b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1098d763cef2SBarry Smith   PetscFunctionReturn(0);
1099d763cef2SBarry Smith }
1100d763cef2SBarry Smith 
1101d763cef2SBarry Smith 
11024a2ae208SSatish Balay #undef __FUNCT__
11034a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1104b07ff414SBarry Smith /*@
1105d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1106d763cef2SBarry Smith    the timesteppers.
1107d763cef2SBarry Smith 
11083f9fe445SBarry Smith    Logically Collective on TS
1109d763cef2SBarry Smith 
1110d763cef2SBarry Smith    Input Parameters:
1111d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1112d763cef2SBarry Smith -  usrP - optional user context
1113d763cef2SBarry Smith 
1114d763cef2SBarry Smith    Level: intermediate
1115d763cef2SBarry Smith 
1116d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1117d763cef2SBarry Smith 
1118d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1119d763cef2SBarry Smith @*/
11207087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1121d763cef2SBarry Smith {
1122d763cef2SBarry Smith   PetscFunctionBegin;
11230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1124d763cef2SBarry Smith   ts->user = usrP;
1125d763cef2SBarry Smith   PetscFunctionReturn(0);
1126d763cef2SBarry Smith }
1127d763cef2SBarry Smith 
11284a2ae208SSatish Balay #undef __FUNCT__
11294a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1130b07ff414SBarry Smith /*@
1131d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1132d763cef2SBarry Smith     timestepper.
1133d763cef2SBarry Smith 
1134d763cef2SBarry Smith     Not Collective
1135d763cef2SBarry Smith 
1136d763cef2SBarry Smith     Input Parameter:
1137d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1138d763cef2SBarry Smith 
1139d763cef2SBarry Smith     Output Parameter:
1140d763cef2SBarry Smith .   usrP - user context
1141d763cef2SBarry Smith 
1142d763cef2SBarry Smith     Level: intermediate
1143d763cef2SBarry Smith 
1144d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1145d763cef2SBarry Smith 
1146d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1147d763cef2SBarry Smith @*/
1148e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1149d763cef2SBarry Smith {
1150d763cef2SBarry Smith   PetscFunctionBegin;
11510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1152e71120c6SJed Brown   *(void**)usrP = ts->user;
1153d763cef2SBarry Smith   PetscFunctionReturn(0);
1154d763cef2SBarry Smith }
1155d763cef2SBarry Smith 
11564a2ae208SSatish Balay #undef __FUNCT__
11574a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1158d763cef2SBarry Smith /*@
1159b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1160d763cef2SBarry Smith 
1161d763cef2SBarry Smith    Not Collective
1162d763cef2SBarry Smith 
1163d763cef2SBarry Smith    Input Parameter:
1164d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1165d763cef2SBarry Smith 
1166d763cef2SBarry Smith    Output Parameter:
1167b8123daeSJed Brown .  iter - number of steps completed so far
1168d763cef2SBarry Smith 
1169d763cef2SBarry Smith    Level: intermediate
1170d763cef2SBarry Smith 
1171d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1172b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1173d763cef2SBarry Smith @*/
11747087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
1175d763cef2SBarry Smith {
1176d763cef2SBarry Smith   PetscFunctionBegin;
11770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11784482741eSBarry Smith   PetscValidIntPointer(iter,2);
1179d763cef2SBarry Smith   *iter = ts->steps;
1180d763cef2SBarry Smith   PetscFunctionReturn(0);
1181d763cef2SBarry Smith }
1182d763cef2SBarry Smith 
11834a2ae208SSatish Balay #undef __FUNCT__
11844a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1185d763cef2SBarry Smith /*@
1186d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1187d763cef2SBarry Smith    as well as the initial time.
1188d763cef2SBarry Smith 
11893f9fe445SBarry Smith    Logically Collective on TS
1190d763cef2SBarry Smith 
1191d763cef2SBarry Smith    Input Parameters:
1192d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1193d763cef2SBarry Smith .  initial_time - the initial time
1194d763cef2SBarry Smith -  time_step - the size of the timestep
1195d763cef2SBarry Smith 
1196d763cef2SBarry Smith    Level: intermediate
1197d763cef2SBarry Smith 
1198d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1199d763cef2SBarry Smith 
1200d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1201d763cef2SBarry Smith @*/
12027087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1203d763cef2SBarry Smith {
1204e144a568SJed Brown   PetscErrorCode ierr;
1205e144a568SJed Brown 
1206d763cef2SBarry Smith   PetscFunctionBegin;
12070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1208e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1209d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1210d763cef2SBarry Smith   PetscFunctionReturn(0);
1211d763cef2SBarry Smith }
1212d763cef2SBarry Smith 
12134a2ae208SSatish Balay #undef __FUNCT__
12144a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1215d763cef2SBarry Smith /*@
1216d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1217d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1218d763cef2SBarry Smith 
12193f9fe445SBarry Smith    Logically Collective on TS
1220d763cef2SBarry Smith 
1221d763cef2SBarry Smith    Input Parameters:
1222d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1223d763cef2SBarry Smith -  time_step - the size of the timestep
1224d763cef2SBarry Smith 
1225d763cef2SBarry Smith    Level: intermediate
1226d763cef2SBarry Smith 
1227d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1228d763cef2SBarry Smith 
1229d763cef2SBarry Smith .keywords: TS, set, timestep
1230d763cef2SBarry Smith @*/
12317087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1232d763cef2SBarry Smith {
1233d763cef2SBarry Smith   PetscFunctionBegin;
12340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1235c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1236d763cef2SBarry Smith   ts->time_step = time_step;
123731748224SBarry Smith   ts->time_step_orig = time_step;
1238d763cef2SBarry Smith   PetscFunctionReturn(0);
1239d763cef2SBarry Smith }
1240d763cef2SBarry Smith 
12414a2ae208SSatish Balay #undef __FUNCT__
1242a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1243a43b19c4SJed Brown /*@
1244a43b19c4SJed Brown    TSSetExactFinalTime - Determines whether to interpolate solution to the
1245a43b19c4SJed Brown       exact final time requested by the user or just returns it at the final time
1246a43b19c4SJed Brown       it computed.
1247a43b19c4SJed Brown 
1248a43b19c4SJed Brown   Logically Collective on TS
1249a43b19c4SJed Brown 
1250a43b19c4SJed Brown    Input Parameter:
1251a43b19c4SJed Brown +   ts - the time-step context
1252a43b19c4SJed Brown -   ft - PETSC_TRUE if interpolates, else PETSC_FALSE
1253a43b19c4SJed Brown 
1254a43b19c4SJed Brown    Level: beginner
1255a43b19c4SJed Brown 
1256a43b19c4SJed Brown .seealso: TSSetDuration()
1257a43b19c4SJed Brown @*/
1258a43b19c4SJed Brown PetscErrorCode  TSSetExactFinalTime(TS ts,PetscBool flg)
1259a43b19c4SJed Brown {
1260a43b19c4SJed Brown   PetscFunctionBegin;
1261a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1262d8cd7023SBarry Smith   PetscValidLogicalCollectiveBool(ts,flg,2);
1263a43b19c4SJed Brown   ts->exact_final_time = flg;
1264a43b19c4SJed Brown   PetscFunctionReturn(0);
1265a43b19c4SJed Brown }
1266a43b19c4SJed Brown 
1267a43b19c4SJed Brown #undef __FUNCT__
12684a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1269d763cef2SBarry Smith /*@
1270d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1271d763cef2SBarry Smith 
1272d763cef2SBarry Smith    Not Collective
1273d763cef2SBarry Smith 
1274d763cef2SBarry Smith    Input Parameter:
1275d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1276d763cef2SBarry Smith 
1277d763cef2SBarry Smith    Output Parameter:
1278d763cef2SBarry Smith .  dt - the current timestep size
1279d763cef2SBarry Smith 
1280d763cef2SBarry Smith    Level: intermediate
1281d763cef2SBarry Smith 
1282d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1283d763cef2SBarry Smith 
1284d763cef2SBarry Smith .keywords: TS, get, timestep
1285d763cef2SBarry Smith @*/
12867087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1287d763cef2SBarry Smith {
1288d763cef2SBarry Smith   PetscFunctionBegin;
12890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1290f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1291d763cef2SBarry Smith   *dt = ts->time_step;
1292d763cef2SBarry Smith   PetscFunctionReturn(0);
1293d763cef2SBarry Smith }
1294d763cef2SBarry Smith 
12954a2ae208SSatish Balay #undef __FUNCT__
12964a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1297d8e5e3e6SSatish Balay /*@
1298d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1299d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1300d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1301d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1302d763cef2SBarry Smith 
1303d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1304d763cef2SBarry Smith 
1305d763cef2SBarry Smith    Input Parameter:
1306d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1307d763cef2SBarry Smith 
1308d763cef2SBarry Smith    Output Parameter:
1309d763cef2SBarry Smith .  v - the vector containing the solution
1310d763cef2SBarry Smith 
1311d763cef2SBarry Smith    Level: intermediate
1312d763cef2SBarry Smith 
1313d763cef2SBarry Smith .seealso: TSGetTimeStep()
1314d763cef2SBarry Smith 
1315d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1316d763cef2SBarry Smith @*/
13177087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1318d763cef2SBarry Smith {
1319d763cef2SBarry Smith   PetscFunctionBegin;
13200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13214482741eSBarry Smith   PetscValidPointer(v,2);
13228737fe31SLisandro Dalcin   *v = ts->vec_sol;
1323d763cef2SBarry Smith   PetscFunctionReturn(0);
1324d763cef2SBarry Smith }
1325d763cef2SBarry Smith 
1326bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
13274a2ae208SSatish Balay #undef __FUNCT__
1328bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1329d8e5e3e6SSatish Balay /*@
1330bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1331d763cef2SBarry Smith 
1332bdad233fSMatthew Knepley   Not collective
1333d763cef2SBarry Smith 
1334bdad233fSMatthew Knepley   Input Parameters:
1335bdad233fSMatthew Knepley + ts   - The TS
1336bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1337d763cef2SBarry Smith .vb
1338*0910c330SBarry Smith          U_t - A U = 0      (linear)
1339*0910c330SBarry Smith          U_t - A(t) U = 0   (linear)
1340*0910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1341d763cef2SBarry Smith .ve
1342d763cef2SBarry Smith 
1343d763cef2SBarry Smith    Level: beginner
1344d763cef2SBarry Smith 
1345bdad233fSMatthew Knepley .keywords: TS, problem type
1346bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1347d763cef2SBarry Smith @*/
13487087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1349a7cc72afSBarry Smith {
13509e2a6581SJed Brown   PetscErrorCode ierr;
13519e2a6581SJed Brown 
1352d763cef2SBarry Smith   PetscFunctionBegin;
13530700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1354bdad233fSMatthew Knepley   ts->problem_type = type;
13559e2a6581SJed Brown   if (type == TS_LINEAR) {
13569e2a6581SJed Brown     SNES snes;
13579e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13589e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
13599e2a6581SJed Brown   }
1360d763cef2SBarry Smith   PetscFunctionReturn(0);
1361d763cef2SBarry Smith }
1362d763cef2SBarry Smith 
1363bdad233fSMatthew Knepley #undef __FUNCT__
1364bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1365bdad233fSMatthew Knepley /*@C
1366bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1367bdad233fSMatthew Knepley 
1368bdad233fSMatthew Knepley   Not collective
1369bdad233fSMatthew Knepley 
1370bdad233fSMatthew Knepley   Input Parameter:
1371bdad233fSMatthew Knepley . ts   - The TS
1372bdad233fSMatthew Knepley 
1373bdad233fSMatthew Knepley   Output Parameter:
1374bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1375bdad233fSMatthew Knepley .vb
1376089b2837SJed Brown          M U_t = A U
1377089b2837SJed Brown          M(t) U_t = A(t) U
1378bdad233fSMatthew Knepley          U_t = F(t,U)
1379bdad233fSMatthew Knepley .ve
1380bdad233fSMatthew Knepley 
1381bdad233fSMatthew Knepley    Level: beginner
1382bdad233fSMatthew Knepley 
1383bdad233fSMatthew Knepley .keywords: TS, problem type
1384bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1385bdad233fSMatthew Knepley @*/
13867087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1387a7cc72afSBarry Smith {
1388bdad233fSMatthew Knepley   PetscFunctionBegin;
13890700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
13904482741eSBarry Smith   PetscValidIntPointer(type,2);
1391bdad233fSMatthew Knepley   *type = ts->problem_type;
1392bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1393bdad233fSMatthew Knepley }
1394d763cef2SBarry Smith 
13954a2ae208SSatish Balay #undef __FUNCT__
13964a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1397d763cef2SBarry Smith /*@
1398d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1399d763cef2SBarry Smith    of a timestepper.
1400d763cef2SBarry Smith 
1401d763cef2SBarry Smith    Collective on TS
1402d763cef2SBarry Smith 
1403d763cef2SBarry Smith    Input Parameter:
1404d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1405d763cef2SBarry Smith 
1406d763cef2SBarry Smith    Notes:
1407d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1408d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1409d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1410d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1411d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1412d763cef2SBarry Smith 
1413d763cef2SBarry Smith    Level: advanced
1414d763cef2SBarry Smith 
1415d763cef2SBarry Smith .keywords: TS, timestep, setup
1416d763cef2SBarry Smith 
1417d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1418d763cef2SBarry Smith @*/
14197087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1420d763cef2SBarry Smith {
1421dfbe8321SBarry Smith   PetscErrorCode ierr;
14226c6b9e74SPeter Brune   DM             dm;
14236c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
14246c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
14256c6b9e74SPeter Brune   TSIJacobian    ijac;
14266c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1427d763cef2SBarry Smith 
1428d763cef2SBarry Smith   PetscFunctionBegin;
14290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1430277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1431277b19d0SLisandro Dalcin 
14327adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
14339596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1434d763cef2SBarry Smith   }
1435a43b19c4SJed Brown   if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE;
1436277b19d0SLisandro Dalcin 
1437277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1438277b19d0SLisandro Dalcin 
14391c3436cfSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
14401c3436cfSJed Brown 
1441277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1442000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1443277b19d0SLisandro Dalcin   }
1444277b19d0SLisandro Dalcin 
14456c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
14466c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
14476c6b9e74SPeter Brune    */
14486c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
14496c6b9e74SPeter Brune   ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr);
14506c6b9e74SPeter Brune   if (!func) {
14516c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
14526c6b9e74SPeter Brune   }
14536c6b9e74SPeter 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.
14546c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
14556c6b9e74SPeter Brune    */
14566c6b9e74SPeter Brune   ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr);
14576c6b9e74SPeter Brune   ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr);
14586c6b9e74SPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr);
14596c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
14606c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
14616c6b9e74SPeter Brune   }
1462277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1463277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1464277b19d0SLisandro Dalcin }
1465277b19d0SLisandro Dalcin 
1466277b19d0SLisandro Dalcin #undef __FUNCT__
1467277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1468277b19d0SLisandro Dalcin /*@
1469277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1470277b19d0SLisandro Dalcin 
1471277b19d0SLisandro Dalcin    Collective on TS
1472277b19d0SLisandro Dalcin 
1473277b19d0SLisandro Dalcin    Input Parameter:
1474277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1475277b19d0SLisandro Dalcin 
1476277b19d0SLisandro Dalcin    Level: beginner
1477277b19d0SLisandro Dalcin 
1478277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1479277b19d0SLisandro Dalcin 
1480277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1481277b19d0SLisandro Dalcin @*/
1482277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1483277b19d0SLisandro Dalcin {
1484277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1485277b19d0SLisandro Dalcin 
1486277b19d0SLisandro Dalcin   PetscFunctionBegin;
1487277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1488277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1489277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1490277b19d0SLisandro Dalcin   }
1491277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
14924e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
14934e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1494214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
14956bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1496e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1497e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
149838637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1499277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1500d763cef2SBarry Smith   PetscFunctionReturn(0);
1501d763cef2SBarry Smith }
1502d763cef2SBarry Smith 
15034a2ae208SSatish Balay #undef __FUNCT__
15044a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1505d8e5e3e6SSatish Balay /*@
1506d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1507d763cef2SBarry Smith    with TSCreate().
1508d763cef2SBarry Smith 
1509d763cef2SBarry Smith    Collective on TS
1510d763cef2SBarry Smith 
1511d763cef2SBarry Smith    Input Parameter:
1512d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1513d763cef2SBarry Smith 
1514d763cef2SBarry Smith    Level: beginner
1515d763cef2SBarry Smith 
1516d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1517d763cef2SBarry Smith 
1518d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1519d763cef2SBarry Smith @*/
15206bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1521d763cef2SBarry Smith {
15226849ba73SBarry Smith   PetscErrorCode ierr;
1523d763cef2SBarry Smith 
1524d763cef2SBarry Smith   PetscFunctionBegin;
15256bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
15266bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
15276bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1528d763cef2SBarry Smith 
15296bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1530277b19d0SLisandro Dalcin 
1531be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
15326bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
15336bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
15346d4c513bSLisandro Dalcin 
153584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
15366bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
15376bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
15386bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
15396d4c513bSLisandro Dalcin 
1540a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1541d763cef2SBarry Smith   PetscFunctionReturn(0);
1542d763cef2SBarry Smith }
1543d763cef2SBarry Smith 
15444a2ae208SSatish Balay #undef __FUNCT__
15454a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1546d8e5e3e6SSatish Balay /*@
1547d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1548d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1549d763cef2SBarry Smith 
1550d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1551d763cef2SBarry Smith 
1552d763cef2SBarry Smith    Input Parameter:
1553d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1554d763cef2SBarry Smith 
1555d763cef2SBarry Smith    Output Parameter:
1556d763cef2SBarry Smith .  snes - the nonlinear solver context
1557d763cef2SBarry Smith 
1558d763cef2SBarry Smith    Notes:
1559d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1560d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
156194b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1562d763cef2SBarry Smith 
1563d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1564d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1565d763cef2SBarry Smith 
1566d763cef2SBarry Smith    Level: beginner
1567d763cef2SBarry Smith 
1568d763cef2SBarry Smith .keywords: timestep, get, SNES
1569d763cef2SBarry Smith @*/
15707087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1571d763cef2SBarry Smith {
1572d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1573d372ba47SLisandro Dalcin 
1574d763cef2SBarry Smith   PetscFunctionBegin;
15750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15764482741eSBarry Smith   PetscValidPointer(snes,2);
1577d372ba47SLisandro Dalcin   if (!ts->snes) {
1578d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
15796c6b9e74SPeter Brune     ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1580d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1581d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1582496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
15839e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
15849e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
15859e2a6581SJed Brown     }
1586d372ba47SLisandro Dalcin   }
1587d763cef2SBarry Smith   *snes = ts->snes;
1588d763cef2SBarry Smith   PetscFunctionReturn(0);
1589d763cef2SBarry Smith }
1590d763cef2SBarry Smith 
15914a2ae208SSatish Balay #undef __FUNCT__
159294b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1593d8e5e3e6SSatish Balay /*@
159494b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1595d763cef2SBarry Smith    a TS (timestepper) context.
1596d763cef2SBarry Smith 
159794b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1598d763cef2SBarry Smith 
1599d763cef2SBarry Smith    Input Parameter:
1600d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1601d763cef2SBarry Smith 
1602d763cef2SBarry Smith    Output Parameter:
160394b7f48cSBarry Smith .  ksp - the nonlinear solver context
1604d763cef2SBarry Smith 
1605d763cef2SBarry Smith    Notes:
160694b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1607d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1608d763cef2SBarry Smith    KSP and PC contexts as well.
1609d763cef2SBarry Smith 
161094b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
161194b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1612d763cef2SBarry Smith 
1613d763cef2SBarry Smith    Level: beginner
1614d763cef2SBarry Smith 
161594b7f48cSBarry Smith .keywords: timestep, get, KSP
1616d763cef2SBarry Smith @*/
16177087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1618d763cef2SBarry Smith {
1619d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1620089b2837SJed Brown   SNES           snes;
1621d372ba47SLisandro Dalcin 
1622d763cef2SBarry Smith   PetscFunctionBegin;
16230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16244482741eSBarry Smith   PetscValidPointer(ksp,2);
162517186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1626e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1627089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1628089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1629d763cef2SBarry Smith   PetscFunctionReturn(0);
1630d763cef2SBarry Smith }
1631d763cef2SBarry Smith 
1632d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1633d763cef2SBarry Smith 
16344a2ae208SSatish Balay #undef __FUNCT__
1635adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1636adb62b0dSMatthew Knepley /*@
1637adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1638adb62b0dSMatthew Knepley    maximum time for iteration.
1639adb62b0dSMatthew Knepley 
16403f9fe445SBarry Smith    Not Collective
1641adb62b0dSMatthew Knepley 
1642adb62b0dSMatthew Knepley    Input Parameters:
1643adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1644adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1645adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1646adb62b0dSMatthew Knepley 
1647adb62b0dSMatthew Knepley    Level: intermediate
1648adb62b0dSMatthew Knepley 
1649adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1650adb62b0dSMatthew Knepley @*/
16517087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1652adb62b0dSMatthew Knepley {
1653adb62b0dSMatthew Knepley   PetscFunctionBegin;
16540700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1655abc0a331SBarry Smith   if (maxsteps) {
16564482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1657adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1658adb62b0dSMatthew Knepley   }
1659abc0a331SBarry Smith   if (maxtime) {
16604482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1661adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1662adb62b0dSMatthew Knepley   }
1663adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1664adb62b0dSMatthew Knepley }
1665adb62b0dSMatthew Knepley 
1666adb62b0dSMatthew Knepley #undef __FUNCT__
16674a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1668d763cef2SBarry Smith /*@
1669d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1670d763cef2SBarry Smith    maximum time for iteration.
1671d763cef2SBarry Smith 
16723f9fe445SBarry Smith    Logically Collective on TS
1673d763cef2SBarry Smith 
1674d763cef2SBarry Smith    Input Parameters:
1675d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1676d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1677d763cef2SBarry Smith -  maxtime - final time to iterate to
1678d763cef2SBarry Smith 
1679d763cef2SBarry Smith    Options Database Keys:
1680d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
16813bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1682d763cef2SBarry Smith 
1683d763cef2SBarry Smith    Notes:
1684d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1685d763cef2SBarry Smith 
1686d763cef2SBarry Smith    Level: intermediate
1687d763cef2SBarry Smith 
1688d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1689a43b19c4SJed Brown 
1690a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1691d763cef2SBarry Smith @*/
16927087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1693d763cef2SBarry Smith {
1694d763cef2SBarry Smith   PetscFunctionBegin;
16950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1696c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1697c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
169839b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
169939b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1700d763cef2SBarry Smith   PetscFunctionReturn(0);
1701d763cef2SBarry Smith }
1702d763cef2SBarry Smith 
17034a2ae208SSatish Balay #undef __FUNCT__
17044a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1705d763cef2SBarry Smith /*@
1706d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1707d763cef2SBarry Smith    for use by the TS routines.
1708d763cef2SBarry Smith 
17093f9fe445SBarry Smith    Logically Collective on TS and Vec
1710d763cef2SBarry Smith 
1711d763cef2SBarry Smith    Input Parameters:
1712d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1713*0910c330SBarry Smith -  u - the solution vector
1714d763cef2SBarry Smith 
1715d763cef2SBarry Smith    Level: beginner
1716d763cef2SBarry Smith 
1717d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1718d763cef2SBarry Smith @*/
1719*0910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
1720d763cef2SBarry Smith {
17218737fe31SLisandro Dalcin   PetscErrorCode ierr;
1722496e6a7aSJed Brown   DM             dm;
17238737fe31SLisandro Dalcin 
1724d763cef2SBarry Smith   PetscFunctionBegin;
17250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1726*0910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1727*0910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
17286bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1729*0910c330SBarry Smith   ts->vec_sol = u;
1730496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1731*0910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
1732d763cef2SBarry Smith   PetscFunctionReturn(0);
1733d763cef2SBarry Smith }
1734d763cef2SBarry Smith 
1735e74ef692SMatthew Knepley #undef __FUNCT__
1736e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1737ac226902SBarry Smith /*@C
1738000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
17393f2090d5SJed Brown   called once at the beginning of each time step.
1740000e7ae3SMatthew Knepley 
17413f9fe445SBarry Smith   Logically Collective on TS
1742000e7ae3SMatthew Knepley 
1743000e7ae3SMatthew Knepley   Input Parameters:
1744000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1745000e7ae3SMatthew Knepley - func - The function
1746000e7ae3SMatthew Knepley 
1747000e7ae3SMatthew Knepley   Calling sequence of func:
1748000e7ae3SMatthew Knepley . func (TS ts);
1749000e7ae3SMatthew Knepley 
1750000e7ae3SMatthew Knepley   Level: intermediate
1751000e7ae3SMatthew Knepley 
1752b8123daeSJed Brown   Note:
1753b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1754b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1755b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1756b8123daeSJed Brown 
1757000e7ae3SMatthew Knepley .keywords: TS, timestep
1758b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1759000e7ae3SMatthew Knepley @*/
17607087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1761000e7ae3SMatthew Knepley {
1762000e7ae3SMatthew Knepley   PetscFunctionBegin;
17630700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1764000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1765000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1766000e7ae3SMatthew Knepley }
1767000e7ae3SMatthew Knepley 
1768e74ef692SMatthew Knepley #undef __FUNCT__
17693f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
177009ee8438SJed Brown /*@
17713f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
17723f2090d5SJed Brown 
17733f2090d5SJed Brown   Collective on TS
17743f2090d5SJed Brown 
17753f2090d5SJed Brown   Input Parameters:
17763f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
17773f2090d5SJed Brown 
17783f2090d5SJed Brown   Notes:
17793f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
17803f2090d5SJed Brown   so most users would not generally call this routine themselves.
17813f2090d5SJed Brown 
17823f2090d5SJed Brown   Level: developer
17833f2090d5SJed Brown 
17843f2090d5SJed Brown .keywords: TS, timestep
1785b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
17863f2090d5SJed Brown @*/
17877087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
17883f2090d5SJed Brown {
17893f2090d5SJed Brown   PetscErrorCode ierr;
17903f2090d5SJed Brown 
17913f2090d5SJed Brown   PetscFunctionBegin;
17920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
179372ac3e02SJed Brown   if (ts->ops->prestep) {
17943f2090d5SJed Brown     PetscStackPush("TS PreStep function");
17953f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
17963f2090d5SJed Brown     PetscStackPop;
1797312ce896SJed Brown   }
17983f2090d5SJed Brown   PetscFunctionReturn(0);
17993f2090d5SJed Brown }
18003f2090d5SJed Brown 
18013f2090d5SJed Brown #undef __FUNCT__
1802b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1803b8123daeSJed Brown /*@C
1804b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1805b8123daeSJed Brown   called once at the beginning of each stage.
1806b8123daeSJed Brown 
1807b8123daeSJed Brown   Logically Collective on TS
1808b8123daeSJed Brown 
1809b8123daeSJed Brown   Input Parameters:
1810b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1811b8123daeSJed Brown - func - The function
1812b8123daeSJed Brown 
1813b8123daeSJed Brown   Calling sequence of func:
1814b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1815b8123daeSJed Brown 
1816b8123daeSJed Brown   Level: intermediate
1817b8123daeSJed Brown 
1818b8123daeSJed Brown   Note:
1819b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
1820b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
1821b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
1822b8123daeSJed Brown 
1823b8123daeSJed Brown .keywords: TS, timestep
1824b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
1825b8123daeSJed Brown @*/
1826b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
1827b8123daeSJed Brown {
1828b8123daeSJed Brown   PetscFunctionBegin;
1829b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1830b8123daeSJed Brown   ts->ops->prestage = func;
1831b8123daeSJed Brown   PetscFunctionReturn(0);
1832b8123daeSJed Brown }
1833b8123daeSJed Brown 
1834b8123daeSJed Brown #undef __FUNCT__
1835b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
1836b8123daeSJed Brown /*@
1837b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
1838b8123daeSJed Brown 
1839b8123daeSJed Brown   Collective on TS
1840b8123daeSJed Brown 
1841b8123daeSJed Brown   Input Parameters:
1842b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
1843b8123daeSJed Brown 
1844b8123daeSJed Brown   Notes:
1845b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
1846b8123daeSJed Brown   most users would not generally call this routine themselves.
1847b8123daeSJed Brown 
1848b8123daeSJed Brown   Level: developer
1849b8123daeSJed Brown 
1850b8123daeSJed Brown .keywords: TS, timestep
1851b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
1852b8123daeSJed Brown @*/
1853b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
1854b8123daeSJed Brown {
1855b8123daeSJed Brown   PetscErrorCode ierr;
1856b8123daeSJed Brown 
1857b8123daeSJed Brown   PetscFunctionBegin;
1858b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1859b8123daeSJed Brown   if (ts->ops->prestage) {
1860b8123daeSJed Brown     PetscStackPush("TS PreStage function");
1861b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
1862b8123daeSJed Brown     PetscStackPop;
1863b8123daeSJed Brown   }
1864b8123daeSJed Brown   PetscFunctionReturn(0);
1865b8123daeSJed Brown }
1866b8123daeSJed Brown 
1867b8123daeSJed Brown #undef __FUNCT__
1868e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1869ac226902SBarry Smith /*@C
1870000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
18713f2090d5SJed Brown   called once at the end of each time step.
1872000e7ae3SMatthew Knepley 
18733f9fe445SBarry Smith   Logically Collective on TS
1874000e7ae3SMatthew Knepley 
1875000e7ae3SMatthew Knepley   Input Parameters:
1876000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1877000e7ae3SMatthew Knepley - func - The function
1878000e7ae3SMatthew Knepley 
1879000e7ae3SMatthew Knepley   Calling sequence of func:
1880b8123daeSJed Brown $ func (TS ts);
1881000e7ae3SMatthew Knepley 
1882000e7ae3SMatthew Knepley   Level: intermediate
1883000e7ae3SMatthew Knepley 
1884000e7ae3SMatthew Knepley .keywords: TS, timestep
1885b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
1886000e7ae3SMatthew Knepley @*/
18877087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1888000e7ae3SMatthew Knepley {
1889000e7ae3SMatthew Knepley   PetscFunctionBegin;
18900700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1891000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1892000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1893000e7ae3SMatthew Knepley }
1894000e7ae3SMatthew Knepley 
1895e74ef692SMatthew Knepley #undef __FUNCT__
18963f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
189709ee8438SJed Brown /*@
18983f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
18993f2090d5SJed Brown 
19003f2090d5SJed Brown   Collective on TS
19013f2090d5SJed Brown 
19023f2090d5SJed Brown   Input Parameters:
19033f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
19043f2090d5SJed Brown 
19053f2090d5SJed Brown   Notes:
19063f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
19073f2090d5SJed Brown   so most users would not generally call this routine themselves.
19083f2090d5SJed Brown 
19093f2090d5SJed Brown   Level: developer
19103f2090d5SJed Brown 
19113f2090d5SJed Brown .keywords: TS, timestep
19123f2090d5SJed Brown @*/
19137087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
19143f2090d5SJed Brown {
19153f2090d5SJed Brown   PetscErrorCode ierr;
19163f2090d5SJed Brown 
19173f2090d5SJed Brown   PetscFunctionBegin;
19180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
191972ac3e02SJed Brown   if (ts->ops->poststep) {
19203f2090d5SJed Brown     PetscStackPush("TS PostStep function");
19213f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
19223f2090d5SJed Brown     PetscStackPop;
192372ac3e02SJed Brown   }
19243f2090d5SJed Brown   PetscFunctionReturn(0);
19253f2090d5SJed Brown }
19263f2090d5SJed Brown 
1927d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1928d763cef2SBarry Smith 
19294a2ae208SSatish Balay #undef __FUNCT__
1930a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1931d763cef2SBarry Smith /*@C
1932a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1933d763cef2SBarry Smith    timestep to display the iteration's  progress.
1934d763cef2SBarry Smith 
19353f9fe445SBarry Smith    Logically Collective on TS
1936d763cef2SBarry Smith 
1937d763cef2SBarry Smith    Input Parameters:
1938d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1939e213d8f1SJed Brown .  monitor - monitoring routine
1940329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1941b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1942b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1943b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1944d763cef2SBarry Smith 
1945e213d8f1SJed Brown    Calling sequence of monitor:
1946*0910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
1947d763cef2SBarry Smith 
1948d763cef2SBarry Smith +    ts - the TS context
1949d763cef2SBarry Smith .    steps - iteration number
19501f06c33eSBarry Smith .    time - current time
1951*0910c330SBarry Smith .    u - current iterate
1952d763cef2SBarry Smith -    mctx - [optional] monitoring context
1953d763cef2SBarry Smith 
1954d763cef2SBarry Smith    Notes:
1955d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1956d763cef2SBarry Smith    already has been loaded.
1957d763cef2SBarry Smith 
1958025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1959025f1a04SBarry Smith 
1960d763cef2SBarry Smith    Level: intermediate
1961d763cef2SBarry Smith 
1962d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1963d763cef2SBarry Smith 
1964a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1965d763cef2SBarry Smith @*/
1966c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1967d763cef2SBarry Smith {
1968d763cef2SBarry Smith   PetscFunctionBegin;
19690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
197017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1971d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
19728704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]    = mdestroy;
1973d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1974d763cef2SBarry Smith   PetscFunctionReturn(0);
1975d763cef2SBarry Smith }
1976d763cef2SBarry Smith 
19774a2ae208SSatish Balay #undef __FUNCT__
1978a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1979d763cef2SBarry Smith /*@C
1980a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1981d763cef2SBarry Smith 
19823f9fe445SBarry Smith    Logically Collective on TS
1983d763cef2SBarry Smith 
1984d763cef2SBarry Smith    Input Parameters:
1985d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1986d763cef2SBarry Smith 
1987d763cef2SBarry Smith    Notes:
1988d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1989d763cef2SBarry Smith 
1990d763cef2SBarry Smith    Level: intermediate
1991d763cef2SBarry Smith 
1992d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1993d763cef2SBarry Smith 
1994a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1995d763cef2SBarry Smith @*/
19967087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1997d763cef2SBarry Smith {
1998d952e501SBarry Smith   PetscErrorCode ierr;
1999d952e501SBarry Smith   PetscInt       i;
2000d952e501SBarry Smith 
2001d763cef2SBarry Smith   PetscFunctionBegin;
20020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2003d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
20048704b422SBarry Smith     if (ts->monitordestroy[i]) {
20058704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2006d952e501SBarry Smith     }
2007d952e501SBarry Smith   }
2008d763cef2SBarry Smith   ts->numbermonitors = 0;
2009d763cef2SBarry Smith   PetscFunctionReturn(0);
2010d763cef2SBarry Smith }
2011d763cef2SBarry Smith 
20124a2ae208SSatish Balay #undef __FUNCT__
2013a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2014d8e5e3e6SSatish Balay /*@
2015a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
20165516499fSSatish Balay 
20175516499fSSatish Balay    Level: intermediate
201841251cbbSSatish Balay 
20195516499fSSatish Balay .keywords: TS, set, monitor
20205516499fSSatish Balay 
202141251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
202241251cbbSSatish Balay @*/
2023649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2024d763cef2SBarry Smith {
2025dfbe8321SBarry Smith   PetscErrorCode ierr;
2026649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
2027d132466eSBarry Smith 
2028d763cef2SBarry Smith   PetscFunctionBegin;
2029649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2030971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2031649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2032d763cef2SBarry Smith   PetscFunctionReturn(0);
2033d763cef2SBarry Smith }
2034d763cef2SBarry Smith 
20354a2ae208SSatish Balay #undef __FUNCT__
2036cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2037cd652676SJed Brown /*@
2038cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2039cd652676SJed Brown 
2040cd652676SJed Brown    Logically Collective on TS
2041cd652676SJed Brown 
2042cd652676SJed Brown    Input Argument:
2043cd652676SJed Brown .  ts - time stepping context
2044cd652676SJed Brown 
2045cd652676SJed Brown    Output Argument:
2046cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2047cd652676SJed Brown 
2048cd652676SJed Brown    Level: intermediate
2049cd652676SJed Brown 
2050cd652676SJed Brown .keywords: TS, set
2051cd652676SJed Brown 
2052cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2053cd652676SJed Brown @*/
2054cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2055cd652676SJed Brown {
2056cd652676SJed Brown   PetscFunctionBegin;
2057cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2058cd652676SJed Brown   ts->retain_stages = flg;
2059cd652676SJed Brown   PetscFunctionReturn(0);
2060cd652676SJed Brown }
2061cd652676SJed Brown 
2062cd652676SJed Brown #undef __FUNCT__
2063cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2064cd652676SJed Brown /*@
2065cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2066cd652676SJed Brown 
2067cd652676SJed Brown    Collective on TS
2068cd652676SJed Brown 
2069cd652676SJed Brown    Input Argument:
2070cd652676SJed Brown +  ts - time stepping context
2071cd652676SJed Brown -  t - time to interpolate to
2072cd652676SJed Brown 
2073cd652676SJed Brown    Output Argument:
2074*0910c330SBarry Smith .  U - state at given time
2075cd652676SJed Brown 
2076cd652676SJed Brown    Notes:
2077cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2078cd652676SJed Brown 
2079cd652676SJed Brown    Level: intermediate
2080cd652676SJed Brown 
2081cd652676SJed Brown    Developer Notes:
2082cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2083cd652676SJed Brown 
2084cd652676SJed Brown .keywords: TS, set
2085cd652676SJed Brown 
2086cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2087cd652676SJed Brown @*/
2088*0910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2089cd652676SJed Brown {
2090cd652676SJed Brown   PetscErrorCode ierr;
2091cd652676SJed Brown 
2092cd652676SJed Brown   PetscFunctionBegin;
2093cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2094d8cd7023SBarry 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);
2095cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
2096*0910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2097cd652676SJed Brown   PetscFunctionReturn(0);
2098cd652676SJed Brown }
2099cd652676SJed Brown 
2100cd652676SJed Brown #undef __FUNCT__
21014a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2102d763cef2SBarry Smith /*@
21036d9e5789SSean Farley    TSStep - Steps one time step
2104d763cef2SBarry Smith 
2105d763cef2SBarry Smith    Collective on TS
2106d763cef2SBarry Smith 
2107d763cef2SBarry Smith    Input Parameter:
2108d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2109d763cef2SBarry Smith 
21106d9e5789SSean Farley    Level: intermediate
2111d763cef2SBarry Smith 
2112b8123daeSJed Brown    Notes:
2113b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2114b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2115b8123daeSJed Brown 
2116d763cef2SBarry Smith .keywords: TS, timestep, solve
2117d763cef2SBarry Smith 
2118b8123daeSJed Brown .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage()
2119d763cef2SBarry Smith @*/
2120193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2121d763cef2SBarry Smith {
2122362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2123dfbe8321SBarry Smith   PetscErrorCode ierr;
2124d763cef2SBarry Smith 
2125d763cef2SBarry Smith   PetscFunctionBegin;
21260700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2127d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2128d405a339SMatthew Knepley 
2129362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2130362cd11cSLisandro Dalcin 
2131362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2132d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2133193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2134d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2135362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2136362cd11cSLisandro Dalcin 
2137362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2138cef5090cSJed Brown     if (ts->errorifstepfailed) {
2139cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2140cef5090cSJed 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]);
2141cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2142cef5090cSJed Brown     }
2143362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2144362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2145362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2146362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2147362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2148362cd11cSLisandro Dalcin   }
2149362cd11cSLisandro Dalcin 
2150d763cef2SBarry Smith   PetscFunctionReturn(0);
2151d763cef2SBarry Smith }
2152d763cef2SBarry Smith 
21534a2ae208SSatish Balay #undef __FUNCT__
215405175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
215505175c85SJed Brown /*@
215605175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
215705175c85SJed Brown 
21581c3436cfSJed Brown    Collective on TS
215905175c85SJed Brown 
216005175c85SJed Brown    Input Arguments:
21611c3436cfSJed Brown +  ts - time stepping context
21621c3436cfSJed Brown .  order - desired order of accuracy
21631c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
216405175c85SJed Brown 
216505175c85SJed Brown    Output Arguments:
2166*0910c330SBarry Smith .  U - state at the end of the current step
216705175c85SJed Brown 
216805175c85SJed Brown    Level: advanced
216905175c85SJed Brown 
2170108c343cSJed Brown    Notes:
2171108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2172108c343cSJed 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.
2173108c343cSJed Brown 
21741c3436cfSJed Brown .seealso: TSStep(), TSAdapt
217505175c85SJed Brown @*/
2176*0910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
217705175c85SJed Brown {
217805175c85SJed Brown   PetscErrorCode ierr;
217905175c85SJed Brown 
218005175c85SJed Brown   PetscFunctionBegin;
218105175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
218205175c85SJed Brown   PetscValidType(ts,1);
2183*0910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
21841c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
2185*0910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
218605175c85SJed Brown   PetscFunctionReturn(0);
218705175c85SJed Brown }
218805175c85SJed Brown 
218905175c85SJed Brown #undef __FUNCT__
21906a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
21916a4d4014SLisandro Dalcin /*@
21926a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
21936a4d4014SLisandro Dalcin 
21946a4d4014SLisandro Dalcin    Collective on TS
21956a4d4014SLisandro Dalcin 
21966a4d4014SLisandro Dalcin    Input Parameter:
21976a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2198*0910c330SBarry Smith -  u - the solution vector
21996a4d4014SLisandro Dalcin 
22005a3a76d0SJed Brown    Output Parameter:
2201*0910c330SBarry Smith .  ftime - time of the state vector u upon completion
22025a3a76d0SJed Brown 
22036a4d4014SLisandro Dalcin    Level: beginner
22046a4d4014SLisandro Dalcin 
22055a3a76d0SJed Brown    Notes:
22065a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
22075a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
22085a3a76d0SJed Brown    stepped over the final time.
22095a3a76d0SJed Brown 
22106a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
22116a4d4014SLisandro Dalcin 
22126a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
22136a4d4014SLisandro Dalcin @*/
2214*0910c330SBarry Smith PetscErrorCode TSSolve(TS ts,Vec u,PetscReal *ftime)
22156a4d4014SLisandro Dalcin {
22164d7d938eSLisandro Dalcin   PetscBool      flg;
22174d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
22184d7d938eSLisandro Dalcin   PetscViewer    viewer;
22196a4d4014SLisandro Dalcin   PetscErrorCode ierr;
2220f22f69f0SBarry Smith 
22216a4d4014SLisandro Dalcin   PetscFunctionBegin;
22220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2223*0910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
22245a3a76d0SJed Brown   if (ts->exact_final_time) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
2225*0910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
22265a3a76d0SJed Brown       Vec y;
2227*0910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
22285a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
22295a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
22305a3a76d0SJed Brown     }
2231*0910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
22325a3a76d0SJed Brown   } else {
2233*0910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
22345a3a76d0SJed Brown   }
2235b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
22366a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2237193ac0bcSJed Brown   ts->steps = 0;
22385ef26d82SJed Brown   ts->ksp_its = 0;
22395ef26d82SJed Brown   ts->snes_its = 0;
2240c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2241c610991cSLisandro Dalcin   ts->reject = 0;
2242193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
2243193ac0bcSJed Brown 
2244193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2245193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
2246*0910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2247a5b82c69SSean Farley     if (ftime) *ftime = ts->ptime;
2248193ac0bcSJed Brown   } else {
22496a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2250362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2251362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2252362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2253362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2254362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2255e1a7a14fSJed Brown     while (!ts->reason) {
2256193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2257193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2258193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2259193ac0bcSJed Brown     }
2260362cd11cSLisandro Dalcin     if (ts->exact_final_time && ts->ptime > ts->max_time) {
2261*0910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
22625a3a76d0SJed Brown       if (ftime) *ftime = ts->max_time;
22630574a7fbSJed Brown     } else {
2264*0910c330SBarry Smith       ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
22650574a7fbSJed Brown       if (ftime) *ftime = ts->ptime;
22660574a7fbSJed Brown     }
2267193ac0bcSJed Brown   }
22683923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
22694d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
22704d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
22714d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
22724d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
22734d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2274193ac0bcSJed Brown   }
22756a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
22766a4d4014SLisandro Dalcin }
22776a4d4014SLisandro Dalcin 
22786a4d4014SLisandro Dalcin #undef __FUNCT__
22794a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2280228d79bcSJed Brown /*@
2281228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2282228d79bcSJed Brown 
2283228d79bcSJed Brown    Collective on TS
2284228d79bcSJed Brown 
2285228d79bcSJed Brown    Input Parameters:
2286228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2287228d79bcSJed Brown .  step - step number that has just completed
2288228d79bcSJed Brown .  ptime - model time of the state
2289*0910c330SBarry Smith -  u - state at the current model time
2290228d79bcSJed Brown 
2291228d79bcSJed Brown    Notes:
2292228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2293228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2294228d79bcSJed Brown 
2295228d79bcSJed Brown    Level: advanced
2296228d79bcSJed Brown 
2297228d79bcSJed Brown .keywords: TS, timestep
2298228d79bcSJed Brown @*/
2299*0910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2300d763cef2SBarry Smith {
23016849ba73SBarry Smith   PetscErrorCode ierr;
2302a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2303d763cef2SBarry Smith 
2304d763cef2SBarry Smith   PetscFunctionBegin;
2305d763cef2SBarry Smith   for (i=0; i<n; i++) {
2306*0910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2307d763cef2SBarry Smith   }
2308d763cef2SBarry Smith   PetscFunctionReturn(0);
2309d763cef2SBarry Smith }
2310d763cef2SBarry Smith 
2311d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
23120b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
23130b039ecaSBarry Smith   PetscDrawLG lg;
23140b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2315201da799SBarry Smith   PetscInt    ksp_its,snes_its;
23160b039ecaSBarry Smith };
23170b039ecaSBarry Smith 
2318d763cef2SBarry Smith 
23194a2ae208SSatish Balay #undef __FUNCT__
2320a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2321d763cef2SBarry Smith /*@C
2322a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2323a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2324d763cef2SBarry Smith 
2325d763cef2SBarry Smith    Collective on TS
2326d763cef2SBarry Smith 
2327d763cef2SBarry Smith    Input Parameters:
2328d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2329d763cef2SBarry Smith .  label - the title to put in the title bar
23307c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2331a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2332a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2333d763cef2SBarry Smith 
2334d763cef2SBarry Smith    Output Parameter:
23350b039ecaSBarry Smith .  ctx - the context
2336d763cef2SBarry Smith 
2337d763cef2SBarry Smith    Options Database Key:
23384f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
23394f09c107SBarry Smith .  -ts_monitor_lg_solution -
234099fdda47SBarry Smith .  -ts_monitor_lg_error -
234199fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
234299fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
234399fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2344d763cef2SBarry Smith 
2345d763cef2SBarry Smith    Notes:
2346a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2347d763cef2SBarry Smith 
2348d763cef2SBarry Smith    Level: intermediate
2349d763cef2SBarry Smith 
23507c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2351d763cef2SBarry Smith 
23524f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
23537c922b88SBarry Smith 
2354d763cef2SBarry Smith @*/
2355a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2356d763cef2SBarry Smith {
2357b0a32e0cSBarry Smith   PetscDraw      win;
2358dfbe8321SBarry Smith   PetscErrorCode ierr;
235999fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2360d763cef2SBarry Smith 
2361d763cef2SBarry Smith   PetscFunctionBegin;
2362201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2363a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
23643fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
23650b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
236699fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr);
236799fdda47SBarry Smith   if (flg) {
23680b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
236999fdda47SBarry Smith   }
23700b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2371a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2372d763cef2SBarry Smith   PetscFunctionReturn(0);
2373d763cef2SBarry Smith }
2374d763cef2SBarry Smith 
23754a2ae208SSatish Balay #undef __FUNCT__
23764f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
23774f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2378d763cef2SBarry Smith {
23790b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2380c32365f1SBarry Smith   PetscReal      x = ptime,y;
2381dfbe8321SBarry Smith   PetscErrorCode ierr;
2382d763cef2SBarry Smith 
2383d763cef2SBarry Smith   PetscFunctionBegin;
2384a9f9c1f6SBarry Smith   if (!n) {
2385a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
2386a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2387a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2388a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2389a9f9c1f6SBarry Smith   }
2390c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
23910b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
239299fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){
23930b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
23943923b477SBarry Smith   }
2395d763cef2SBarry Smith   PetscFunctionReturn(0);
2396d763cef2SBarry Smith }
2397d763cef2SBarry Smith 
23984a2ae208SSatish Balay #undef __FUNCT__
2399a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2400d763cef2SBarry Smith /*@C
2401a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2402a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2403d763cef2SBarry Smith 
24040b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2405d763cef2SBarry Smith 
2406d763cef2SBarry Smith    Input Parameter:
24070b039ecaSBarry Smith .  ctx - the monitor context
2408d763cef2SBarry Smith 
2409d763cef2SBarry Smith    Level: intermediate
2410d763cef2SBarry Smith 
2411d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2412d763cef2SBarry Smith 
24134f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2414d763cef2SBarry Smith @*/
2415a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2416d763cef2SBarry Smith {
2417b0a32e0cSBarry Smith   PetscDraw      draw;
2418dfbe8321SBarry Smith   PetscErrorCode ierr;
2419d763cef2SBarry Smith 
2420d763cef2SBarry Smith   PetscFunctionBegin;
24210b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
24226bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
24230b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
24240b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2425d763cef2SBarry Smith   PetscFunctionReturn(0);
2426d763cef2SBarry Smith }
2427d763cef2SBarry Smith 
24284a2ae208SSatish Balay #undef __FUNCT__
24294a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2430d763cef2SBarry Smith /*@
2431b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2432d763cef2SBarry Smith 
2433d763cef2SBarry Smith    Not Collective
2434d763cef2SBarry Smith 
2435d763cef2SBarry Smith    Input Parameter:
2436d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2437d763cef2SBarry Smith 
2438d763cef2SBarry Smith    Output Parameter:
2439d763cef2SBarry Smith .  t  - the current time
2440d763cef2SBarry Smith 
2441d763cef2SBarry Smith    Level: beginner
2442d763cef2SBarry Smith 
2443b8123daeSJed Brown    Note:
2444b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2445b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2446b8123daeSJed Brown 
2447d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2448d763cef2SBarry Smith 
2449d763cef2SBarry Smith .keywords: TS, get, time
2450d763cef2SBarry Smith @*/
24517087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2452d763cef2SBarry Smith {
2453d763cef2SBarry Smith   PetscFunctionBegin;
24540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2455f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2456d763cef2SBarry Smith   *t = ts->ptime;
2457d763cef2SBarry Smith   PetscFunctionReturn(0);
2458d763cef2SBarry Smith }
2459d763cef2SBarry Smith 
24604a2ae208SSatish Balay #undef __FUNCT__
24616a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
24626a4d4014SLisandro Dalcin /*@
24636a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
24646a4d4014SLisandro Dalcin 
24653f9fe445SBarry Smith    Logically Collective on TS
24666a4d4014SLisandro Dalcin 
24676a4d4014SLisandro Dalcin    Input Parameters:
24686a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
24696a4d4014SLisandro Dalcin -  time - the time
24706a4d4014SLisandro Dalcin 
24716a4d4014SLisandro Dalcin    Level: intermediate
24726a4d4014SLisandro Dalcin 
24736a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
24746a4d4014SLisandro Dalcin 
24756a4d4014SLisandro Dalcin .keywords: TS, set, time
24766a4d4014SLisandro Dalcin @*/
24777087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
24786a4d4014SLisandro Dalcin {
24796a4d4014SLisandro Dalcin   PetscFunctionBegin;
24800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2481c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
24826a4d4014SLisandro Dalcin   ts->ptime = t;
24836a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
24846a4d4014SLisandro Dalcin }
24856a4d4014SLisandro Dalcin 
24866a4d4014SLisandro Dalcin #undef __FUNCT__
24874a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2488d763cef2SBarry Smith /*@C
2489d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2490d763cef2SBarry Smith    TS options in the database.
2491d763cef2SBarry Smith 
24923f9fe445SBarry Smith    Logically Collective on TS
2493d763cef2SBarry Smith 
2494d763cef2SBarry Smith    Input Parameter:
2495d763cef2SBarry Smith +  ts     - The TS context
2496d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2497d763cef2SBarry Smith 
2498d763cef2SBarry Smith    Notes:
2499d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2500d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2501d763cef2SBarry Smith    hyphen.
2502d763cef2SBarry Smith 
2503d763cef2SBarry Smith    Level: advanced
2504d763cef2SBarry Smith 
2505d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2506d763cef2SBarry Smith 
2507d763cef2SBarry Smith .seealso: TSSetFromOptions()
2508d763cef2SBarry Smith 
2509d763cef2SBarry Smith @*/
25107087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2511d763cef2SBarry Smith {
2512dfbe8321SBarry Smith   PetscErrorCode ierr;
2513089b2837SJed Brown   SNES           snes;
2514d763cef2SBarry Smith 
2515d763cef2SBarry Smith   PetscFunctionBegin;
25160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2517d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2518089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2519089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2520d763cef2SBarry Smith   PetscFunctionReturn(0);
2521d763cef2SBarry Smith }
2522d763cef2SBarry Smith 
2523d763cef2SBarry Smith 
25244a2ae208SSatish Balay #undef __FUNCT__
25254a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2526d763cef2SBarry Smith /*@C
2527d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2528d763cef2SBarry Smith    TS options in the database.
2529d763cef2SBarry Smith 
25303f9fe445SBarry Smith    Logically Collective on TS
2531d763cef2SBarry Smith 
2532d763cef2SBarry Smith    Input Parameter:
2533d763cef2SBarry Smith +  ts     - The TS context
2534d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2535d763cef2SBarry Smith 
2536d763cef2SBarry Smith    Notes:
2537d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2538d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2539d763cef2SBarry Smith    hyphen.
2540d763cef2SBarry Smith 
2541d763cef2SBarry Smith    Level: advanced
2542d763cef2SBarry Smith 
2543d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2544d763cef2SBarry Smith 
2545d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2546d763cef2SBarry Smith 
2547d763cef2SBarry Smith @*/
25487087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2549d763cef2SBarry Smith {
2550dfbe8321SBarry Smith   PetscErrorCode ierr;
2551089b2837SJed Brown   SNES           snes;
2552d763cef2SBarry Smith 
2553d763cef2SBarry Smith   PetscFunctionBegin;
25540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2555d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2556089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2557089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2558d763cef2SBarry Smith   PetscFunctionReturn(0);
2559d763cef2SBarry Smith }
2560d763cef2SBarry Smith 
25614a2ae208SSatish Balay #undef __FUNCT__
25624a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2563d763cef2SBarry Smith /*@C
2564d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2565d763cef2SBarry Smith    TS options in the database.
2566d763cef2SBarry Smith 
2567d763cef2SBarry Smith    Not Collective
2568d763cef2SBarry Smith 
2569d763cef2SBarry Smith    Input Parameter:
2570d763cef2SBarry Smith .  ts - The TS context
2571d763cef2SBarry Smith 
2572d763cef2SBarry Smith    Output Parameter:
2573d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2574d763cef2SBarry Smith 
2575d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2576d763cef2SBarry Smith    sufficient length to hold the prefix.
2577d763cef2SBarry Smith 
2578d763cef2SBarry Smith    Level: intermediate
2579d763cef2SBarry Smith 
2580d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2581d763cef2SBarry Smith 
2582d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2583d763cef2SBarry Smith @*/
25847087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2585d763cef2SBarry Smith {
2586dfbe8321SBarry Smith   PetscErrorCode ierr;
2587d763cef2SBarry Smith 
2588d763cef2SBarry Smith   PetscFunctionBegin;
25890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25904482741eSBarry Smith   PetscValidPointer(prefix,2);
2591d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2592d763cef2SBarry Smith   PetscFunctionReturn(0);
2593d763cef2SBarry Smith }
2594d763cef2SBarry Smith 
25954a2ae208SSatish Balay #undef __FUNCT__
25964a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2597d763cef2SBarry Smith /*@C
2598d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2599d763cef2SBarry Smith 
2600d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2601d763cef2SBarry Smith 
2602d763cef2SBarry Smith    Input Parameter:
2603d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2604d763cef2SBarry Smith 
2605d763cef2SBarry Smith    Output Parameters:
2606d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2607d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2608089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2609d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2610d763cef2SBarry Smith 
2611d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2612d763cef2SBarry Smith 
2613d763cef2SBarry Smith    Level: intermediate
2614d763cef2SBarry Smith 
261526d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2616d763cef2SBarry Smith 
2617d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2618d763cef2SBarry Smith @*/
2619089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2620d763cef2SBarry Smith {
2621089b2837SJed Brown   PetscErrorCode ierr;
2622089b2837SJed Brown   SNES           snes;
262324989b8cSPeter Brune   DM             dm;
2624089b2837SJed Brown 
2625d763cef2SBarry Smith   PetscFunctionBegin;
2626089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2627089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
262824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
262924989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2630d763cef2SBarry Smith   PetscFunctionReturn(0);
2631d763cef2SBarry Smith }
2632d763cef2SBarry Smith 
26331713a123SBarry Smith #undef __FUNCT__
26342eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
26352eca1d9cSJed Brown /*@C
26362eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
26372eca1d9cSJed Brown 
26382eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
26392eca1d9cSJed Brown 
26402eca1d9cSJed Brown    Input Parameter:
26412eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
26422eca1d9cSJed Brown 
26432eca1d9cSJed Brown    Output Parameters:
26442eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
26452eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
26462eca1d9cSJed Brown .  f   - The function to compute the matrices
26472eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
26482eca1d9cSJed Brown 
26492eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
26502eca1d9cSJed Brown 
26512eca1d9cSJed Brown    Level: advanced
26522eca1d9cSJed Brown 
26532eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
26542eca1d9cSJed Brown 
26552eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
26562eca1d9cSJed Brown @*/
26577087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
26582eca1d9cSJed Brown {
2659089b2837SJed Brown   PetscErrorCode ierr;
2660089b2837SJed Brown   SNES           snes;
266124989b8cSPeter Brune   DM             dm;
2662*0910c330SBarry Smith 
26632eca1d9cSJed Brown   PetscFunctionBegin;
2664089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2665089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
266624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
266724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
26682eca1d9cSJed Brown   PetscFunctionReturn(0);
26692eca1d9cSJed Brown }
26702eca1d9cSJed Brown 
267183a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
26726083293cSBarry Smith   PetscViewer viewer;
26736083293cSBarry Smith   Vec         initialsolution;
26746083293cSBarry Smith   PetscBool   showinitial;
267583a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
267683a4ac43SBarry Smith };
26776083293cSBarry Smith 
26782eca1d9cSJed Brown #undef __FUNCT__
267983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
26801713a123SBarry Smith /*@C
268183a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
26821713a123SBarry Smith    VecView() for the solution at each timestep
26831713a123SBarry Smith 
26841713a123SBarry Smith    Collective on TS
26851713a123SBarry Smith 
26861713a123SBarry Smith    Input Parameters:
26871713a123SBarry Smith +  ts - the TS context
26881713a123SBarry Smith .  step - current time-step
2689142b95e3SSatish Balay .  ptime - current time
26901713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
26911713a123SBarry Smith 
269299fdda47SBarry Smith    Options Database:
269399fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
269499fdda47SBarry Smith 
269599fdda47SBarry Smith    Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
269699fdda47SBarry Smith        will look bad
269799fdda47SBarry Smith 
26981713a123SBarry Smith    Level: intermediate
26991713a123SBarry Smith 
27001713a123SBarry Smith .keywords: TS,  vector, monitor, view
27011713a123SBarry Smith 
2702a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
27031713a123SBarry Smith @*/
2704*0910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
27051713a123SBarry Smith {
2706dfbe8321SBarry Smith   PetscErrorCode   ierr;
270783a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
27081713a123SBarry Smith 
27091713a123SBarry Smith   PetscFunctionBegin;
27106083293cSBarry Smith   if (!step && ictx->showinitial) {
27116083293cSBarry Smith     if (!ictx->initialsolution) {
2712*0910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
27131713a123SBarry Smith     }
2714*0910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
27156083293cSBarry Smith   }
27163fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
27170dcf80beSBarry Smith 
27186083293cSBarry Smith   if (ictx->showinitial) {
27196083293cSBarry Smith     PetscReal pause;
27206083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
27216083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
27226083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
27236083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
27246083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
27256083293cSBarry Smith   }
2726*0910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
27276083293cSBarry Smith   if (ictx->showinitial) {
27286083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
27296083293cSBarry Smith   }
27301713a123SBarry Smith   PetscFunctionReturn(0);
27311713a123SBarry Smith }
27321713a123SBarry Smith 
27331713a123SBarry Smith 
27346c699258SBarry Smith #undef __FUNCT__
273583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
27366083293cSBarry Smith /*@C
273783a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
27386083293cSBarry Smith 
27396083293cSBarry Smith    Collective on TS
27406083293cSBarry Smith 
27416083293cSBarry Smith    Input Parameters:
27426083293cSBarry Smith .    ctx - the monitor context
27436083293cSBarry Smith 
27446083293cSBarry Smith    Level: intermediate
27456083293cSBarry Smith 
27466083293cSBarry Smith .keywords: TS,  vector, monitor, view
27476083293cSBarry Smith 
274883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
27496083293cSBarry Smith @*/
275083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
27516083293cSBarry Smith {
27526083293cSBarry Smith   PetscErrorCode       ierr;
27536083293cSBarry Smith 
27546083293cSBarry Smith   PetscFunctionBegin;
275583a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
275683a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
275783a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
27586083293cSBarry Smith   PetscFunctionReturn(0);
27596083293cSBarry Smith }
27606083293cSBarry Smith 
27616083293cSBarry Smith #undef __FUNCT__
276283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
27636083293cSBarry Smith /*@C
276483a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
27656083293cSBarry Smith 
27666083293cSBarry Smith    Collective on TS
27676083293cSBarry Smith 
27686083293cSBarry Smith    Input Parameter:
27696083293cSBarry Smith .    ts - time-step context
27706083293cSBarry Smith 
27716083293cSBarry Smith    Output Patameter:
27726083293cSBarry Smith .    ctx - the monitor context
27736083293cSBarry Smith 
277499fdda47SBarry Smith    Options Database:
277599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
277699fdda47SBarry Smith 
27776083293cSBarry Smith    Level: intermediate
27786083293cSBarry Smith 
27796083293cSBarry Smith .keywords: TS,  vector, monitor, view
27806083293cSBarry Smith 
278183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
27826083293cSBarry Smith @*/
278383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
27846083293cSBarry Smith {
27856083293cSBarry Smith   PetscErrorCode   ierr;
27866083293cSBarry Smith 
27876083293cSBarry Smith   PetscFunctionBegin;
278883a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
278983a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
279083a4ac43SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
279183a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
279299fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr);
27936083293cSBarry Smith   PetscFunctionReturn(0);
27946083293cSBarry Smith }
27956083293cSBarry Smith 
27966083293cSBarry Smith #undef __FUNCT__
279783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
27983a471f94SBarry Smith /*@C
279983a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
28003a471f94SBarry Smith    VecView() for the error at each timestep
28013a471f94SBarry Smith 
28023a471f94SBarry Smith    Collective on TS
28033a471f94SBarry Smith 
28043a471f94SBarry Smith    Input Parameters:
28053a471f94SBarry Smith +  ts - the TS context
28063a471f94SBarry Smith .  step - current time-step
28073a471f94SBarry Smith .  ptime - current time
28083a471f94SBarry Smith -  dummy - either a viewer or PETSC_NULL
28093a471f94SBarry Smith 
28103a471f94SBarry Smith    Level: intermediate
28113a471f94SBarry Smith 
28123a471f94SBarry Smith .keywords: TS,  vector, monitor, view
28133a471f94SBarry Smith 
28143a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
28153a471f94SBarry Smith @*/
2816*0910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
28173a471f94SBarry Smith {
28183a471f94SBarry Smith   PetscErrorCode   ierr;
281983a4ac43SBarry Smith   TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy;
282083a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
28213a471f94SBarry Smith   Vec              work;
28223a471f94SBarry Smith 
28233a471f94SBarry Smith   PetscFunctionBegin;
28243fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
2825*0910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
28263a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
2827*0910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
28283a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
28293a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
28303a471f94SBarry Smith   PetscFunctionReturn(0);
28313a471f94SBarry Smith }
28323a471f94SBarry Smith 
28333a471f94SBarry Smith #undef __FUNCT__
28346c699258SBarry Smith #define __FUNCT__ "TSSetDM"
28356c699258SBarry Smith /*@
28366c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
28376c699258SBarry Smith 
28383f9fe445SBarry Smith    Logically Collective on TS and DM
28396c699258SBarry Smith 
28406c699258SBarry Smith    Input Parameters:
28416c699258SBarry Smith +  ts - the preconditioner context
28426c699258SBarry Smith -  dm - the dm
28436c699258SBarry Smith 
28446c699258SBarry Smith    Level: intermediate
28456c699258SBarry Smith 
28466c699258SBarry Smith 
28476c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
28486c699258SBarry Smith @*/
28497087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
28506c699258SBarry Smith {
28516c699258SBarry Smith   PetscErrorCode ierr;
2852089b2837SJed Brown   SNES           snes;
285324989b8cSPeter Brune   TSDM           tsdm;
28546c699258SBarry Smith 
28556c699258SBarry Smith   PetscFunctionBegin;
28560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
285770663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
285824989b8cSPeter Brune   if (ts->dm) {               /* Move the TSDM context over to the new DM unless the new DM already has one */
285924989b8cSPeter Brune     PetscContainer oldcontainer,container;
286024989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr);
286124989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr);
286224989b8cSPeter Brune     if (oldcontainer && !container) {
286324989b8cSPeter Brune       ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr);
286424989b8cSPeter Brune       ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr);
286524989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
286624989b8cSPeter Brune         tsdm->originaldm = dm;
286724989b8cSPeter Brune       }
286824989b8cSPeter Brune     }
28696bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
287024989b8cSPeter Brune   }
28716c699258SBarry Smith   ts->dm = dm;
2872089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2873089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
28746c699258SBarry Smith   PetscFunctionReturn(0);
28756c699258SBarry Smith }
28766c699258SBarry Smith 
28776c699258SBarry Smith #undef __FUNCT__
28786c699258SBarry Smith #define __FUNCT__ "TSGetDM"
28796c699258SBarry Smith /*@
28806c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
28816c699258SBarry Smith 
28823f9fe445SBarry Smith    Not Collective
28836c699258SBarry Smith 
28846c699258SBarry Smith    Input Parameter:
28856c699258SBarry Smith . ts - the preconditioner context
28866c699258SBarry Smith 
28876c699258SBarry Smith    Output Parameter:
28886c699258SBarry Smith .  dm - the dm
28896c699258SBarry Smith 
28906c699258SBarry Smith    Level: intermediate
28916c699258SBarry Smith 
28926c699258SBarry Smith 
28936c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
28946c699258SBarry Smith @*/
28957087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
28966c699258SBarry Smith {
2897496e6a7aSJed Brown   PetscErrorCode ierr;
2898496e6a7aSJed Brown 
28996c699258SBarry Smith   PetscFunctionBegin;
29000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2901496e6a7aSJed Brown   if (!ts->dm) {
2902496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
2903496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
2904496e6a7aSJed Brown   }
29056c699258SBarry Smith   *dm = ts->dm;
29066c699258SBarry Smith   PetscFunctionReturn(0);
29076c699258SBarry Smith }
29081713a123SBarry Smith 
29090f5c6efeSJed Brown #undef __FUNCT__
29100f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
29110f5c6efeSJed Brown /*@
29120f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
29130f5c6efeSJed Brown 
29143f9fe445SBarry Smith    Logically Collective on SNES
29150f5c6efeSJed Brown 
29160f5c6efeSJed Brown    Input Parameter:
2917d42a1c89SJed Brown + snes - nonlinear solver
2918*0910c330SBarry Smith . U - the current state at which to evaluate the residual
2919d42a1c89SJed Brown - ctx - user context, must be a TS
29200f5c6efeSJed Brown 
29210f5c6efeSJed Brown    Output Parameter:
29220f5c6efeSJed Brown . F - the nonlinear residual
29230f5c6efeSJed Brown 
29240f5c6efeSJed Brown    Notes:
29250f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
29260f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
29270f5c6efeSJed Brown 
29280f5c6efeSJed Brown    Level: advanced
29290f5c6efeSJed Brown 
29300f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
29310f5c6efeSJed Brown @*/
2932*0910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
29330f5c6efeSJed Brown {
29340f5c6efeSJed Brown   TS             ts = (TS)ctx;
29350f5c6efeSJed Brown   PetscErrorCode ierr;
29360f5c6efeSJed Brown 
29370f5c6efeSJed Brown   PetscFunctionBegin;
29380f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2939*0910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
29400f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
29410f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
2942*0910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
29430f5c6efeSJed Brown   PetscFunctionReturn(0);
29440f5c6efeSJed Brown }
29450f5c6efeSJed Brown 
29460f5c6efeSJed Brown #undef __FUNCT__
29470f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
29480f5c6efeSJed Brown /*@
29490f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
29500f5c6efeSJed Brown 
29510f5c6efeSJed Brown    Collective on SNES
29520f5c6efeSJed Brown 
29530f5c6efeSJed Brown    Input Parameter:
29540f5c6efeSJed Brown + snes - nonlinear solver
2955*0910c330SBarry Smith . U - the current state at which to evaluate the residual
29560f5c6efeSJed Brown - ctx - user context, must be a TS
29570f5c6efeSJed Brown 
29580f5c6efeSJed Brown    Output Parameter:
29590f5c6efeSJed Brown + A - the Jacobian
29600f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
29610f5c6efeSJed Brown - flag - indicates any structure change in the matrix
29620f5c6efeSJed Brown 
29630f5c6efeSJed Brown    Notes:
29640f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
29650f5c6efeSJed Brown 
29660f5c6efeSJed Brown    Level: developer
29670f5c6efeSJed Brown 
29680f5c6efeSJed Brown .seealso: SNESSetJacobian()
29690f5c6efeSJed Brown @*/
2970*0910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
29710f5c6efeSJed Brown {
29720f5c6efeSJed Brown   TS             ts = (TS)ctx;
29730f5c6efeSJed Brown   PetscErrorCode ierr;
29740f5c6efeSJed Brown 
29750f5c6efeSJed Brown   PetscFunctionBegin;
29760f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2977*0910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
29780f5c6efeSJed Brown   PetscValidPointer(A,3);
29790f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
29800f5c6efeSJed Brown   PetscValidPointer(B,4);
29810f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
29820f5c6efeSJed Brown   PetscValidPointer(flag,5);
29830f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
2984*0910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
29850f5c6efeSJed Brown   PetscFunctionReturn(0);
29860f5c6efeSJed Brown }
2987325fc9f4SBarry Smith 
29880e4ef248SJed Brown #undef __FUNCT__
29890e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
29900e4ef248SJed Brown /*@C
29910e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
29920e4ef248SJed Brown 
29930e4ef248SJed Brown    Collective on TS
29940e4ef248SJed Brown 
29950e4ef248SJed Brown    Input Arguments:
29960e4ef248SJed Brown +  ts - time stepping context
29970e4ef248SJed Brown .  t - time at which to evaluate
2998*0910c330SBarry Smith .  U - state at which to evaluate
29990e4ef248SJed Brown -  ctx - context
30000e4ef248SJed Brown 
30010e4ef248SJed Brown    Output Arguments:
30020e4ef248SJed Brown .  F - right hand side
30030e4ef248SJed Brown 
30040e4ef248SJed Brown    Level: intermediate
30050e4ef248SJed Brown 
30060e4ef248SJed Brown    Notes:
30070e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
30080e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
30090e4ef248SJed Brown 
30100e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
30110e4ef248SJed Brown @*/
3012*0910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
30130e4ef248SJed Brown {
30140e4ef248SJed Brown   PetscErrorCode ierr;
30150e4ef248SJed Brown   Mat            Arhs,Brhs;
30160e4ef248SJed Brown   MatStructure   flg2;
30170e4ef248SJed Brown 
30180e4ef248SJed Brown   PetscFunctionBegin;
30190e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
3020*0910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
3021*0910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
30220e4ef248SJed Brown   PetscFunctionReturn(0);
30230e4ef248SJed Brown }
30240e4ef248SJed Brown 
30250e4ef248SJed Brown #undef __FUNCT__
30260e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
30270e4ef248SJed Brown /*@C
30280e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
30290e4ef248SJed Brown 
30300e4ef248SJed Brown    Collective on TS
30310e4ef248SJed Brown 
30320e4ef248SJed Brown    Input Arguments:
30330e4ef248SJed Brown +  ts - time stepping context
30340e4ef248SJed Brown .  t - time at which to evaluate
3035*0910c330SBarry Smith .  U - state at which to evaluate
30360e4ef248SJed Brown -  ctx - context
30370e4ef248SJed Brown 
30380e4ef248SJed Brown    Output Arguments:
30390e4ef248SJed Brown +  A - pointer to operator
30400e4ef248SJed Brown .  B - pointer to preconditioning matrix
30410e4ef248SJed Brown -  flg - matrix structure flag
30420e4ef248SJed Brown 
30430e4ef248SJed Brown    Level: intermediate
30440e4ef248SJed Brown 
30450e4ef248SJed Brown    Notes:
30460e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
30470e4ef248SJed Brown 
30480e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
30490e4ef248SJed Brown @*/
3050*0910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
30510e4ef248SJed Brown {
30520e4ef248SJed Brown   PetscFunctionBegin;
30530e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
30540e4ef248SJed Brown   PetscFunctionReturn(0);
30550e4ef248SJed Brown }
30560e4ef248SJed Brown 
30570026cea9SSean Farley #undef __FUNCT__
30580026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
30590026cea9SSean Farley /*@C
30600026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
30610026cea9SSean Farley 
30620026cea9SSean Farley    Collective on TS
30630026cea9SSean Farley 
30640026cea9SSean Farley    Input Arguments:
30650026cea9SSean Farley +  ts - time stepping context
30660026cea9SSean Farley .  t - time at which to evaluate
3067*0910c330SBarry Smith .  U - state at which to evaluate
3068*0910c330SBarry Smith .  Udot - time derivative of state vector
30690026cea9SSean Farley -  ctx - context
30700026cea9SSean Farley 
30710026cea9SSean Farley    Output Arguments:
30720026cea9SSean Farley .  F - left hand side
30730026cea9SSean Farley 
30740026cea9SSean Farley    Level: intermediate
30750026cea9SSean Farley 
30760026cea9SSean Farley    Notes:
3077*0910c330SBarry Smith    The assumption here is that the left hand side is of the form A*Udot (and not A*Udot + B*U). For other cases, the
30780026cea9SSean Farley    user is required to write their own TSComputeIFunction.
30790026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
30800026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
30810026cea9SSean Farley 
30820026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
30830026cea9SSean Farley @*/
3084*0910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
30850026cea9SSean Farley {
30860026cea9SSean Farley   PetscErrorCode ierr;
30870026cea9SSean Farley   Mat            A,B;
30880026cea9SSean Farley   MatStructure   flg2;
30890026cea9SSean Farley 
30900026cea9SSean Farley   PetscFunctionBegin;
30910026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
3092*0910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
3093*0910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
30940026cea9SSean Farley   PetscFunctionReturn(0);
30950026cea9SSean Farley }
30960026cea9SSean Farley 
30970026cea9SSean Farley #undef __FUNCT__
30980026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
30990026cea9SSean Farley /*@C
310024e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
31010026cea9SSean Farley 
31020026cea9SSean Farley    Collective on TS
31030026cea9SSean Farley 
31040026cea9SSean Farley    Input Arguments:
31050026cea9SSean Farley +  ts - time stepping context
31060026cea9SSean Farley .  t - time at which to evaluate
3107*0910c330SBarry Smith .  U - state at which to evaluate
3108*0910c330SBarry Smith .  Udot - time derivative of state vector
31090026cea9SSean Farley .  shift - shift to apply
31100026cea9SSean Farley -  ctx - context
31110026cea9SSean Farley 
31120026cea9SSean Farley    Output Arguments:
31130026cea9SSean Farley +  A - pointer to operator
31140026cea9SSean Farley .  B - pointer to preconditioning matrix
31150026cea9SSean Farley -  flg - matrix structure flag
31160026cea9SSean Farley 
31170026cea9SSean Farley    Level: intermediate
31180026cea9SSean Farley 
31190026cea9SSean Farley    Notes:
31200026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
31210026cea9SSean Farley 
31220026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
31230026cea9SSean Farley @*/
3124*0910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
31250026cea9SSean Farley {
31260026cea9SSean Farley   PetscFunctionBegin;
31270026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
31280026cea9SSean Farley   PetscFunctionReturn(0);
31290026cea9SSean Farley }
31300026cea9SSean Farley 
31314af1b03aSJed Brown 
31324af1b03aSJed Brown #undef __FUNCT__
31334af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
31344af1b03aSJed Brown /*@
31354af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
31364af1b03aSJed Brown 
31374af1b03aSJed Brown    Not Collective
31384af1b03aSJed Brown 
31394af1b03aSJed Brown    Input Parameter:
31404af1b03aSJed Brown .  ts - the TS context
31414af1b03aSJed Brown 
31424af1b03aSJed Brown    Output Parameter:
31434af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
31444af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
31454af1b03aSJed Brown 
31464af1b03aSJed Brown    Level: intermediate
31474af1b03aSJed Brown 
3148cd652676SJed Brown    Notes:
3149cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
31504af1b03aSJed Brown 
31514af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
31524af1b03aSJed Brown 
31534af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
31544af1b03aSJed Brown @*/
31554af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
31564af1b03aSJed Brown {
31574af1b03aSJed Brown   PetscFunctionBegin;
31584af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31594af1b03aSJed Brown   PetscValidPointer(reason,2);
31604af1b03aSJed Brown   *reason = ts->reason;
31614af1b03aSJed Brown   PetscFunctionReturn(0);
31624af1b03aSJed Brown }
31634af1b03aSJed Brown 
3164fb1732b5SBarry Smith #undef __FUNCT__
31655ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
31669f67acb7SJed Brown /*@
31675ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
31689f67acb7SJed Brown    used by the time integrator.
31699f67acb7SJed Brown 
31709f67acb7SJed Brown    Not Collective
31719f67acb7SJed Brown 
31729f67acb7SJed Brown    Input Parameter:
31739f67acb7SJed Brown .  ts - TS context
31749f67acb7SJed Brown 
31759f67acb7SJed Brown    Output Parameter:
31769f67acb7SJed Brown .  nits - number of nonlinear iterations
31779f67acb7SJed Brown 
31789f67acb7SJed Brown    Notes:
31799f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
31809f67acb7SJed Brown 
31819f67acb7SJed Brown    Level: intermediate
31829f67acb7SJed Brown 
31839f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
31849f67acb7SJed Brown 
31855ef26d82SJed Brown .seealso:  TSGetKSPIterations()
31869f67acb7SJed Brown @*/
31875ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
31889f67acb7SJed Brown {
31899f67acb7SJed Brown   PetscFunctionBegin;
31909f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31919f67acb7SJed Brown   PetscValidIntPointer(nits,2);
31925ef26d82SJed Brown   *nits = ts->snes_its;
31939f67acb7SJed Brown   PetscFunctionReturn(0);
31949f67acb7SJed Brown }
31959f67acb7SJed Brown 
31969f67acb7SJed Brown #undef __FUNCT__
31975ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
31989f67acb7SJed Brown /*@
31995ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
32009f67acb7SJed Brown    used by the time integrator.
32019f67acb7SJed Brown 
32029f67acb7SJed Brown    Not Collective
32039f67acb7SJed Brown 
32049f67acb7SJed Brown    Input Parameter:
32059f67acb7SJed Brown .  ts - TS context
32069f67acb7SJed Brown 
32079f67acb7SJed Brown    Output Parameter:
32089f67acb7SJed Brown .  lits - number of linear iterations
32099f67acb7SJed Brown 
32109f67acb7SJed Brown    Notes:
32119f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
32129f67acb7SJed Brown 
32139f67acb7SJed Brown    Level: intermediate
32149f67acb7SJed Brown 
32159f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
32169f67acb7SJed Brown 
32175ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
32189f67acb7SJed Brown @*/
32195ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
32209f67acb7SJed Brown {
32219f67acb7SJed Brown   PetscFunctionBegin;
32229f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32239f67acb7SJed Brown   PetscValidIntPointer(lits,2);
32245ef26d82SJed Brown   *lits = ts->ksp_its;
32259f67acb7SJed Brown   PetscFunctionReturn(0);
32269f67acb7SJed Brown }
32279f67acb7SJed Brown 
32289f67acb7SJed Brown #undef __FUNCT__
3229cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3230cef5090cSJed Brown /*@
3231cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3232cef5090cSJed Brown 
3233cef5090cSJed Brown    Not Collective
3234cef5090cSJed Brown 
3235cef5090cSJed Brown    Input Parameter:
3236cef5090cSJed Brown .  ts - TS context
3237cef5090cSJed Brown 
3238cef5090cSJed Brown    Output Parameter:
3239cef5090cSJed Brown .  rejects - number of steps rejected
3240cef5090cSJed Brown 
3241cef5090cSJed Brown    Notes:
3242cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3243cef5090cSJed Brown 
3244cef5090cSJed Brown    Level: intermediate
3245cef5090cSJed Brown 
3246cef5090cSJed Brown .keywords: TS, get, number
3247cef5090cSJed Brown 
32485ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3249cef5090cSJed Brown @*/
3250cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3251cef5090cSJed Brown {
3252cef5090cSJed Brown   PetscFunctionBegin;
3253cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3254cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3255cef5090cSJed Brown   *rejects = ts->reject;
3256cef5090cSJed Brown   PetscFunctionReturn(0);
3257cef5090cSJed Brown }
3258cef5090cSJed Brown 
3259cef5090cSJed Brown #undef __FUNCT__
3260cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3261cef5090cSJed Brown /*@
3262cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3263cef5090cSJed Brown 
3264cef5090cSJed Brown    Not Collective
3265cef5090cSJed Brown 
3266cef5090cSJed Brown    Input Parameter:
3267cef5090cSJed Brown .  ts - TS context
3268cef5090cSJed Brown 
3269cef5090cSJed Brown    Output Parameter:
3270cef5090cSJed Brown .  fails - number of failed nonlinear solves
3271cef5090cSJed Brown 
3272cef5090cSJed Brown    Notes:
3273cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3274cef5090cSJed Brown 
3275cef5090cSJed Brown    Level: intermediate
3276cef5090cSJed Brown 
3277cef5090cSJed Brown .keywords: TS, get, number
3278cef5090cSJed Brown 
32795ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3280cef5090cSJed Brown @*/
3281cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3282cef5090cSJed Brown {
3283cef5090cSJed Brown   PetscFunctionBegin;
3284cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3285cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3286cef5090cSJed Brown   *fails = ts->num_snes_failures;
3287cef5090cSJed Brown   PetscFunctionReturn(0);
3288cef5090cSJed Brown }
3289cef5090cSJed Brown 
3290cef5090cSJed Brown #undef __FUNCT__
3291cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3292cef5090cSJed Brown /*@
3293cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3294cef5090cSJed Brown 
3295cef5090cSJed Brown    Not Collective
3296cef5090cSJed Brown 
3297cef5090cSJed Brown    Input Parameter:
3298cef5090cSJed Brown +  ts - TS context
3299cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3300cef5090cSJed Brown 
3301cef5090cSJed Brown    Notes:
3302cef5090cSJed Brown    The counter is reset to zero for each step
3303cef5090cSJed Brown 
3304cef5090cSJed Brown    Options Database Key:
3305cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3306cef5090cSJed Brown 
3307cef5090cSJed Brown    Level: intermediate
3308cef5090cSJed Brown 
3309cef5090cSJed Brown .keywords: TS, set, maximum, number
3310cef5090cSJed Brown 
33115ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3312cef5090cSJed Brown @*/
3313cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3314cef5090cSJed Brown {
3315cef5090cSJed Brown   PetscFunctionBegin;
3316cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3317cef5090cSJed Brown   ts->max_reject = rejects;
3318cef5090cSJed Brown   PetscFunctionReturn(0);
3319cef5090cSJed Brown }
3320cef5090cSJed Brown 
3321cef5090cSJed Brown #undef __FUNCT__
3322cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3323cef5090cSJed Brown /*@
3324cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3325cef5090cSJed Brown 
3326cef5090cSJed Brown    Not Collective
3327cef5090cSJed Brown 
3328cef5090cSJed Brown    Input Parameter:
3329cef5090cSJed Brown +  ts - TS context
3330cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3331cef5090cSJed Brown 
3332cef5090cSJed Brown    Notes:
3333cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3334cef5090cSJed Brown 
3335cef5090cSJed Brown    Options Database Key:
3336cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3337cef5090cSJed Brown 
3338cef5090cSJed Brown    Level: intermediate
3339cef5090cSJed Brown 
3340cef5090cSJed Brown .keywords: TS, set, maximum, number
3341cef5090cSJed Brown 
33425ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3343cef5090cSJed Brown @*/
3344cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3345cef5090cSJed Brown {
3346cef5090cSJed Brown   PetscFunctionBegin;
3347cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3348cef5090cSJed Brown   ts->max_snes_failures = fails;
3349cef5090cSJed Brown   PetscFunctionReturn(0);
3350cef5090cSJed Brown }
3351cef5090cSJed Brown 
3352cef5090cSJed Brown #undef __FUNCT__
3353cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3354cef5090cSJed Brown /*@
3355cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3356cef5090cSJed Brown 
3357cef5090cSJed Brown    Not Collective
3358cef5090cSJed Brown 
3359cef5090cSJed Brown    Input Parameter:
3360cef5090cSJed Brown +  ts - TS context
3361cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3362cef5090cSJed Brown 
3363cef5090cSJed Brown    Options Database Key:
3364cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3365cef5090cSJed Brown 
3366cef5090cSJed Brown    Level: intermediate
3367cef5090cSJed Brown 
3368cef5090cSJed Brown .keywords: TS, set, error
3369cef5090cSJed Brown 
33705ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3371cef5090cSJed Brown @*/
3372cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3373cef5090cSJed Brown {
3374cef5090cSJed Brown   PetscFunctionBegin;
3375cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3376cef5090cSJed Brown   ts->errorifstepfailed = err;
3377cef5090cSJed Brown   PetscFunctionReturn(0);
3378cef5090cSJed Brown }
3379cef5090cSJed Brown 
3380cef5090cSJed Brown #undef __FUNCT__
3381fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3382fb1732b5SBarry Smith /*@C
3383fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3384fb1732b5SBarry Smith 
3385fb1732b5SBarry Smith    Collective on TS
3386fb1732b5SBarry Smith 
3387fb1732b5SBarry Smith    Input Parameters:
3388fb1732b5SBarry Smith +  ts - the TS context
3389fb1732b5SBarry Smith .  step - current time-step
3390fb1732b5SBarry Smith .  ptime - current time
3391*0910c330SBarry Smith .  u - current state
3392fb1732b5SBarry Smith -  viewer - binary viewer
3393fb1732b5SBarry Smith 
3394fb1732b5SBarry Smith    Level: intermediate
3395fb1732b5SBarry Smith 
3396fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3397fb1732b5SBarry Smith 
3398fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3399fb1732b5SBarry Smith @*/
3400*0910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3401fb1732b5SBarry Smith {
3402fb1732b5SBarry Smith   PetscErrorCode ierr;
3403ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3404fb1732b5SBarry Smith 
3405fb1732b5SBarry Smith   PetscFunctionBegin;
3406*0910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3407ed81e22dSJed Brown   PetscFunctionReturn(0);
3408ed81e22dSJed Brown }
3409ed81e22dSJed Brown 
3410ed81e22dSJed Brown #undef __FUNCT__
3411ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3412ed81e22dSJed Brown /*@C
3413ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3414ed81e22dSJed Brown 
3415ed81e22dSJed Brown    Collective on TS
3416ed81e22dSJed Brown 
3417ed81e22dSJed Brown    Input Parameters:
3418ed81e22dSJed Brown +  ts - the TS context
3419ed81e22dSJed Brown .  step - current time-step
3420ed81e22dSJed Brown .  ptime - current time
3421*0910c330SBarry Smith .  u - current state
3422ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3423ed81e22dSJed Brown 
3424ed81e22dSJed Brown    Level: intermediate
3425ed81e22dSJed Brown 
3426ed81e22dSJed Brown    Notes:
3427ed81e22dSJed 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.
3428ed81e22dSJed Brown    These are named according to the file name template.
3429ed81e22dSJed Brown 
3430ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3431ed81e22dSJed Brown 
3432ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3433ed81e22dSJed Brown 
3434ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3435ed81e22dSJed Brown @*/
3436*0910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3437ed81e22dSJed Brown {
3438ed81e22dSJed Brown   PetscErrorCode ierr;
3439ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3440ed81e22dSJed Brown   PetscViewer    viewer;
3441ed81e22dSJed Brown 
3442ed81e22dSJed Brown   PetscFunctionBegin;
34438caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3444ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
3445*0910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3446ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3447ed81e22dSJed Brown   PetscFunctionReturn(0);
3448ed81e22dSJed Brown }
3449ed81e22dSJed Brown 
3450ed81e22dSJed Brown #undef __FUNCT__
3451ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3452ed81e22dSJed Brown /*@C
3453ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3454ed81e22dSJed Brown 
3455ed81e22dSJed Brown    Collective on TS
3456ed81e22dSJed Brown 
3457ed81e22dSJed Brown    Input Parameters:
3458ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3459ed81e22dSJed Brown 
3460ed81e22dSJed Brown    Level: intermediate
3461ed81e22dSJed Brown 
3462ed81e22dSJed Brown    Note:
3463ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3464ed81e22dSJed Brown 
3465ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3466ed81e22dSJed Brown 
3467ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3468ed81e22dSJed Brown @*/
3469ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3470ed81e22dSJed Brown {
3471ed81e22dSJed Brown   PetscErrorCode ierr;
3472ed81e22dSJed Brown 
3473ed81e22dSJed Brown   PetscFunctionBegin;
3474ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3475fb1732b5SBarry Smith   PetscFunctionReturn(0);
3476fb1732b5SBarry Smith }
3477fb1732b5SBarry Smith 
347884df9cb4SJed Brown #undef __FUNCT__
347984df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt"
348084df9cb4SJed Brown /*@
348184df9cb4SJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
348284df9cb4SJed Brown 
3483ed81e22dSJed Brown    Collective on TS if controller has not been created yet
348484df9cb4SJed Brown 
348584df9cb4SJed Brown    Input Arguments:
3486ed81e22dSJed Brown .  ts - time stepping context
348784df9cb4SJed Brown 
348884df9cb4SJed Brown    Output Arguments:
3489ed81e22dSJed Brown .  adapt - adaptive controller
349084df9cb4SJed Brown 
349184df9cb4SJed Brown    Level: intermediate
349284df9cb4SJed Brown 
3493ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
349484df9cb4SJed Brown @*/
349584df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
349684df9cb4SJed Brown {
349784df9cb4SJed Brown   PetscErrorCode ierr;
349884df9cb4SJed Brown 
349984df9cb4SJed Brown   PetscFunctionBegin;
350084df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
350184df9cb4SJed Brown   PetscValidPointer(adapt,2);
350284df9cb4SJed Brown   if (!ts->adapt) {
350384df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
35041c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
35051c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
350684df9cb4SJed Brown   }
350784df9cb4SJed Brown   *adapt = ts->adapt;
350884df9cb4SJed Brown   PetscFunctionReturn(0);
350984df9cb4SJed Brown }
3510d6ebe24aSShri Abhyankar 
3511d6ebe24aSShri Abhyankar #undef __FUNCT__
35121c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
35131c3436cfSJed Brown /*@
35141c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
35151c3436cfSJed Brown 
35161c3436cfSJed Brown    Logically Collective
35171c3436cfSJed Brown 
35181c3436cfSJed Brown    Input Arguments:
35191c3436cfSJed Brown +  ts - time integration context
35201c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
35211c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
35221c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
35231c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
35241c3436cfSJed Brown 
35251c3436cfSJed Brown    Level: beginner
35261c3436cfSJed Brown 
3527c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
35281c3436cfSJed Brown @*/
35291c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
35301c3436cfSJed Brown {
35311c3436cfSJed Brown   PetscErrorCode ierr;
35321c3436cfSJed Brown 
35331c3436cfSJed Brown   PetscFunctionBegin;
3534c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
35351c3436cfSJed Brown   if (vatol) {
35361c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
35371c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
35381c3436cfSJed Brown     ts->vatol = vatol;
35391c3436cfSJed Brown   }
3540c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
35411c3436cfSJed Brown   if (vrtol) {
35421c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
35431c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
35441c3436cfSJed Brown     ts->vrtol = vrtol;
35451c3436cfSJed Brown   }
35461c3436cfSJed Brown   PetscFunctionReturn(0);
35471c3436cfSJed Brown }
35481c3436cfSJed Brown 
35491c3436cfSJed Brown #undef __FUNCT__
3550c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3551c5033834SJed Brown /*@
3552c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3553c5033834SJed Brown 
3554c5033834SJed Brown    Logically Collective
3555c5033834SJed Brown 
3556c5033834SJed Brown    Input Arguments:
3557c5033834SJed Brown .  ts - time integration context
3558c5033834SJed Brown 
3559c5033834SJed Brown    Output Arguments:
3560c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3561c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3562c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3563c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3564c5033834SJed Brown 
3565c5033834SJed Brown    Level: beginner
3566c5033834SJed Brown 
3567c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3568c5033834SJed Brown @*/
3569c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3570c5033834SJed Brown {
3571c5033834SJed Brown   PetscFunctionBegin;
3572c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3573c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3574c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3575c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3576c5033834SJed Brown   PetscFunctionReturn(0);
3577c5033834SJed Brown }
3578c5033834SJed Brown 
3579c5033834SJed Brown #undef __FUNCT__
35801c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
35811c3436cfSJed Brown /*@
35821c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
35831c3436cfSJed Brown 
35841c3436cfSJed Brown    Collective on TS
35851c3436cfSJed Brown 
35861c3436cfSJed Brown    Input Arguments:
35871c3436cfSJed Brown +  ts - time stepping context
35881c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
35891c3436cfSJed Brown 
35901c3436cfSJed Brown    Output Arguments:
35911c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
35921c3436cfSJed Brown 
35931c3436cfSJed Brown    Level: developer
35941c3436cfSJed Brown 
35951c3436cfSJed Brown .seealso: TSSetTolerances()
35961c3436cfSJed Brown @*/
35971c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
35981c3436cfSJed Brown {
35991c3436cfSJed Brown   PetscErrorCode    ierr;
36001c3436cfSJed Brown   PetscInt          i,n,N;
3601*0910c330SBarry Smith   const PetscScalar *u,*y;
3602*0910c330SBarry Smith   Vec               U;
36031c3436cfSJed Brown   PetscReal         sum,gsum;
36041c3436cfSJed Brown 
36051c3436cfSJed Brown   PetscFunctionBegin;
36061c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36071c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
36081c3436cfSJed Brown   PetscValidPointer(norm,3);
3609*0910c330SBarry Smith   U = ts->vec_sol;
3610*0910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
3611*0910c330SBarry Smith   if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
36121c3436cfSJed Brown 
3613*0910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
3614*0910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
3615*0910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
36161c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
36171c3436cfSJed Brown   sum = 0.;
3618ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3619ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3620ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3621ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3622ceefc113SJed Brown     for (i=0; i<n; i++) {
3623*0910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
3624*0910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3625ceefc113SJed Brown     }
3626ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3627ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3628ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3629ceefc113SJed Brown     const PetscScalar *atol;
3630ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3631ceefc113SJed Brown     for (i=0; i<n; i++) {
3632*0910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
3633*0910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3634ceefc113SJed Brown     }
3635ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3636ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3637ceefc113SJed Brown     const PetscScalar *rtol;
3638ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3639ceefc113SJed Brown     for (i=0; i<n; i++) {
3640*0910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
3641*0910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3642ceefc113SJed Brown     }
3643ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3644ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
36451c3436cfSJed Brown     for (i=0; i<n; i++) {
3646*0910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
3647*0910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
36481c3436cfSJed Brown     }
3649ceefc113SJed Brown   }
3650*0910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
36511c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
36521c3436cfSJed Brown 
36531c3436cfSJed Brown   ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
36541c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
36551c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
36561c3436cfSJed Brown   PetscFunctionReturn(0);
36571c3436cfSJed Brown }
36581c3436cfSJed Brown 
36591c3436cfSJed Brown #undef __FUNCT__
36608d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
36618d59e960SJed Brown /*@
36628d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
36638d59e960SJed Brown 
36648d59e960SJed Brown    Logically Collective on TS
36658d59e960SJed Brown 
36668d59e960SJed Brown    Input Arguments:
36678d59e960SJed Brown +  ts - time stepping context
36688d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
36698d59e960SJed Brown 
36708d59e960SJed Brown    Note:
36718d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
36728d59e960SJed Brown 
36738d59e960SJed Brown    Level: intermediate
36748d59e960SJed Brown 
36758d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
36768d59e960SJed Brown @*/
36778d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
36788d59e960SJed Brown {
36798d59e960SJed Brown   PetscFunctionBegin;
36808d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36818d59e960SJed Brown   ts->cfltime_local = cfltime;
36828d59e960SJed Brown   ts->cfltime = -1.;
36838d59e960SJed Brown   PetscFunctionReturn(0);
36848d59e960SJed Brown }
36858d59e960SJed Brown 
36868d59e960SJed Brown #undef __FUNCT__
36878d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
36888d59e960SJed Brown /*@
36898d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
36908d59e960SJed Brown 
36918d59e960SJed Brown    Collective on TS
36928d59e960SJed Brown 
36938d59e960SJed Brown    Input Arguments:
36948d59e960SJed Brown .  ts - time stepping context
36958d59e960SJed Brown 
36968d59e960SJed Brown    Output Arguments:
36978d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
36988d59e960SJed Brown 
36998d59e960SJed Brown    Level: advanced
37008d59e960SJed Brown 
37018d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
37028d59e960SJed Brown @*/
37038d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
37048d59e960SJed Brown {
37058d59e960SJed Brown   PetscErrorCode ierr;
37068d59e960SJed Brown 
37078d59e960SJed Brown   PetscFunctionBegin;
37088d59e960SJed Brown   if (ts->cfltime < 0) {
37098d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
37108d59e960SJed Brown   }
37118d59e960SJed Brown   *cfltime = ts->cfltime;
37128d59e960SJed Brown   PetscFunctionReturn(0);
37138d59e960SJed Brown }
37148d59e960SJed Brown 
37158d59e960SJed Brown #undef __FUNCT__
3716d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
3717d6ebe24aSShri Abhyankar /*@
3718d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
3719d6ebe24aSShri Abhyankar 
3720d6ebe24aSShri Abhyankar    Input Parameters:
3721d6ebe24aSShri Abhyankar .  ts   - the TS context.
3722d6ebe24aSShri Abhyankar .  xl   - lower bound.
3723d6ebe24aSShri Abhyankar .  xu   - upper bound.
3724d6ebe24aSShri Abhyankar 
3725d6ebe24aSShri Abhyankar    Notes:
3726d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
372733c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
3728d6ebe24aSShri Abhyankar 
37292bd2b0e6SSatish Balay    Level: advanced
37302bd2b0e6SSatish Balay 
3731d6ebe24aSShri Abhyankar @*/
3732d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
3733d6ebe24aSShri Abhyankar {
3734d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
3735d6ebe24aSShri Abhyankar   SNES           snes;
3736d6ebe24aSShri Abhyankar 
3737d6ebe24aSShri Abhyankar   PetscFunctionBegin;
3738d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3739d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
3740d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
3741d6ebe24aSShri Abhyankar }
3742d6ebe24aSShri Abhyankar 
3743325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3744c6db04a5SJed Brown #include <mex.h>
3745325fc9f4SBarry Smith 
3746325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
3747325fc9f4SBarry Smith 
3748325fc9f4SBarry Smith #undef __FUNCT__
3749325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
3750325fc9f4SBarry Smith /*
3751325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
3752325fc9f4SBarry Smith                          TSSetFunctionMatlab().
3753325fc9f4SBarry Smith 
3754325fc9f4SBarry Smith    Collective on TS
3755325fc9f4SBarry Smith 
3756325fc9f4SBarry Smith    Input Parameters:
3757325fc9f4SBarry Smith +  snes - the TS context
3758*0910c330SBarry Smith -  u - input vector
3759325fc9f4SBarry Smith 
3760325fc9f4SBarry Smith    Output Parameter:
3761325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
3762325fc9f4SBarry Smith 
3763325fc9f4SBarry Smith    Notes:
3764325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
3765325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
3766325fc9f4SBarry Smith    themselves.
3767325fc9f4SBarry Smith 
3768325fc9f4SBarry Smith    Level: developer
3769325fc9f4SBarry Smith 
3770325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3771325fc9f4SBarry Smith 
3772325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3773325fc9f4SBarry Smith */
3774*0910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
3775325fc9f4SBarry Smith {
3776325fc9f4SBarry Smith   PetscErrorCode   ierr;
3777325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3778325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
3779325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
3780325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
3781325fc9f4SBarry Smith 
3782325fc9f4SBarry Smith   PetscFunctionBegin;
3783325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
3784*0910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
3785*0910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
3786325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
3787*0910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
3788325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
3789325fc9f4SBarry Smith 
3790325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3791*0910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
3792*0910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
3793*0910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
3794325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3795325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
3796325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3797325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3798325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
3799325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
3800325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
3801325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
3802325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3803325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3804325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3805325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3806325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3807325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3808325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3809325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3810325fc9f4SBarry Smith   PetscFunctionReturn(0);
3811325fc9f4SBarry Smith }
3812325fc9f4SBarry Smith 
3813325fc9f4SBarry Smith 
3814325fc9f4SBarry Smith #undef __FUNCT__
3815325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
3816325fc9f4SBarry Smith /*
3817325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
3818325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
3819e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
3820325fc9f4SBarry Smith 
3821325fc9f4SBarry Smith    Logically Collective on TS
3822325fc9f4SBarry Smith 
3823325fc9f4SBarry Smith    Input Parameters:
3824325fc9f4SBarry Smith +  ts - the TS context
3825325fc9f4SBarry Smith -  func - function evaluation routine
3826325fc9f4SBarry Smith 
3827325fc9f4SBarry Smith    Calling sequence of func:
3828*0910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
3829325fc9f4SBarry Smith 
3830325fc9f4SBarry Smith    Level: beginner
3831325fc9f4SBarry Smith 
3832325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3833325fc9f4SBarry Smith 
3834325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3835325fc9f4SBarry Smith */
3836cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
3837325fc9f4SBarry Smith {
3838325fc9f4SBarry Smith   PetscErrorCode  ierr;
3839325fc9f4SBarry Smith   TSMatlabContext *sctx;
3840325fc9f4SBarry Smith 
3841325fc9f4SBarry Smith   PetscFunctionBegin;
3842325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3843325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3844325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3845325fc9f4SBarry Smith   /*
3846325fc9f4SBarry Smith      This should work, but it doesn't
3847325fc9f4SBarry Smith   sctx->ctx = ctx;
3848325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3849325fc9f4SBarry Smith   */
3850325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3851cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
3852325fc9f4SBarry Smith   PetscFunctionReturn(0);
3853325fc9f4SBarry Smith }
3854325fc9f4SBarry Smith 
3855325fc9f4SBarry Smith #undef __FUNCT__
3856325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
3857325fc9f4SBarry Smith /*
3858325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
3859325fc9f4SBarry Smith                          TSSetJacobianMatlab().
3860325fc9f4SBarry Smith 
3861325fc9f4SBarry Smith    Collective on TS
3862325fc9f4SBarry Smith 
3863325fc9f4SBarry Smith    Input Parameters:
3864cdcf91faSSean Farley +  ts - the TS context
3865*0910c330SBarry Smith .  u - input vector
3866325fc9f4SBarry Smith .  A, B - the matrices
3867325fc9f4SBarry Smith -  ctx - user context
3868325fc9f4SBarry Smith 
3869325fc9f4SBarry Smith    Output Parameter:
3870325fc9f4SBarry Smith .  flag - structure of the matrix
3871325fc9f4SBarry Smith 
3872325fc9f4SBarry Smith    Level: developer
3873325fc9f4SBarry Smith 
3874325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3875325fc9f4SBarry Smith 
3876325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3877325fc9f4SBarry Smith @*/
3878*0910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
3879325fc9f4SBarry Smith {
3880325fc9f4SBarry Smith   PetscErrorCode  ierr;
3881325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3882325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
3883325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
3884325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
3885325fc9f4SBarry Smith 
3886325fc9f4SBarry Smith   PetscFunctionBegin;
3887cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3888*0910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
3889325fc9f4SBarry Smith 
3890*0910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
3891325fc9f4SBarry Smith 
3892cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3893*0910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
3894*0910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
3895*0910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
3896*0910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
3897325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3898325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
3899325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3900325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3901325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
3902325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
3903325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
3904325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
3905325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
3906325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
3907325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3908325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
3909325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3910325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3911325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3912325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3913325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3914325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3915325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
3916325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
3917325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3918325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
3919325fc9f4SBarry Smith   PetscFunctionReturn(0);
3920325fc9f4SBarry Smith }
3921325fc9f4SBarry Smith 
3922325fc9f4SBarry Smith 
3923325fc9f4SBarry Smith #undef __FUNCT__
3924325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
3925325fc9f4SBarry Smith /*
3926325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
3927e3c5b3baSBarry 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
3928325fc9f4SBarry Smith 
3929325fc9f4SBarry Smith    Logically Collective on TS
3930325fc9f4SBarry Smith 
3931325fc9f4SBarry Smith    Input Parameters:
3932cdcf91faSSean Farley +  ts - the TS context
3933325fc9f4SBarry Smith .  A,B - Jacobian matrices
3934325fc9f4SBarry Smith .  func - function evaluation routine
3935325fc9f4SBarry Smith -  ctx - user context
3936325fc9f4SBarry Smith 
3937325fc9f4SBarry Smith    Calling sequence of func:
3938*0910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
3939325fc9f4SBarry Smith 
3940325fc9f4SBarry Smith 
3941325fc9f4SBarry Smith    Level: developer
3942325fc9f4SBarry Smith 
3943325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3944325fc9f4SBarry Smith 
3945325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3946325fc9f4SBarry Smith */
3947cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
3948325fc9f4SBarry Smith {
3949325fc9f4SBarry Smith   PetscErrorCode    ierr;
3950325fc9f4SBarry Smith   TSMatlabContext *sctx;
3951325fc9f4SBarry Smith 
3952325fc9f4SBarry Smith   PetscFunctionBegin;
3953325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3954325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3955325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3956325fc9f4SBarry Smith   /*
3957325fc9f4SBarry Smith      This should work, but it doesn't
3958325fc9f4SBarry Smith   sctx->ctx = ctx;
3959325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3960325fc9f4SBarry Smith   */
3961325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3962cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
3963325fc9f4SBarry Smith   PetscFunctionReturn(0);
3964325fc9f4SBarry Smith }
3965325fc9f4SBarry Smith 
3966b5b1a830SBarry Smith #undef __FUNCT__
3967b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
3968b5b1a830SBarry Smith /*
3969b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
3970b5b1a830SBarry Smith 
3971b5b1a830SBarry Smith    Collective on TS
3972b5b1a830SBarry Smith 
3973b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3974b5b1a830SBarry Smith @*/
3975*0910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
3976b5b1a830SBarry Smith {
3977b5b1a830SBarry Smith   PetscErrorCode  ierr;
3978b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3979a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
3980b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
3981b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
3982b5b1a830SBarry Smith 
3983b5b1a830SBarry Smith   PetscFunctionBegin;
3984cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3985*0910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
3986b5b1a830SBarry Smith 
3987cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3988*0910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
3989b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3990b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
3991b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
3992b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
3993b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
3994b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
3995b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
3996b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3997b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
3998b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
3999b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4000b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4001b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4002b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4003b5b1a830SBarry Smith   PetscFunctionReturn(0);
4004b5b1a830SBarry Smith }
4005b5b1a830SBarry Smith 
4006b5b1a830SBarry Smith 
4007b5b1a830SBarry Smith #undef __FUNCT__
4008b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4009b5b1a830SBarry Smith /*
4010b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4011b5b1a830SBarry Smith 
4012b5b1a830SBarry Smith    Level: developer
4013b5b1a830SBarry Smith 
4014b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4015b5b1a830SBarry Smith 
4016b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4017b5b1a830SBarry Smith */
4018cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4019b5b1a830SBarry Smith {
4020b5b1a830SBarry Smith   PetscErrorCode    ierr;
4021b5b1a830SBarry Smith   TSMatlabContext *sctx;
4022b5b1a830SBarry Smith 
4023b5b1a830SBarry Smith   PetscFunctionBegin;
4024b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4025b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4026b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4027b5b1a830SBarry Smith   /*
4028b5b1a830SBarry Smith      This should work, but it doesn't
4029b5b1a830SBarry Smith   sctx->ctx = ctx;
4030b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4031b5b1a830SBarry Smith   */
4032b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4033cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
4034b5b1a830SBarry Smith   PetscFunctionReturn(0);
4035b5b1a830SBarry Smith }
4036325fc9f4SBarry Smith #endif
4037b3603a34SBarry Smith 
40380b039ecaSBarry Smith 
40390b039ecaSBarry Smith 
4040b3603a34SBarry Smith #undef __FUNCT__
40414f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4042b3603a34SBarry Smith /*@C
40434f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4044b3603a34SBarry Smith        in a time based line graph
4045b3603a34SBarry Smith 
4046b3603a34SBarry Smith    Collective on TS
4047b3603a34SBarry Smith 
4048b3603a34SBarry Smith    Input Parameters:
4049b3603a34SBarry Smith +  ts - the TS context
4050b3603a34SBarry Smith .  step - current time-step
4051b3603a34SBarry Smith .  ptime - current time
4052b3603a34SBarry Smith -  lg - a line graph object
4053b3603a34SBarry Smith 
4054b3603a34SBarry Smith    Level: intermediate
4055b3603a34SBarry Smith 
40560b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4057b3603a34SBarry Smith 
4058b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4059b3603a34SBarry Smith 
4060b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4061b3603a34SBarry Smith @*/
4062*0910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4063b3603a34SBarry Smith {
4064b3603a34SBarry Smith   PetscErrorCode    ierr;
40650b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4066b3603a34SBarry Smith   const PetscScalar *yy;
406758ff32f7SBarry Smith   PetscInt          dim;
4068b3603a34SBarry Smith 
4069b3603a34SBarry Smith   PetscFunctionBegin;
407058ff32f7SBarry Smith   if (!step) {
4071a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4072a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4073a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
4074*0910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
40750b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
40760b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
407758ff32f7SBarry Smith   }
4078*0910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4079e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4080e3efe391SJed Brown   {
4081e3efe391SJed Brown     PetscReal *yreal;
4082e3efe391SJed Brown     PetscInt i,n;
4083*0910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4084e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4085e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
40860b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4087e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4088e3efe391SJed Brown   }
4089e3efe391SJed Brown #else
40900b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4091e3efe391SJed Brown #endif
4092*0910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
40933fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
40940b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
40953923b477SBarry Smith   }
4096b3603a34SBarry Smith   PetscFunctionReturn(0);
4097b3603a34SBarry Smith }
4098b3603a34SBarry Smith 
4099b3603a34SBarry Smith #undef __FUNCT__
41004f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4101ef20d060SBarry Smith /*@C
41024f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4103ef20d060SBarry Smith        in a time based line graph
4104ef20d060SBarry Smith 
4105ef20d060SBarry Smith    Collective on TS
4106ef20d060SBarry Smith 
4107ef20d060SBarry Smith    Input Parameters:
4108ef20d060SBarry Smith +  ts - the TS context
4109ef20d060SBarry Smith .  step - current time-step
4110ef20d060SBarry Smith .  ptime - current time
4111ef20d060SBarry Smith -  lg - a line graph object
4112ef20d060SBarry Smith 
4113ef20d060SBarry Smith    Level: intermediate
4114ef20d060SBarry Smith 
4115abd5a294SJed Brown    Notes:
4116abd5a294SJed Brown    Only for sequential solves.
4117abd5a294SJed Brown 
4118abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4119abd5a294SJed Brown 
4120abd5a294SJed Brown    Options Database Keys:
41214f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4122ef20d060SBarry Smith 
4123ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4124ef20d060SBarry Smith 
4125abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4126ef20d060SBarry Smith @*/
4127*0910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4128ef20d060SBarry Smith {
4129ef20d060SBarry Smith   PetscErrorCode    ierr;
41300b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4131ef20d060SBarry Smith   const PetscScalar *yy;
4132ef20d060SBarry Smith   Vec               y;
4133a9f9c1f6SBarry Smith   PetscInt          dim;
4134ef20d060SBarry Smith 
4135ef20d060SBarry Smith   PetscFunctionBegin;
4136a9f9c1f6SBarry Smith   if (!step) {
4137a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4138a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
41391ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
4140*0910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4141a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4142a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4143a9f9c1f6SBarry Smith   }
4144*0910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4145ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
4146*0910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4147ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4148e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4149e3efe391SJed Brown   {
4150e3efe391SJed Brown     PetscReal *yreal;
4151e3efe391SJed Brown     PetscInt  i,n;
4152e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4153e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4154e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
41550b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4156e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4157e3efe391SJed Brown   }
4158e3efe391SJed Brown #else
41590b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4160e3efe391SJed Brown #endif
4161ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4162ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
41633fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
41640b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
41653923b477SBarry Smith   }
4166ef20d060SBarry Smith   PetscFunctionReturn(0);
4167ef20d060SBarry Smith }
4168ef20d060SBarry Smith 
4169201da799SBarry Smith #undef __FUNCT__
4170201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4171201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4172201da799SBarry Smith {
4173201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4174201da799SBarry Smith   PetscReal      x = ptime,y;
4175201da799SBarry Smith   PetscErrorCode ierr;
4176201da799SBarry Smith   PetscInt       its;
4177201da799SBarry Smith 
4178201da799SBarry Smith   PetscFunctionBegin;
4179201da799SBarry Smith   if (!n) {
4180201da799SBarry Smith     PetscDrawAxis  axis;
4181201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4182201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4183201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4184201da799SBarry Smith     ctx->snes_its  = 0;
4185201da799SBarry Smith   }
4186201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4187201da799SBarry Smith   y    = its - ctx->snes_its;
4188201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
41893fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){
4190201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4191201da799SBarry Smith   }
4192201da799SBarry Smith   ctx->snes_its = its;
4193201da799SBarry Smith   PetscFunctionReturn(0);
4194201da799SBarry Smith }
4195201da799SBarry Smith 
4196201da799SBarry Smith #undef __FUNCT__
4197201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4198201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4199201da799SBarry Smith {
4200201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4201201da799SBarry Smith   PetscReal      x = ptime,y;
4202201da799SBarry Smith   PetscErrorCode ierr;
4203201da799SBarry Smith   PetscInt       its;
4204201da799SBarry Smith 
4205201da799SBarry Smith   PetscFunctionBegin;
4206201da799SBarry Smith   if (!n) {
4207201da799SBarry Smith     PetscDrawAxis  axis;
4208201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4209201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4210201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4211201da799SBarry Smith     ctx->ksp_its = 0;
4212201da799SBarry Smith   }
4213201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4214201da799SBarry Smith   y    = its - ctx->ksp_its;
4215201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
421699fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){
4217201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4218201da799SBarry Smith   }
4219201da799SBarry Smith   ctx->ksp_its = its;
4220201da799SBarry Smith   PetscFunctionReturn(0);
4221201da799SBarry Smith }
4222