xref: /petsc/src/ts/interface/ts.c (revision deb2cd25b4974e90da8065ee93cb8b2449f9ae06)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
4d763cef2SBarry Smith 
5d5ba7fb7SMatthew Knepley /* Logging support */
6d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
7166c7f25SBarry Smith PetscLogEvent  TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
8d405a339SMatthew Knepley 
949354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1049354f04SShri Abhyankar 
114a2ae208SSatish Balay #undef __FUNCT__
12bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
13bdad233fSMatthew Knepley /*
14bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
15bdad233fSMatthew Knepley 
16bdad233fSMatthew Knepley   Collective on TS
17bdad233fSMatthew Knepley 
18bdad233fSMatthew Knepley   Input Parameter:
19bdad233fSMatthew Knepley . ts - The ts
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley   Level: intermediate
22bdad233fSMatthew Knepley 
23bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
24bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
25bdad233fSMatthew Knepley */
266849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
27bdad233fSMatthew Knepley {
28ace3abfcSBarry Smith   PetscBool      opt;
292fc52814SBarry Smith   const char     *defaultType;
30bdad233fSMatthew Knepley   char           typeName[256];
31dfbe8321SBarry Smith   PetscErrorCode ierr;
32bdad233fSMatthew Knepley 
33bdad233fSMatthew Knepley   PetscFunctionBegin;
347adad957SLisandro Dalcin   if (((PetscObject)ts)->type_name) {
357adad957SLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
36bdad233fSMatthew Knepley   } else {
379596e0b4SJed Brown     defaultType = TSEULER;
38bdad233fSMatthew Knepley   }
39bdad233fSMatthew Knepley 
40cce0b1b2SLisandro Dalcin   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
41bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
42a7cc72afSBarry Smith   if (opt) {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   } else {
45bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
46bdad233fSMatthew Knepley   }
47bdad233fSMatthew Knepley   PetscFunctionReturn(0);
48bdad233fSMatthew Knepley }
49bdad233fSMatthew Knepley 
50bdad233fSMatthew Knepley #undef __FUNCT__
51bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
52bdad233fSMatthew Knepley /*@
53bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
54bdad233fSMatthew Knepley 
55bdad233fSMatthew Knepley    Collective on TS
56bdad233fSMatthew Knepley 
57bdad233fSMatthew Knepley    Input Parameter:
58bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
59bdad233fSMatthew Knepley 
60bdad233fSMatthew Knepley    Options Database Keys:
614d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
62bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
633bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
64bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
65bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
66de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
67de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
68de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
69de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
70de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
71de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
72de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
73de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
74de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
75de06c3feSJed Brown .  -ts_monitor_draw_solution_binary <filename> - Save each solution to a binary file
76de06c3feSJed Brown -  -ts_monitor_draw_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
77bdad233fSMatthew Knepley 
78bdad233fSMatthew Knepley    Level: beginner
79bdad233fSMatthew Knepley 
80bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
81bdad233fSMatthew Knepley 
82a313700dSBarry Smith .seealso: TSGetType()
83bdad233fSMatthew Knepley @*/
847087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
85bdad233fSMatthew Knepley {
86ace3abfcSBarry Smith   PetscBool              opt,flg;
87dfbe8321SBarry Smith   PetscErrorCode         ierr;
88649052a6SBarry Smith   PetscViewer            monviewer;
89eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
90089b2837SJed Brown   SNES                   snes;
911c3436cfSJed Brown   TSAdapt                adapt;
9231748224SBarry Smith   PetscReal              time_step;
9349354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
94d1212d36SBarry Smith   char                   dir[16];
95bdad233fSMatthew Knepley 
96bdad233fSMatthew Knepley   PetscFunctionBegin;
970700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
983194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
99a43b19c4SJed Brown     /* Handle TS type options */
100a43b19c4SJed Brown     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
101bdad233fSMatthew Knepley 
102bdad233fSMatthew Knepley     /* Handle generic TS options */
103bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
1043bca7d26SBarry Smith     ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
105d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr);
10631748224SBarry Smith     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
10731748224SBarry Smith     if (flg) {
10831748224SBarry Smith       ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
10931748224SBarry Smith     }
11049354f04SShri Abhyankar     ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr);
11149354f04SShri Abhyankar     if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
112cef5090cSJed 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);
113cef5090cSJed 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);
114cef5090cSJed Brown     ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
1151c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr);
1161c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr);
117bdad233fSMatthew Knepley 
118bdad233fSMatthew Knepley     /* Monitor options */
119a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
120eabae89aSBarry Smith     if (flg) {
121649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
122649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
123bdad233fSMatthew Knepley     }
1245180491cSLisandro Dalcin     ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1255180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1265180491cSLisandro Dalcin 
1274f09c107SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
128a7cc72afSBarry Smith     if (opt) {
1290b039ecaSBarry Smith       TSMonitorLGCtx ctx;
1303923b477SBarry Smith       PetscInt       howoften = 1;
131a80ad3e0SBarry Smith 
1324f09c107SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
1331d1e9da1SBarry Smith       ierr = TSMonitorLGCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1344f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
135b3603a34SBarry Smith     }
1364f09c107SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
137b3603a34SBarry Smith     if (opt) {
1380b039ecaSBarry Smith       TSMonitorLGCtx ctx;
1393923b477SBarry Smith       PetscInt       howoften = 1;
140b3603a34SBarry Smith 
1414f09c107SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
14222d28d08SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1434f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
144bdad233fSMatthew Knepley     }
1454f09c107SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
146ef20d060SBarry Smith     if (opt) {
1470b039ecaSBarry Smith       TSMonitorLGCtx ctx;
1483923b477SBarry Smith       PetscInt       howoften = 1;
149ef20d060SBarry Smith 
1504f09c107SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
15122d28d08SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1524f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
153ef20d060SBarry Smith     }
154201da799SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
155201da799SBarry Smith     if (opt) {
156201da799SBarry Smith       TSMonitorLGCtx ctx;
157201da799SBarry Smith       PetscInt       howoften = 1;
158201da799SBarry Smith 
159201da799SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
16022d28d08SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
161201da799SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
162201da799SBarry Smith     }
163201da799SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
164201da799SBarry Smith     if (opt) {
165201da799SBarry Smith       TSMonitorLGCtx ctx;
166201da799SBarry Smith       PetscInt       howoften = 1;
167201da799SBarry Smith 
168201da799SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
16922d28d08SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
170201da799SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
171201da799SBarry Smith     }
1728189c53fSBarry Smith     ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1738189c53fSBarry Smith     if (opt) {
1748189c53fSBarry Smith       TSMonitorSPEigCtx ctx;
1758189c53fSBarry Smith       PetscInt          howoften = 1;
1768189c53fSBarry Smith 
1778189c53fSBarry Smith       ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
17822d28d08SBarry Smith       ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1798189c53fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
1808189c53fSBarry Smith     }
181ef20d060SBarry Smith     opt  = PETSC_FALSE;
1820dcf80beSBarry Smith     ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
183a7cc72afSBarry Smith     if (opt) {
18483a4ac43SBarry Smith       TSMonitorDrawCtx ctx;
18583a4ac43SBarry Smith       PetscInt         howoften = 1;
186a80ad3e0SBarry Smith 
18783a4ac43SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
18822d28d08SBarry Smith       ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
18983a4ac43SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
190bdad233fSMatthew Knepley     }
191fb1732b5SBarry Smith     opt  = PETSC_FALSE;
1920dcf80beSBarry Smith     ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
1933a471f94SBarry Smith     if (opt) {
19483a4ac43SBarry Smith       TSMonitorDrawCtx ctx;
19583a4ac43SBarry Smith       PetscInt         howoften = 1;
1963a471f94SBarry Smith 
19783a4ac43SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
19822d28d08SBarry Smith       ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
19983a4ac43SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2003a471f94SBarry Smith     }
2013a471f94SBarry Smith     opt  = PETSC_FALSE;
202de06c3feSJed Brown     ierr = PetscOptionsString("-ts_monitor_draw_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
203fb1732b5SBarry Smith     if (flg) {
204fb1732b5SBarry Smith       PetscViewer ctx;
205fb1732b5SBarry Smith       if (monfilename[0]) {
206fb1732b5SBarry Smith         ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
207fb1732b5SBarry Smith       } else {
208fb1732b5SBarry Smith         ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
209fb1732b5SBarry Smith       }
210fb1732b5SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
211fb1732b5SBarry Smith     }
212ed81e22dSJed Brown     opt  = PETSC_FALSE;
213de06c3feSJed Brown     ierr = PetscOptionsString("-ts_monitor_draw_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
214ed81e22dSJed Brown     if (flg) {
215ed81e22dSJed Brown       const char *ptr,*ptr2;
216ed81e22dSJed Brown       char *filetemplate;
217de06c3feSJed Brown       if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts");
218ed81e22dSJed Brown       /* Do some cursory validation of the input. */
219ed81e22dSJed Brown       ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
220de06c3feSJed Brown       if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts");
221ed81e22dSJed Brown       for (ptr++ ; ptr && *ptr; ptr++) {
222ed81e22dSJed Brown         ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
223de06c3feSJed Brown         if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_draw_solution_vtk, should look like filename-%%03D.vts");
224ed81e22dSJed Brown         if (ptr2) break;
225ed81e22dSJed Brown       }
226ed81e22dSJed Brown       ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
227ed81e22dSJed Brown       ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
228ed81e22dSJed Brown     }
229bdad233fSMatthew Knepley 
230d1212d36SBarry Smith     ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
231d1212d36SBarry Smith     if (flg) {
232d1212d36SBarry Smith       TSMonitorDMDARayCtx *rayctx;
233d1212d36SBarry Smith       int                 ray = 0;
234d1212d36SBarry Smith       DMDADirection       ddir;
235d1212d36SBarry Smith       DM                  da;
236d1212d36SBarry Smith       PetscMPIInt         rank;
237d1212d36SBarry Smith 
238d1212d36SBarry Smith       if (dir[1] != '=') SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
239d1212d36SBarry Smith       if (dir[0] == 'x') ddir = DMDA_X;
240d1212d36SBarry Smith       else if (dir[0] == 'y') ddir = DMDA_Y;
241d1212d36SBarry Smith       else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
242d1212d36SBarry Smith       sscanf(dir+2,"%d",&ray);
243d1212d36SBarry Smith 
244d1212d36SBarry Smith       ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
245d1212d36SBarry Smith       ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr);
246d1212d36SBarry Smith       ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
247d1212d36SBarry Smith       ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
248d1212d36SBarry Smith       ierr = MPI_Comm_rank(((PetscObject)ts)->comm,&rank);CHKERRQ(ierr);
249d1212d36SBarry Smith       if (!rank) {
250d1212d36SBarry Smith         ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
251d1212d36SBarry Smith       }
252d1212d36SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
253d1212d36SBarry Smith     }
254d1212d36SBarry Smith 
255ad6bc421SBarry Smith     ierr = TSGetTSAdapt(ts,&adapt);CHKERRQ(ierr);
2561c3436cfSJed Brown     ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2571c3436cfSJed Brown 
258d52bd9f3SBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
259d52bd9f3SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
260d52bd9f3SBarry Smith 
261bdad233fSMatthew Knepley     /* Handle specific TS options */
262abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
263bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
264bdad233fSMatthew Knepley     }
2655d973c19SBarry Smith 
2665d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
2675d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
268bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
269bdad233fSMatthew Knepley   PetscFunctionReturn(0);
270bdad233fSMatthew Knepley }
271bdad233fSMatthew Knepley 
272bdad233fSMatthew Knepley #undef __FUNCT__
273cdcf91faSSean Farley #undef __FUNCT__
2744a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
275a7a1495cSBarry Smith /*@
2768c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
277a7a1495cSBarry Smith       set with TSSetRHSJacobian().
278a7a1495cSBarry Smith 
279a7a1495cSBarry Smith    Collective on TS and Vec
280a7a1495cSBarry Smith 
281a7a1495cSBarry Smith    Input Parameters:
282316643e7SJed Brown +  ts - the TS context
283a7a1495cSBarry Smith .  t - current timestep
2840910c330SBarry Smith -  U - input vector
285a7a1495cSBarry Smith 
286a7a1495cSBarry Smith    Output Parameters:
287a7a1495cSBarry Smith +  A - Jacobian matrix
288a7a1495cSBarry Smith .  B - optional preconditioning matrix
289a7a1495cSBarry Smith -  flag - flag indicating matrix structure
290a7a1495cSBarry Smith 
291a7a1495cSBarry Smith    Notes:
292a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
293a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
294a7a1495cSBarry Smith 
29594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
296a7a1495cSBarry Smith    flag parameter.
297a7a1495cSBarry Smith 
298a7a1495cSBarry Smith    Level: developer
299a7a1495cSBarry Smith 
300a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
301a7a1495cSBarry Smith 
30294b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
303a7a1495cSBarry Smith @*/
3040910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
305a7a1495cSBarry Smith {
306dfbe8321SBarry Smith   PetscErrorCode ierr;
3070910c330SBarry Smith   PetscInt       Ustate;
30824989b8cSPeter Brune   DM             dm;
309942e3340SBarry Smith   DMTS           tsdm;
31024989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
31124989b8cSPeter Brune   void           *ctx;
31224989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
313a7a1495cSBarry Smith 
314a7a1495cSBarry Smith   PetscFunctionBegin;
3150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3160910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3170910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
31824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
319942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
32024989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
32124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
3220910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
3230910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
3240e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
3250e4ef248SJed Brown     PetscFunctionReturn(0);
326f8ede8e7SJed Brown   }
327d90be118SSean Farley 
32824989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
329d90be118SSean Farley 
33024989b8cSPeter Brune   if (rhsjacobianfunc) {
3310910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
332a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
333a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3340910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
335a7a1495cSBarry Smith     PetscStackPop;
3360910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
337a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3380700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3390700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
340ef66eb69SBarry Smith   } else {
341214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
342214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
343214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
344ef66eb69SBarry Smith   }
3450e4ef248SJed Brown   ts->rhsjacobian.time = t;
3460910c330SBarry Smith   ts->rhsjacobian.X = U;
3470910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3480e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
349a7a1495cSBarry Smith   PetscFunctionReturn(0);
350a7a1495cSBarry Smith }
351a7a1495cSBarry Smith 
3524a2ae208SSatish Balay #undef __FUNCT__
3534a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
354316643e7SJed Brown /*@
355d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
356d763cef2SBarry Smith 
357316643e7SJed Brown    Collective on TS and Vec
358316643e7SJed Brown 
359316643e7SJed Brown    Input Parameters:
360316643e7SJed Brown +  ts - the TS context
361316643e7SJed Brown .  t - current time
3620910c330SBarry Smith -  U - state vector
363316643e7SJed Brown 
364316643e7SJed Brown    Output Parameter:
365316643e7SJed Brown .  y - right hand side
366316643e7SJed Brown 
367316643e7SJed Brown    Note:
368316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
369316643e7SJed Brown    is used internally within the nonlinear solvers.
370316643e7SJed Brown 
371316643e7SJed Brown    Level: developer
372316643e7SJed Brown 
373316643e7SJed Brown .keywords: TS, compute
374316643e7SJed Brown 
375316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
376316643e7SJed Brown @*/
3770910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
378d763cef2SBarry Smith {
379dfbe8321SBarry Smith   PetscErrorCode ierr;
38024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
38124989b8cSPeter Brune   TSIFunction    ifunction;
38224989b8cSPeter Brune   void           *ctx;
38324989b8cSPeter Brune   DM             dm;
38424989b8cSPeter Brune 
385d763cef2SBarry Smith   PetscFunctionBegin;
3860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3870910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3880700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
38924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
39024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
39124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
392d763cef2SBarry Smith 
39324989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
394d763cef2SBarry Smith 
3950910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
39624989b8cSPeter Brune   if (rhsfunction) {
397d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
3980910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
399d763cef2SBarry Smith     PetscStackPop;
400214bc6a2SJed Brown   } else {
401214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
402b2cd27e8SJed Brown   }
40344a41b28SSean Farley 
4040910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
405d763cef2SBarry Smith   PetscFunctionReturn(0);
406d763cef2SBarry Smith }
407d763cef2SBarry Smith 
4084a2ae208SSatish Balay #undef __FUNCT__
409ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
410ef20d060SBarry Smith /*@
411ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
412ef20d060SBarry Smith 
413ef20d060SBarry Smith    Collective on TS and Vec
414ef20d060SBarry Smith 
415ef20d060SBarry Smith    Input Parameters:
416ef20d060SBarry Smith +  ts - the TS context
417ef20d060SBarry Smith -  t - current time
418ef20d060SBarry Smith 
419ef20d060SBarry Smith    Output Parameter:
4200910c330SBarry Smith .  U - the solution
421ef20d060SBarry Smith 
422ef20d060SBarry Smith    Note:
423ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
424ef20d060SBarry Smith    is used internally within the nonlinear solvers.
425ef20d060SBarry Smith 
426ef20d060SBarry Smith    Level: developer
427ef20d060SBarry Smith 
428ef20d060SBarry Smith .keywords: TS, compute
429ef20d060SBarry Smith 
430abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
431ef20d060SBarry Smith @*/
4320910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
433ef20d060SBarry Smith {
434ef20d060SBarry Smith   PetscErrorCode     ierr;
435ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
436ef20d060SBarry Smith   void               *ctx;
437ef20d060SBarry Smith   DM                 dm;
438ef20d060SBarry Smith 
439ef20d060SBarry Smith   PetscFunctionBegin;
440ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4410910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
442ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
443ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
444ef20d060SBarry Smith 
445ef20d060SBarry Smith   if (solutionfunction) {
446ef20d060SBarry Smith     PetscStackPush("TS user right-hand-side function");
4470910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
448ef20d060SBarry Smith     PetscStackPop;
449ef20d060SBarry Smith   }
450ef20d060SBarry Smith   PetscFunctionReturn(0);
451ef20d060SBarry Smith }
452ef20d060SBarry Smith 
453ef20d060SBarry Smith #undef __FUNCT__
454214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
455214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
456214bc6a2SJed Brown {
4572dd45cf8SJed Brown   Vec            F;
458214bc6a2SJed Brown   PetscErrorCode ierr;
459214bc6a2SJed Brown 
460214bc6a2SJed Brown   PetscFunctionBegin;
4610da9a4f0SJed Brown   *Frhs = PETSC_NULL;
4622dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
463214bc6a2SJed Brown   if (!ts->Frhs) {
4642dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
465214bc6a2SJed Brown   }
466214bc6a2SJed Brown   *Frhs = ts->Frhs;
467214bc6a2SJed Brown   PetscFunctionReturn(0);
468214bc6a2SJed Brown }
469214bc6a2SJed Brown 
470214bc6a2SJed Brown #undef __FUNCT__
471214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
472214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
473214bc6a2SJed Brown {
474214bc6a2SJed Brown   Mat            A,B;
4752dd45cf8SJed Brown   PetscErrorCode ierr;
476214bc6a2SJed Brown 
477214bc6a2SJed Brown   PetscFunctionBegin;
478214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
479214bc6a2SJed Brown   if (Arhs) {
480214bc6a2SJed Brown     if (!ts->Arhs) {
481214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
482214bc6a2SJed Brown     }
483214bc6a2SJed Brown     *Arhs = ts->Arhs;
484214bc6a2SJed Brown   }
485214bc6a2SJed Brown   if (Brhs) {
486214bc6a2SJed Brown     if (!ts->Brhs) {
487214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
488214bc6a2SJed Brown     }
489214bc6a2SJed Brown     *Brhs = ts->Brhs;
490214bc6a2SJed Brown   }
491214bc6a2SJed Brown   PetscFunctionReturn(0);
492214bc6a2SJed Brown }
493214bc6a2SJed Brown 
494214bc6a2SJed Brown #undef __FUNCT__
495316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
496316643e7SJed Brown /*@
4970910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
498316643e7SJed Brown 
499316643e7SJed Brown    Collective on TS and Vec
500316643e7SJed Brown 
501316643e7SJed Brown    Input Parameters:
502316643e7SJed Brown +  ts - the TS context
503316643e7SJed Brown .  t - current time
5040910c330SBarry Smith .  U - state vector
5050910c330SBarry Smith .  Udot - time derivative of state vector
506214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
507316643e7SJed Brown 
508316643e7SJed Brown    Output Parameter:
509316643e7SJed Brown .  Y - right hand side
510316643e7SJed Brown 
511316643e7SJed Brown    Note:
512316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
513316643e7SJed Brown    is used internally within the nonlinear solvers.
514316643e7SJed Brown 
515316643e7SJed Brown    If the user did did not write their equations in implicit form, this
516316643e7SJed Brown    function recasts them in implicit form.
517316643e7SJed Brown 
518316643e7SJed Brown    Level: developer
519316643e7SJed Brown 
520316643e7SJed Brown .keywords: TS, compute
521316643e7SJed Brown 
522316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
523316643e7SJed Brown @*/
5240910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
525316643e7SJed Brown {
526316643e7SJed Brown   PetscErrorCode ierr;
52724989b8cSPeter Brune   TSIFunction    ifunction;
52824989b8cSPeter Brune   TSRHSFunction  rhsfunction;
52924989b8cSPeter Brune   void           *ctx;
53024989b8cSPeter Brune   DM             dm;
531316643e7SJed Brown 
532316643e7SJed Brown   PetscFunctionBegin;
5330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5340910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5350910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
5360700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
537316643e7SJed Brown 
53824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
53924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
54024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
54124989b8cSPeter Brune 
54224989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
543d90be118SSean Farley 
5440910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
54524989b8cSPeter Brune   if (ifunction) {
546316643e7SJed Brown     PetscStackPush("TS user implicit function");
5470910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
548316643e7SJed Brown     PetscStackPop;
549214bc6a2SJed Brown   }
550214bc6a2SJed Brown   if (imex) {
55124989b8cSPeter Brune     if (!ifunction) {
5520910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
5532dd45cf8SJed Brown     }
55424989b8cSPeter Brune   } else if (rhsfunction) {
55524989b8cSPeter Brune     if (ifunction) {
556214bc6a2SJed Brown       Vec Frhs;
557214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
5580910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
559214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
5602dd45cf8SJed Brown     } else {
5610910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
5620910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
563316643e7SJed Brown     }
5644a6899ffSJed Brown   }
5650910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
566316643e7SJed Brown   PetscFunctionReturn(0);
567316643e7SJed Brown }
568316643e7SJed Brown 
569316643e7SJed Brown #undef __FUNCT__
570316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
571316643e7SJed Brown /*@
572316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
573316643e7SJed Brown 
574316643e7SJed Brown    Collective on TS and Vec
575316643e7SJed Brown 
576316643e7SJed Brown    Input
577316643e7SJed Brown       Input Parameters:
578316643e7SJed Brown +  ts - the TS context
579316643e7SJed Brown .  t - current timestep
5800910c330SBarry Smith .  U - state vector
5810910c330SBarry Smith .  Udot - time derivative of state vector
582214bc6a2SJed Brown .  shift - shift to apply, see note below
583214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
584316643e7SJed Brown 
585316643e7SJed Brown    Output Parameters:
586316643e7SJed Brown +  A - Jacobian matrix
587316643e7SJed Brown .  B - optional preconditioning matrix
588316643e7SJed Brown -  flag - flag indicating matrix structure
589316643e7SJed Brown 
590316643e7SJed Brown    Notes:
5910910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
592316643e7SJed Brown 
5930910c330SBarry Smith    dF/dU + shift*dF/dUdot
594316643e7SJed Brown 
595316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
596316643e7SJed Brown    is used internally within the nonlinear solvers.
597316643e7SJed Brown 
598316643e7SJed Brown    Level: developer
599316643e7SJed Brown 
600316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
601316643e7SJed Brown 
602316643e7SJed Brown .seealso:  TSSetIJacobian()
60363495f91SJed Brown @*/
6040910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
605316643e7SJed Brown {
6060910c330SBarry Smith   PetscInt       Ustate, Udotstate;
607316643e7SJed Brown   PetscErrorCode ierr;
60824989b8cSPeter Brune   TSIJacobian    ijacobian;
60924989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
61024989b8cSPeter Brune   DM             dm;
61124989b8cSPeter Brune   void           *ctx;
612316643e7SJed Brown 
613316643e7SJed Brown   PetscFunctionBegin;
61424989b8cSPeter Brune 
6150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6160910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6170910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
618316643e7SJed Brown   PetscValidPointer(A,6);
6190700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
620316643e7SJed Brown   PetscValidPointer(B,7);
6210700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
622316643e7SJed Brown   PetscValidPointer(flg,8);
62324989b8cSPeter Brune 
62424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
62524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
62624989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
62724989b8cSPeter Brune 
6280910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
6290910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
6300910c330SBarry 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))) {
6310026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
6320026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
6330026cea9SSean Farley     PetscFunctionReturn(0);
6340026cea9SSean Farley   }
635316643e7SJed Brown 
63624989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
637316643e7SJed Brown 
6384e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
6390910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
64024989b8cSPeter Brune   if (ijacobian) {
6412dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
642316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
6430910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
644316643e7SJed Brown     PetscStackPop;
645214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
646214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
647214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
6484a6899ffSJed Brown   }
649214bc6a2SJed Brown   if (imex) {
650b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
651214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
6522dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
653214bc6a2SJed Brown       if (*A != *B) {
654214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
655214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
656214bc6a2SJed Brown       }
657214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
658214bc6a2SJed Brown     }
659214bc6a2SJed Brown   } else {
66024989b8cSPeter Brune     if (!ijacobian) {
6610910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr);
662214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
663214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
664316643e7SJed Brown       if (*A != *B) {
665316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
666316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
667316643e7SJed Brown       }
66824989b8cSPeter Brune     } else if (rhsjacobian) {
669214bc6a2SJed Brown       Mat Arhs,Brhs;
670214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
671214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
6720910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
673214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
674214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
675214bc6a2SJed Brown       if (*A != *B) {
676214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
677214bc6a2SJed Brown       }
678214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
679214bc6a2SJed Brown     }
680316643e7SJed Brown   }
6810026cea9SSean Farley 
6820026cea9SSean Farley   ts->ijacobian.time = t;
6830910c330SBarry Smith   ts->ijacobian.X = U;
6840910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
6850910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
6860910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
6870026cea9SSean Farley   ts->ijacobian.shift = shift;
6880026cea9SSean Farley   ts->ijacobian.imex = imex;
6890026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
6900910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
691316643e7SJed Brown   PetscFunctionReturn(0);
692316643e7SJed Brown }
693316643e7SJed Brown 
694316643e7SJed Brown #undef __FUNCT__
6954a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
696d763cef2SBarry Smith /*@C
697d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
698b5abc632SBarry Smith     where U_t = G(t,u).
699d763cef2SBarry Smith 
7003f9fe445SBarry Smith     Logically Collective on TS
701d763cef2SBarry Smith 
702d763cef2SBarry Smith     Input Parameters:
703d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
704ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
705d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
706d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
707d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
708d763cef2SBarry Smith 
709d763cef2SBarry Smith     Calling sequence of func:
71087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
711d763cef2SBarry Smith 
712d763cef2SBarry Smith +   t - current timestep
713d763cef2SBarry Smith .   u - input vector
714d763cef2SBarry Smith .   F - function vector
715d763cef2SBarry Smith -   ctx - [optional] user-defined function context
716d763cef2SBarry Smith 
717d763cef2SBarry Smith     Level: beginner
718d763cef2SBarry Smith 
719d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
720d763cef2SBarry Smith 
721d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
722d763cef2SBarry Smith @*/
723089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
724d763cef2SBarry Smith {
725089b2837SJed Brown   PetscErrorCode ierr;
726089b2837SJed Brown   SNES           snes;
727e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
72824989b8cSPeter Brune   DM             dm;
729d763cef2SBarry Smith 
730089b2837SJed Brown   PetscFunctionBegin;
7310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
732ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
73324989b8cSPeter Brune 
73424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
73524989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
736089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
737e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
738e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
739e856ceecSJed Brown     r = ralloc;
740e856ceecSJed Brown   }
741089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
742e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
743d763cef2SBarry Smith   PetscFunctionReturn(0);
744d763cef2SBarry Smith }
745d763cef2SBarry Smith 
7464a2ae208SSatish Balay #undef __FUNCT__
747ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
748ef20d060SBarry Smith /*@C
749abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
750ef20d060SBarry Smith 
751ef20d060SBarry Smith     Logically Collective on TS
752ef20d060SBarry Smith 
753ef20d060SBarry Smith     Input Parameters:
754ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
755ef20d060SBarry Smith .   f - routine for evaluating the solution
756ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
757ef20d060SBarry Smith           function evaluation routine (may be PETSC_NULL)
758ef20d060SBarry Smith 
759ef20d060SBarry Smith     Calling sequence of func:
760ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
761ef20d060SBarry Smith 
762ef20d060SBarry Smith +   t - current timestep
763ef20d060SBarry Smith .   u - output vector
764ef20d060SBarry Smith -   ctx - [optional] user-defined function context
765ef20d060SBarry Smith 
766abd5a294SJed Brown     Notes:
767abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
768abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
769abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
770abd5a294SJed Brown 
7714f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
772abd5a294SJed Brown 
773ef20d060SBarry Smith     Level: beginner
774ef20d060SBarry Smith 
775ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
776ef20d060SBarry Smith 
777abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction()
778ef20d060SBarry Smith @*/
779ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
780ef20d060SBarry Smith {
781ef20d060SBarry Smith   PetscErrorCode ierr;
782ef20d060SBarry Smith   DM             dm;
783ef20d060SBarry Smith 
784ef20d060SBarry Smith   PetscFunctionBegin;
785ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
786ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
787ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
788ef20d060SBarry Smith   PetscFunctionReturn(0);
789ef20d060SBarry Smith }
790ef20d060SBarry Smith 
791ef20d060SBarry Smith #undef __FUNCT__
7924a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
793d763cef2SBarry Smith /*@C
794d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
795b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
796d763cef2SBarry Smith 
7973f9fe445SBarry Smith    Logically Collective on TS
798d763cef2SBarry Smith 
799d763cef2SBarry Smith    Input Parameters:
800d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
801d763cef2SBarry Smith .  A   - Jacobian matrix
802d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
803d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
804d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
805d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
806d763cef2SBarry Smith 
807d763cef2SBarry Smith    Calling sequence of func:
80887828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
809d763cef2SBarry Smith 
810d763cef2SBarry Smith +  t - current timestep
811d763cef2SBarry Smith .  u - input vector
812d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
813d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
814d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
81594b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
816d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
817d763cef2SBarry Smith 
818d763cef2SBarry Smith    Notes:
81994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
820d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
821d763cef2SBarry Smith 
822d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
823d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
82456335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
825d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
826d763cef2SBarry Smith    throughout the global iterations.
827d763cef2SBarry Smith 
828d763cef2SBarry Smith    Level: beginner
829d763cef2SBarry Smith 
830d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
831d763cef2SBarry Smith 
832ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
833d763cef2SBarry Smith 
834d763cef2SBarry Smith @*/
835089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
836d763cef2SBarry Smith {
837277b19d0SLisandro Dalcin   PetscErrorCode ierr;
838089b2837SJed Brown   SNES           snes;
83924989b8cSPeter Brune   DM             dm;
84024989b8cSPeter Brune   TSIJacobian    ijacobian;
841277b19d0SLisandro Dalcin 
842d763cef2SBarry Smith   PetscFunctionBegin;
8430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
844277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
845277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
846277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
847277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
848d763cef2SBarry Smith 
84924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
85024989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
85124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
85224989b8cSPeter Brune 
853089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
8545f659677SPeter Brune   if (!ijacobian) {
855089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
8560e4ef248SJed Brown   }
8570e4ef248SJed Brown   if (A) {
8580e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8590e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
8600e4ef248SJed Brown     ts->Arhs = A;
8610e4ef248SJed Brown   }
8620e4ef248SJed Brown   if (B) {
8630e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8640e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
8650e4ef248SJed Brown     ts->Brhs = B;
8660e4ef248SJed Brown   }
867d763cef2SBarry Smith   PetscFunctionReturn(0);
868d763cef2SBarry Smith }
869d763cef2SBarry Smith 
870316643e7SJed Brown 
871316643e7SJed Brown #undef __FUNCT__
872316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
873316643e7SJed Brown /*@C
874b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
875316643e7SJed Brown 
8763f9fe445SBarry Smith    Logically Collective on TS
877316643e7SJed Brown 
878316643e7SJed Brown    Input Parameters:
879316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
880ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
881316643e7SJed Brown .  f   - the function evaluation routine
882316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
883316643e7SJed Brown 
884316643e7SJed Brown    Calling sequence of f:
885316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
886316643e7SJed Brown 
887316643e7SJed Brown +  t   - time at step/stage being solved
888316643e7SJed Brown .  u   - state vector
889316643e7SJed Brown .  u_t - time derivative of state vector
890316643e7SJed Brown .  F   - function vector
891316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
892316643e7SJed Brown 
893316643e7SJed Brown    Important:
894d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
895316643e7SJed Brown 
896316643e7SJed Brown    Level: beginner
897316643e7SJed Brown 
898316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
899316643e7SJed Brown 
900d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
901316643e7SJed Brown @*/
902089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
903316643e7SJed Brown {
904089b2837SJed Brown   PetscErrorCode ierr;
905089b2837SJed Brown   SNES           snes;
906e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
90724989b8cSPeter Brune   DM             dm;
908316643e7SJed Brown 
909316643e7SJed Brown   PetscFunctionBegin;
9100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
911ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
91224989b8cSPeter Brune 
91324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
91424989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
91524989b8cSPeter Brune 
916089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
917e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
918e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
919e856ceecSJed Brown     res = resalloc;
920e856ceecSJed Brown   }
921089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
922e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
92324989b8cSPeter Brune 
924089b2837SJed Brown   PetscFunctionReturn(0);
925089b2837SJed Brown }
926089b2837SJed Brown 
927089b2837SJed Brown #undef __FUNCT__
928089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
929089b2837SJed Brown /*@C
930089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex 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 residual (or PETSC_NULL)
939089b2837SJed Brown .  func - the function to compute residual (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: TSSetIFunction(), SNESGetFunction()
947089b2837SJed Brown @*/
948089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *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 = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
960089b2837SJed Brown   PetscFunctionReturn(0);
961089b2837SJed Brown }
962089b2837SJed Brown 
963089b2837SJed Brown #undef __FUNCT__
964089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
965089b2837SJed Brown /*@C
966089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
967089b2837SJed Brown 
968089b2837SJed Brown    Not Collective
969089b2837SJed Brown 
970089b2837SJed Brown    Input Parameter:
971089b2837SJed Brown .  ts - the TS context
972089b2837SJed Brown 
973089b2837SJed Brown    Output Parameter:
974089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
975089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
976089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
977089b2837SJed Brown 
978089b2837SJed Brown    Level: advanced
979089b2837SJed Brown 
980089b2837SJed Brown .keywords: TS, nonlinear, get, function
981089b2837SJed Brown 
982089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
983089b2837SJed Brown @*/
984089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
985089b2837SJed Brown {
986089b2837SJed Brown   PetscErrorCode ierr;
987089b2837SJed Brown   SNES           snes;
98824989b8cSPeter Brune   DM             dm;
989089b2837SJed Brown 
990089b2837SJed Brown   PetscFunctionBegin;
991089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
992089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
993089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
99424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
99524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
996316643e7SJed Brown   PetscFunctionReturn(0);
997316643e7SJed Brown }
998316643e7SJed Brown 
999316643e7SJed Brown #undef __FUNCT__
1000316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1001316643e7SJed Brown /*@C
1002a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1003a4f0a591SBarry Smith         you provided with TSSetIFunction().
1004316643e7SJed Brown 
10053f9fe445SBarry Smith    Logically Collective on TS
1006316643e7SJed Brown 
1007316643e7SJed Brown    Input Parameters:
1008316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1009316643e7SJed Brown .  A   - Jacobian matrix
1010316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
1011316643e7SJed Brown .  f   - the Jacobian evaluation routine
1012316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
1013316643e7SJed Brown 
1014316643e7SJed Brown    Calling sequence of f:
10151b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
1016316643e7SJed Brown 
1017316643e7SJed Brown +  t    - time at step/stage being solved
10181b4a444bSJed Brown .  U    - state vector
10191b4a444bSJed Brown .  U_t  - time derivative of state vector
1020316643e7SJed Brown .  a    - shift
1021b5abc632SBarry Smith .  A    - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1022316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
1023316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1024316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1025316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1026316643e7SJed Brown 
1027316643e7SJed Brown    Notes:
1028316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
1029316643e7SJed Brown 
1030a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1031b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1032a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1033a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1034a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1035a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1036a4f0a591SBarry Smith 
1037316643e7SJed Brown    Level: beginner
1038316643e7SJed Brown 
1039316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1040316643e7SJed Brown 
1041ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
1042316643e7SJed Brown 
1043316643e7SJed Brown @*/
10447087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1045316643e7SJed Brown {
1046316643e7SJed Brown   PetscErrorCode ierr;
1047089b2837SJed Brown   SNES           snes;
104824989b8cSPeter Brune   DM             dm;
1049316643e7SJed Brown 
1050316643e7SJed Brown   PetscFunctionBegin;
10510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10520700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
10530700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1054316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1055316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
105624989b8cSPeter Brune 
105724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
105824989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
105924989b8cSPeter Brune 
1060089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1061089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1062316643e7SJed Brown   PetscFunctionReturn(0);
1063316643e7SJed Brown }
1064316643e7SJed Brown 
10654a2ae208SSatish Balay #undef __FUNCT__
106655849f57SBarry Smith #define __FUNCT__ "TSLoad"
106755849f57SBarry Smith /*@C
106855849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
106955849f57SBarry Smith 
107055849f57SBarry Smith   Collective on PetscViewer
107155849f57SBarry Smith 
107255849f57SBarry Smith   Input Parameters:
107355849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
107455849f57SBarry Smith            some related function before a call to TSLoad().
107555849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
107655849f57SBarry Smith 
107755849f57SBarry Smith    Level: intermediate
107855849f57SBarry Smith 
107955849f57SBarry Smith   Notes:
108055849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
108155849f57SBarry Smith 
108255849f57SBarry Smith   Notes for advanced users:
108355849f57SBarry Smith   Most users should not need to know the details of the binary storage
108455849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
108555849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
108655849f57SBarry Smith   format is
108755849f57SBarry Smith .vb
108855849f57SBarry Smith      has not yet been determined
108955849f57SBarry Smith .ve
109055849f57SBarry Smith 
109155849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
109255849f57SBarry Smith @*/
1093f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
109455849f57SBarry Smith {
109555849f57SBarry Smith   PetscErrorCode ierr;
109655849f57SBarry Smith   PetscBool      isbinary;
109755849f57SBarry Smith   PetscInt       classid;
109855849f57SBarry Smith   char           type[256];
10992d53ad75SBarry Smith   DMTS           sdm;
1100ad6bc421SBarry Smith   DM             dm;
110155849f57SBarry Smith 
110255849f57SBarry Smith   PetscFunctionBegin;
1103f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
110455849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
110555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
110655849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
110755849f57SBarry Smith 
110855849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1109f2c2a1b9SBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Not TS next in file");
111055849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1111f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1112f2c2a1b9SBarry Smith   if (ts->ops->load) {
1113f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1114f2c2a1b9SBarry Smith   }
1115ad6bc421SBarry Smith   ierr = DMCreate(((PetscObject)ts)->comm,&dm);CHKERRQ(ierr);
1116ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1117ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1118f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1119f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
11202d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
11212d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
112255849f57SBarry Smith   PetscFunctionReturn(0);
112355849f57SBarry Smith }
112455849f57SBarry Smith 
112555849f57SBarry Smith #undef __FUNCT__
11264a2ae208SSatish Balay #define __FUNCT__ "TSView"
11277e2c5f70SBarry Smith /*@C
1128d763cef2SBarry Smith     TSView - Prints the TS data structure.
1129d763cef2SBarry Smith 
11304c49b128SBarry Smith     Collective on TS
1131d763cef2SBarry Smith 
1132d763cef2SBarry Smith     Input Parameters:
1133d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1134d763cef2SBarry Smith -   viewer - visualization context
1135d763cef2SBarry Smith 
1136d763cef2SBarry Smith     Options Database Key:
1137d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1138d763cef2SBarry Smith 
1139d763cef2SBarry Smith     Notes:
1140d763cef2SBarry Smith     The available visualization contexts include
1141b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1142b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1143d763cef2SBarry Smith          output where only the first processor opens
1144d763cef2SBarry Smith          the file.  All other processors send their
1145d763cef2SBarry Smith          data to the first processor to print.
1146d763cef2SBarry Smith 
1147d763cef2SBarry Smith     The user can open an alternative visualization context with
1148b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1149d763cef2SBarry Smith 
1150d763cef2SBarry Smith     Level: beginner
1151d763cef2SBarry Smith 
1152d763cef2SBarry Smith .keywords: TS, timestep, view
1153d763cef2SBarry Smith 
1154b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1155d763cef2SBarry Smith @*/
11567087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1157d763cef2SBarry Smith {
1158dfbe8321SBarry Smith   PetscErrorCode ierr;
115919fd82e9SBarry Smith   TSType         type;
11602b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
11612d53ad75SBarry Smith   DMTS           sdm;
1162d763cef2SBarry Smith 
1163d763cef2SBarry Smith   PetscFunctionBegin;
11640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11653050cee2SBarry Smith   if (!viewer) {
11667adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
11673050cee2SBarry Smith   }
11680700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1169c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1170fd16b177SBarry Smith 
1171251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1172251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
117355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
11742b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
117532077d6dSBarry Smith   if (iascii) {
1176317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
117777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1178a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1179d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
11805ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1181c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1182d763cef2SBarry Smith     }
11835ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1184193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
11852d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
11862d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1187d52bd9f3SBarry Smith     if (ts->ops->view) {
1188d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1189d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1190d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1191d52bd9f3SBarry Smith     }
11920f5bd95cSBarry Smith   } else if (isstring) {
1193a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1194b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
119555849f57SBarry Smith   } else if (isbinary) {
119655849f57SBarry Smith     PetscInt         classid = TS_FILE_CLASSID;
119755849f57SBarry Smith     MPI_Comm         comm;
119855849f57SBarry Smith     PetscMPIInt      rank;
119955849f57SBarry Smith     char             type[256];
120055849f57SBarry Smith 
120155849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
120255849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
120355849f57SBarry Smith     if (!rank) {
120455849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
120555849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
120655849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
120755849f57SBarry Smith     }
120855849f57SBarry Smith     if (ts->ops->view) {
120955849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
121055849f57SBarry Smith     }
1211f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1212f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
12132d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12142d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
12152b0a91c0SBarry Smith   } else if (isdraw) {
12162b0a91c0SBarry Smith     PetscDraw draw;
12172b0a91c0SBarry Smith     char      str[36];
121889fd9fafSBarry Smith     PetscReal x,y,bottom,h;
12192b0a91c0SBarry Smith 
12202b0a91c0SBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
12212b0a91c0SBarry Smith     ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
12222b0a91c0SBarry Smith     ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
12232b0a91c0SBarry Smith     ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
122489fd9fafSBarry Smith     ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,PETSC_NULL,&h);CHKERRQ(ierr);
122589fd9fafSBarry Smith     bottom = y - h;
12262b0a91c0SBarry Smith     ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
12272b0a91c0SBarry Smith     if (ts->ops->view) {
12282b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
12292b0a91c0SBarry Smith     }
12302b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1231d763cef2SBarry Smith   }
1232b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1233251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1234b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1235d763cef2SBarry Smith   PetscFunctionReturn(0);
1236d763cef2SBarry Smith }
1237d763cef2SBarry Smith 
1238d763cef2SBarry Smith 
12394a2ae208SSatish Balay #undef __FUNCT__
12404a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1241b07ff414SBarry Smith /*@
1242d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1243d763cef2SBarry Smith    the timesteppers.
1244d763cef2SBarry Smith 
12453f9fe445SBarry Smith    Logically Collective on TS
1246d763cef2SBarry Smith 
1247d763cef2SBarry Smith    Input Parameters:
1248d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1249d763cef2SBarry Smith -  usrP - optional user context
1250d763cef2SBarry Smith 
1251d763cef2SBarry Smith    Level: intermediate
1252d763cef2SBarry Smith 
1253d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1254d763cef2SBarry Smith 
1255d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1256d763cef2SBarry Smith @*/
12577087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1258d763cef2SBarry Smith {
1259d763cef2SBarry Smith   PetscFunctionBegin;
12600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1261d763cef2SBarry Smith   ts->user = usrP;
1262d763cef2SBarry Smith   PetscFunctionReturn(0);
1263d763cef2SBarry Smith }
1264d763cef2SBarry Smith 
12654a2ae208SSatish Balay #undef __FUNCT__
12664a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1267b07ff414SBarry Smith /*@
1268d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1269d763cef2SBarry Smith     timestepper.
1270d763cef2SBarry Smith 
1271d763cef2SBarry Smith     Not Collective
1272d763cef2SBarry Smith 
1273d763cef2SBarry Smith     Input Parameter:
1274d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1275d763cef2SBarry Smith 
1276d763cef2SBarry Smith     Output Parameter:
1277d763cef2SBarry Smith .   usrP - user context
1278d763cef2SBarry Smith 
1279d763cef2SBarry Smith     Level: intermediate
1280d763cef2SBarry Smith 
1281d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1282d763cef2SBarry Smith 
1283d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1284d763cef2SBarry Smith @*/
1285e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1286d763cef2SBarry Smith {
1287d763cef2SBarry Smith   PetscFunctionBegin;
12880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1289e71120c6SJed Brown   *(void**)usrP = ts->user;
1290d763cef2SBarry Smith   PetscFunctionReturn(0);
1291d763cef2SBarry Smith }
1292d763cef2SBarry Smith 
12934a2ae208SSatish Balay #undef __FUNCT__
12944a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1295d763cef2SBarry Smith /*@
1296b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1297d763cef2SBarry Smith 
1298d763cef2SBarry Smith    Not Collective
1299d763cef2SBarry Smith 
1300d763cef2SBarry Smith    Input Parameter:
1301d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1302d763cef2SBarry Smith 
1303d763cef2SBarry Smith    Output Parameter:
1304b8123daeSJed Brown .  iter - number of steps completed so far
1305d763cef2SBarry Smith 
1306d763cef2SBarry Smith    Level: intermediate
1307d763cef2SBarry Smith 
1308d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1309b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1310d763cef2SBarry Smith @*/
13117087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
1312d763cef2SBarry Smith {
1313d763cef2SBarry Smith   PetscFunctionBegin;
13140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13154482741eSBarry Smith   PetscValidIntPointer(iter,2);
1316d763cef2SBarry Smith   *iter = ts->steps;
1317d763cef2SBarry Smith   PetscFunctionReturn(0);
1318d763cef2SBarry Smith }
1319d763cef2SBarry Smith 
13204a2ae208SSatish Balay #undef __FUNCT__
13214a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1322d763cef2SBarry Smith /*@
1323d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1324d763cef2SBarry Smith    as well as the initial time.
1325d763cef2SBarry Smith 
13263f9fe445SBarry Smith    Logically Collective on TS
1327d763cef2SBarry Smith 
1328d763cef2SBarry Smith    Input Parameters:
1329d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1330d763cef2SBarry Smith .  initial_time - the initial time
1331d763cef2SBarry Smith -  time_step - the size of the timestep
1332d763cef2SBarry Smith 
1333d763cef2SBarry Smith    Level: intermediate
1334d763cef2SBarry Smith 
1335d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1336d763cef2SBarry Smith 
1337d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1338d763cef2SBarry Smith @*/
13397087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1340d763cef2SBarry Smith {
1341e144a568SJed Brown   PetscErrorCode ierr;
1342e144a568SJed Brown 
1343d763cef2SBarry Smith   PetscFunctionBegin;
13440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1345e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1346d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1347d763cef2SBarry Smith   PetscFunctionReturn(0);
1348d763cef2SBarry Smith }
1349d763cef2SBarry Smith 
13504a2ae208SSatish Balay #undef __FUNCT__
13514a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1352d763cef2SBarry Smith /*@
1353d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1354d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1355d763cef2SBarry Smith 
13563f9fe445SBarry Smith    Logically Collective on TS
1357d763cef2SBarry Smith 
1358d763cef2SBarry Smith    Input Parameters:
1359d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1360d763cef2SBarry Smith -  time_step - the size of the timestep
1361d763cef2SBarry Smith 
1362d763cef2SBarry Smith    Level: intermediate
1363d763cef2SBarry Smith 
1364d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1365d763cef2SBarry Smith 
1366d763cef2SBarry Smith .keywords: TS, set, timestep
1367d763cef2SBarry Smith @*/
13687087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1369d763cef2SBarry Smith {
1370d763cef2SBarry Smith   PetscFunctionBegin;
13710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1372c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1373d763cef2SBarry Smith   ts->time_step = time_step;
137431748224SBarry Smith   ts->time_step_orig = time_step;
1375d763cef2SBarry Smith   PetscFunctionReturn(0);
1376d763cef2SBarry Smith }
1377d763cef2SBarry Smith 
13784a2ae208SSatish Balay #undef __FUNCT__
1379a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1380a43b19c4SJed Brown /*@
138149354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
138249354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
138349354f04SShri Abhyankar      or just return at the final time TS computed.
1384a43b19c4SJed Brown 
1385a43b19c4SJed Brown   Logically Collective on TS
1386a43b19c4SJed Brown 
1387a43b19c4SJed Brown    Input Parameter:
1388a43b19c4SJed Brown +   ts - the time-step context
138949354f04SShri Abhyankar -   eftopt - exact final time option
1390a43b19c4SJed Brown 
1391a43b19c4SJed Brown    Level: beginner
1392a43b19c4SJed Brown 
1393a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1394a43b19c4SJed Brown @*/
139549354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1396a43b19c4SJed Brown {
1397a43b19c4SJed Brown   PetscFunctionBegin;
1398a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
139949354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
140049354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1401a43b19c4SJed Brown   PetscFunctionReturn(0);
1402a43b19c4SJed Brown }
1403a43b19c4SJed Brown 
1404a43b19c4SJed Brown #undef __FUNCT__
14054a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1406d763cef2SBarry Smith /*@
1407d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1408d763cef2SBarry Smith 
1409d763cef2SBarry Smith    Not Collective
1410d763cef2SBarry Smith 
1411d763cef2SBarry Smith    Input Parameter:
1412d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1413d763cef2SBarry Smith 
1414d763cef2SBarry Smith    Output Parameter:
1415d763cef2SBarry Smith .  dt - the current timestep size
1416d763cef2SBarry Smith 
1417d763cef2SBarry Smith    Level: intermediate
1418d763cef2SBarry Smith 
1419d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1420d763cef2SBarry Smith 
1421d763cef2SBarry Smith .keywords: TS, get, timestep
1422d763cef2SBarry Smith @*/
14237087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1424d763cef2SBarry Smith {
1425d763cef2SBarry Smith   PetscFunctionBegin;
14260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1427f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1428d763cef2SBarry Smith   *dt = ts->time_step;
1429d763cef2SBarry Smith   PetscFunctionReturn(0);
1430d763cef2SBarry Smith }
1431d763cef2SBarry Smith 
14324a2ae208SSatish Balay #undef __FUNCT__
14334a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1434d8e5e3e6SSatish Balay /*@
1435d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1436d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1437d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1438d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1439d763cef2SBarry Smith 
1440d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1441d763cef2SBarry Smith 
1442d763cef2SBarry Smith    Input Parameter:
1443d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1444d763cef2SBarry Smith 
1445d763cef2SBarry Smith    Output Parameter:
1446d763cef2SBarry Smith .  v - the vector containing the solution
1447d763cef2SBarry Smith 
1448d763cef2SBarry Smith    Level: intermediate
1449d763cef2SBarry Smith 
1450d763cef2SBarry Smith .seealso: TSGetTimeStep()
1451d763cef2SBarry Smith 
1452d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1453d763cef2SBarry Smith @*/
14547087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1455d763cef2SBarry Smith {
1456d763cef2SBarry Smith   PetscFunctionBegin;
14570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14584482741eSBarry Smith   PetscValidPointer(v,2);
14598737fe31SLisandro Dalcin   *v = ts->vec_sol;
1460d763cef2SBarry Smith   PetscFunctionReturn(0);
1461d763cef2SBarry Smith }
1462d763cef2SBarry Smith 
1463bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
14644a2ae208SSatish Balay #undef __FUNCT__
1465bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1466d8e5e3e6SSatish Balay /*@
1467bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1468d763cef2SBarry Smith 
1469bdad233fSMatthew Knepley   Not collective
1470d763cef2SBarry Smith 
1471bdad233fSMatthew Knepley   Input Parameters:
1472bdad233fSMatthew Knepley + ts   - The TS
1473bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1474d763cef2SBarry Smith .vb
14750910c330SBarry Smith          U_t - A U = 0      (linear)
14760910c330SBarry Smith          U_t - A(t) U = 0   (linear)
14770910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1478d763cef2SBarry Smith .ve
1479d763cef2SBarry Smith 
1480d763cef2SBarry Smith    Level: beginner
1481d763cef2SBarry Smith 
1482bdad233fSMatthew Knepley .keywords: TS, problem type
1483bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1484d763cef2SBarry Smith @*/
14857087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1486a7cc72afSBarry Smith {
14879e2a6581SJed Brown   PetscErrorCode ierr;
14889e2a6581SJed Brown 
1489d763cef2SBarry Smith   PetscFunctionBegin;
14900700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1491bdad233fSMatthew Knepley   ts->problem_type = type;
14929e2a6581SJed Brown   if (type == TS_LINEAR) {
14939e2a6581SJed Brown     SNES snes;
14949e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
14959e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
14969e2a6581SJed Brown   }
1497d763cef2SBarry Smith   PetscFunctionReturn(0);
1498d763cef2SBarry Smith }
1499d763cef2SBarry Smith 
1500bdad233fSMatthew Knepley #undef __FUNCT__
1501bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1502bdad233fSMatthew Knepley /*@C
1503bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1504bdad233fSMatthew Knepley 
1505bdad233fSMatthew Knepley   Not collective
1506bdad233fSMatthew Knepley 
1507bdad233fSMatthew Knepley   Input Parameter:
1508bdad233fSMatthew Knepley . ts   - The TS
1509bdad233fSMatthew Knepley 
1510bdad233fSMatthew Knepley   Output Parameter:
1511bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1512bdad233fSMatthew Knepley .vb
1513089b2837SJed Brown          M U_t = A U
1514089b2837SJed Brown          M(t) U_t = A(t) U
1515b5abc632SBarry Smith          F(t,U,U_t)
1516bdad233fSMatthew Knepley .ve
1517bdad233fSMatthew Knepley 
1518bdad233fSMatthew Knepley    Level: beginner
1519bdad233fSMatthew Knepley 
1520bdad233fSMatthew Knepley .keywords: TS, problem type
1521bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1522bdad233fSMatthew Knepley @*/
15237087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1524a7cc72afSBarry Smith {
1525bdad233fSMatthew Knepley   PetscFunctionBegin;
15260700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
15274482741eSBarry Smith   PetscValidIntPointer(type,2);
1528bdad233fSMatthew Knepley   *type = ts->problem_type;
1529bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1530bdad233fSMatthew Knepley }
1531d763cef2SBarry Smith 
15324a2ae208SSatish Balay #undef __FUNCT__
15334a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1534d763cef2SBarry Smith /*@
1535d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1536d763cef2SBarry Smith    of a timestepper.
1537d763cef2SBarry Smith 
1538d763cef2SBarry Smith    Collective on TS
1539d763cef2SBarry Smith 
1540d763cef2SBarry Smith    Input Parameter:
1541d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith    Notes:
1544d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1545d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1546d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1547d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1548d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1549d763cef2SBarry Smith 
1550d763cef2SBarry Smith    Level: advanced
1551d763cef2SBarry Smith 
1552d763cef2SBarry Smith .keywords: TS, timestep, setup
1553d763cef2SBarry Smith 
1554d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1555d763cef2SBarry Smith @*/
15567087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1557d763cef2SBarry Smith {
1558dfbe8321SBarry Smith   PetscErrorCode ierr;
15596c6b9e74SPeter Brune   DM             dm;
15606c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
15616c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
15626c6b9e74SPeter Brune   TSIJacobian    ijac;
15636c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1564d763cef2SBarry Smith 
1565d763cef2SBarry Smith   PetscFunctionBegin;
15660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1567277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1568277b19d0SLisandro Dalcin 
15697adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
15709596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1571d763cef2SBarry Smith   }
1572277b19d0SLisandro Dalcin 
1573277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1574277b19d0SLisandro Dalcin 
1575ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr);
15761c3436cfSJed Brown 
1577277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1578000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1579277b19d0SLisandro Dalcin   }
1580277b19d0SLisandro Dalcin 
15816c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
15826c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
15836c6b9e74SPeter Brune    */
15846c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
15856c6b9e74SPeter Brune   ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr);
15866c6b9e74SPeter Brune   if (!func) {
15876c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
15886c6b9e74SPeter Brune   }
15896c6b9e74SPeter 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.
15906c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
15916c6b9e74SPeter Brune    */
15926c6b9e74SPeter Brune   ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr);
15936c6b9e74SPeter Brune   ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr);
15946c6b9e74SPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr);
15956c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
15966c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
15976c6b9e74SPeter Brune   }
1598277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1599277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1600277b19d0SLisandro Dalcin }
1601277b19d0SLisandro Dalcin 
1602277b19d0SLisandro Dalcin #undef __FUNCT__
1603277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1604277b19d0SLisandro Dalcin /*@
1605277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1606277b19d0SLisandro Dalcin 
1607277b19d0SLisandro Dalcin    Collective on TS
1608277b19d0SLisandro Dalcin 
1609277b19d0SLisandro Dalcin    Input Parameter:
1610277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1611277b19d0SLisandro Dalcin 
1612277b19d0SLisandro Dalcin    Level: beginner
1613277b19d0SLisandro Dalcin 
1614277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1615277b19d0SLisandro Dalcin 
1616277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1617277b19d0SLisandro Dalcin @*/
1618277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1619277b19d0SLisandro Dalcin {
1620277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1621277b19d0SLisandro Dalcin 
1622277b19d0SLisandro Dalcin   PetscFunctionBegin;
1623277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1624277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1625277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1626277b19d0SLisandro Dalcin   }
1627277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
16284e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
16294e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1630214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
16316bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1632e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1633e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
163438637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1635277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1636d763cef2SBarry Smith   PetscFunctionReturn(0);
1637d763cef2SBarry Smith }
1638d763cef2SBarry Smith 
16394a2ae208SSatish Balay #undef __FUNCT__
16404a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1641d8e5e3e6SSatish Balay /*@
1642d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1643d763cef2SBarry Smith    with TSCreate().
1644d763cef2SBarry Smith 
1645d763cef2SBarry Smith    Collective on TS
1646d763cef2SBarry Smith 
1647d763cef2SBarry Smith    Input Parameter:
1648d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1649d763cef2SBarry Smith 
1650d763cef2SBarry Smith    Level: beginner
1651d763cef2SBarry Smith 
1652d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1653d763cef2SBarry Smith 
1654d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1655d763cef2SBarry Smith @*/
16566bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1657d763cef2SBarry Smith {
16586849ba73SBarry Smith   PetscErrorCode ierr;
1659d763cef2SBarry Smith 
1660d763cef2SBarry Smith   PetscFunctionBegin;
16616bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
16626bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
16636bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1664d763cef2SBarry Smith 
16656bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1666277b19d0SLisandro Dalcin 
1667be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
16686bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
16696bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
16706d4c513bSLisandro Dalcin 
167184df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
16726bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
16736bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
16746bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
16756d4c513bSLisandro Dalcin 
1676a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1677d763cef2SBarry Smith   PetscFunctionReturn(0);
1678d763cef2SBarry Smith }
1679d763cef2SBarry Smith 
16804a2ae208SSatish Balay #undef __FUNCT__
16814a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1682d8e5e3e6SSatish Balay /*@
1683d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1684d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1685d763cef2SBarry Smith 
1686d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1687d763cef2SBarry Smith 
1688d763cef2SBarry Smith    Input Parameter:
1689d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1690d763cef2SBarry Smith 
1691d763cef2SBarry Smith    Output Parameter:
1692d763cef2SBarry Smith .  snes - the nonlinear solver context
1693d763cef2SBarry Smith 
1694d763cef2SBarry Smith    Notes:
1695d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1696d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
169794b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1698d763cef2SBarry Smith 
1699d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1700d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1701d763cef2SBarry Smith 
1702d763cef2SBarry Smith    Level: beginner
1703d763cef2SBarry Smith 
1704d763cef2SBarry Smith .keywords: timestep, get, SNES
1705d763cef2SBarry Smith @*/
17067087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1707d763cef2SBarry Smith {
1708d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1709d372ba47SLisandro Dalcin 
1710d763cef2SBarry Smith   PetscFunctionBegin;
17110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17124482741eSBarry Smith   PetscValidPointer(snes,2);
1713d372ba47SLisandro Dalcin   if (!ts->snes) {
1714d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
17156c6b9e74SPeter Brune     ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1716d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1717d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1718496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
17199e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
17209e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
17219e2a6581SJed Brown     }
1722d372ba47SLisandro Dalcin   }
1723d763cef2SBarry Smith   *snes = ts->snes;
1724d763cef2SBarry Smith   PetscFunctionReturn(0);
1725d763cef2SBarry Smith }
1726d763cef2SBarry Smith 
17274a2ae208SSatish Balay #undef __FUNCT__
1728*deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1729*deb2cd25SJed Brown /*@
1730*deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1731*deb2cd25SJed Brown 
1732*deb2cd25SJed Brown    Collective
1733*deb2cd25SJed Brown 
1734*deb2cd25SJed Brown    Input Parameter:
1735*deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1736*deb2cd25SJed Brown -  snes - the nonlinear solver context
1737*deb2cd25SJed Brown 
1738*deb2cd25SJed Brown    Notes:
1739*deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1740*deb2cd25SJed Brown 
1741*deb2cd25SJed Brown    Level: developer
1742*deb2cd25SJed Brown 
1743*deb2cd25SJed Brown .keywords: timestep, set, SNES
1744*deb2cd25SJed Brown @*/
1745*deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1746*deb2cd25SJed Brown {
1747*deb2cd25SJed Brown   PetscErrorCode ierr;
1748*deb2cd25SJed Brown 
1749*deb2cd25SJed Brown   PetscFunctionBegin;
1750*deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1751*deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1752*deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1753*deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1754*deb2cd25SJed Brown   ts->snes = snes;
1755*deb2cd25SJed Brown   PetscFunctionReturn(0);
1756*deb2cd25SJed Brown }
1757*deb2cd25SJed Brown 
1758*deb2cd25SJed Brown #undef __FUNCT__
175994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1760d8e5e3e6SSatish Balay /*@
176194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1762d763cef2SBarry Smith    a TS (timestepper) context.
1763d763cef2SBarry Smith 
176494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1765d763cef2SBarry Smith 
1766d763cef2SBarry Smith    Input Parameter:
1767d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1768d763cef2SBarry Smith 
1769d763cef2SBarry Smith    Output Parameter:
177094b7f48cSBarry Smith .  ksp - the nonlinear solver context
1771d763cef2SBarry Smith 
1772d763cef2SBarry Smith    Notes:
177394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1774d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1775d763cef2SBarry Smith    KSP and PC contexts as well.
1776d763cef2SBarry Smith 
177794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
177894b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1779d763cef2SBarry Smith 
1780d763cef2SBarry Smith    Level: beginner
1781d763cef2SBarry Smith 
178294b7f48cSBarry Smith .keywords: timestep, get, KSP
1783d763cef2SBarry Smith @*/
17847087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1785d763cef2SBarry Smith {
1786d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1787089b2837SJed Brown   SNES           snes;
1788d372ba47SLisandro Dalcin 
1789d763cef2SBarry Smith   PetscFunctionBegin;
17900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17914482741eSBarry Smith   PetscValidPointer(ksp,2);
179217186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1793e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1794089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1795089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1796d763cef2SBarry Smith   PetscFunctionReturn(0);
1797d763cef2SBarry Smith }
1798d763cef2SBarry Smith 
1799d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1800d763cef2SBarry Smith 
18014a2ae208SSatish Balay #undef __FUNCT__
1802adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1803adb62b0dSMatthew Knepley /*@
1804adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1805adb62b0dSMatthew Knepley    maximum time for iteration.
1806adb62b0dSMatthew Knepley 
18073f9fe445SBarry Smith    Not Collective
1808adb62b0dSMatthew Knepley 
1809adb62b0dSMatthew Knepley    Input Parameters:
1810adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1811adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1812adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1813adb62b0dSMatthew Knepley 
1814adb62b0dSMatthew Knepley    Level: intermediate
1815adb62b0dSMatthew Knepley 
1816adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1817adb62b0dSMatthew Knepley @*/
18187087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1819adb62b0dSMatthew Knepley {
1820adb62b0dSMatthew Knepley   PetscFunctionBegin;
18210700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1822abc0a331SBarry Smith   if (maxsteps) {
18234482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1824adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1825adb62b0dSMatthew Knepley   }
1826abc0a331SBarry Smith   if (maxtime) {
18274482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1828adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1829adb62b0dSMatthew Knepley   }
1830adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1831adb62b0dSMatthew Knepley }
1832adb62b0dSMatthew Knepley 
1833adb62b0dSMatthew Knepley #undef __FUNCT__
18344a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1835d763cef2SBarry Smith /*@
1836d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1837d763cef2SBarry Smith    maximum time for iteration.
1838d763cef2SBarry Smith 
18393f9fe445SBarry Smith    Logically Collective on TS
1840d763cef2SBarry Smith 
1841d763cef2SBarry Smith    Input Parameters:
1842d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1843d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1844d763cef2SBarry Smith -  maxtime - final time to iterate to
1845d763cef2SBarry Smith 
1846d763cef2SBarry Smith    Options Database Keys:
1847d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
18483bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1849d763cef2SBarry Smith 
1850d763cef2SBarry Smith    Notes:
1851d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1852d763cef2SBarry Smith 
1853d763cef2SBarry Smith    Level: intermediate
1854d763cef2SBarry Smith 
1855d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1856a43b19c4SJed Brown 
1857a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1858d763cef2SBarry Smith @*/
18597087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1860d763cef2SBarry Smith {
1861d763cef2SBarry Smith   PetscFunctionBegin;
18620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1863c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1864c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
186539b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
186639b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1867d763cef2SBarry Smith   PetscFunctionReturn(0);
1868d763cef2SBarry Smith }
1869d763cef2SBarry Smith 
18704a2ae208SSatish Balay #undef __FUNCT__
18714a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1872d763cef2SBarry Smith /*@
1873d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1874d763cef2SBarry Smith    for use by the TS routines.
1875d763cef2SBarry Smith 
18763f9fe445SBarry Smith    Logically Collective on TS and Vec
1877d763cef2SBarry Smith 
1878d763cef2SBarry Smith    Input Parameters:
1879d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
18800910c330SBarry Smith -  u - the solution vector
1881d763cef2SBarry Smith 
1882d763cef2SBarry Smith    Level: beginner
1883d763cef2SBarry Smith 
1884d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1885d763cef2SBarry Smith @*/
18860910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
1887d763cef2SBarry Smith {
18888737fe31SLisandro Dalcin   PetscErrorCode ierr;
1889496e6a7aSJed Brown   DM             dm;
18908737fe31SLisandro Dalcin 
1891d763cef2SBarry Smith   PetscFunctionBegin;
18920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18930910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
18940910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
18956bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
18960910c330SBarry Smith   ts->vec_sol = u;
1897496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
18980910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
1899d763cef2SBarry Smith   PetscFunctionReturn(0);
1900d763cef2SBarry Smith }
1901d763cef2SBarry Smith 
1902e74ef692SMatthew Knepley #undef __FUNCT__
1903e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1904ac226902SBarry Smith /*@C
1905000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
19063f2090d5SJed Brown   called once at the beginning of each time step.
1907000e7ae3SMatthew Knepley 
19083f9fe445SBarry Smith   Logically Collective on TS
1909000e7ae3SMatthew Knepley 
1910000e7ae3SMatthew Knepley   Input Parameters:
1911000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1912000e7ae3SMatthew Knepley - func - The function
1913000e7ae3SMatthew Knepley 
1914000e7ae3SMatthew Knepley   Calling sequence of func:
1915000e7ae3SMatthew Knepley . func (TS ts);
1916000e7ae3SMatthew Knepley 
1917000e7ae3SMatthew Knepley   Level: intermediate
1918000e7ae3SMatthew Knepley 
1919b8123daeSJed Brown   Note:
1920b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1921b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1922b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1923b8123daeSJed Brown 
1924000e7ae3SMatthew Knepley .keywords: TS, timestep
1925b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1926000e7ae3SMatthew Knepley @*/
19277087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1928000e7ae3SMatthew Knepley {
1929000e7ae3SMatthew Knepley   PetscFunctionBegin;
19300700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1931000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1932000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1933000e7ae3SMatthew Knepley }
1934000e7ae3SMatthew Knepley 
1935e74ef692SMatthew Knepley #undef __FUNCT__
19363f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
193709ee8438SJed Brown /*@
19383f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
19393f2090d5SJed Brown 
19403f2090d5SJed Brown   Collective on TS
19413f2090d5SJed Brown 
19423f2090d5SJed Brown   Input Parameters:
19433f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
19443f2090d5SJed Brown 
19453f2090d5SJed Brown   Notes:
19463f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
19473f2090d5SJed Brown   so most users would not generally call this routine themselves.
19483f2090d5SJed Brown 
19493f2090d5SJed Brown   Level: developer
19503f2090d5SJed Brown 
19513f2090d5SJed Brown .keywords: TS, timestep
1952b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
19533f2090d5SJed Brown @*/
19547087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
19553f2090d5SJed Brown {
19563f2090d5SJed Brown   PetscErrorCode ierr;
19573f2090d5SJed Brown 
19583f2090d5SJed Brown   PetscFunctionBegin;
19590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
196072ac3e02SJed Brown   if (ts->ops->prestep) {
19613f2090d5SJed Brown     PetscStackPush("TS PreStep function");
19623f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
19633f2090d5SJed Brown     PetscStackPop;
1964312ce896SJed Brown   }
19653f2090d5SJed Brown   PetscFunctionReturn(0);
19663f2090d5SJed Brown }
19673f2090d5SJed Brown 
19683f2090d5SJed Brown #undef __FUNCT__
1969b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1970b8123daeSJed Brown /*@C
1971b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1972b8123daeSJed Brown   called once at the beginning of each stage.
1973b8123daeSJed Brown 
1974b8123daeSJed Brown   Logically Collective on TS
1975b8123daeSJed Brown 
1976b8123daeSJed Brown   Input Parameters:
1977b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1978b8123daeSJed Brown - func - The function
1979b8123daeSJed Brown 
1980b8123daeSJed Brown   Calling sequence of func:
1981b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1982b8123daeSJed Brown 
1983b8123daeSJed Brown   Level: intermediate
1984b8123daeSJed Brown 
1985b8123daeSJed Brown   Note:
1986b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
1987b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
1988b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
1989b8123daeSJed Brown 
1990b8123daeSJed Brown .keywords: TS, timestep
1991b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
1992b8123daeSJed Brown @*/
1993b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
1994b8123daeSJed Brown {
1995b8123daeSJed Brown   PetscFunctionBegin;
1996b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1997b8123daeSJed Brown   ts->ops->prestage = func;
1998b8123daeSJed Brown   PetscFunctionReturn(0);
1999b8123daeSJed Brown }
2000b8123daeSJed Brown 
2001b8123daeSJed Brown #undef __FUNCT__
2002b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2003b8123daeSJed Brown /*@
2004b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2005b8123daeSJed Brown 
2006b8123daeSJed Brown   Collective on TS
2007b8123daeSJed Brown 
2008b8123daeSJed Brown   Input Parameters:
2009b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2010b8123daeSJed Brown 
2011b8123daeSJed Brown   Notes:
2012b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2013b8123daeSJed Brown   most users would not generally call this routine themselves.
2014b8123daeSJed Brown 
2015b8123daeSJed Brown   Level: developer
2016b8123daeSJed Brown 
2017b8123daeSJed Brown .keywords: TS, timestep
2018b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2019b8123daeSJed Brown @*/
2020b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2021b8123daeSJed Brown {
2022b8123daeSJed Brown   PetscErrorCode ierr;
2023b8123daeSJed Brown 
2024b8123daeSJed Brown   PetscFunctionBegin;
2025b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2026b8123daeSJed Brown   if (ts->ops->prestage) {
2027b8123daeSJed Brown     PetscStackPush("TS PreStage function");
2028b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
2029b8123daeSJed Brown     PetscStackPop;
2030b8123daeSJed Brown   }
2031b8123daeSJed Brown   PetscFunctionReturn(0);
2032b8123daeSJed Brown }
2033b8123daeSJed Brown 
2034b8123daeSJed Brown #undef __FUNCT__
2035e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2036ac226902SBarry Smith /*@C
2037000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
20383f2090d5SJed Brown   called once at the end of each time step.
2039000e7ae3SMatthew Knepley 
20403f9fe445SBarry Smith   Logically Collective on TS
2041000e7ae3SMatthew Knepley 
2042000e7ae3SMatthew Knepley   Input Parameters:
2043000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2044000e7ae3SMatthew Knepley - func - The function
2045000e7ae3SMatthew Knepley 
2046000e7ae3SMatthew Knepley   Calling sequence of func:
2047b8123daeSJed Brown $ func (TS ts);
2048000e7ae3SMatthew Knepley 
2049000e7ae3SMatthew Knepley   Level: intermediate
2050000e7ae3SMatthew Knepley 
2051000e7ae3SMatthew Knepley .keywords: TS, timestep
2052b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2053000e7ae3SMatthew Knepley @*/
20547087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2055000e7ae3SMatthew Knepley {
2056000e7ae3SMatthew Knepley   PetscFunctionBegin;
20570700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2058000e7ae3SMatthew Knepley   ts->ops->poststep = func;
2059000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2060000e7ae3SMatthew Knepley }
2061000e7ae3SMatthew Knepley 
2062e74ef692SMatthew Knepley #undef __FUNCT__
20633f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
206409ee8438SJed Brown /*@
20653f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
20663f2090d5SJed Brown 
20673f2090d5SJed Brown   Collective on TS
20683f2090d5SJed Brown 
20693f2090d5SJed Brown   Input Parameters:
20703f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
20713f2090d5SJed Brown 
20723f2090d5SJed Brown   Notes:
20733f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
20743f2090d5SJed Brown   so most users would not generally call this routine themselves.
20753f2090d5SJed Brown 
20763f2090d5SJed Brown   Level: developer
20773f2090d5SJed Brown 
20783f2090d5SJed Brown .keywords: TS, timestep
20793f2090d5SJed Brown @*/
20807087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
20813f2090d5SJed Brown {
20823f2090d5SJed Brown   PetscErrorCode ierr;
20833f2090d5SJed Brown 
20843f2090d5SJed Brown   PetscFunctionBegin;
20850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
208672ac3e02SJed Brown   if (ts->ops->poststep) {
20873f2090d5SJed Brown     PetscStackPush("TS PostStep function");
20883f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
20893f2090d5SJed Brown     PetscStackPop;
209072ac3e02SJed Brown   }
20913f2090d5SJed Brown   PetscFunctionReturn(0);
20923f2090d5SJed Brown }
20933f2090d5SJed Brown 
2094d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2095d763cef2SBarry Smith 
20964a2ae208SSatish Balay #undef __FUNCT__
2097a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2098d763cef2SBarry Smith /*@C
2099a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2100d763cef2SBarry Smith    timestep to display the iteration's  progress.
2101d763cef2SBarry Smith 
21023f9fe445SBarry Smith    Logically Collective on TS
2103d763cef2SBarry Smith 
2104d763cef2SBarry Smith    Input Parameters:
2105d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2106e213d8f1SJed Brown .  monitor - monitoring routine
2107329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
2108b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
2109b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2110b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
2111d763cef2SBarry Smith 
2112e213d8f1SJed Brown    Calling sequence of monitor:
21130910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2114d763cef2SBarry Smith 
2115d763cef2SBarry Smith +    ts - the TS context
211688c05cc5SBarry Smith .    steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have
211788c05cc5SBarry Smith                                been interpolated to)
21181f06c33eSBarry Smith .    time - current time
21190910c330SBarry Smith .    u - current iterate
2120d763cef2SBarry Smith -    mctx - [optional] monitoring context
2121d763cef2SBarry Smith 
2122d763cef2SBarry Smith    Notes:
2123d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2124d763cef2SBarry Smith    already has been loaded.
2125d763cef2SBarry Smith 
2126025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2127025f1a04SBarry Smith 
2128d763cef2SBarry Smith    Level: intermediate
2129d763cef2SBarry Smith 
2130d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2131d763cef2SBarry Smith 
2132a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2133d763cef2SBarry Smith @*/
2134c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2135d763cef2SBarry Smith {
2136d763cef2SBarry Smith   PetscFunctionBegin;
21370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
213817186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2139d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
21408704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]    = mdestroy;
2141d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
2142d763cef2SBarry Smith   PetscFunctionReturn(0);
2143d763cef2SBarry Smith }
2144d763cef2SBarry Smith 
21454a2ae208SSatish Balay #undef __FUNCT__
2146a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2147d763cef2SBarry Smith /*@C
2148a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2149d763cef2SBarry Smith 
21503f9fe445SBarry Smith    Logically Collective on TS
2151d763cef2SBarry Smith 
2152d763cef2SBarry Smith    Input Parameters:
2153d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2154d763cef2SBarry Smith 
2155d763cef2SBarry Smith    Notes:
2156d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2157d763cef2SBarry Smith 
2158d763cef2SBarry Smith    Level: intermediate
2159d763cef2SBarry Smith 
2160d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2161d763cef2SBarry Smith 
2162a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2163d763cef2SBarry Smith @*/
21647087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2165d763cef2SBarry Smith {
2166d952e501SBarry Smith   PetscErrorCode ierr;
2167d952e501SBarry Smith   PetscInt       i;
2168d952e501SBarry Smith 
2169d763cef2SBarry Smith   PetscFunctionBegin;
21700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2171d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
21728704b422SBarry Smith     if (ts->monitordestroy[i]) {
21738704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2174d952e501SBarry Smith     }
2175d952e501SBarry Smith   }
2176d763cef2SBarry Smith   ts->numbermonitors = 0;
2177d763cef2SBarry Smith   PetscFunctionReturn(0);
2178d763cef2SBarry Smith }
2179d763cef2SBarry Smith 
21804a2ae208SSatish Balay #undef __FUNCT__
2181a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2182d8e5e3e6SSatish Balay /*@
2183a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
21845516499fSSatish Balay 
21855516499fSSatish Balay    Level: intermediate
218641251cbbSSatish Balay 
21875516499fSSatish Balay .keywords: TS, set, monitor
21885516499fSSatish Balay 
218941251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
219041251cbbSSatish Balay @*/
2191649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2192d763cef2SBarry Smith {
2193dfbe8321SBarry Smith   PetscErrorCode ierr;
2194649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
2195d132466eSBarry Smith 
2196d763cef2SBarry Smith   PetscFunctionBegin;
2197649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2198971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2199649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2200d763cef2SBarry Smith   PetscFunctionReturn(0);
2201d763cef2SBarry Smith }
2202d763cef2SBarry Smith 
22034a2ae208SSatish Balay #undef __FUNCT__
2204cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2205cd652676SJed Brown /*@
2206cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2207cd652676SJed Brown 
2208cd652676SJed Brown    Logically Collective on TS
2209cd652676SJed Brown 
2210cd652676SJed Brown    Input Argument:
2211cd652676SJed Brown .  ts - time stepping context
2212cd652676SJed Brown 
2213cd652676SJed Brown    Output Argument:
2214cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2215cd652676SJed Brown 
2216cd652676SJed Brown    Level: intermediate
2217cd652676SJed Brown 
2218cd652676SJed Brown .keywords: TS, set
2219cd652676SJed Brown 
2220cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2221cd652676SJed Brown @*/
2222cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2223cd652676SJed Brown {
2224cd652676SJed Brown   PetscFunctionBegin;
2225cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2226cd652676SJed Brown   ts->retain_stages = flg;
2227cd652676SJed Brown   PetscFunctionReturn(0);
2228cd652676SJed Brown }
2229cd652676SJed Brown 
2230cd652676SJed Brown #undef __FUNCT__
2231cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2232cd652676SJed Brown /*@
2233cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2234cd652676SJed Brown 
2235cd652676SJed Brown    Collective on TS
2236cd652676SJed Brown 
2237cd652676SJed Brown    Input Argument:
2238cd652676SJed Brown +  ts - time stepping context
2239cd652676SJed Brown -  t - time to interpolate to
2240cd652676SJed Brown 
2241cd652676SJed Brown    Output Argument:
22420910c330SBarry Smith .  U - state at given time
2243cd652676SJed Brown 
2244cd652676SJed Brown    Notes:
2245cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2246cd652676SJed Brown 
2247cd652676SJed Brown    Level: intermediate
2248cd652676SJed Brown 
2249cd652676SJed Brown    Developer Notes:
2250cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2251cd652676SJed Brown 
2252cd652676SJed Brown .keywords: TS, set
2253cd652676SJed Brown 
2254cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2255cd652676SJed Brown @*/
22560910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2257cd652676SJed Brown {
2258cd652676SJed Brown   PetscErrorCode ierr;
2259cd652676SJed Brown 
2260cd652676SJed Brown   PetscFunctionBegin;
2261cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2262d8cd7023SBarry 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);
2263cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
22640910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2265cd652676SJed Brown   PetscFunctionReturn(0);
2266cd652676SJed Brown }
2267cd652676SJed Brown 
2268cd652676SJed Brown #undef __FUNCT__
22694a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2270d763cef2SBarry Smith /*@
22716d9e5789SSean Farley    TSStep - Steps one time step
2272d763cef2SBarry Smith 
2273d763cef2SBarry Smith    Collective on TS
2274d763cef2SBarry Smith 
2275d763cef2SBarry Smith    Input Parameter:
2276d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2277d763cef2SBarry Smith 
22786d9e5789SSean Farley    Level: intermediate
2279d763cef2SBarry Smith 
2280b8123daeSJed Brown    Notes:
2281b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2282b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2283b8123daeSJed Brown 
228425cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
228525cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
228625cb2221SBarry Smith 
2287d763cef2SBarry Smith .keywords: TS, timestep, solve
2288d763cef2SBarry Smith 
228925cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2290d763cef2SBarry Smith @*/
2291193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2292d763cef2SBarry Smith {
2293362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2294dfbe8321SBarry Smith   PetscErrorCode ierr;
2295d763cef2SBarry Smith 
2296d763cef2SBarry Smith   PetscFunctionBegin;
22970700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2298d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2299d405a339SMatthew Knepley 
2300362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2301362cd11cSLisandro Dalcin 
2302362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2303d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2304193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2305d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2306362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2307362cd11cSLisandro Dalcin 
2308362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2309cef5090cSJed Brown     if (ts->errorifstepfailed) {
2310cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2311cef5090cSJed 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]);
2312cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2313cef5090cSJed Brown     }
2314362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2315362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2316362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2317362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2318362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2319362cd11cSLisandro Dalcin   }
2320362cd11cSLisandro Dalcin 
2321d763cef2SBarry Smith   PetscFunctionReturn(0);
2322d763cef2SBarry Smith }
2323d763cef2SBarry Smith 
23244a2ae208SSatish Balay #undef __FUNCT__
232505175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
232605175c85SJed Brown /*@
232705175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
232805175c85SJed Brown 
23291c3436cfSJed Brown    Collective on TS
233005175c85SJed Brown 
233105175c85SJed Brown    Input Arguments:
23321c3436cfSJed Brown +  ts - time stepping context
23331c3436cfSJed Brown .  order - desired order of accuracy
23341c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
233505175c85SJed Brown 
233605175c85SJed Brown    Output Arguments:
23370910c330SBarry Smith .  U - state at the end of the current step
233805175c85SJed Brown 
233905175c85SJed Brown    Level: advanced
234005175c85SJed Brown 
2341108c343cSJed Brown    Notes:
2342108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2343108c343cSJed 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.
2344108c343cSJed Brown 
23451c3436cfSJed Brown .seealso: TSStep(), TSAdapt
234605175c85SJed Brown @*/
23470910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
234805175c85SJed Brown {
234905175c85SJed Brown   PetscErrorCode ierr;
235005175c85SJed Brown 
235105175c85SJed Brown   PetscFunctionBegin;
235205175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
235305175c85SJed Brown   PetscValidType(ts,1);
23540910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
23551c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
23560910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
235705175c85SJed Brown   PetscFunctionReturn(0);
235805175c85SJed Brown }
235905175c85SJed Brown 
236005175c85SJed Brown #undef __FUNCT__
23616a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
23626a4d4014SLisandro Dalcin /*@
23636a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
23646a4d4014SLisandro Dalcin 
23656a4d4014SLisandro Dalcin    Collective on TS
23666a4d4014SLisandro Dalcin 
23676a4d4014SLisandro Dalcin    Input Parameter:
23686a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2369cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
23705a3a76d0SJed Brown 
23716a4d4014SLisandro Dalcin    Level: beginner
23726a4d4014SLisandro Dalcin 
23735a3a76d0SJed Brown    Notes:
23745a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
23755a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
23765a3a76d0SJed Brown    stepped over the final time.
23775a3a76d0SJed Brown 
23786a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
23796a4d4014SLisandro Dalcin 
23806a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
23816a4d4014SLisandro Dalcin @*/
2382cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
23836a4d4014SLisandro Dalcin {
23844d7d938eSLisandro Dalcin   PetscBool         flg;
23854d7d938eSLisandro Dalcin   PetscViewer       viewer;
23866a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2387cffb1e40SBarry Smith   PetscViewerFormat format;
2388f22f69f0SBarry Smith 
23896a4d4014SLisandro Dalcin   PetscFunctionBegin;
23900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2391f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
239249354f04SShri Abhyankar   if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
23930910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
23945a3a76d0SJed Brown       Vec y;
23950910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
23965a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
23975a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
23985a3a76d0SJed Brown     }
2399f2c2a1b9SBarry Smith     if (u) {
24000910c330SBarry Smith       ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2401f2c2a1b9SBarry Smith     }
24025a3a76d0SJed Brown   } else {
2403f2c2a1b9SBarry Smith     if (u) {
24040910c330SBarry Smith       ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
24055a3a76d0SJed Brown     }
2406f2c2a1b9SBarry Smith   }
2407b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
24086a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2409193ac0bcSJed Brown   ts->steps = 0;
24105ef26d82SJed Brown   ts->ksp_its = 0;
24115ef26d82SJed Brown   ts->snes_its = 0;
2412c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2413c610991cSLisandro Dalcin   ts->reject = 0;
2414193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
2415193ac0bcSJed Brown 
2416193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2417193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
24180910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2419cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2420193ac0bcSJed Brown   } else {
24216a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2422362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2423362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2424362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2425362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2426362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2427e1a7a14fSJed Brown     while (!ts->reason) {
2428193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2429193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2430193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2431193ac0bcSJed Brown     }
243249354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
24330910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2434cc708dedSBarry Smith       ts->solvetime = ts->max_time;
24350574a7fbSJed Brown     } else {
2436ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2437cc708dedSBarry Smith       ts->solvetime = ts->ptime;
24380574a7fbSJed Brown     }
2439193ac0bcSJed Brown   }
24403923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2441cffb1e40SBarry Smith   ierr = PetscOptionsGetViewer(((PetscObject)ts)->comm,((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
24424d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2443cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
24444d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2445cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2446cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
24472b0a91c0SBarry Smith   }
24486a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
24496a4d4014SLisandro Dalcin }
24506a4d4014SLisandro Dalcin 
24516a4d4014SLisandro Dalcin #undef __FUNCT__
24524a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2453228d79bcSJed Brown /*@
2454228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2455228d79bcSJed Brown 
2456228d79bcSJed Brown    Collective on TS
2457228d79bcSJed Brown 
2458228d79bcSJed Brown    Input Parameters:
2459228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2460228d79bcSJed Brown .  step - step number that has just completed
2461228d79bcSJed Brown .  ptime - model time of the state
24620910c330SBarry Smith -  u - state at the current model time
2463228d79bcSJed Brown 
2464228d79bcSJed Brown    Notes:
2465228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2466228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2467228d79bcSJed Brown 
2468228d79bcSJed Brown    Level: advanced
2469228d79bcSJed Brown 
2470228d79bcSJed Brown .keywords: TS, timestep
2471228d79bcSJed Brown @*/
24720910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2473d763cef2SBarry Smith {
24746849ba73SBarry Smith   PetscErrorCode ierr;
2475a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2476d763cef2SBarry Smith 
2477d763cef2SBarry Smith   PetscFunctionBegin;
2478d763cef2SBarry Smith   for (i=0; i<n; i++) {
24790910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2480d763cef2SBarry Smith   }
2481d763cef2SBarry Smith   PetscFunctionReturn(0);
2482d763cef2SBarry Smith }
2483d763cef2SBarry Smith 
2484d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
24850b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
24860b039ecaSBarry Smith   PetscDrawLG lg;
24870b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2488201da799SBarry Smith   PetscInt    ksp_its,snes_its;
24890b039ecaSBarry Smith };
24900b039ecaSBarry Smith 
2491d763cef2SBarry Smith 
24924a2ae208SSatish Balay #undef __FUNCT__
2493a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2494d763cef2SBarry Smith /*@C
2495a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2496a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2497d763cef2SBarry Smith 
2498d763cef2SBarry Smith    Collective on TS
2499d763cef2SBarry Smith 
2500d763cef2SBarry Smith    Input Parameters:
2501d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2502d763cef2SBarry Smith .  label - the title to put in the title bar
25037c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2504a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2505a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2506d763cef2SBarry Smith 
2507d763cef2SBarry Smith    Output Parameter:
25080b039ecaSBarry Smith .  ctx - the context
2509d763cef2SBarry Smith 
2510d763cef2SBarry Smith    Options Database Key:
25114f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
25124f09c107SBarry Smith .  -ts_monitor_lg_solution -
251399fdda47SBarry Smith .  -ts_monitor_lg_error -
251499fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
251599fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
251699fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2517d763cef2SBarry Smith 
2518d763cef2SBarry Smith    Notes:
2519a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2520d763cef2SBarry Smith 
2521d763cef2SBarry Smith    Level: intermediate
2522d763cef2SBarry Smith 
25237c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2524d763cef2SBarry Smith 
25254f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
25267c922b88SBarry Smith 
2527d763cef2SBarry Smith @*/
2528a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2529d763cef2SBarry Smith {
2530b0a32e0cSBarry Smith   PetscDraw      win;
2531dfbe8321SBarry Smith   PetscErrorCode ierr;
253299fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2533d763cef2SBarry Smith 
2534d763cef2SBarry Smith   PetscFunctionBegin;
2535201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2536a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
25373fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
25380b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
253999fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr);
254099fdda47SBarry Smith   if (flg) {
25410b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
254299fdda47SBarry Smith   }
25430b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2544a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2545d763cef2SBarry Smith   PetscFunctionReturn(0);
2546d763cef2SBarry Smith }
2547d763cef2SBarry Smith 
25484a2ae208SSatish Balay #undef __FUNCT__
25494f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
25504f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2551d763cef2SBarry Smith {
25520b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2553c32365f1SBarry Smith   PetscReal      x = ptime,y;
2554dfbe8321SBarry Smith   PetscErrorCode ierr;
2555d763cef2SBarry Smith 
2556d763cef2SBarry Smith   PetscFunctionBegin;
2557a9f9c1f6SBarry Smith   if (!n) {
2558a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
2559a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2560a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2561a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2562a9f9c1f6SBarry Smith   }
2563c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
25640b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
256599fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){
25660b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
25673923b477SBarry Smith   }
2568d763cef2SBarry Smith   PetscFunctionReturn(0);
2569d763cef2SBarry Smith }
2570d763cef2SBarry Smith 
25714a2ae208SSatish Balay #undef __FUNCT__
2572a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2573d763cef2SBarry Smith /*@C
2574a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2575a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2576d763cef2SBarry Smith 
25770b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2578d763cef2SBarry Smith 
2579d763cef2SBarry Smith    Input Parameter:
25800b039ecaSBarry Smith .  ctx - the monitor context
2581d763cef2SBarry Smith 
2582d763cef2SBarry Smith    Level: intermediate
2583d763cef2SBarry Smith 
2584d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2585d763cef2SBarry Smith 
25864f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2587d763cef2SBarry Smith @*/
2588a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2589d763cef2SBarry Smith {
2590b0a32e0cSBarry Smith   PetscDraw      draw;
2591dfbe8321SBarry Smith   PetscErrorCode ierr;
2592d763cef2SBarry Smith 
2593d763cef2SBarry Smith   PetscFunctionBegin;
25940b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
25956bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
25960b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
25970b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2598d763cef2SBarry Smith   PetscFunctionReturn(0);
2599d763cef2SBarry Smith }
2600d763cef2SBarry Smith 
26014a2ae208SSatish Balay #undef __FUNCT__
26024a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2603d763cef2SBarry Smith /*@
2604b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2605d763cef2SBarry Smith 
2606d763cef2SBarry Smith    Not Collective
2607d763cef2SBarry Smith 
2608d763cef2SBarry Smith    Input Parameter:
2609d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2610d763cef2SBarry Smith 
2611d763cef2SBarry Smith    Output Parameter:
2612d763cef2SBarry Smith .  t  - the current time
2613d763cef2SBarry Smith 
2614d763cef2SBarry Smith    Level: beginner
2615d763cef2SBarry Smith 
2616b8123daeSJed Brown    Note:
2617b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2618b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2619b8123daeSJed Brown 
2620d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2621d763cef2SBarry Smith 
2622d763cef2SBarry Smith .keywords: TS, get, time
2623d763cef2SBarry Smith @*/
26247087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2625d763cef2SBarry Smith {
2626d763cef2SBarry Smith   PetscFunctionBegin;
26270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2628f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2629d763cef2SBarry Smith   *t = ts->ptime;
2630d763cef2SBarry Smith   PetscFunctionReturn(0);
2631d763cef2SBarry Smith }
2632d763cef2SBarry Smith 
26334a2ae208SSatish Balay #undef __FUNCT__
26346a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
26356a4d4014SLisandro Dalcin /*@
26366a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
26376a4d4014SLisandro Dalcin 
26383f9fe445SBarry Smith    Logically Collective on TS
26396a4d4014SLisandro Dalcin 
26406a4d4014SLisandro Dalcin    Input Parameters:
26416a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
26426a4d4014SLisandro Dalcin -  time - the time
26436a4d4014SLisandro Dalcin 
26446a4d4014SLisandro Dalcin    Level: intermediate
26456a4d4014SLisandro Dalcin 
26466a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
26476a4d4014SLisandro Dalcin 
26486a4d4014SLisandro Dalcin .keywords: TS, set, time
26496a4d4014SLisandro Dalcin @*/
26507087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
26516a4d4014SLisandro Dalcin {
26526a4d4014SLisandro Dalcin   PetscFunctionBegin;
26530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2654c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
26556a4d4014SLisandro Dalcin   ts->ptime = t;
26566a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
26576a4d4014SLisandro Dalcin }
26586a4d4014SLisandro Dalcin 
26596a4d4014SLisandro Dalcin #undef __FUNCT__
26604a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2661d763cef2SBarry Smith /*@C
2662d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2663d763cef2SBarry Smith    TS options in the database.
2664d763cef2SBarry Smith 
26653f9fe445SBarry Smith    Logically Collective on TS
2666d763cef2SBarry Smith 
2667d763cef2SBarry Smith    Input Parameter:
2668d763cef2SBarry Smith +  ts     - The TS context
2669d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2670d763cef2SBarry Smith 
2671d763cef2SBarry Smith    Notes:
2672d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2673d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2674d763cef2SBarry Smith    hyphen.
2675d763cef2SBarry Smith 
2676d763cef2SBarry Smith    Level: advanced
2677d763cef2SBarry Smith 
2678d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2679d763cef2SBarry Smith 
2680d763cef2SBarry Smith .seealso: TSSetFromOptions()
2681d763cef2SBarry Smith 
2682d763cef2SBarry Smith @*/
26837087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2684d763cef2SBarry Smith {
2685dfbe8321SBarry Smith   PetscErrorCode ierr;
2686089b2837SJed Brown   SNES           snes;
2687d763cef2SBarry Smith 
2688d763cef2SBarry Smith   PetscFunctionBegin;
26890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2690d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2691089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2692089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2693d763cef2SBarry Smith   PetscFunctionReturn(0);
2694d763cef2SBarry Smith }
2695d763cef2SBarry Smith 
2696d763cef2SBarry Smith 
26974a2ae208SSatish Balay #undef __FUNCT__
26984a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2699d763cef2SBarry Smith /*@C
2700d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2701d763cef2SBarry Smith    TS options in the database.
2702d763cef2SBarry Smith 
27033f9fe445SBarry Smith    Logically Collective on TS
2704d763cef2SBarry Smith 
2705d763cef2SBarry Smith    Input Parameter:
2706d763cef2SBarry Smith +  ts     - The TS context
2707d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2708d763cef2SBarry Smith 
2709d763cef2SBarry Smith    Notes:
2710d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2711d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2712d763cef2SBarry Smith    hyphen.
2713d763cef2SBarry Smith 
2714d763cef2SBarry Smith    Level: advanced
2715d763cef2SBarry Smith 
2716d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2717d763cef2SBarry Smith 
2718d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2719d763cef2SBarry Smith 
2720d763cef2SBarry Smith @*/
27217087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2722d763cef2SBarry Smith {
2723dfbe8321SBarry Smith   PetscErrorCode ierr;
2724089b2837SJed Brown   SNES           snes;
2725d763cef2SBarry Smith 
2726d763cef2SBarry Smith   PetscFunctionBegin;
27270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2728d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2729089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2730089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2731d763cef2SBarry Smith   PetscFunctionReturn(0);
2732d763cef2SBarry Smith }
2733d763cef2SBarry Smith 
27344a2ae208SSatish Balay #undef __FUNCT__
27354a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2736d763cef2SBarry Smith /*@C
2737d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2738d763cef2SBarry Smith    TS options in the database.
2739d763cef2SBarry Smith 
2740d763cef2SBarry Smith    Not Collective
2741d763cef2SBarry Smith 
2742d763cef2SBarry Smith    Input Parameter:
2743d763cef2SBarry Smith .  ts - The TS context
2744d763cef2SBarry Smith 
2745d763cef2SBarry Smith    Output Parameter:
2746d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2747d763cef2SBarry Smith 
2748d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2749d763cef2SBarry Smith    sufficient length to hold the prefix.
2750d763cef2SBarry Smith 
2751d763cef2SBarry Smith    Level: intermediate
2752d763cef2SBarry Smith 
2753d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2754d763cef2SBarry Smith 
2755d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2756d763cef2SBarry Smith @*/
27577087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2758d763cef2SBarry Smith {
2759dfbe8321SBarry Smith   PetscErrorCode ierr;
2760d763cef2SBarry Smith 
2761d763cef2SBarry Smith   PetscFunctionBegin;
27620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27634482741eSBarry Smith   PetscValidPointer(prefix,2);
2764d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2765d763cef2SBarry Smith   PetscFunctionReturn(0);
2766d763cef2SBarry Smith }
2767d763cef2SBarry Smith 
27684a2ae208SSatish Balay #undef __FUNCT__
27694a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2770d763cef2SBarry Smith /*@C
2771d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2772d763cef2SBarry Smith 
2773d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2774d763cef2SBarry Smith 
2775d763cef2SBarry Smith    Input Parameter:
2776d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2777d763cef2SBarry Smith 
2778d763cef2SBarry Smith    Output Parameters:
2779b5abc632SBarry Smith +  J   - The Jacobian J of F, where U_t = G(U,t)
2780d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2781089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2782d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2783d763cef2SBarry Smith 
2784d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2785d763cef2SBarry Smith 
2786d763cef2SBarry Smith    Level: intermediate
2787d763cef2SBarry Smith 
278826d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2789d763cef2SBarry Smith 
2790d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2791d763cef2SBarry Smith @*/
2792089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2793d763cef2SBarry Smith {
2794089b2837SJed Brown   PetscErrorCode ierr;
2795089b2837SJed Brown   SNES           snes;
279624989b8cSPeter Brune   DM             dm;
2797089b2837SJed Brown 
2798d763cef2SBarry Smith   PetscFunctionBegin;
2799089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2800089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
280124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
280224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2803d763cef2SBarry Smith   PetscFunctionReturn(0);
2804d763cef2SBarry Smith }
2805d763cef2SBarry Smith 
28061713a123SBarry Smith #undef __FUNCT__
28072eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
28082eca1d9cSJed Brown /*@C
28092eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
28102eca1d9cSJed Brown 
28112eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
28122eca1d9cSJed Brown 
28132eca1d9cSJed Brown    Input Parameter:
28142eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
28152eca1d9cSJed Brown 
28162eca1d9cSJed Brown    Output Parameters:
28172eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
28182eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
28192eca1d9cSJed Brown .  f   - The function to compute the matrices
28202eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
28212eca1d9cSJed Brown 
28222eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
28232eca1d9cSJed Brown 
28242eca1d9cSJed Brown    Level: advanced
28252eca1d9cSJed Brown 
28262eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
28272eca1d9cSJed Brown 
28282eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
28292eca1d9cSJed Brown @*/
28307087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
28312eca1d9cSJed Brown {
2832089b2837SJed Brown   PetscErrorCode ierr;
2833089b2837SJed Brown   SNES           snes;
283424989b8cSPeter Brune   DM             dm;
28350910c330SBarry Smith 
28362eca1d9cSJed Brown   PetscFunctionBegin;
2837089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2838f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
2839089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
284024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
284124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
28422eca1d9cSJed Brown   PetscFunctionReturn(0);
28432eca1d9cSJed Brown }
28442eca1d9cSJed Brown 
284583a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
28466083293cSBarry Smith   PetscViewer viewer;
28476083293cSBarry Smith   Vec         initialsolution;
28486083293cSBarry Smith   PetscBool   showinitial;
284983a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
285083a4ac43SBarry Smith };
28516083293cSBarry Smith 
28522eca1d9cSJed Brown #undef __FUNCT__
285383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
28541713a123SBarry Smith /*@C
285583a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
28561713a123SBarry Smith    VecView() for the solution at each timestep
28571713a123SBarry Smith 
28581713a123SBarry Smith    Collective on TS
28591713a123SBarry Smith 
28601713a123SBarry Smith    Input Parameters:
28611713a123SBarry Smith +  ts - the TS context
28621713a123SBarry Smith .  step - current time-step
2863142b95e3SSatish Balay .  ptime - current time
28641713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
28651713a123SBarry Smith 
286699fdda47SBarry Smith    Options Database:
286799fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
286899fdda47SBarry Smith 
286999fdda47SBarry 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
287099fdda47SBarry Smith        will look bad
287199fdda47SBarry Smith 
28721713a123SBarry Smith    Level: intermediate
28731713a123SBarry Smith 
28741713a123SBarry Smith .keywords: TS,  vector, monitor, view
28751713a123SBarry Smith 
2876a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
28771713a123SBarry Smith @*/
28780910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
28791713a123SBarry Smith {
2880dfbe8321SBarry Smith   PetscErrorCode   ierr;
288183a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
28821713a123SBarry Smith 
28831713a123SBarry Smith   PetscFunctionBegin;
28846083293cSBarry Smith   if (!step && ictx->showinitial) {
28856083293cSBarry Smith     if (!ictx->initialsolution) {
28860910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
28871713a123SBarry Smith     }
28880910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
28896083293cSBarry Smith   }
28903fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
28910dcf80beSBarry Smith 
28926083293cSBarry Smith   if (ictx->showinitial) {
28936083293cSBarry Smith     PetscReal pause;
28946083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
28956083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
28966083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
28976083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
28986083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
28996083293cSBarry Smith   }
29000910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
29016083293cSBarry Smith   if (ictx->showinitial) {
29026083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
29036083293cSBarry Smith   }
29041713a123SBarry Smith   PetscFunctionReturn(0);
29051713a123SBarry Smith }
29061713a123SBarry Smith 
29071713a123SBarry Smith 
29086c699258SBarry Smith #undef __FUNCT__
290983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
29106083293cSBarry Smith /*@C
291183a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
29126083293cSBarry Smith 
29136083293cSBarry Smith    Collective on TS
29146083293cSBarry Smith 
29156083293cSBarry Smith    Input Parameters:
29166083293cSBarry Smith .    ctx - the monitor context
29176083293cSBarry Smith 
29186083293cSBarry Smith    Level: intermediate
29196083293cSBarry Smith 
29206083293cSBarry Smith .keywords: TS,  vector, monitor, view
29216083293cSBarry Smith 
292283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
29236083293cSBarry Smith @*/
292483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
29256083293cSBarry Smith {
29266083293cSBarry Smith   PetscErrorCode       ierr;
29276083293cSBarry Smith 
29286083293cSBarry Smith   PetscFunctionBegin;
292983a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
293083a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
293183a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
29326083293cSBarry Smith   PetscFunctionReturn(0);
29336083293cSBarry Smith }
29346083293cSBarry Smith 
29356083293cSBarry Smith #undef __FUNCT__
293683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
29376083293cSBarry Smith /*@C
293883a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
29396083293cSBarry Smith 
29406083293cSBarry Smith    Collective on TS
29416083293cSBarry Smith 
29426083293cSBarry Smith    Input Parameter:
29436083293cSBarry Smith .    ts - time-step context
29446083293cSBarry Smith 
29456083293cSBarry Smith    Output Patameter:
29466083293cSBarry Smith .    ctx - the monitor context
29476083293cSBarry Smith 
294899fdda47SBarry Smith    Options Database:
294999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
295099fdda47SBarry Smith 
29516083293cSBarry Smith    Level: intermediate
29526083293cSBarry Smith 
29536083293cSBarry Smith .keywords: TS,  vector, monitor, view
29546083293cSBarry Smith 
295583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
29566083293cSBarry Smith @*/
295783a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
29586083293cSBarry Smith {
29596083293cSBarry Smith   PetscErrorCode   ierr;
29606083293cSBarry Smith 
29616083293cSBarry Smith   PetscFunctionBegin;
296283a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
296383a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
296483a4ac43SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
296583a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
296699fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr);
29676083293cSBarry Smith   PetscFunctionReturn(0);
29686083293cSBarry Smith }
29696083293cSBarry Smith 
29706083293cSBarry Smith #undef __FUNCT__
297183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
29723a471f94SBarry Smith /*@C
297383a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
29743a471f94SBarry Smith    VecView() for the error at each timestep
29753a471f94SBarry Smith 
29763a471f94SBarry Smith    Collective on TS
29773a471f94SBarry Smith 
29783a471f94SBarry Smith    Input Parameters:
29793a471f94SBarry Smith +  ts - the TS context
29803a471f94SBarry Smith .  step - current time-step
29813a471f94SBarry Smith .  ptime - current time
29823a471f94SBarry Smith -  dummy - either a viewer or PETSC_NULL
29833a471f94SBarry Smith 
29843a471f94SBarry Smith    Level: intermediate
29853a471f94SBarry Smith 
29863a471f94SBarry Smith .keywords: TS,  vector, monitor, view
29873a471f94SBarry Smith 
29883a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
29893a471f94SBarry Smith @*/
29900910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
29913a471f94SBarry Smith {
29923a471f94SBarry Smith   PetscErrorCode   ierr;
299383a4ac43SBarry Smith   TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy;
299483a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
29953a471f94SBarry Smith   Vec              work;
29963a471f94SBarry Smith 
29973a471f94SBarry Smith   PetscFunctionBegin;
29983fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
29990910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
30003a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
30010910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
30023a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
30033a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
30043a471f94SBarry Smith   PetscFunctionReturn(0);
30053a471f94SBarry Smith }
30063a471f94SBarry Smith 
30072a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
30083a471f94SBarry Smith #undef __FUNCT__
30096c699258SBarry Smith #define __FUNCT__ "TSSetDM"
30106c699258SBarry Smith /*@
30116c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
30126c699258SBarry Smith 
30133f9fe445SBarry Smith    Logically Collective on TS and DM
30146c699258SBarry Smith 
30156c699258SBarry Smith    Input Parameters:
30166c699258SBarry Smith +  ts - the preconditioner context
30176c699258SBarry Smith -  dm - the dm
30186c699258SBarry Smith 
30196c699258SBarry Smith    Level: intermediate
30206c699258SBarry Smith 
30216c699258SBarry Smith 
30226c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
30236c699258SBarry Smith @*/
30247087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
30256c699258SBarry Smith {
30266c699258SBarry Smith   PetscErrorCode ierr;
3027089b2837SJed Brown   SNES           snes;
3028942e3340SBarry Smith   DMTS           tsdm;
30296c699258SBarry Smith 
30306c699258SBarry Smith   PetscFunctionBegin;
30310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
303270663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3033942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
30342a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3035942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3036942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
303724989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
303824989b8cSPeter Brune         tsdm->originaldm = dm;
303924989b8cSPeter Brune       }
304024989b8cSPeter Brune     }
30416bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
304224989b8cSPeter Brune   }
30436c699258SBarry Smith   ts->dm = dm;
3044089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3045089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
30466c699258SBarry Smith   PetscFunctionReturn(0);
30476c699258SBarry Smith }
30486c699258SBarry Smith 
30496c699258SBarry Smith #undef __FUNCT__
30506c699258SBarry Smith #define __FUNCT__ "TSGetDM"
30516c699258SBarry Smith /*@
30526c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
30536c699258SBarry Smith 
30543f9fe445SBarry Smith    Not Collective
30556c699258SBarry Smith 
30566c699258SBarry Smith    Input Parameter:
30576c699258SBarry Smith . ts - the preconditioner context
30586c699258SBarry Smith 
30596c699258SBarry Smith    Output Parameter:
30606c699258SBarry Smith .  dm - the dm
30616c699258SBarry Smith 
30626c699258SBarry Smith    Level: intermediate
30636c699258SBarry Smith 
30646c699258SBarry Smith 
30656c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
30666c699258SBarry Smith @*/
30677087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
30686c699258SBarry Smith {
3069496e6a7aSJed Brown   PetscErrorCode ierr;
3070496e6a7aSJed Brown 
30716c699258SBarry Smith   PetscFunctionBegin;
30720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3073496e6a7aSJed Brown   if (!ts->dm) {
3074496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
3075496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3076496e6a7aSJed Brown   }
30776c699258SBarry Smith   *dm = ts->dm;
30786c699258SBarry Smith   PetscFunctionReturn(0);
30796c699258SBarry Smith }
30801713a123SBarry Smith 
30810f5c6efeSJed Brown #undef __FUNCT__
30820f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
30830f5c6efeSJed Brown /*@
30840f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
30850f5c6efeSJed Brown 
30863f9fe445SBarry Smith    Logically Collective on SNES
30870f5c6efeSJed Brown 
30880f5c6efeSJed Brown    Input Parameter:
3089d42a1c89SJed Brown + snes - nonlinear solver
30900910c330SBarry Smith . U - the current state at which to evaluate the residual
3091d42a1c89SJed Brown - ctx - user context, must be a TS
30920f5c6efeSJed Brown 
30930f5c6efeSJed Brown    Output Parameter:
30940f5c6efeSJed Brown . F - the nonlinear residual
30950f5c6efeSJed Brown 
30960f5c6efeSJed Brown    Notes:
30970f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
30980f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
30990f5c6efeSJed Brown 
31000f5c6efeSJed Brown    Level: advanced
31010f5c6efeSJed Brown 
31020f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
31030f5c6efeSJed Brown @*/
31040910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
31050f5c6efeSJed Brown {
31060f5c6efeSJed Brown   TS             ts = (TS)ctx;
31070f5c6efeSJed Brown   PetscErrorCode ierr;
31080f5c6efeSJed Brown 
31090f5c6efeSJed Brown   PetscFunctionBegin;
31100f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31110910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
31120f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
31130f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
31140910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
31150f5c6efeSJed Brown   PetscFunctionReturn(0);
31160f5c6efeSJed Brown }
31170f5c6efeSJed Brown 
31180f5c6efeSJed Brown #undef __FUNCT__
31190f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
31200f5c6efeSJed Brown /*@
31210f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
31220f5c6efeSJed Brown 
31230f5c6efeSJed Brown    Collective on SNES
31240f5c6efeSJed Brown 
31250f5c6efeSJed Brown    Input Parameter:
31260f5c6efeSJed Brown + snes - nonlinear solver
31270910c330SBarry Smith . U - the current state at which to evaluate the residual
31280f5c6efeSJed Brown - ctx - user context, must be a TS
31290f5c6efeSJed Brown 
31300f5c6efeSJed Brown    Output Parameter:
31310f5c6efeSJed Brown + A - the Jacobian
31320f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
31330f5c6efeSJed Brown - flag - indicates any structure change in the matrix
31340f5c6efeSJed Brown 
31350f5c6efeSJed Brown    Notes:
31360f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
31370f5c6efeSJed Brown 
31380f5c6efeSJed Brown    Level: developer
31390f5c6efeSJed Brown 
31400f5c6efeSJed Brown .seealso: SNESSetJacobian()
31410f5c6efeSJed Brown @*/
31420910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
31430f5c6efeSJed Brown {
31440f5c6efeSJed Brown   TS             ts = (TS)ctx;
31450f5c6efeSJed Brown   PetscErrorCode ierr;
31460f5c6efeSJed Brown 
31470f5c6efeSJed Brown   PetscFunctionBegin;
31480f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31490910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
31500f5c6efeSJed Brown   PetscValidPointer(A,3);
31510f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
31520f5c6efeSJed Brown   PetscValidPointer(B,4);
31530f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
31540f5c6efeSJed Brown   PetscValidPointer(flag,5);
31550f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
31560910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
31570f5c6efeSJed Brown   PetscFunctionReturn(0);
31580f5c6efeSJed Brown }
3159325fc9f4SBarry Smith 
31600e4ef248SJed Brown #undef __FUNCT__
31610e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
31620e4ef248SJed Brown /*@C
31630e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
31640e4ef248SJed Brown 
31650e4ef248SJed Brown    Collective on TS
31660e4ef248SJed Brown 
31670e4ef248SJed Brown    Input Arguments:
31680e4ef248SJed Brown +  ts - time stepping context
31690e4ef248SJed Brown .  t - time at which to evaluate
31700910c330SBarry Smith .  U - state at which to evaluate
31710e4ef248SJed Brown -  ctx - context
31720e4ef248SJed Brown 
31730e4ef248SJed Brown    Output Arguments:
31740e4ef248SJed Brown .  F - right hand side
31750e4ef248SJed Brown 
31760e4ef248SJed Brown    Level: intermediate
31770e4ef248SJed Brown 
31780e4ef248SJed Brown    Notes:
31790e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
31800e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
31810e4ef248SJed Brown 
31820e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
31830e4ef248SJed Brown @*/
31840910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
31850e4ef248SJed Brown {
31860e4ef248SJed Brown   PetscErrorCode ierr;
31870e4ef248SJed Brown   Mat            Arhs,Brhs;
31880e4ef248SJed Brown   MatStructure   flg2;
31890e4ef248SJed Brown 
31900e4ef248SJed Brown   PetscFunctionBegin;
31910e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
31920910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
31930910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
31940e4ef248SJed Brown   PetscFunctionReturn(0);
31950e4ef248SJed Brown }
31960e4ef248SJed Brown 
31970e4ef248SJed Brown #undef __FUNCT__
31980e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
31990e4ef248SJed Brown /*@C
32000e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
32010e4ef248SJed Brown 
32020e4ef248SJed Brown    Collective on TS
32030e4ef248SJed Brown 
32040e4ef248SJed Brown    Input Arguments:
32050e4ef248SJed Brown +  ts - time stepping context
32060e4ef248SJed Brown .  t - time at which to evaluate
32070910c330SBarry Smith .  U - state at which to evaluate
32080e4ef248SJed Brown -  ctx - context
32090e4ef248SJed Brown 
32100e4ef248SJed Brown    Output Arguments:
32110e4ef248SJed Brown +  A - pointer to operator
32120e4ef248SJed Brown .  B - pointer to preconditioning matrix
32130e4ef248SJed Brown -  flg - matrix structure flag
32140e4ef248SJed Brown 
32150e4ef248SJed Brown    Level: intermediate
32160e4ef248SJed Brown 
32170e4ef248SJed Brown    Notes:
32180e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
32190e4ef248SJed Brown 
32200e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
32210e4ef248SJed Brown @*/
32220910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
32230e4ef248SJed Brown {
32240e4ef248SJed Brown   PetscFunctionBegin;
32250e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
32260e4ef248SJed Brown   PetscFunctionReturn(0);
32270e4ef248SJed Brown }
32280e4ef248SJed Brown 
32290026cea9SSean Farley #undef __FUNCT__
32300026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
32310026cea9SSean Farley /*@C
32320026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
32330026cea9SSean Farley 
32340026cea9SSean Farley    Collective on TS
32350026cea9SSean Farley 
32360026cea9SSean Farley    Input Arguments:
32370026cea9SSean Farley +  ts - time stepping context
32380026cea9SSean Farley .  t - time at which to evaluate
32390910c330SBarry Smith .  U - state at which to evaluate
32400910c330SBarry Smith .  Udot - time derivative of state vector
32410026cea9SSean Farley -  ctx - context
32420026cea9SSean Farley 
32430026cea9SSean Farley    Output Arguments:
32440026cea9SSean Farley .  F - left hand side
32450026cea9SSean Farley 
32460026cea9SSean Farley    Level: intermediate
32470026cea9SSean Farley 
32480026cea9SSean Farley    Notes:
32490910c330SBarry 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
32500026cea9SSean Farley    user is required to write their own TSComputeIFunction.
32510026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
32520026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
32530026cea9SSean Farley 
32540026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
32550026cea9SSean Farley @*/
32560910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
32570026cea9SSean Farley {
32580026cea9SSean Farley   PetscErrorCode ierr;
32590026cea9SSean Farley   Mat            A,B;
32600026cea9SSean Farley   MatStructure   flg2;
32610026cea9SSean Farley 
32620026cea9SSean Farley   PetscFunctionBegin;
32630026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
32640910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
32650910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
32660026cea9SSean Farley   PetscFunctionReturn(0);
32670026cea9SSean Farley }
32680026cea9SSean Farley 
32690026cea9SSean Farley #undef __FUNCT__
32700026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
32710026cea9SSean Farley /*@C
327224e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
32730026cea9SSean Farley 
32740026cea9SSean Farley    Collective on TS
32750026cea9SSean Farley 
32760026cea9SSean Farley    Input Arguments:
32770026cea9SSean Farley +  ts - time stepping context
32780026cea9SSean Farley .  t - time at which to evaluate
32790910c330SBarry Smith .  U - state at which to evaluate
32800910c330SBarry Smith .  Udot - time derivative of state vector
32810026cea9SSean Farley .  shift - shift to apply
32820026cea9SSean Farley -  ctx - context
32830026cea9SSean Farley 
32840026cea9SSean Farley    Output Arguments:
32850026cea9SSean Farley +  A - pointer to operator
32860026cea9SSean Farley .  B - pointer to preconditioning matrix
32870026cea9SSean Farley -  flg - matrix structure flag
32880026cea9SSean Farley 
32890026cea9SSean Farley    Level: intermediate
32900026cea9SSean Farley 
32910026cea9SSean Farley    Notes:
32920026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
32930026cea9SSean Farley 
32940026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
32950026cea9SSean Farley @*/
32960910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
32970026cea9SSean Farley {
32980026cea9SSean Farley   PetscFunctionBegin;
32990026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
33000026cea9SSean Farley   PetscFunctionReturn(0);
33010026cea9SSean Farley }
33020026cea9SSean Farley 
33034af1b03aSJed Brown #undef __FUNCT__
33044af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
33054af1b03aSJed Brown /*@
33064af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
33074af1b03aSJed Brown 
33084af1b03aSJed Brown    Not Collective
33094af1b03aSJed Brown 
33104af1b03aSJed Brown    Input Parameter:
33114af1b03aSJed Brown .  ts - the TS context
33124af1b03aSJed Brown 
33134af1b03aSJed Brown    Output Parameter:
33144af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
33154af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
33164af1b03aSJed Brown 
3317487e0bb9SJed Brown    Level: beginner
33184af1b03aSJed Brown 
3319cd652676SJed Brown    Notes:
3320cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
33214af1b03aSJed Brown 
33224af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
33234af1b03aSJed Brown 
33244af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
33254af1b03aSJed Brown @*/
33264af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
33274af1b03aSJed Brown {
33284af1b03aSJed Brown   PetscFunctionBegin;
33294af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33304af1b03aSJed Brown   PetscValidPointer(reason,2);
33314af1b03aSJed Brown   *reason = ts->reason;
33324af1b03aSJed Brown   PetscFunctionReturn(0);
33334af1b03aSJed Brown }
33344af1b03aSJed Brown 
3335fb1732b5SBarry Smith #undef __FUNCT__
3336d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3337d6ad946cSShri Abhyankar /*@
3338d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3339d6ad946cSShri Abhyankar 
3340d6ad946cSShri Abhyankar    Not Collective
3341d6ad946cSShri Abhyankar 
3342d6ad946cSShri Abhyankar    Input Parameter:
3343d6ad946cSShri Abhyankar +  ts - the TS context
3344d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3345d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3346d6ad946cSShri Abhyankar 
3347f5abba47SShri Abhyankar    Level: advanced
3348d6ad946cSShri Abhyankar 
3349d6ad946cSShri Abhyankar    Notes:
3350d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3351d6ad946cSShri Abhyankar 
3352d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3353d6ad946cSShri Abhyankar 
3354d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3355d6ad946cSShri Abhyankar @*/
3356d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3357d6ad946cSShri Abhyankar {
3358d6ad946cSShri Abhyankar   PetscFunctionBegin;
3359d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3360d6ad946cSShri Abhyankar   ts->reason = reason;
3361d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3362d6ad946cSShri Abhyankar }
3363d6ad946cSShri Abhyankar 
3364d6ad946cSShri Abhyankar #undef __FUNCT__
3365cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3366cc708dedSBarry Smith /*@
3367cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3368cc708dedSBarry Smith 
3369cc708dedSBarry Smith    Not Collective
3370cc708dedSBarry Smith 
3371cc708dedSBarry Smith    Input Parameter:
3372cc708dedSBarry Smith .  ts - the TS context
3373cc708dedSBarry Smith 
3374cc708dedSBarry Smith    Output Parameter:
3375cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3376cc708dedSBarry Smith 
3377487e0bb9SJed Brown    Level: beginner
3378cc708dedSBarry Smith 
3379cc708dedSBarry Smith    Notes:
3380cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3381cc708dedSBarry Smith 
3382cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3383cc708dedSBarry Smith 
3384cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3385cc708dedSBarry Smith @*/
3386cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3387cc708dedSBarry Smith {
3388cc708dedSBarry Smith   PetscFunctionBegin;
3389cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3390cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3391cc708dedSBarry Smith   *ftime = ts->solvetime;
3392cc708dedSBarry Smith   PetscFunctionReturn(0);
3393cc708dedSBarry Smith }
3394cc708dedSBarry Smith 
3395cc708dedSBarry Smith #undef __FUNCT__
33965ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
33979f67acb7SJed Brown /*@
33985ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
33999f67acb7SJed Brown    used by the time integrator.
34009f67acb7SJed Brown 
34019f67acb7SJed Brown    Not Collective
34029f67acb7SJed Brown 
34039f67acb7SJed Brown    Input Parameter:
34049f67acb7SJed Brown .  ts - TS context
34059f67acb7SJed Brown 
34069f67acb7SJed Brown    Output Parameter:
34079f67acb7SJed Brown .  nits - number of nonlinear iterations
34089f67acb7SJed Brown 
34099f67acb7SJed Brown    Notes:
34109f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
34119f67acb7SJed Brown 
34129f67acb7SJed Brown    Level: intermediate
34139f67acb7SJed Brown 
34149f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
34159f67acb7SJed Brown 
34165ef26d82SJed Brown .seealso:  TSGetKSPIterations()
34179f67acb7SJed Brown @*/
34185ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
34199f67acb7SJed Brown {
34209f67acb7SJed Brown   PetscFunctionBegin;
34219f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34229f67acb7SJed Brown   PetscValidIntPointer(nits,2);
34235ef26d82SJed Brown   *nits = ts->snes_its;
34249f67acb7SJed Brown   PetscFunctionReturn(0);
34259f67acb7SJed Brown }
34269f67acb7SJed Brown 
34279f67acb7SJed Brown #undef __FUNCT__
34285ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
34299f67acb7SJed Brown /*@
34305ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
34319f67acb7SJed Brown    used by the time integrator.
34329f67acb7SJed Brown 
34339f67acb7SJed Brown    Not Collective
34349f67acb7SJed Brown 
34359f67acb7SJed Brown    Input Parameter:
34369f67acb7SJed Brown .  ts - TS context
34379f67acb7SJed Brown 
34389f67acb7SJed Brown    Output Parameter:
34399f67acb7SJed Brown .  lits - number of linear iterations
34409f67acb7SJed Brown 
34419f67acb7SJed Brown    Notes:
34429f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
34439f67acb7SJed Brown 
34449f67acb7SJed Brown    Level: intermediate
34459f67acb7SJed Brown 
34469f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
34479f67acb7SJed Brown 
34485ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
34499f67acb7SJed Brown @*/
34505ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
34519f67acb7SJed Brown {
34529f67acb7SJed Brown   PetscFunctionBegin;
34539f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34549f67acb7SJed Brown   PetscValidIntPointer(lits,2);
34555ef26d82SJed Brown   *lits = ts->ksp_its;
34569f67acb7SJed Brown   PetscFunctionReturn(0);
34579f67acb7SJed Brown }
34589f67acb7SJed Brown 
34599f67acb7SJed Brown #undef __FUNCT__
3460cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3461cef5090cSJed Brown /*@
3462cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3463cef5090cSJed Brown 
3464cef5090cSJed Brown    Not Collective
3465cef5090cSJed Brown 
3466cef5090cSJed Brown    Input Parameter:
3467cef5090cSJed Brown .  ts - TS context
3468cef5090cSJed Brown 
3469cef5090cSJed Brown    Output Parameter:
3470cef5090cSJed Brown .  rejects - number of steps rejected
3471cef5090cSJed Brown 
3472cef5090cSJed Brown    Notes:
3473cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3474cef5090cSJed Brown 
3475cef5090cSJed Brown    Level: intermediate
3476cef5090cSJed Brown 
3477cef5090cSJed Brown .keywords: TS, get, number
3478cef5090cSJed Brown 
34795ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3480cef5090cSJed Brown @*/
3481cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3482cef5090cSJed Brown {
3483cef5090cSJed Brown   PetscFunctionBegin;
3484cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3485cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3486cef5090cSJed Brown   *rejects = ts->reject;
3487cef5090cSJed Brown   PetscFunctionReturn(0);
3488cef5090cSJed Brown }
3489cef5090cSJed Brown 
3490cef5090cSJed Brown #undef __FUNCT__
3491cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3492cef5090cSJed Brown /*@
3493cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3494cef5090cSJed Brown 
3495cef5090cSJed Brown    Not Collective
3496cef5090cSJed Brown 
3497cef5090cSJed Brown    Input Parameter:
3498cef5090cSJed Brown .  ts - TS context
3499cef5090cSJed Brown 
3500cef5090cSJed Brown    Output Parameter:
3501cef5090cSJed Brown .  fails - number of failed nonlinear solves
3502cef5090cSJed Brown 
3503cef5090cSJed Brown    Notes:
3504cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3505cef5090cSJed Brown 
3506cef5090cSJed Brown    Level: intermediate
3507cef5090cSJed Brown 
3508cef5090cSJed Brown .keywords: TS, get, number
3509cef5090cSJed Brown 
35105ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3511cef5090cSJed Brown @*/
3512cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3513cef5090cSJed Brown {
3514cef5090cSJed Brown   PetscFunctionBegin;
3515cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3516cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3517cef5090cSJed Brown   *fails = ts->num_snes_failures;
3518cef5090cSJed Brown   PetscFunctionReturn(0);
3519cef5090cSJed Brown }
3520cef5090cSJed Brown 
3521cef5090cSJed Brown #undef __FUNCT__
3522cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3523cef5090cSJed Brown /*@
3524cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3525cef5090cSJed Brown 
3526cef5090cSJed Brown    Not Collective
3527cef5090cSJed Brown 
3528cef5090cSJed Brown    Input Parameter:
3529cef5090cSJed Brown +  ts - TS context
3530cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3531cef5090cSJed Brown 
3532cef5090cSJed Brown    Notes:
3533cef5090cSJed Brown    The counter is reset to zero for each step
3534cef5090cSJed Brown 
3535cef5090cSJed Brown    Options Database Key:
3536cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3537cef5090cSJed Brown 
3538cef5090cSJed Brown    Level: intermediate
3539cef5090cSJed Brown 
3540cef5090cSJed Brown .keywords: TS, set, maximum, number
3541cef5090cSJed Brown 
35425ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3543cef5090cSJed Brown @*/
3544cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3545cef5090cSJed Brown {
3546cef5090cSJed Brown   PetscFunctionBegin;
3547cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3548cef5090cSJed Brown   ts->max_reject = rejects;
3549cef5090cSJed Brown   PetscFunctionReturn(0);
3550cef5090cSJed Brown }
3551cef5090cSJed Brown 
3552cef5090cSJed Brown #undef __FUNCT__
3553cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3554cef5090cSJed Brown /*@
3555cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3556cef5090cSJed Brown 
3557cef5090cSJed Brown    Not Collective
3558cef5090cSJed Brown 
3559cef5090cSJed Brown    Input Parameter:
3560cef5090cSJed Brown +  ts - TS context
3561cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3562cef5090cSJed Brown 
3563cef5090cSJed Brown    Notes:
3564cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3565cef5090cSJed Brown 
3566cef5090cSJed Brown    Options Database Key:
3567cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3568cef5090cSJed Brown 
3569cef5090cSJed Brown    Level: intermediate
3570cef5090cSJed Brown 
3571cef5090cSJed Brown .keywords: TS, set, maximum, number
3572cef5090cSJed Brown 
35735ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3574cef5090cSJed Brown @*/
3575cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3576cef5090cSJed Brown {
3577cef5090cSJed Brown   PetscFunctionBegin;
3578cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3579cef5090cSJed Brown   ts->max_snes_failures = fails;
3580cef5090cSJed Brown   PetscFunctionReturn(0);
3581cef5090cSJed Brown }
3582cef5090cSJed Brown 
3583cef5090cSJed Brown #undef __FUNCT__
3584cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3585cef5090cSJed Brown /*@
3586cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3587cef5090cSJed Brown 
3588cef5090cSJed Brown    Not Collective
3589cef5090cSJed Brown 
3590cef5090cSJed Brown    Input Parameter:
3591cef5090cSJed Brown +  ts - TS context
3592cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3593cef5090cSJed Brown 
3594cef5090cSJed Brown    Options Database Key:
3595cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3596cef5090cSJed Brown 
3597cef5090cSJed Brown    Level: intermediate
3598cef5090cSJed Brown 
3599cef5090cSJed Brown .keywords: TS, set, error
3600cef5090cSJed Brown 
36015ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3602cef5090cSJed Brown @*/
3603cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3604cef5090cSJed Brown {
3605cef5090cSJed Brown   PetscFunctionBegin;
3606cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3607cef5090cSJed Brown   ts->errorifstepfailed = err;
3608cef5090cSJed Brown   PetscFunctionReturn(0);
3609cef5090cSJed Brown }
3610cef5090cSJed Brown 
3611cef5090cSJed Brown #undef __FUNCT__
3612fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3613fb1732b5SBarry Smith /*@C
3614fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3615fb1732b5SBarry Smith 
3616fb1732b5SBarry Smith    Collective on TS
3617fb1732b5SBarry Smith 
3618fb1732b5SBarry Smith    Input Parameters:
3619fb1732b5SBarry Smith +  ts - the TS context
3620fb1732b5SBarry Smith .  step - current time-step
3621fb1732b5SBarry Smith .  ptime - current time
36220910c330SBarry Smith .  u - current state
3623fb1732b5SBarry Smith -  viewer - binary viewer
3624fb1732b5SBarry Smith 
3625fb1732b5SBarry Smith    Level: intermediate
3626fb1732b5SBarry Smith 
3627fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3628fb1732b5SBarry Smith 
3629fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3630fb1732b5SBarry Smith @*/
36310910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3632fb1732b5SBarry Smith {
3633fb1732b5SBarry Smith   PetscErrorCode ierr;
3634ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3635fb1732b5SBarry Smith 
3636fb1732b5SBarry Smith   PetscFunctionBegin;
36370910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3638ed81e22dSJed Brown   PetscFunctionReturn(0);
3639ed81e22dSJed Brown }
3640ed81e22dSJed Brown 
3641ed81e22dSJed Brown #undef __FUNCT__
3642ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3643ed81e22dSJed Brown /*@C
3644ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3645ed81e22dSJed Brown 
3646ed81e22dSJed Brown    Collective on TS
3647ed81e22dSJed Brown 
3648ed81e22dSJed Brown    Input Parameters:
3649ed81e22dSJed Brown +  ts - the TS context
3650ed81e22dSJed Brown .  step - current time-step
3651ed81e22dSJed Brown .  ptime - current time
36520910c330SBarry Smith .  u - current state
3653ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3654ed81e22dSJed Brown 
3655ed81e22dSJed Brown    Level: intermediate
3656ed81e22dSJed Brown 
3657ed81e22dSJed Brown    Notes:
3658ed81e22dSJed 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.
3659ed81e22dSJed Brown    These are named according to the file name template.
3660ed81e22dSJed Brown 
3661ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3662ed81e22dSJed Brown 
3663ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3664ed81e22dSJed Brown 
3665ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3666ed81e22dSJed Brown @*/
36670910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3668ed81e22dSJed Brown {
3669ed81e22dSJed Brown   PetscErrorCode ierr;
3670ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3671ed81e22dSJed Brown   PetscViewer    viewer;
3672ed81e22dSJed Brown 
3673ed81e22dSJed Brown   PetscFunctionBegin;
36748caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3675ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
36760910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3677ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3678ed81e22dSJed Brown   PetscFunctionReturn(0);
3679ed81e22dSJed Brown }
3680ed81e22dSJed Brown 
3681ed81e22dSJed Brown #undef __FUNCT__
3682ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3683ed81e22dSJed Brown /*@C
3684ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3685ed81e22dSJed Brown 
3686ed81e22dSJed Brown    Collective on TS
3687ed81e22dSJed Brown 
3688ed81e22dSJed Brown    Input Parameters:
3689ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3690ed81e22dSJed Brown 
3691ed81e22dSJed Brown    Level: intermediate
3692ed81e22dSJed Brown 
3693ed81e22dSJed Brown    Note:
3694ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3695ed81e22dSJed Brown 
3696ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3697ed81e22dSJed Brown 
3698ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3699ed81e22dSJed Brown @*/
3700ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3701ed81e22dSJed Brown {
3702ed81e22dSJed Brown   PetscErrorCode ierr;
3703ed81e22dSJed Brown 
3704ed81e22dSJed Brown   PetscFunctionBegin;
3705ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3706fb1732b5SBarry Smith   PetscFunctionReturn(0);
3707fb1732b5SBarry Smith }
3708fb1732b5SBarry Smith 
370984df9cb4SJed Brown #undef __FUNCT__
3710ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt"
371184df9cb4SJed Brown /*@
3712ad6bc421SBarry Smith    TSGetTSAdapt - Get the adaptive controller context for the current method
371384df9cb4SJed Brown 
3714ed81e22dSJed Brown    Collective on TS if controller has not been created yet
371584df9cb4SJed Brown 
371684df9cb4SJed Brown    Input Arguments:
3717ed81e22dSJed Brown .  ts - time stepping context
371884df9cb4SJed Brown 
371984df9cb4SJed Brown    Output Arguments:
3720ed81e22dSJed Brown .  adapt - adaptive controller
372184df9cb4SJed Brown 
372284df9cb4SJed Brown    Level: intermediate
372384df9cb4SJed Brown 
3724ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
372584df9cb4SJed Brown @*/
3726ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt)
372784df9cb4SJed Brown {
372884df9cb4SJed Brown   PetscErrorCode ierr;
372984df9cb4SJed Brown 
373084df9cb4SJed Brown   PetscFunctionBegin;
373184df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
373284df9cb4SJed Brown   PetscValidPointer(adapt,2);
373384df9cb4SJed Brown   if (!ts->adapt) {
373484df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
37351c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
37361c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
373784df9cb4SJed Brown   }
373884df9cb4SJed Brown   *adapt = ts->adapt;
373984df9cb4SJed Brown   PetscFunctionReturn(0);
374084df9cb4SJed Brown }
3741d6ebe24aSShri Abhyankar 
3742d6ebe24aSShri Abhyankar #undef __FUNCT__
37431c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
37441c3436cfSJed Brown /*@
37451c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
37461c3436cfSJed Brown 
37471c3436cfSJed Brown    Logically Collective
37481c3436cfSJed Brown 
37491c3436cfSJed Brown    Input Arguments:
37501c3436cfSJed Brown +  ts - time integration context
37511c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
37521c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
37531c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
37541c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
37551c3436cfSJed Brown 
37561c3436cfSJed Brown    Level: beginner
37571c3436cfSJed Brown 
3758c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
37591c3436cfSJed Brown @*/
37601c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
37611c3436cfSJed Brown {
37621c3436cfSJed Brown   PetscErrorCode ierr;
37631c3436cfSJed Brown 
37641c3436cfSJed Brown   PetscFunctionBegin;
3765c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
37661c3436cfSJed Brown   if (vatol) {
37671c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
37681c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
37691c3436cfSJed Brown     ts->vatol = vatol;
37701c3436cfSJed Brown   }
3771c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
37721c3436cfSJed Brown   if (vrtol) {
37731c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
37741c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
37751c3436cfSJed Brown     ts->vrtol = vrtol;
37761c3436cfSJed Brown   }
37771c3436cfSJed Brown   PetscFunctionReturn(0);
37781c3436cfSJed Brown }
37791c3436cfSJed Brown 
37801c3436cfSJed Brown #undef __FUNCT__
3781c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3782c5033834SJed Brown /*@
3783c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3784c5033834SJed Brown 
3785c5033834SJed Brown    Logically Collective
3786c5033834SJed Brown 
3787c5033834SJed Brown    Input Arguments:
3788c5033834SJed Brown .  ts - time integration context
3789c5033834SJed Brown 
3790c5033834SJed Brown    Output Arguments:
3791c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3792c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3793c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3794c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3795c5033834SJed Brown 
3796c5033834SJed Brown    Level: beginner
3797c5033834SJed Brown 
3798c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3799c5033834SJed Brown @*/
3800c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3801c5033834SJed Brown {
3802c5033834SJed Brown   PetscFunctionBegin;
3803c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3804c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3805c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3806c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3807c5033834SJed Brown   PetscFunctionReturn(0);
3808c5033834SJed Brown }
3809c5033834SJed Brown 
3810c5033834SJed Brown #undef __FUNCT__
38111c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
38121c3436cfSJed Brown /*@
38131c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
38141c3436cfSJed Brown 
38151c3436cfSJed Brown    Collective on TS
38161c3436cfSJed Brown 
38171c3436cfSJed Brown    Input Arguments:
38181c3436cfSJed Brown +  ts - time stepping context
38191c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
38201c3436cfSJed Brown 
38211c3436cfSJed Brown    Output Arguments:
38221c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
38231c3436cfSJed Brown 
38241c3436cfSJed Brown    Level: developer
38251c3436cfSJed Brown 
38261c3436cfSJed Brown .seealso: TSSetTolerances()
38271c3436cfSJed Brown @*/
38281c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
38291c3436cfSJed Brown {
38301c3436cfSJed Brown   PetscErrorCode    ierr;
38311c3436cfSJed Brown   PetscInt          i,n,N;
38320910c330SBarry Smith   const PetscScalar *u,*y;
38330910c330SBarry Smith   Vec               U;
38341c3436cfSJed Brown   PetscReal         sum,gsum;
38351c3436cfSJed Brown 
38361c3436cfSJed Brown   PetscFunctionBegin;
38371c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38381c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
38391c3436cfSJed Brown   PetscValidPointer(norm,3);
38400910c330SBarry Smith   U = ts->vec_sol;
38410910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
38420910c330SBarry Smith   if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
38431c3436cfSJed Brown 
38440910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
38450910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
38460910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
38471c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
38481c3436cfSJed Brown   sum = 0.;
3849ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3850ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3851ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3852ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3853ceefc113SJed Brown     for (i=0; i<n; i++) {
38540910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
38550910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3856ceefc113SJed Brown     }
3857ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3858ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3859ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3860ceefc113SJed Brown     const PetscScalar *atol;
3861ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3862ceefc113SJed Brown     for (i=0; i<n; i++) {
38630910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
38640910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3865ceefc113SJed Brown     }
3866ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3867ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3868ceefc113SJed Brown     const PetscScalar *rtol;
3869ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3870ceefc113SJed Brown     for (i=0; i<n; i++) {
38710910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
38720910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3873ceefc113SJed Brown     }
3874ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3875ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
38761c3436cfSJed Brown     for (i=0; i<n; i++) {
38770910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
38780910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
38791c3436cfSJed Brown     }
3880ceefc113SJed Brown   }
38810910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
38821c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
38831c3436cfSJed Brown 
38841c3436cfSJed Brown   ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
38851c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
38861c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
38871c3436cfSJed Brown   PetscFunctionReturn(0);
38881c3436cfSJed Brown }
38891c3436cfSJed Brown 
38901c3436cfSJed Brown #undef __FUNCT__
38918d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
38928d59e960SJed Brown /*@
38938d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
38948d59e960SJed Brown 
38958d59e960SJed Brown    Logically Collective on TS
38968d59e960SJed Brown 
38978d59e960SJed Brown    Input Arguments:
38988d59e960SJed Brown +  ts - time stepping context
38998d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
39008d59e960SJed Brown 
39018d59e960SJed Brown    Note:
39028d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
39038d59e960SJed Brown 
39048d59e960SJed Brown    Level: intermediate
39058d59e960SJed Brown 
39068d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
39078d59e960SJed Brown @*/
39088d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
39098d59e960SJed Brown {
39108d59e960SJed Brown   PetscFunctionBegin;
39118d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39128d59e960SJed Brown   ts->cfltime_local = cfltime;
39138d59e960SJed Brown   ts->cfltime = -1.;
39148d59e960SJed Brown   PetscFunctionReturn(0);
39158d59e960SJed Brown }
39168d59e960SJed Brown 
39178d59e960SJed Brown #undef __FUNCT__
39188d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
39198d59e960SJed Brown /*@
39208d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
39218d59e960SJed Brown 
39228d59e960SJed Brown    Collective on TS
39238d59e960SJed Brown 
39248d59e960SJed Brown    Input Arguments:
39258d59e960SJed Brown .  ts - time stepping context
39268d59e960SJed Brown 
39278d59e960SJed Brown    Output Arguments:
39288d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
39298d59e960SJed Brown 
39308d59e960SJed Brown    Level: advanced
39318d59e960SJed Brown 
39328d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
39338d59e960SJed Brown @*/
39348d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
39358d59e960SJed Brown {
39368d59e960SJed Brown   PetscErrorCode ierr;
39378d59e960SJed Brown 
39388d59e960SJed Brown   PetscFunctionBegin;
39398d59e960SJed Brown   if (ts->cfltime < 0) {
39408d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
39418d59e960SJed Brown   }
39428d59e960SJed Brown   *cfltime = ts->cfltime;
39438d59e960SJed Brown   PetscFunctionReturn(0);
39448d59e960SJed Brown }
39458d59e960SJed Brown 
39468d59e960SJed Brown #undef __FUNCT__
3947d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
3948d6ebe24aSShri Abhyankar /*@
3949d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
3950d6ebe24aSShri Abhyankar 
3951d6ebe24aSShri Abhyankar    Input Parameters:
3952d6ebe24aSShri Abhyankar .  ts   - the TS context.
3953d6ebe24aSShri Abhyankar .  xl   - lower bound.
3954d6ebe24aSShri Abhyankar .  xu   - upper bound.
3955d6ebe24aSShri Abhyankar 
3956d6ebe24aSShri Abhyankar    Notes:
3957d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
395833c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
3959d6ebe24aSShri Abhyankar 
39602bd2b0e6SSatish Balay    Level: advanced
39612bd2b0e6SSatish Balay 
3962d6ebe24aSShri Abhyankar @*/
3963d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
3964d6ebe24aSShri Abhyankar {
3965d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
3966d6ebe24aSShri Abhyankar   SNES           snes;
3967d6ebe24aSShri Abhyankar 
3968d6ebe24aSShri Abhyankar   PetscFunctionBegin;
3969d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3970d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
3971d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
3972d6ebe24aSShri Abhyankar }
3973d6ebe24aSShri Abhyankar 
3974325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3975c6db04a5SJed Brown #include <mex.h>
3976325fc9f4SBarry Smith 
3977325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
3978325fc9f4SBarry Smith 
3979325fc9f4SBarry Smith #undef __FUNCT__
3980325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
3981325fc9f4SBarry Smith /*
3982325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
3983325fc9f4SBarry Smith                          TSSetFunctionMatlab().
3984325fc9f4SBarry Smith 
3985325fc9f4SBarry Smith    Collective on TS
3986325fc9f4SBarry Smith 
3987325fc9f4SBarry Smith    Input Parameters:
3988325fc9f4SBarry Smith +  snes - the TS context
39890910c330SBarry Smith -  u - input vector
3990325fc9f4SBarry Smith 
3991325fc9f4SBarry Smith    Output Parameter:
3992325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
3993325fc9f4SBarry Smith 
3994325fc9f4SBarry Smith    Notes:
3995325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
3996325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
3997325fc9f4SBarry Smith    themselves.
3998325fc9f4SBarry Smith 
3999325fc9f4SBarry Smith    Level: developer
4000325fc9f4SBarry Smith 
4001325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4002325fc9f4SBarry Smith 
4003325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4004325fc9f4SBarry Smith */
40050910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4006325fc9f4SBarry Smith {
4007325fc9f4SBarry Smith   PetscErrorCode   ierr;
4008325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
4009325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
4010325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
4011325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
4012325fc9f4SBarry Smith 
4013325fc9f4SBarry Smith   PetscFunctionBegin;
4014325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
40150910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
40160910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4017325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
40180910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4019325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4020325fc9f4SBarry Smith 
4021325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
40220910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
40230910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
40240910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4025325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4026325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4027325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4028325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4029325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4030325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4031325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4032325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4033325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4034325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4035325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4036325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4037325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4038325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4039325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4040325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4041325fc9f4SBarry Smith   PetscFunctionReturn(0);
4042325fc9f4SBarry Smith }
4043325fc9f4SBarry Smith 
4044325fc9f4SBarry Smith 
4045325fc9f4SBarry Smith #undef __FUNCT__
4046325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4047325fc9f4SBarry Smith /*
4048325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4049325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4050e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4051325fc9f4SBarry Smith 
4052325fc9f4SBarry Smith    Logically Collective on TS
4053325fc9f4SBarry Smith 
4054325fc9f4SBarry Smith    Input Parameters:
4055325fc9f4SBarry Smith +  ts - the TS context
4056325fc9f4SBarry Smith -  func - function evaluation routine
4057325fc9f4SBarry Smith 
4058325fc9f4SBarry Smith    Calling sequence of func:
40590910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4060325fc9f4SBarry Smith 
4061325fc9f4SBarry Smith    Level: beginner
4062325fc9f4SBarry Smith 
4063325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4064325fc9f4SBarry Smith 
4065325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4066325fc9f4SBarry Smith */
4067cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4068325fc9f4SBarry Smith {
4069325fc9f4SBarry Smith   PetscErrorCode  ierr;
4070325fc9f4SBarry Smith   TSMatlabContext *sctx;
4071325fc9f4SBarry Smith 
4072325fc9f4SBarry Smith   PetscFunctionBegin;
4073325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4074325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4075325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4076325fc9f4SBarry Smith   /*
4077325fc9f4SBarry Smith      This should work, but it doesn't
4078325fc9f4SBarry Smith   sctx->ctx = ctx;
4079325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4080325fc9f4SBarry Smith   */
4081325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4082cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4083325fc9f4SBarry Smith   PetscFunctionReturn(0);
4084325fc9f4SBarry Smith }
4085325fc9f4SBarry Smith 
4086325fc9f4SBarry Smith #undef __FUNCT__
4087325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4088325fc9f4SBarry Smith /*
4089325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4090325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4091325fc9f4SBarry Smith 
4092325fc9f4SBarry Smith    Collective on TS
4093325fc9f4SBarry Smith 
4094325fc9f4SBarry Smith    Input Parameters:
4095cdcf91faSSean Farley +  ts - the TS context
40960910c330SBarry Smith .  u - input vector
4097325fc9f4SBarry Smith .  A, B - the matrices
4098325fc9f4SBarry Smith -  ctx - user context
4099325fc9f4SBarry Smith 
4100325fc9f4SBarry Smith    Output Parameter:
4101325fc9f4SBarry Smith .  flag - structure of the matrix
4102325fc9f4SBarry Smith 
4103325fc9f4SBarry Smith    Level: developer
4104325fc9f4SBarry Smith 
4105325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4106325fc9f4SBarry Smith 
4107325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4108325fc9f4SBarry Smith @*/
41090910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4110325fc9f4SBarry Smith {
4111325fc9f4SBarry Smith   PetscErrorCode  ierr;
4112325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
4113325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
4114325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4115325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4116325fc9f4SBarry Smith 
4117325fc9f4SBarry Smith   PetscFunctionBegin;
4118cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41190910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4120325fc9f4SBarry Smith 
41210910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4122325fc9f4SBarry Smith 
4123cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
41240910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
41250910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
41260910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
41270910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4128325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4129325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4130325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4131325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4132325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4133325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4134325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4135325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4136325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4137325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4138325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4139325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4140325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4141325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4142325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4143325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4144325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4145325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4146325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4147325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4148325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4149325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4150325fc9f4SBarry Smith   PetscFunctionReturn(0);
4151325fc9f4SBarry Smith }
4152325fc9f4SBarry Smith 
4153325fc9f4SBarry Smith 
4154325fc9f4SBarry Smith #undef __FUNCT__
4155325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4156325fc9f4SBarry Smith /*
4157325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4158e3c5b3baSBarry 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
4159325fc9f4SBarry Smith 
4160325fc9f4SBarry Smith    Logically Collective on TS
4161325fc9f4SBarry Smith 
4162325fc9f4SBarry Smith    Input Parameters:
4163cdcf91faSSean Farley +  ts - the TS context
4164325fc9f4SBarry Smith .  A,B - Jacobian matrices
4165325fc9f4SBarry Smith .  func - function evaluation routine
4166325fc9f4SBarry Smith -  ctx - user context
4167325fc9f4SBarry Smith 
4168325fc9f4SBarry Smith    Calling sequence of func:
41690910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4170325fc9f4SBarry Smith 
4171325fc9f4SBarry Smith 
4172325fc9f4SBarry Smith    Level: developer
4173325fc9f4SBarry Smith 
4174325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4175325fc9f4SBarry Smith 
4176325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4177325fc9f4SBarry Smith */
4178cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4179325fc9f4SBarry Smith {
4180325fc9f4SBarry Smith   PetscErrorCode    ierr;
4181325fc9f4SBarry Smith   TSMatlabContext *sctx;
4182325fc9f4SBarry Smith 
4183325fc9f4SBarry Smith   PetscFunctionBegin;
4184325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4185325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4186325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4187325fc9f4SBarry Smith   /*
4188325fc9f4SBarry Smith      This should work, but it doesn't
4189325fc9f4SBarry Smith   sctx->ctx = ctx;
4190325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4191325fc9f4SBarry Smith   */
4192325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4193cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4194325fc9f4SBarry Smith   PetscFunctionReturn(0);
4195325fc9f4SBarry Smith }
4196325fc9f4SBarry Smith 
4197b5b1a830SBarry Smith #undef __FUNCT__
4198b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4199b5b1a830SBarry Smith /*
4200b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4201b5b1a830SBarry Smith 
4202b5b1a830SBarry Smith    Collective on TS
4203b5b1a830SBarry Smith 
4204b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4205b5b1a830SBarry Smith @*/
42060910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4207b5b1a830SBarry Smith {
4208b5b1a830SBarry Smith   PetscErrorCode  ierr;
4209b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
4210a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
4211b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4212b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4213b5b1a830SBarry Smith 
4214b5b1a830SBarry Smith   PetscFunctionBegin;
4215cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42160910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4217b5b1a830SBarry Smith 
4218cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
42190910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4220b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4221b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4222b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4223b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4224b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4225b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4226b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4227b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4228b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4229b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4230b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4231b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4232b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4233b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4234b5b1a830SBarry Smith   PetscFunctionReturn(0);
4235b5b1a830SBarry Smith }
4236b5b1a830SBarry Smith 
4237b5b1a830SBarry Smith 
4238b5b1a830SBarry Smith #undef __FUNCT__
4239b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4240b5b1a830SBarry Smith /*
4241b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4242b5b1a830SBarry Smith 
4243b5b1a830SBarry Smith    Level: developer
4244b5b1a830SBarry Smith 
4245b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4246b5b1a830SBarry Smith 
4247b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4248b5b1a830SBarry Smith */
4249cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4250b5b1a830SBarry Smith {
4251b5b1a830SBarry Smith   PetscErrorCode    ierr;
4252b5b1a830SBarry Smith   TSMatlabContext *sctx;
4253b5b1a830SBarry Smith 
4254b5b1a830SBarry Smith   PetscFunctionBegin;
4255b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4256b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4257b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4258b5b1a830SBarry Smith   /*
4259b5b1a830SBarry Smith      This should work, but it doesn't
4260b5b1a830SBarry Smith   sctx->ctx = ctx;
4261b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4262b5b1a830SBarry Smith   */
4263b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4264cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
4265b5b1a830SBarry Smith   PetscFunctionReturn(0);
4266b5b1a830SBarry Smith }
4267325fc9f4SBarry Smith #endif
4268b3603a34SBarry Smith 
42690b039ecaSBarry Smith 
42700b039ecaSBarry Smith 
4271b3603a34SBarry Smith #undef __FUNCT__
42724f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4273b3603a34SBarry Smith /*@C
42744f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4275b3603a34SBarry Smith        in a time based line graph
4276b3603a34SBarry Smith 
4277b3603a34SBarry Smith    Collective on TS
4278b3603a34SBarry Smith 
4279b3603a34SBarry Smith    Input Parameters:
4280b3603a34SBarry Smith +  ts - the TS context
4281b3603a34SBarry Smith .  step - current time-step
4282b3603a34SBarry Smith .  ptime - current time
4283b3603a34SBarry Smith -  lg - a line graph object
4284b3603a34SBarry Smith 
4285b3603a34SBarry Smith    Level: intermediate
4286b3603a34SBarry Smith 
42870b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4288b3603a34SBarry Smith 
4289b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4290b3603a34SBarry Smith 
4291b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4292b3603a34SBarry Smith @*/
42930910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4294b3603a34SBarry Smith {
4295b3603a34SBarry Smith   PetscErrorCode    ierr;
42960b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4297b3603a34SBarry Smith   const PetscScalar *yy;
429858ff32f7SBarry Smith   PetscInt          dim;
4299b3603a34SBarry Smith 
4300b3603a34SBarry Smith   PetscFunctionBegin;
430158ff32f7SBarry Smith   if (!step) {
4302a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4303a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4304a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
43050910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
43060b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
43070b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
430858ff32f7SBarry Smith   }
43090910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4310e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4311e3efe391SJed Brown   {
4312e3efe391SJed Brown     PetscReal *yreal;
4313e3efe391SJed Brown     PetscInt  i,n;
43140910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4315e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4316e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
43170b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4318e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4319e3efe391SJed Brown   }
4320e3efe391SJed Brown #else
43210b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4322e3efe391SJed Brown #endif
43230910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
43243fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
43250b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43263923b477SBarry Smith   }
4327b3603a34SBarry Smith   PetscFunctionReturn(0);
4328b3603a34SBarry Smith }
4329b3603a34SBarry Smith 
4330b3603a34SBarry Smith #undef __FUNCT__
43314f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4332ef20d060SBarry Smith /*@C
43334f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4334ef20d060SBarry Smith        in a time based line graph
4335ef20d060SBarry Smith 
4336ef20d060SBarry Smith    Collective on TS
4337ef20d060SBarry Smith 
4338ef20d060SBarry Smith    Input Parameters:
4339ef20d060SBarry Smith +  ts - the TS context
4340ef20d060SBarry Smith .  step - current time-step
4341ef20d060SBarry Smith .  ptime - current time
4342ef20d060SBarry Smith -  lg - a line graph object
4343ef20d060SBarry Smith 
4344ef20d060SBarry Smith    Level: intermediate
4345ef20d060SBarry Smith 
4346abd5a294SJed Brown    Notes:
4347abd5a294SJed Brown    Only for sequential solves.
4348abd5a294SJed Brown 
4349abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4350abd5a294SJed Brown 
4351abd5a294SJed Brown    Options Database Keys:
43524f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4353ef20d060SBarry Smith 
4354ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4355ef20d060SBarry Smith 
4356abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4357ef20d060SBarry Smith @*/
43580910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4359ef20d060SBarry Smith {
4360ef20d060SBarry Smith   PetscErrorCode    ierr;
43610b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4362ef20d060SBarry Smith   const PetscScalar *yy;
4363ef20d060SBarry Smith   Vec               y;
4364a9f9c1f6SBarry Smith   PetscInt          dim;
4365ef20d060SBarry Smith 
4366ef20d060SBarry Smith   PetscFunctionBegin;
4367a9f9c1f6SBarry Smith   if (!step) {
4368a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4369a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
43701ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
43710910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4372a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4373a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4374a9f9c1f6SBarry Smith   }
43750910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4376ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
43770910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4378ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4379e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4380e3efe391SJed Brown   {
4381e3efe391SJed Brown     PetscReal *yreal;
4382e3efe391SJed Brown     PetscInt  i,n;
4383e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4384e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4385e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
43860b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4387e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4388e3efe391SJed Brown   }
4389e3efe391SJed Brown #else
43900b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4391e3efe391SJed Brown #endif
4392ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4393ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
43943fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
43950b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43963923b477SBarry Smith   }
4397ef20d060SBarry Smith   PetscFunctionReturn(0);
4398ef20d060SBarry Smith }
4399ef20d060SBarry Smith 
4400201da799SBarry Smith #undef __FUNCT__
4401201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4402201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4403201da799SBarry Smith {
4404201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4405201da799SBarry Smith   PetscReal      x = ptime,y;
4406201da799SBarry Smith   PetscErrorCode ierr;
4407201da799SBarry Smith   PetscInt       its;
4408201da799SBarry Smith 
4409201da799SBarry Smith   PetscFunctionBegin;
4410201da799SBarry Smith   if (!n) {
4411201da799SBarry Smith     PetscDrawAxis  axis;
4412201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4413201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4414201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4415201da799SBarry Smith     ctx->snes_its  = 0;
4416201da799SBarry Smith   }
4417201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4418201da799SBarry Smith   y    = its - ctx->snes_its;
4419201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
44203fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){
4421201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4422201da799SBarry Smith   }
4423201da799SBarry Smith   ctx->snes_its = its;
4424201da799SBarry Smith   PetscFunctionReturn(0);
4425201da799SBarry Smith }
4426201da799SBarry Smith 
4427201da799SBarry Smith #undef __FUNCT__
4428201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4429201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4430201da799SBarry Smith {
4431201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4432201da799SBarry Smith   PetscReal      x = ptime,y;
4433201da799SBarry Smith   PetscErrorCode ierr;
4434201da799SBarry Smith   PetscInt       its;
4435201da799SBarry Smith 
4436201da799SBarry Smith   PetscFunctionBegin;
4437201da799SBarry Smith   if (!n) {
4438201da799SBarry Smith     PetscDrawAxis  axis;
4439201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4440201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4441201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4442201da799SBarry Smith     ctx->ksp_its = 0;
4443201da799SBarry Smith   }
4444201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4445201da799SBarry Smith   y    = its - ctx->ksp_its;
4446201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
444799fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){
4448201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4449201da799SBarry Smith   }
4450201da799SBarry Smith   ctx->ksp_its = its;
4451201da799SBarry Smith   PetscFunctionReturn(0);
4452201da799SBarry Smith }
4453f9c1d6abSBarry Smith 
4454f9c1d6abSBarry Smith #undef __FUNCT__
4455f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4456f9c1d6abSBarry Smith /*@
4457f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4458f9c1d6abSBarry Smith 
4459f9c1d6abSBarry Smith    Collective on TS and Vec
4460f9c1d6abSBarry Smith 
4461f9c1d6abSBarry Smith    Input Parameters:
4462f9c1d6abSBarry Smith +  ts - the TS context
4463f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4464f9c1d6abSBarry Smith 
4465f9c1d6abSBarry Smith    Output Parameters:
4466f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4467f9c1d6abSBarry Smith 
4468f9c1d6abSBarry Smith    Level: developer
4469f9c1d6abSBarry Smith 
4470f9c1d6abSBarry Smith .keywords: TS, compute
4471f9c1d6abSBarry Smith 
4472f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4473f9c1d6abSBarry Smith @*/
4474f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4475f9c1d6abSBarry Smith {
4476f9c1d6abSBarry Smith   PetscErrorCode ierr;
4477f9c1d6abSBarry Smith 
4478f9c1d6abSBarry Smith   PetscFunctionBegin;
4479f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4480f9c1d6abSBarry Smith   if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4481f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4482f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4483f9c1d6abSBarry Smith }
4484