xref: /petsc/src/ts/interface/ts.c (revision db4deed73f06ae556a0f2e02a0fd6e016e156849)
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
76e817cc15SEmil Constantinescu -  -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__
1728deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1729deb2cd25SJed Brown /*@
1730deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1731deb2cd25SJed Brown 
1732deb2cd25SJed Brown    Collective
1733deb2cd25SJed Brown 
1734deb2cd25SJed Brown    Input Parameter:
1735deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1736deb2cd25SJed Brown -  snes - the nonlinear solver context
1737deb2cd25SJed Brown 
1738deb2cd25SJed Brown    Notes:
1739deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1740deb2cd25SJed Brown 
1741deb2cd25SJed Brown    Level: developer
1742deb2cd25SJed Brown 
1743deb2cd25SJed Brown .keywords: timestep, set, SNES
1744deb2cd25SJed Brown @*/
1745deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1746deb2cd25SJed Brown {
1747deb2cd25SJed Brown   PetscErrorCode ierr;
1748740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat *,Mat *,MatStructure *,void *);
1749deb2cd25SJed Brown 
1750deb2cd25SJed Brown   PetscFunctionBegin;
1751deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1752deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1753deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1754deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1755deb2cd25SJed Brown   ts->snes = snes;
1756740132f1SEmil Constantinescu   ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1757740132f1SEmil Constantinescu   ierr = SNESGetJacobian(ts->snes,PETSC_NULL,PETSC_NULL,&func,PETSC_NULL);CHKERRQ(ierr);
1758740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
1759740132f1SEmil Constantinescu     ierr = SNESSetJacobian(ts->snes,PETSC_NULL,PETSC_NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1760740132f1SEmil Constantinescu   }
1761deb2cd25SJed Brown   PetscFunctionReturn(0);
1762deb2cd25SJed Brown }
1763deb2cd25SJed Brown 
1764deb2cd25SJed Brown #undef __FUNCT__
176594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1766d8e5e3e6SSatish Balay /*@
176794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1768d763cef2SBarry Smith    a TS (timestepper) context.
1769d763cef2SBarry Smith 
177094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1771d763cef2SBarry Smith 
1772d763cef2SBarry Smith    Input Parameter:
1773d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1774d763cef2SBarry Smith 
1775d763cef2SBarry Smith    Output Parameter:
177694b7f48cSBarry Smith .  ksp - the nonlinear solver context
1777d763cef2SBarry Smith 
1778d763cef2SBarry Smith    Notes:
177994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1780d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1781d763cef2SBarry Smith    KSP and PC contexts as well.
1782d763cef2SBarry Smith 
178394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
178494b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1785d763cef2SBarry Smith 
1786d763cef2SBarry Smith    Level: beginner
1787d763cef2SBarry Smith 
178894b7f48cSBarry Smith .keywords: timestep, get, KSP
1789d763cef2SBarry Smith @*/
17907087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1791d763cef2SBarry Smith {
1792d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1793089b2837SJed Brown   SNES           snes;
1794d372ba47SLisandro Dalcin 
1795d763cef2SBarry Smith   PetscFunctionBegin;
17960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17974482741eSBarry Smith   PetscValidPointer(ksp,2);
179817186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1799e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1800089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1801089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1802d763cef2SBarry Smith   PetscFunctionReturn(0);
1803d763cef2SBarry Smith }
1804d763cef2SBarry Smith 
1805d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1806d763cef2SBarry Smith 
18074a2ae208SSatish Balay #undef __FUNCT__
1808adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1809adb62b0dSMatthew Knepley /*@
1810adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1811adb62b0dSMatthew Knepley    maximum time for iteration.
1812adb62b0dSMatthew Knepley 
18133f9fe445SBarry Smith    Not Collective
1814adb62b0dSMatthew Knepley 
1815adb62b0dSMatthew Knepley    Input Parameters:
1816adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1817adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1818adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1819adb62b0dSMatthew Knepley 
1820adb62b0dSMatthew Knepley    Level: intermediate
1821adb62b0dSMatthew Knepley 
1822adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1823adb62b0dSMatthew Knepley @*/
18247087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1825adb62b0dSMatthew Knepley {
1826adb62b0dSMatthew Knepley   PetscFunctionBegin;
18270700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1828abc0a331SBarry Smith   if (maxsteps) {
18294482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1830adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1831adb62b0dSMatthew Knepley   }
1832abc0a331SBarry Smith   if (maxtime) {
18334482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1834adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1835adb62b0dSMatthew Knepley   }
1836adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1837adb62b0dSMatthew Knepley }
1838adb62b0dSMatthew Knepley 
1839adb62b0dSMatthew Knepley #undef __FUNCT__
18404a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1841d763cef2SBarry Smith /*@
1842d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1843d763cef2SBarry Smith    maximum time for iteration.
1844d763cef2SBarry Smith 
18453f9fe445SBarry Smith    Logically Collective on TS
1846d763cef2SBarry Smith 
1847d763cef2SBarry Smith    Input Parameters:
1848d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1849d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1850d763cef2SBarry Smith -  maxtime - final time to iterate to
1851d763cef2SBarry Smith 
1852d763cef2SBarry Smith    Options Database Keys:
1853d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
18543bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1855d763cef2SBarry Smith 
1856d763cef2SBarry Smith    Notes:
1857d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1858d763cef2SBarry Smith 
1859d763cef2SBarry Smith    Level: intermediate
1860d763cef2SBarry Smith 
1861d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1862a43b19c4SJed Brown 
1863a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1864d763cef2SBarry Smith @*/
18657087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1866d763cef2SBarry Smith {
1867d763cef2SBarry Smith   PetscFunctionBegin;
18680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1869c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1870c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
187139b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
187239b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1873d763cef2SBarry Smith   PetscFunctionReturn(0);
1874d763cef2SBarry Smith }
1875d763cef2SBarry Smith 
18764a2ae208SSatish Balay #undef __FUNCT__
18774a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1878d763cef2SBarry Smith /*@
1879d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1880d763cef2SBarry Smith    for use by the TS routines.
1881d763cef2SBarry Smith 
18823f9fe445SBarry Smith    Logically Collective on TS and Vec
1883d763cef2SBarry Smith 
1884d763cef2SBarry Smith    Input Parameters:
1885d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
18860910c330SBarry Smith -  u - the solution vector
1887d763cef2SBarry Smith 
1888d763cef2SBarry Smith    Level: beginner
1889d763cef2SBarry Smith 
1890d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1891d763cef2SBarry Smith @*/
18920910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
1893d763cef2SBarry Smith {
18948737fe31SLisandro Dalcin   PetscErrorCode ierr;
1895496e6a7aSJed Brown   DM             dm;
18968737fe31SLisandro Dalcin 
1897d763cef2SBarry Smith   PetscFunctionBegin;
18980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18990910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
19000910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
19016bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
19020910c330SBarry Smith   ts->vec_sol = u;
1903496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
19040910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
1905d763cef2SBarry Smith   PetscFunctionReturn(0);
1906d763cef2SBarry Smith }
1907d763cef2SBarry Smith 
1908e74ef692SMatthew Knepley #undef __FUNCT__
1909e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1910ac226902SBarry Smith /*@C
1911000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
19123f2090d5SJed Brown   called once at the beginning of each time step.
1913000e7ae3SMatthew Knepley 
19143f9fe445SBarry Smith   Logically Collective on TS
1915000e7ae3SMatthew Knepley 
1916000e7ae3SMatthew Knepley   Input Parameters:
1917000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1918000e7ae3SMatthew Knepley - func - The function
1919000e7ae3SMatthew Knepley 
1920000e7ae3SMatthew Knepley   Calling sequence of func:
1921000e7ae3SMatthew Knepley . func (TS ts);
1922000e7ae3SMatthew Knepley 
1923000e7ae3SMatthew Knepley   Level: intermediate
1924000e7ae3SMatthew Knepley 
1925b8123daeSJed Brown   Note:
1926b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1927b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1928b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1929b8123daeSJed Brown 
1930000e7ae3SMatthew Knepley .keywords: TS, timestep
1931b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1932000e7ae3SMatthew Knepley @*/
19337087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1934000e7ae3SMatthew Knepley {
1935000e7ae3SMatthew Knepley   PetscFunctionBegin;
19360700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1937000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1938000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1939000e7ae3SMatthew Knepley }
1940000e7ae3SMatthew Knepley 
1941e74ef692SMatthew Knepley #undef __FUNCT__
19423f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
194309ee8438SJed Brown /*@
19443f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
19453f2090d5SJed Brown 
19463f2090d5SJed Brown   Collective on TS
19473f2090d5SJed Brown 
19483f2090d5SJed Brown   Input Parameters:
19493f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
19503f2090d5SJed Brown 
19513f2090d5SJed Brown   Notes:
19523f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
19533f2090d5SJed Brown   so most users would not generally call this routine themselves.
19543f2090d5SJed Brown 
19553f2090d5SJed Brown   Level: developer
19563f2090d5SJed Brown 
19573f2090d5SJed Brown .keywords: TS, timestep
1958b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
19593f2090d5SJed Brown @*/
19607087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
19613f2090d5SJed Brown {
19623f2090d5SJed Brown   PetscErrorCode ierr;
19633f2090d5SJed Brown 
19643f2090d5SJed Brown   PetscFunctionBegin;
19650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
196672ac3e02SJed Brown   if (ts->ops->prestep) {
19673f2090d5SJed Brown     PetscStackPush("TS PreStep function");
19683f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
19693f2090d5SJed Brown     PetscStackPop;
1970312ce896SJed Brown   }
19713f2090d5SJed Brown   PetscFunctionReturn(0);
19723f2090d5SJed Brown }
19733f2090d5SJed Brown 
19743f2090d5SJed Brown #undef __FUNCT__
1975b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1976b8123daeSJed Brown /*@C
1977b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1978b8123daeSJed Brown   called once at the beginning of each stage.
1979b8123daeSJed Brown 
1980b8123daeSJed Brown   Logically Collective on TS
1981b8123daeSJed Brown 
1982b8123daeSJed Brown   Input Parameters:
1983b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1984b8123daeSJed Brown - func - The function
1985b8123daeSJed Brown 
1986b8123daeSJed Brown   Calling sequence of func:
1987b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1988b8123daeSJed Brown 
1989b8123daeSJed Brown   Level: intermediate
1990b8123daeSJed Brown 
1991b8123daeSJed Brown   Note:
1992b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
1993b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
1994b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
1995b8123daeSJed Brown 
1996b8123daeSJed Brown .keywords: TS, timestep
1997b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
1998b8123daeSJed Brown @*/
1999b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2000b8123daeSJed Brown {
2001b8123daeSJed Brown   PetscFunctionBegin;
2002b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2003b8123daeSJed Brown   ts->ops->prestage = func;
2004b8123daeSJed Brown   PetscFunctionReturn(0);
2005b8123daeSJed Brown }
2006b8123daeSJed Brown 
2007b8123daeSJed Brown #undef __FUNCT__
2008b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2009b8123daeSJed Brown /*@
2010b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2011b8123daeSJed Brown 
2012b8123daeSJed Brown   Collective on TS
2013b8123daeSJed Brown 
2014b8123daeSJed Brown   Input Parameters:
2015b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2016b8123daeSJed Brown 
2017b8123daeSJed Brown   Notes:
2018b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2019b8123daeSJed Brown   most users would not generally call this routine themselves.
2020b8123daeSJed Brown 
2021b8123daeSJed Brown   Level: developer
2022b8123daeSJed Brown 
2023b8123daeSJed Brown .keywords: TS, timestep
2024b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2025b8123daeSJed Brown @*/
2026b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2027b8123daeSJed Brown {
2028b8123daeSJed Brown   PetscErrorCode ierr;
2029b8123daeSJed Brown 
2030b8123daeSJed Brown   PetscFunctionBegin;
2031b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2032b8123daeSJed Brown   if (ts->ops->prestage) {
2033b8123daeSJed Brown     PetscStackPush("TS PreStage function");
2034b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
2035b8123daeSJed Brown     PetscStackPop;
2036b8123daeSJed Brown   }
2037b8123daeSJed Brown   PetscFunctionReturn(0);
2038b8123daeSJed Brown }
2039b8123daeSJed Brown 
2040b8123daeSJed Brown #undef __FUNCT__
2041e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2042ac226902SBarry Smith /*@C
2043000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
20443f2090d5SJed Brown   called once at the end of each time step.
2045000e7ae3SMatthew Knepley 
20463f9fe445SBarry Smith   Logically Collective on TS
2047000e7ae3SMatthew Knepley 
2048000e7ae3SMatthew Knepley   Input Parameters:
2049000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2050000e7ae3SMatthew Knepley - func - The function
2051000e7ae3SMatthew Knepley 
2052000e7ae3SMatthew Knepley   Calling sequence of func:
2053b8123daeSJed Brown $ func (TS ts);
2054000e7ae3SMatthew Knepley 
2055000e7ae3SMatthew Knepley   Level: intermediate
2056000e7ae3SMatthew Knepley 
2057000e7ae3SMatthew Knepley .keywords: TS, timestep
2058b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2059000e7ae3SMatthew Knepley @*/
20607087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2061000e7ae3SMatthew Knepley {
2062000e7ae3SMatthew Knepley   PetscFunctionBegin;
20630700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2064000e7ae3SMatthew Knepley   ts->ops->poststep = func;
2065000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2066000e7ae3SMatthew Knepley }
2067000e7ae3SMatthew Knepley 
2068e74ef692SMatthew Knepley #undef __FUNCT__
20693f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
207009ee8438SJed Brown /*@
20713f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
20723f2090d5SJed Brown 
20733f2090d5SJed Brown   Collective on TS
20743f2090d5SJed Brown 
20753f2090d5SJed Brown   Input Parameters:
20763f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
20773f2090d5SJed Brown 
20783f2090d5SJed Brown   Notes:
20793f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
20803f2090d5SJed Brown   so most users would not generally call this routine themselves.
20813f2090d5SJed Brown 
20823f2090d5SJed Brown   Level: developer
20833f2090d5SJed Brown 
20843f2090d5SJed Brown .keywords: TS, timestep
20853f2090d5SJed Brown @*/
20867087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
20873f2090d5SJed Brown {
20883f2090d5SJed Brown   PetscErrorCode ierr;
20893f2090d5SJed Brown 
20903f2090d5SJed Brown   PetscFunctionBegin;
20910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
209272ac3e02SJed Brown   if (ts->ops->poststep) {
20933f2090d5SJed Brown     PetscStackPush("TS PostStep function");
20943f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
20953f2090d5SJed Brown     PetscStackPop;
209672ac3e02SJed Brown   }
20973f2090d5SJed Brown   PetscFunctionReturn(0);
20983f2090d5SJed Brown }
20993f2090d5SJed Brown 
2100d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2101d763cef2SBarry Smith 
21024a2ae208SSatish Balay #undef __FUNCT__
2103a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2104d763cef2SBarry Smith /*@C
2105a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2106d763cef2SBarry Smith    timestep to display the iteration's  progress.
2107d763cef2SBarry Smith 
21083f9fe445SBarry Smith    Logically Collective on TS
2109d763cef2SBarry Smith 
2110d763cef2SBarry Smith    Input Parameters:
2111d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2112e213d8f1SJed Brown .  monitor - monitoring routine
2113329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
2114b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
2115b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2116b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
2117d763cef2SBarry Smith 
2118e213d8f1SJed Brown    Calling sequence of monitor:
21190910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2120d763cef2SBarry Smith 
2121d763cef2SBarry Smith +    ts - the TS context
212288c05cc5SBarry 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
212388c05cc5SBarry Smith                                been interpolated to)
21241f06c33eSBarry Smith .    time - current time
21250910c330SBarry Smith .    u - current iterate
2126d763cef2SBarry Smith -    mctx - [optional] monitoring context
2127d763cef2SBarry Smith 
2128d763cef2SBarry Smith    Notes:
2129d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2130d763cef2SBarry Smith    already has been loaded.
2131d763cef2SBarry Smith 
2132025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2133025f1a04SBarry Smith 
2134d763cef2SBarry Smith    Level: intermediate
2135d763cef2SBarry Smith 
2136d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2137d763cef2SBarry Smith 
2138a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2139d763cef2SBarry Smith @*/
2140c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2141d763cef2SBarry Smith {
2142d763cef2SBarry Smith   PetscFunctionBegin;
21430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
214417186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2145d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
21468704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]    = mdestroy;
2147d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
2148d763cef2SBarry Smith   PetscFunctionReturn(0);
2149d763cef2SBarry Smith }
2150d763cef2SBarry Smith 
21514a2ae208SSatish Balay #undef __FUNCT__
2152a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2153d763cef2SBarry Smith /*@C
2154a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2155d763cef2SBarry Smith 
21563f9fe445SBarry Smith    Logically Collective on TS
2157d763cef2SBarry Smith 
2158d763cef2SBarry Smith    Input Parameters:
2159d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2160d763cef2SBarry Smith 
2161d763cef2SBarry Smith    Notes:
2162d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2163d763cef2SBarry Smith 
2164d763cef2SBarry Smith    Level: intermediate
2165d763cef2SBarry Smith 
2166d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2167d763cef2SBarry Smith 
2168a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2169d763cef2SBarry Smith @*/
21707087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2171d763cef2SBarry Smith {
2172d952e501SBarry Smith   PetscErrorCode ierr;
2173d952e501SBarry Smith   PetscInt       i;
2174d952e501SBarry Smith 
2175d763cef2SBarry Smith   PetscFunctionBegin;
21760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2177d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
21788704b422SBarry Smith     if (ts->monitordestroy[i]) {
21798704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2180d952e501SBarry Smith     }
2181d952e501SBarry Smith   }
2182d763cef2SBarry Smith   ts->numbermonitors = 0;
2183d763cef2SBarry Smith   PetscFunctionReturn(0);
2184d763cef2SBarry Smith }
2185d763cef2SBarry Smith 
21864a2ae208SSatish Balay #undef __FUNCT__
2187a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2188d8e5e3e6SSatish Balay /*@
2189a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
21905516499fSSatish Balay 
21915516499fSSatish Balay    Level: intermediate
219241251cbbSSatish Balay 
21935516499fSSatish Balay .keywords: TS, set, monitor
21945516499fSSatish Balay 
219541251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
219641251cbbSSatish Balay @*/
2197649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2198d763cef2SBarry Smith {
2199dfbe8321SBarry Smith   PetscErrorCode ierr;
2200649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
2201d132466eSBarry Smith 
2202d763cef2SBarry Smith   PetscFunctionBegin;
2203649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2204971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2205649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2206d763cef2SBarry Smith   PetscFunctionReturn(0);
2207d763cef2SBarry Smith }
2208d763cef2SBarry Smith 
22094a2ae208SSatish Balay #undef __FUNCT__
2210cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2211cd652676SJed Brown /*@
2212cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2213cd652676SJed Brown 
2214cd652676SJed Brown    Logically Collective on TS
2215cd652676SJed Brown 
2216cd652676SJed Brown    Input Argument:
2217cd652676SJed Brown .  ts - time stepping context
2218cd652676SJed Brown 
2219cd652676SJed Brown    Output Argument:
2220cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2221cd652676SJed Brown 
2222cd652676SJed Brown    Level: intermediate
2223cd652676SJed Brown 
2224cd652676SJed Brown .keywords: TS, set
2225cd652676SJed Brown 
2226cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2227cd652676SJed Brown @*/
2228cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2229cd652676SJed Brown {
2230cd652676SJed Brown   PetscFunctionBegin;
2231cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2232cd652676SJed Brown   ts->retain_stages = flg;
2233cd652676SJed Brown   PetscFunctionReturn(0);
2234cd652676SJed Brown }
2235cd652676SJed Brown 
2236cd652676SJed Brown #undef __FUNCT__
2237cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2238cd652676SJed Brown /*@
2239cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2240cd652676SJed Brown 
2241cd652676SJed Brown    Collective on TS
2242cd652676SJed Brown 
2243cd652676SJed Brown    Input Argument:
2244cd652676SJed Brown +  ts - time stepping context
2245cd652676SJed Brown -  t - time to interpolate to
2246cd652676SJed Brown 
2247cd652676SJed Brown    Output Argument:
22480910c330SBarry Smith .  U - state at given time
2249cd652676SJed Brown 
2250cd652676SJed Brown    Notes:
2251cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2252cd652676SJed Brown 
2253cd652676SJed Brown    Level: intermediate
2254cd652676SJed Brown 
2255cd652676SJed Brown    Developer Notes:
2256cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2257cd652676SJed Brown 
2258cd652676SJed Brown .keywords: TS, set
2259cd652676SJed Brown 
2260cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2261cd652676SJed Brown @*/
22620910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2263cd652676SJed Brown {
2264cd652676SJed Brown   PetscErrorCode ierr;
2265cd652676SJed Brown 
2266cd652676SJed Brown   PetscFunctionBegin;
2267cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2268d8cd7023SBarry 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);
2269cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
22700910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2271cd652676SJed Brown   PetscFunctionReturn(0);
2272cd652676SJed Brown }
2273cd652676SJed Brown 
2274cd652676SJed Brown #undef __FUNCT__
22754a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2276d763cef2SBarry Smith /*@
22776d9e5789SSean Farley    TSStep - Steps one time step
2278d763cef2SBarry Smith 
2279d763cef2SBarry Smith    Collective on TS
2280d763cef2SBarry Smith 
2281d763cef2SBarry Smith    Input Parameter:
2282d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2283d763cef2SBarry Smith 
22846d9e5789SSean Farley    Level: intermediate
2285d763cef2SBarry Smith 
2286b8123daeSJed Brown    Notes:
2287b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2288b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2289b8123daeSJed Brown 
229025cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
229125cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
229225cb2221SBarry Smith 
2293d763cef2SBarry Smith .keywords: TS, timestep, solve
2294d763cef2SBarry Smith 
229525cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2296d763cef2SBarry Smith @*/
2297193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2298d763cef2SBarry Smith {
2299362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2300dfbe8321SBarry Smith   PetscErrorCode ierr;
2301d763cef2SBarry Smith 
2302d763cef2SBarry Smith   PetscFunctionBegin;
23030700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2304d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2305d405a339SMatthew Knepley 
2306362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2307362cd11cSLisandro Dalcin 
2308362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2309d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2310193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2311d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2312362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2313362cd11cSLisandro Dalcin 
2314362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2315cef5090cSJed Brown     if (ts->errorifstepfailed) {
2316cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2317cef5090cSJed 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]);
2318cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2319cef5090cSJed Brown     }
2320362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2321*db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2322*db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2323362cd11cSLisandro Dalcin   }
2324362cd11cSLisandro Dalcin 
2325d763cef2SBarry Smith   PetscFunctionReturn(0);
2326d763cef2SBarry Smith }
2327d763cef2SBarry Smith 
23284a2ae208SSatish Balay #undef __FUNCT__
232905175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
233005175c85SJed Brown /*@
233105175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
233205175c85SJed Brown 
23331c3436cfSJed Brown    Collective on TS
233405175c85SJed Brown 
233505175c85SJed Brown    Input Arguments:
23361c3436cfSJed Brown +  ts - time stepping context
23371c3436cfSJed Brown .  order - desired order of accuracy
23381c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
233905175c85SJed Brown 
234005175c85SJed Brown    Output Arguments:
23410910c330SBarry Smith .  U - state at the end of the current step
234205175c85SJed Brown 
234305175c85SJed Brown    Level: advanced
234405175c85SJed Brown 
2345108c343cSJed Brown    Notes:
2346108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2347108c343cSJed 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.
2348108c343cSJed Brown 
23491c3436cfSJed Brown .seealso: TSStep(), TSAdapt
235005175c85SJed Brown @*/
23510910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
235205175c85SJed Brown {
235305175c85SJed Brown   PetscErrorCode ierr;
235405175c85SJed Brown 
235505175c85SJed Brown   PetscFunctionBegin;
235605175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
235705175c85SJed Brown   PetscValidType(ts,1);
23580910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
23591c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
23600910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
236105175c85SJed Brown   PetscFunctionReturn(0);
236205175c85SJed Brown }
236305175c85SJed Brown 
236405175c85SJed Brown #undef __FUNCT__
23656a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
23666a4d4014SLisandro Dalcin /*@
23676a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
23686a4d4014SLisandro Dalcin 
23696a4d4014SLisandro Dalcin    Collective on TS
23706a4d4014SLisandro Dalcin 
23716a4d4014SLisandro Dalcin    Input Parameter:
23726a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2373cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
23745a3a76d0SJed Brown 
23756a4d4014SLisandro Dalcin    Level: beginner
23766a4d4014SLisandro Dalcin 
23775a3a76d0SJed Brown    Notes:
23785a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
23795a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
23805a3a76d0SJed Brown    stepped over the final time.
23815a3a76d0SJed Brown 
23826a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
23836a4d4014SLisandro Dalcin 
23846a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
23856a4d4014SLisandro Dalcin @*/
2386cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
23876a4d4014SLisandro Dalcin {
23884d7d938eSLisandro Dalcin   PetscBool         flg;
23894d7d938eSLisandro Dalcin   PetscViewer       viewer;
23906a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2391cffb1e40SBarry Smith   PetscViewerFormat format;
2392f22f69f0SBarry Smith 
23936a4d4014SLisandro Dalcin   PetscFunctionBegin;
23940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2395f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
239649354f04SShri 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 */
23970910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
23985a3a76d0SJed Brown       Vec y;
23990910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
24005a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
24015a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
24025a3a76d0SJed Brown     }
2403f2c2a1b9SBarry Smith     if (u) {
24040910c330SBarry Smith       ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2405f2c2a1b9SBarry Smith     }
24065a3a76d0SJed Brown   } else {
2407f2c2a1b9SBarry Smith     if (u) {
24080910c330SBarry Smith       ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
24095a3a76d0SJed Brown     }
2410f2c2a1b9SBarry Smith   }
2411b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
24126a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2413193ac0bcSJed Brown   ts->steps = 0;
24145ef26d82SJed Brown   ts->ksp_its = 0;
24155ef26d82SJed Brown   ts->snes_its = 0;
2416c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2417c610991cSLisandro Dalcin   ts->reject = 0;
2418193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
2419193ac0bcSJed Brown 
2420193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2421193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
24220910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2423cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2424193ac0bcSJed Brown   } else {
24256a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2426362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2427*db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2428*db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2429e1a7a14fSJed Brown     while (!ts->reason) {
2430193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2431193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2432193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2433193ac0bcSJed Brown     }
243449354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
24350910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2436cc708dedSBarry Smith       ts->solvetime = ts->max_time;
24370574a7fbSJed Brown     } else {
2438ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2439cc708dedSBarry Smith       ts->solvetime = ts->ptime;
24400574a7fbSJed Brown     }
2441193ac0bcSJed Brown   }
24423923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2443cffb1e40SBarry Smith   ierr = PetscOptionsGetViewer(((PetscObject)ts)->comm,((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
24444d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2445cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
24464d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2447cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2448cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
24492b0a91c0SBarry Smith   }
24506a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
24516a4d4014SLisandro Dalcin }
24526a4d4014SLisandro Dalcin 
24536a4d4014SLisandro Dalcin #undef __FUNCT__
24544a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2455228d79bcSJed Brown /*@
2456228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2457228d79bcSJed Brown 
2458228d79bcSJed Brown    Collective on TS
2459228d79bcSJed Brown 
2460228d79bcSJed Brown    Input Parameters:
2461228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2462228d79bcSJed Brown .  step - step number that has just completed
2463228d79bcSJed Brown .  ptime - model time of the state
24640910c330SBarry Smith -  u - state at the current model time
2465228d79bcSJed Brown 
2466228d79bcSJed Brown    Notes:
2467228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2468228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2469228d79bcSJed Brown 
2470228d79bcSJed Brown    Level: advanced
2471228d79bcSJed Brown 
2472228d79bcSJed Brown .keywords: TS, timestep
2473228d79bcSJed Brown @*/
24740910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2475d763cef2SBarry Smith {
24766849ba73SBarry Smith   PetscErrorCode ierr;
2477a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2478d763cef2SBarry Smith 
2479d763cef2SBarry Smith   PetscFunctionBegin;
2480d763cef2SBarry Smith   for (i=0; i<n; i++) {
24810910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2482d763cef2SBarry Smith   }
2483d763cef2SBarry Smith   PetscFunctionReturn(0);
2484d763cef2SBarry Smith }
2485d763cef2SBarry Smith 
2486d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
24870b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
24880b039ecaSBarry Smith   PetscDrawLG lg;
24890b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2490201da799SBarry Smith   PetscInt    ksp_its,snes_its;
24910b039ecaSBarry Smith };
24920b039ecaSBarry Smith 
2493d763cef2SBarry Smith 
24944a2ae208SSatish Balay #undef __FUNCT__
2495a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2496d763cef2SBarry Smith /*@C
2497a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2498a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2499d763cef2SBarry Smith 
2500d763cef2SBarry Smith    Collective on TS
2501d763cef2SBarry Smith 
2502d763cef2SBarry Smith    Input Parameters:
2503d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2504d763cef2SBarry Smith .  label - the title to put in the title bar
25057c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2506a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2507a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2508d763cef2SBarry Smith 
2509d763cef2SBarry Smith    Output Parameter:
25100b039ecaSBarry Smith .  ctx - the context
2511d763cef2SBarry Smith 
2512d763cef2SBarry Smith    Options Database Key:
25134f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
25144f09c107SBarry Smith .  -ts_monitor_lg_solution -
251599fdda47SBarry Smith .  -ts_monitor_lg_error -
251699fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
251799fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
251899fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2519d763cef2SBarry Smith 
2520d763cef2SBarry Smith    Notes:
2521a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2522d763cef2SBarry Smith 
2523d763cef2SBarry Smith    Level: intermediate
2524d763cef2SBarry Smith 
25257c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2526d763cef2SBarry Smith 
25274f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
25287c922b88SBarry Smith 
2529d763cef2SBarry Smith @*/
2530a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2531d763cef2SBarry Smith {
2532b0a32e0cSBarry Smith   PetscDraw      win;
2533dfbe8321SBarry Smith   PetscErrorCode ierr;
253499fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2535d763cef2SBarry Smith 
2536d763cef2SBarry Smith   PetscFunctionBegin;
2537201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2538a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
25393fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
25400b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
254199fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr);
254299fdda47SBarry Smith   if (flg) {
25430b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
254499fdda47SBarry Smith   }
25450b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2546a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2547d763cef2SBarry Smith   PetscFunctionReturn(0);
2548d763cef2SBarry Smith }
2549d763cef2SBarry Smith 
25504a2ae208SSatish Balay #undef __FUNCT__
25514f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
25524f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2553d763cef2SBarry Smith {
25540b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2555c32365f1SBarry Smith   PetscReal      x = ptime,y;
2556dfbe8321SBarry Smith   PetscErrorCode ierr;
2557d763cef2SBarry Smith 
2558d763cef2SBarry Smith   PetscFunctionBegin;
2559a9f9c1f6SBarry Smith   if (!n) {
2560a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
2561a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2562a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2563a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2564a9f9c1f6SBarry Smith   }
2565c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
25660b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
256799fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){
25680b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
25693923b477SBarry Smith   }
2570d763cef2SBarry Smith   PetscFunctionReturn(0);
2571d763cef2SBarry Smith }
2572d763cef2SBarry Smith 
25734a2ae208SSatish Balay #undef __FUNCT__
2574a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2575d763cef2SBarry Smith /*@C
2576a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2577a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2578d763cef2SBarry Smith 
25790b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2580d763cef2SBarry Smith 
2581d763cef2SBarry Smith    Input Parameter:
25820b039ecaSBarry Smith .  ctx - the monitor context
2583d763cef2SBarry Smith 
2584d763cef2SBarry Smith    Level: intermediate
2585d763cef2SBarry Smith 
2586d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2587d763cef2SBarry Smith 
25884f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2589d763cef2SBarry Smith @*/
2590a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2591d763cef2SBarry Smith {
2592b0a32e0cSBarry Smith   PetscDraw      draw;
2593dfbe8321SBarry Smith   PetscErrorCode ierr;
2594d763cef2SBarry Smith 
2595d763cef2SBarry Smith   PetscFunctionBegin;
25960b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
25976bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
25980b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
25990b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2600d763cef2SBarry Smith   PetscFunctionReturn(0);
2601d763cef2SBarry Smith }
2602d763cef2SBarry Smith 
26034a2ae208SSatish Balay #undef __FUNCT__
26044a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2605d763cef2SBarry Smith /*@
2606b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2607d763cef2SBarry Smith 
2608d763cef2SBarry Smith    Not Collective
2609d763cef2SBarry Smith 
2610d763cef2SBarry Smith    Input Parameter:
2611d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2612d763cef2SBarry Smith 
2613d763cef2SBarry Smith    Output Parameter:
2614d763cef2SBarry Smith .  t  - the current time
2615d763cef2SBarry Smith 
2616d763cef2SBarry Smith    Level: beginner
2617d763cef2SBarry Smith 
2618b8123daeSJed Brown    Note:
2619b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2620b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2621b8123daeSJed Brown 
2622d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2623d763cef2SBarry Smith 
2624d763cef2SBarry Smith .keywords: TS, get, time
2625d763cef2SBarry Smith @*/
26267087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2627d763cef2SBarry Smith {
2628d763cef2SBarry Smith   PetscFunctionBegin;
26290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2630f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2631d763cef2SBarry Smith   *t = ts->ptime;
2632d763cef2SBarry Smith   PetscFunctionReturn(0);
2633d763cef2SBarry Smith }
2634d763cef2SBarry Smith 
26354a2ae208SSatish Balay #undef __FUNCT__
26366a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
26376a4d4014SLisandro Dalcin /*@
26386a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
26396a4d4014SLisandro Dalcin 
26403f9fe445SBarry Smith    Logically Collective on TS
26416a4d4014SLisandro Dalcin 
26426a4d4014SLisandro Dalcin    Input Parameters:
26436a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
26446a4d4014SLisandro Dalcin -  time - the time
26456a4d4014SLisandro Dalcin 
26466a4d4014SLisandro Dalcin    Level: intermediate
26476a4d4014SLisandro Dalcin 
26486a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
26496a4d4014SLisandro Dalcin 
26506a4d4014SLisandro Dalcin .keywords: TS, set, time
26516a4d4014SLisandro Dalcin @*/
26527087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
26536a4d4014SLisandro Dalcin {
26546a4d4014SLisandro Dalcin   PetscFunctionBegin;
26550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2656c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
26576a4d4014SLisandro Dalcin   ts->ptime = t;
26586a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
26596a4d4014SLisandro Dalcin }
26606a4d4014SLisandro Dalcin 
26616a4d4014SLisandro Dalcin #undef __FUNCT__
26624a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2663d763cef2SBarry Smith /*@C
2664d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2665d763cef2SBarry Smith    TS options in the database.
2666d763cef2SBarry Smith 
26673f9fe445SBarry Smith    Logically Collective on TS
2668d763cef2SBarry Smith 
2669d763cef2SBarry Smith    Input Parameter:
2670d763cef2SBarry Smith +  ts     - The TS context
2671d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2672d763cef2SBarry Smith 
2673d763cef2SBarry Smith    Notes:
2674d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2675d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2676d763cef2SBarry Smith    hyphen.
2677d763cef2SBarry Smith 
2678d763cef2SBarry Smith    Level: advanced
2679d763cef2SBarry Smith 
2680d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2681d763cef2SBarry Smith 
2682d763cef2SBarry Smith .seealso: TSSetFromOptions()
2683d763cef2SBarry Smith 
2684d763cef2SBarry Smith @*/
26857087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2686d763cef2SBarry Smith {
2687dfbe8321SBarry Smith   PetscErrorCode ierr;
2688089b2837SJed Brown   SNES           snes;
2689d763cef2SBarry Smith 
2690d763cef2SBarry Smith   PetscFunctionBegin;
26910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2692d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2693089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2694089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2695d763cef2SBarry Smith   PetscFunctionReturn(0);
2696d763cef2SBarry Smith }
2697d763cef2SBarry Smith 
2698d763cef2SBarry Smith 
26994a2ae208SSatish Balay #undef __FUNCT__
27004a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2701d763cef2SBarry Smith /*@C
2702d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2703d763cef2SBarry Smith    TS options in the database.
2704d763cef2SBarry Smith 
27053f9fe445SBarry Smith    Logically Collective on TS
2706d763cef2SBarry Smith 
2707d763cef2SBarry Smith    Input Parameter:
2708d763cef2SBarry Smith +  ts     - The TS context
2709d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2710d763cef2SBarry Smith 
2711d763cef2SBarry Smith    Notes:
2712d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2713d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2714d763cef2SBarry Smith    hyphen.
2715d763cef2SBarry Smith 
2716d763cef2SBarry Smith    Level: advanced
2717d763cef2SBarry Smith 
2718d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2719d763cef2SBarry Smith 
2720d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2721d763cef2SBarry Smith 
2722d763cef2SBarry Smith @*/
27237087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2724d763cef2SBarry Smith {
2725dfbe8321SBarry Smith   PetscErrorCode ierr;
2726089b2837SJed Brown   SNES           snes;
2727d763cef2SBarry Smith 
2728d763cef2SBarry Smith   PetscFunctionBegin;
27290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2730d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2731089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2732089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2733d763cef2SBarry Smith   PetscFunctionReturn(0);
2734d763cef2SBarry Smith }
2735d763cef2SBarry Smith 
27364a2ae208SSatish Balay #undef __FUNCT__
27374a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2738d763cef2SBarry Smith /*@C
2739d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2740d763cef2SBarry Smith    TS options in the database.
2741d763cef2SBarry Smith 
2742d763cef2SBarry Smith    Not Collective
2743d763cef2SBarry Smith 
2744d763cef2SBarry Smith    Input Parameter:
2745d763cef2SBarry Smith .  ts - The TS context
2746d763cef2SBarry Smith 
2747d763cef2SBarry Smith    Output Parameter:
2748d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2749d763cef2SBarry Smith 
2750d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2751d763cef2SBarry Smith    sufficient length to hold the prefix.
2752d763cef2SBarry Smith 
2753d763cef2SBarry Smith    Level: intermediate
2754d763cef2SBarry Smith 
2755d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2756d763cef2SBarry Smith 
2757d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2758d763cef2SBarry Smith @*/
27597087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2760d763cef2SBarry Smith {
2761dfbe8321SBarry Smith   PetscErrorCode ierr;
2762d763cef2SBarry Smith 
2763d763cef2SBarry Smith   PetscFunctionBegin;
27640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27654482741eSBarry Smith   PetscValidPointer(prefix,2);
2766d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2767d763cef2SBarry Smith   PetscFunctionReturn(0);
2768d763cef2SBarry Smith }
2769d763cef2SBarry Smith 
27704a2ae208SSatish Balay #undef __FUNCT__
27714a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2772d763cef2SBarry Smith /*@C
2773d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2774d763cef2SBarry Smith 
2775d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2776d763cef2SBarry Smith 
2777d763cef2SBarry Smith    Input Parameter:
2778d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2779d763cef2SBarry Smith 
2780d763cef2SBarry Smith    Output Parameters:
2781b5abc632SBarry Smith +  J   - The Jacobian J of F, where U_t = G(U,t)
2782d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2783089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2784d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2785d763cef2SBarry Smith 
2786d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2787d763cef2SBarry Smith 
2788d763cef2SBarry Smith    Level: intermediate
2789d763cef2SBarry Smith 
279026d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2791d763cef2SBarry Smith 
2792d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2793d763cef2SBarry Smith @*/
2794089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2795d763cef2SBarry Smith {
2796089b2837SJed Brown   PetscErrorCode ierr;
2797089b2837SJed Brown   SNES           snes;
279824989b8cSPeter Brune   DM             dm;
2799089b2837SJed Brown 
2800d763cef2SBarry Smith   PetscFunctionBegin;
2801089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2802089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
280324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
280424989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2805d763cef2SBarry Smith   PetscFunctionReturn(0);
2806d763cef2SBarry Smith }
2807d763cef2SBarry Smith 
28081713a123SBarry Smith #undef __FUNCT__
28092eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
28102eca1d9cSJed Brown /*@C
28112eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
28122eca1d9cSJed Brown 
28132eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
28142eca1d9cSJed Brown 
28152eca1d9cSJed Brown    Input Parameter:
28162eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
28172eca1d9cSJed Brown 
28182eca1d9cSJed Brown    Output Parameters:
28192eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
28202eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
28212eca1d9cSJed Brown .  f   - The function to compute the matrices
28222eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
28232eca1d9cSJed Brown 
28242eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
28252eca1d9cSJed Brown 
28262eca1d9cSJed Brown    Level: advanced
28272eca1d9cSJed Brown 
28282eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
28292eca1d9cSJed Brown 
28302eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
28312eca1d9cSJed Brown @*/
28327087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
28332eca1d9cSJed Brown {
2834089b2837SJed Brown   PetscErrorCode ierr;
2835089b2837SJed Brown   SNES           snes;
283624989b8cSPeter Brune   DM             dm;
28370910c330SBarry Smith 
28382eca1d9cSJed Brown   PetscFunctionBegin;
2839089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2840f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
2841089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
284224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
284324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
28442eca1d9cSJed Brown   PetscFunctionReturn(0);
28452eca1d9cSJed Brown }
28462eca1d9cSJed Brown 
284783a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
28486083293cSBarry Smith   PetscViewer viewer;
28496083293cSBarry Smith   Vec         initialsolution;
28506083293cSBarry Smith   PetscBool   showinitial;
285183a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
285283a4ac43SBarry Smith };
28536083293cSBarry Smith 
28542eca1d9cSJed Brown #undef __FUNCT__
285583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
28561713a123SBarry Smith /*@C
285783a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
28581713a123SBarry Smith    VecView() for the solution at each timestep
28591713a123SBarry Smith 
28601713a123SBarry Smith    Collective on TS
28611713a123SBarry Smith 
28621713a123SBarry Smith    Input Parameters:
28631713a123SBarry Smith +  ts - the TS context
28641713a123SBarry Smith .  step - current time-step
2865142b95e3SSatish Balay .  ptime - current time
28661713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
28671713a123SBarry Smith 
286899fdda47SBarry Smith    Options Database:
286999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
287099fdda47SBarry Smith 
287199fdda47SBarry 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
287299fdda47SBarry Smith        will look bad
287399fdda47SBarry Smith 
28741713a123SBarry Smith    Level: intermediate
28751713a123SBarry Smith 
28761713a123SBarry Smith .keywords: TS,  vector, monitor, view
28771713a123SBarry Smith 
2878a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
28791713a123SBarry Smith @*/
28800910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
28811713a123SBarry Smith {
2882dfbe8321SBarry Smith   PetscErrorCode   ierr;
288383a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
28841713a123SBarry Smith 
28851713a123SBarry Smith   PetscFunctionBegin;
28866083293cSBarry Smith   if (!step && ictx->showinitial) {
28876083293cSBarry Smith     if (!ictx->initialsolution) {
28880910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
28891713a123SBarry Smith     }
28900910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
28916083293cSBarry Smith   }
28923fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
28930dcf80beSBarry Smith 
28946083293cSBarry Smith   if (ictx->showinitial) {
28956083293cSBarry Smith     PetscReal pause;
28966083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
28976083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
28986083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
28996083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
29006083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
29016083293cSBarry Smith   }
29020910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
29036083293cSBarry Smith   if (ictx->showinitial) {
29046083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
29056083293cSBarry Smith   }
29061713a123SBarry Smith   PetscFunctionReturn(0);
29071713a123SBarry Smith }
29081713a123SBarry Smith 
29091713a123SBarry Smith 
29106c699258SBarry Smith #undef __FUNCT__
291183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
29126083293cSBarry Smith /*@C
291383a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
29146083293cSBarry Smith 
29156083293cSBarry Smith    Collective on TS
29166083293cSBarry Smith 
29176083293cSBarry Smith    Input Parameters:
29186083293cSBarry Smith .    ctx - the monitor context
29196083293cSBarry Smith 
29206083293cSBarry Smith    Level: intermediate
29216083293cSBarry Smith 
29226083293cSBarry Smith .keywords: TS,  vector, monitor, view
29236083293cSBarry Smith 
292483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
29256083293cSBarry Smith @*/
292683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
29276083293cSBarry Smith {
29286083293cSBarry Smith   PetscErrorCode       ierr;
29296083293cSBarry Smith 
29306083293cSBarry Smith   PetscFunctionBegin;
293183a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
293283a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
293383a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
29346083293cSBarry Smith   PetscFunctionReturn(0);
29356083293cSBarry Smith }
29366083293cSBarry Smith 
29376083293cSBarry Smith #undef __FUNCT__
293883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
29396083293cSBarry Smith /*@C
294083a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
29416083293cSBarry Smith 
29426083293cSBarry Smith    Collective on TS
29436083293cSBarry Smith 
29446083293cSBarry Smith    Input Parameter:
29456083293cSBarry Smith .    ts - time-step context
29466083293cSBarry Smith 
29476083293cSBarry Smith    Output Patameter:
29486083293cSBarry Smith .    ctx - the monitor context
29496083293cSBarry Smith 
295099fdda47SBarry Smith    Options Database:
295199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
295299fdda47SBarry Smith 
29536083293cSBarry Smith    Level: intermediate
29546083293cSBarry Smith 
29556083293cSBarry Smith .keywords: TS,  vector, monitor, view
29566083293cSBarry Smith 
295783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
29586083293cSBarry Smith @*/
295983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
29606083293cSBarry Smith {
29616083293cSBarry Smith   PetscErrorCode   ierr;
29626083293cSBarry Smith 
29636083293cSBarry Smith   PetscFunctionBegin;
296483a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
296583a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
296683a4ac43SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
296783a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
296899fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr);
29696083293cSBarry Smith   PetscFunctionReturn(0);
29706083293cSBarry Smith }
29716083293cSBarry Smith 
29726083293cSBarry Smith #undef __FUNCT__
297383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
29743a471f94SBarry Smith /*@C
297583a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
29763a471f94SBarry Smith    VecView() for the error at each timestep
29773a471f94SBarry Smith 
29783a471f94SBarry Smith    Collective on TS
29793a471f94SBarry Smith 
29803a471f94SBarry Smith    Input Parameters:
29813a471f94SBarry Smith +  ts - the TS context
29823a471f94SBarry Smith .  step - current time-step
29833a471f94SBarry Smith .  ptime - current time
29843a471f94SBarry Smith -  dummy - either a viewer or PETSC_NULL
29853a471f94SBarry Smith 
29863a471f94SBarry Smith    Level: intermediate
29873a471f94SBarry Smith 
29883a471f94SBarry Smith .keywords: TS,  vector, monitor, view
29893a471f94SBarry Smith 
29903a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
29913a471f94SBarry Smith @*/
29920910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
29933a471f94SBarry Smith {
29943a471f94SBarry Smith   PetscErrorCode   ierr;
299583a4ac43SBarry Smith   TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy;
299683a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
29973a471f94SBarry Smith   Vec              work;
29983a471f94SBarry Smith 
29993a471f94SBarry Smith   PetscFunctionBegin;
30003fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
30010910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
30023a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
30030910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
30043a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
30053a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
30063a471f94SBarry Smith   PetscFunctionReturn(0);
30073a471f94SBarry Smith }
30083a471f94SBarry Smith 
30092a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
30103a471f94SBarry Smith #undef __FUNCT__
30116c699258SBarry Smith #define __FUNCT__ "TSSetDM"
30126c699258SBarry Smith /*@
30136c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
30146c699258SBarry Smith 
30153f9fe445SBarry Smith    Logically Collective on TS and DM
30166c699258SBarry Smith 
30176c699258SBarry Smith    Input Parameters:
30186c699258SBarry Smith +  ts - the preconditioner context
30196c699258SBarry Smith -  dm - the dm
30206c699258SBarry Smith 
30216c699258SBarry Smith    Level: intermediate
30226c699258SBarry Smith 
30236c699258SBarry Smith 
30246c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
30256c699258SBarry Smith @*/
30267087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
30276c699258SBarry Smith {
30286c699258SBarry Smith   PetscErrorCode ierr;
3029089b2837SJed Brown   SNES           snes;
3030942e3340SBarry Smith   DMTS           tsdm;
30316c699258SBarry Smith 
30326c699258SBarry Smith   PetscFunctionBegin;
30330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
303470663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3035942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
30362a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3037942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3038942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
303924989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
304024989b8cSPeter Brune         tsdm->originaldm = dm;
304124989b8cSPeter Brune       }
304224989b8cSPeter Brune     }
30436bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
304424989b8cSPeter Brune   }
30456c699258SBarry Smith   ts->dm = dm;
3046089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3047089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
30486c699258SBarry Smith   PetscFunctionReturn(0);
30496c699258SBarry Smith }
30506c699258SBarry Smith 
30516c699258SBarry Smith #undef __FUNCT__
30526c699258SBarry Smith #define __FUNCT__ "TSGetDM"
30536c699258SBarry Smith /*@
30546c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
30556c699258SBarry Smith 
30563f9fe445SBarry Smith    Not Collective
30576c699258SBarry Smith 
30586c699258SBarry Smith    Input Parameter:
30596c699258SBarry Smith . ts - the preconditioner context
30606c699258SBarry Smith 
30616c699258SBarry Smith    Output Parameter:
30626c699258SBarry Smith .  dm - the dm
30636c699258SBarry Smith 
30646c699258SBarry Smith    Level: intermediate
30656c699258SBarry Smith 
30666c699258SBarry Smith 
30676c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
30686c699258SBarry Smith @*/
30697087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
30706c699258SBarry Smith {
3071496e6a7aSJed Brown   PetscErrorCode ierr;
3072496e6a7aSJed Brown 
30736c699258SBarry Smith   PetscFunctionBegin;
30740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3075496e6a7aSJed Brown   if (!ts->dm) {
3076496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
3077496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3078496e6a7aSJed Brown   }
30796c699258SBarry Smith   *dm = ts->dm;
30806c699258SBarry Smith   PetscFunctionReturn(0);
30816c699258SBarry Smith }
30821713a123SBarry Smith 
30830f5c6efeSJed Brown #undef __FUNCT__
30840f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
30850f5c6efeSJed Brown /*@
30860f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
30870f5c6efeSJed Brown 
30883f9fe445SBarry Smith    Logically Collective on SNES
30890f5c6efeSJed Brown 
30900f5c6efeSJed Brown    Input Parameter:
3091d42a1c89SJed Brown + snes - nonlinear solver
30920910c330SBarry Smith . U - the current state at which to evaluate the residual
3093d42a1c89SJed Brown - ctx - user context, must be a TS
30940f5c6efeSJed Brown 
30950f5c6efeSJed Brown    Output Parameter:
30960f5c6efeSJed Brown . F - the nonlinear residual
30970f5c6efeSJed Brown 
30980f5c6efeSJed Brown    Notes:
30990f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
31000f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
31010f5c6efeSJed Brown 
31020f5c6efeSJed Brown    Level: advanced
31030f5c6efeSJed Brown 
31040f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
31050f5c6efeSJed Brown @*/
31060910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
31070f5c6efeSJed Brown {
31080f5c6efeSJed Brown   TS             ts = (TS)ctx;
31090f5c6efeSJed Brown   PetscErrorCode ierr;
31100f5c6efeSJed Brown 
31110f5c6efeSJed Brown   PetscFunctionBegin;
31120f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31130910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
31140f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
31150f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
31160910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
31170f5c6efeSJed Brown   PetscFunctionReturn(0);
31180f5c6efeSJed Brown }
31190f5c6efeSJed Brown 
31200f5c6efeSJed Brown #undef __FUNCT__
31210f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
31220f5c6efeSJed Brown /*@
31230f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
31240f5c6efeSJed Brown 
31250f5c6efeSJed Brown    Collective on SNES
31260f5c6efeSJed Brown 
31270f5c6efeSJed Brown    Input Parameter:
31280f5c6efeSJed Brown + snes - nonlinear solver
31290910c330SBarry Smith . U - the current state at which to evaluate the residual
31300f5c6efeSJed Brown - ctx - user context, must be a TS
31310f5c6efeSJed Brown 
31320f5c6efeSJed Brown    Output Parameter:
31330f5c6efeSJed Brown + A - the Jacobian
31340f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
31350f5c6efeSJed Brown - flag - indicates any structure change in the matrix
31360f5c6efeSJed Brown 
31370f5c6efeSJed Brown    Notes:
31380f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
31390f5c6efeSJed Brown 
31400f5c6efeSJed Brown    Level: developer
31410f5c6efeSJed Brown 
31420f5c6efeSJed Brown .seealso: SNESSetJacobian()
31430f5c6efeSJed Brown @*/
31440910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
31450f5c6efeSJed Brown {
31460f5c6efeSJed Brown   TS             ts = (TS)ctx;
31470f5c6efeSJed Brown   PetscErrorCode ierr;
31480f5c6efeSJed Brown 
31490f5c6efeSJed Brown   PetscFunctionBegin;
31500f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31510910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
31520f5c6efeSJed Brown   PetscValidPointer(A,3);
31530f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
31540f5c6efeSJed Brown   PetscValidPointer(B,4);
31550f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
31560f5c6efeSJed Brown   PetscValidPointer(flag,5);
31570f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
31580910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
31590f5c6efeSJed Brown   PetscFunctionReturn(0);
31600f5c6efeSJed Brown }
3161325fc9f4SBarry Smith 
31620e4ef248SJed Brown #undef __FUNCT__
31630e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
31640e4ef248SJed Brown /*@C
31650e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
31660e4ef248SJed Brown 
31670e4ef248SJed Brown    Collective on TS
31680e4ef248SJed Brown 
31690e4ef248SJed Brown    Input Arguments:
31700e4ef248SJed Brown +  ts - time stepping context
31710e4ef248SJed Brown .  t - time at which to evaluate
31720910c330SBarry Smith .  U - state at which to evaluate
31730e4ef248SJed Brown -  ctx - context
31740e4ef248SJed Brown 
31750e4ef248SJed Brown    Output Arguments:
31760e4ef248SJed Brown .  F - right hand side
31770e4ef248SJed Brown 
31780e4ef248SJed Brown    Level: intermediate
31790e4ef248SJed Brown 
31800e4ef248SJed Brown    Notes:
31810e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
31820e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
31830e4ef248SJed Brown 
31840e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
31850e4ef248SJed Brown @*/
31860910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
31870e4ef248SJed Brown {
31880e4ef248SJed Brown   PetscErrorCode ierr;
31890e4ef248SJed Brown   Mat            Arhs,Brhs;
31900e4ef248SJed Brown   MatStructure   flg2;
31910e4ef248SJed Brown 
31920e4ef248SJed Brown   PetscFunctionBegin;
31930e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
31940910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
31950910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
31960e4ef248SJed Brown   PetscFunctionReturn(0);
31970e4ef248SJed Brown }
31980e4ef248SJed Brown 
31990e4ef248SJed Brown #undef __FUNCT__
32000e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
32010e4ef248SJed Brown /*@C
32020e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
32030e4ef248SJed Brown 
32040e4ef248SJed Brown    Collective on TS
32050e4ef248SJed Brown 
32060e4ef248SJed Brown    Input Arguments:
32070e4ef248SJed Brown +  ts - time stepping context
32080e4ef248SJed Brown .  t - time at which to evaluate
32090910c330SBarry Smith .  U - state at which to evaluate
32100e4ef248SJed Brown -  ctx - context
32110e4ef248SJed Brown 
32120e4ef248SJed Brown    Output Arguments:
32130e4ef248SJed Brown +  A - pointer to operator
32140e4ef248SJed Brown .  B - pointer to preconditioning matrix
32150e4ef248SJed Brown -  flg - matrix structure flag
32160e4ef248SJed Brown 
32170e4ef248SJed Brown    Level: intermediate
32180e4ef248SJed Brown 
32190e4ef248SJed Brown    Notes:
32200e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
32210e4ef248SJed Brown 
32220e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
32230e4ef248SJed Brown @*/
32240910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
32250e4ef248SJed Brown {
32260e4ef248SJed Brown   PetscFunctionBegin;
32270e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
32280e4ef248SJed Brown   PetscFunctionReturn(0);
32290e4ef248SJed Brown }
32300e4ef248SJed Brown 
32310026cea9SSean Farley #undef __FUNCT__
32320026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
32330026cea9SSean Farley /*@C
32340026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
32350026cea9SSean Farley 
32360026cea9SSean Farley    Collective on TS
32370026cea9SSean Farley 
32380026cea9SSean Farley    Input Arguments:
32390026cea9SSean Farley +  ts - time stepping context
32400026cea9SSean Farley .  t - time at which to evaluate
32410910c330SBarry Smith .  U - state at which to evaluate
32420910c330SBarry Smith .  Udot - time derivative of state vector
32430026cea9SSean Farley -  ctx - context
32440026cea9SSean Farley 
32450026cea9SSean Farley    Output Arguments:
32460026cea9SSean Farley .  F - left hand side
32470026cea9SSean Farley 
32480026cea9SSean Farley    Level: intermediate
32490026cea9SSean Farley 
32500026cea9SSean Farley    Notes:
32510910c330SBarry 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
32520026cea9SSean Farley    user is required to write their own TSComputeIFunction.
32530026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
32540026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
32550026cea9SSean Farley 
32560026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
32570026cea9SSean Farley @*/
32580910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
32590026cea9SSean Farley {
32600026cea9SSean Farley   PetscErrorCode ierr;
32610026cea9SSean Farley   Mat            A,B;
32620026cea9SSean Farley   MatStructure   flg2;
32630026cea9SSean Farley 
32640026cea9SSean Farley   PetscFunctionBegin;
32650026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
32660910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
32670910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
32680026cea9SSean Farley   PetscFunctionReturn(0);
32690026cea9SSean Farley }
32700026cea9SSean Farley 
32710026cea9SSean Farley #undef __FUNCT__
32720026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
32730026cea9SSean Farley /*@C
327424e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
32750026cea9SSean Farley 
32760026cea9SSean Farley    Collective on TS
32770026cea9SSean Farley 
32780026cea9SSean Farley    Input Arguments:
32790026cea9SSean Farley +  ts - time stepping context
32800026cea9SSean Farley .  t - time at which to evaluate
32810910c330SBarry Smith .  U - state at which to evaluate
32820910c330SBarry Smith .  Udot - time derivative of state vector
32830026cea9SSean Farley .  shift - shift to apply
32840026cea9SSean Farley -  ctx - context
32850026cea9SSean Farley 
32860026cea9SSean Farley    Output Arguments:
32870026cea9SSean Farley +  A - pointer to operator
32880026cea9SSean Farley .  B - pointer to preconditioning matrix
32890026cea9SSean Farley -  flg - matrix structure flag
32900026cea9SSean Farley 
32910026cea9SSean Farley    Level: intermediate
32920026cea9SSean Farley 
32930026cea9SSean Farley    Notes:
32940026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
32950026cea9SSean Farley 
32960026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
32970026cea9SSean Farley @*/
32980910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
32990026cea9SSean Farley {
33000026cea9SSean Farley   PetscFunctionBegin;
33010026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
33020026cea9SSean Farley   PetscFunctionReturn(0);
33030026cea9SSean Farley }
3304e817cc15SEmil Constantinescu #undef __FUNCT__
3305e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3306e817cc15SEmil Constantinescu /*@
3307e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3308e817cc15SEmil Constantinescu 
3309e817cc15SEmil Constantinescu    Not Collective
3310e817cc15SEmil Constantinescu 
3311e817cc15SEmil Constantinescu    Input Parameter:
3312e817cc15SEmil Constantinescu .  ts - the TS context
3313e817cc15SEmil Constantinescu 
3314e817cc15SEmil Constantinescu    Output Parameter:
3315e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3316e817cc15SEmil Constantinescu 
3317e817cc15SEmil Constantinescu    Level: beginner
3318e817cc15SEmil Constantinescu 
3319e817cc15SEmil Constantinescu .keywords: TS, equation type
3320e817cc15SEmil Constantinescu 
3321e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3322e817cc15SEmil Constantinescu @*/
3323e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3324e817cc15SEmil Constantinescu {
3325e817cc15SEmil Constantinescu   PetscFunctionBegin;
3326e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3327e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3328e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3329e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3330e817cc15SEmil Constantinescu }
3331e817cc15SEmil Constantinescu 
3332e817cc15SEmil Constantinescu #undef __FUNCT__
3333e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3334e817cc15SEmil Constantinescu /*@
3335e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3336e817cc15SEmil Constantinescu 
3337e817cc15SEmil Constantinescu    Not Collective
3338e817cc15SEmil Constantinescu 
3339e817cc15SEmil Constantinescu    Input Parameter:
3340e817cc15SEmil Constantinescu +  ts - the TS context
3341e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3342e817cc15SEmil Constantinescu 
3343e817cc15SEmil Constantinescu    Level: advanced
3344e817cc15SEmil Constantinescu 
3345e817cc15SEmil Constantinescu .keywords: TS, equation type
3346e817cc15SEmil Constantinescu 
3347e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3348e817cc15SEmil Constantinescu @*/
3349e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3350e817cc15SEmil Constantinescu {
3351e817cc15SEmil Constantinescu   PetscFunctionBegin;
3352e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3353e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3354e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3355e817cc15SEmil Constantinescu }
33560026cea9SSean Farley 
33574af1b03aSJed Brown #undef __FUNCT__
33584af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
33594af1b03aSJed Brown /*@
33604af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
33614af1b03aSJed Brown 
33624af1b03aSJed Brown    Not Collective
33634af1b03aSJed Brown 
33644af1b03aSJed Brown    Input Parameter:
33654af1b03aSJed Brown .  ts - the TS context
33664af1b03aSJed Brown 
33674af1b03aSJed Brown    Output Parameter:
33684af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
33694af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
33704af1b03aSJed Brown 
3371487e0bb9SJed Brown    Level: beginner
33724af1b03aSJed Brown 
3373cd652676SJed Brown    Notes:
3374cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
33754af1b03aSJed Brown 
33764af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
33774af1b03aSJed Brown 
33784af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
33794af1b03aSJed Brown @*/
33804af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
33814af1b03aSJed Brown {
33824af1b03aSJed Brown   PetscFunctionBegin;
33834af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33844af1b03aSJed Brown   PetscValidPointer(reason,2);
33854af1b03aSJed Brown   *reason = ts->reason;
33864af1b03aSJed Brown   PetscFunctionReturn(0);
33874af1b03aSJed Brown }
33884af1b03aSJed Brown 
3389fb1732b5SBarry Smith #undef __FUNCT__
3390d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3391d6ad946cSShri Abhyankar /*@
3392d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3393d6ad946cSShri Abhyankar 
3394d6ad946cSShri Abhyankar    Not Collective
3395d6ad946cSShri Abhyankar 
3396d6ad946cSShri Abhyankar    Input Parameter:
3397d6ad946cSShri Abhyankar +  ts - the TS context
3398d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3399d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3400d6ad946cSShri Abhyankar 
3401f5abba47SShri Abhyankar    Level: advanced
3402d6ad946cSShri Abhyankar 
3403d6ad946cSShri Abhyankar    Notes:
3404d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3405d6ad946cSShri Abhyankar 
3406d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3407d6ad946cSShri Abhyankar 
3408d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3409d6ad946cSShri Abhyankar @*/
3410d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3411d6ad946cSShri Abhyankar {
3412d6ad946cSShri Abhyankar   PetscFunctionBegin;
3413d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3414d6ad946cSShri Abhyankar   ts->reason = reason;
3415d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3416d6ad946cSShri Abhyankar }
3417d6ad946cSShri Abhyankar 
3418d6ad946cSShri Abhyankar #undef __FUNCT__
3419cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3420cc708dedSBarry Smith /*@
3421cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3422cc708dedSBarry Smith 
3423cc708dedSBarry Smith    Not Collective
3424cc708dedSBarry Smith 
3425cc708dedSBarry Smith    Input Parameter:
3426cc708dedSBarry Smith .  ts - the TS context
3427cc708dedSBarry Smith 
3428cc708dedSBarry Smith    Output Parameter:
3429cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3430cc708dedSBarry Smith 
3431487e0bb9SJed Brown    Level: beginner
3432cc708dedSBarry Smith 
3433cc708dedSBarry Smith    Notes:
3434cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3435cc708dedSBarry Smith 
3436cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3437cc708dedSBarry Smith 
3438cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3439cc708dedSBarry Smith @*/
3440cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3441cc708dedSBarry Smith {
3442cc708dedSBarry Smith   PetscFunctionBegin;
3443cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3444cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3445cc708dedSBarry Smith   *ftime = ts->solvetime;
3446cc708dedSBarry Smith   PetscFunctionReturn(0);
3447cc708dedSBarry Smith }
3448cc708dedSBarry Smith 
3449cc708dedSBarry Smith #undef __FUNCT__
34505ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
34519f67acb7SJed Brown /*@
34525ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
34539f67acb7SJed Brown    used by the time integrator.
34549f67acb7SJed Brown 
34559f67acb7SJed Brown    Not Collective
34569f67acb7SJed Brown 
34579f67acb7SJed Brown    Input Parameter:
34589f67acb7SJed Brown .  ts - TS context
34599f67acb7SJed Brown 
34609f67acb7SJed Brown    Output Parameter:
34619f67acb7SJed Brown .  nits - number of nonlinear iterations
34629f67acb7SJed Brown 
34639f67acb7SJed Brown    Notes:
34649f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
34659f67acb7SJed Brown 
34669f67acb7SJed Brown    Level: intermediate
34679f67acb7SJed Brown 
34689f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
34699f67acb7SJed Brown 
34705ef26d82SJed Brown .seealso:  TSGetKSPIterations()
34719f67acb7SJed Brown @*/
34725ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
34739f67acb7SJed Brown {
34749f67acb7SJed Brown   PetscFunctionBegin;
34759f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34769f67acb7SJed Brown   PetscValidIntPointer(nits,2);
34775ef26d82SJed Brown   *nits = ts->snes_its;
34789f67acb7SJed Brown   PetscFunctionReturn(0);
34799f67acb7SJed Brown }
34809f67acb7SJed Brown 
34819f67acb7SJed Brown #undef __FUNCT__
34825ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
34839f67acb7SJed Brown /*@
34845ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
34859f67acb7SJed Brown    used by the time integrator.
34869f67acb7SJed Brown 
34879f67acb7SJed Brown    Not Collective
34889f67acb7SJed Brown 
34899f67acb7SJed Brown    Input Parameter:
34909f67acb7SJed Brown .  ts - TS context
34919f67acb7SJed Brown 
34929f67acb7SJed Brown    Output Parameter:
34939f67acb7SJed Brown .  lits - number of linear iterations
34949f67acb7SJed Brown 
34959f67acb7SJed Brown    Notes:
34969f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
34979f67acb7SJed Brown 
34989f67acb7SJed Brown    Level: intermediate
34999f67acb7SJed Brown 
35009f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
35019f67acb7SJed Brown 
35025ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
35039f67acb7SJed Brown @*/
35045ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
35059f67acb7SJed Brown {
35069f67acb7SJed Brown   PetscFunctionBegin;
35079f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35089f67acb7SJed Brown   PetscValidIntPointer(lits,2);
35095ef26d82SJed Brown   *lits = ts->ksp_its;
35109f67acb7SJed Brown   PetscFunctionReturn(0);
35119f67acb7SJed Brown }
35129f67acb7SJed Brown 
35139f67acb7SJed Brown #undef __FUNCT__
3514cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3515cef5090cSJed Brown /*@
3516cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3517cef5090cSJed Brown 
3518cef5090cSJed Brown    Not Collective
3519cef5090cSJed Brown 
3520cef5090cSJed Brown    Input Parameter:
3521cef5090cSJed Brown .  ts - TS context
3522cef5090cSJed Brown 
3523cef5090cSJed Brown    Output Parameter:
3524cef5090cSJed Brown .  rejects - number of steps rejected
3525cef5090cSJed Brown 
3526cef5090cSJed Brown    Notes:
3527cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3528cef5090cSJed Brown 
3529cef5090cSJed Brown    Level: intermediate
3530cef5090cSJed Brown 
3531cef5090cSJed Brown .keywords: TS, get, number
3532cef5090cSJed Brown 
35335ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3534cef5090cSJed Brown @*/
3535cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3536cef5090cSJed Brown {
3537cef5090cSJed Brown   PetscFunctionBegin;
3538cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3539cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3540cef5090cSJed Brown   *rejects = ts->reject;
3541cef5090cSJed Brown   PetscFunctionReturn(0);
3542cef5090cSJed Brown }
3543cef5090cSJed Brown 
3544cef5090cSJed Brown #undef __FUNCT__
3545cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3546cef5090cSJed Brown /*@
3547cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3548cef5090cSJed Brown 
3549cef5090cSJed Brown    Not Collective
3550cef5090cSJed Brown 
3551cef5090cSJed Brown    Input Parameter:
3552cef5090cSJed Brown .  ts - TS context
3553cef5090cSJed Brown 
3554cef5090cSJed Brown    Output Parameter:
3555cef5090cSJed Brown .  fails - number of failed nonlinear solves
3556cef5090cSJed Brown 
3557cef5090cSJed Brown    Notes:
3558cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3559cef5090cSJed Brown 
3560cef5090cSJed Brown    Level: intermediate
3561cef5090cSJed Brown 
3562cef5090cSJed Brown .keywords: TS, get, number
3563cef5090cSJed Brown 
35645ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3565cef5090cSJed Brown @*/
3566cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3567cef5090cSJed Brown {
3568cef5090cSJed Brown   PetscFunctionBegin;
3569cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3570cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3571cef5090cSJed Brown   *fails = ts->num_snes_failures;
3572cef5090cSJed Brown   PetscFunctionReturn(0);
3573cef5090cSJed Brown }
3574cef5090cSJed Brown 
3575cef5090cSJed Brown #undef __FUNCT__
3576cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3577cef5090cSJed Brown /*@
3578cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3579cef5090cSJed Brown 
3580cef5090cSJed Brown    Not Collective
3581cef5090cSJed Brown 
3582cef5090cSJed Brown    Input Parameter:
3583cef5090cSJed Brown +  ts - TS context
3584cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3585cef5090cSJed Brown 
3586cef5090cSJed Brown    Notes:
3587cef5090cSJed Brown    The counter is reset to zero for each step
3588cef5090cSJed Brown 
3589cef5090cSJed Brown    Options Database Key:
3590cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3591cef5090cSJed Brown 
3592cef5090cSJed Brown    Level: intermediate
3593cef5090cSJed Brown 
3594cef5090cSJed Brown .keywords: TS, set, maximum, number
3595cef5090cSJed Brown 
35965ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3597cef5090cSJed Brown @*/
3598cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3599cef5090cSJed Brown {
3600cef5090cSJed Brown   PetscFunctionBegin;
3601cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3602cef5090cSJed Brown   ts->max_reject = rejects;
3603cef5090cSJed Brown   PetscFunctionReturn(0);
3604cef5090cSJed Brown }
3605cef5090cSJed Brown 
3606cef5090cSJed Brown #undef __FUNCT__
3607cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3608cef5090cSJed Brown /*@
3609cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3610cef5090cSJed Brown 
3611cef5090cSJed Brown    Not Collective
3612cef5090cSJed Brown 
3613cef5090cSJed Brown    Input Parameter:
3614cef5090cSJed Brown +  ts - TS context
3615cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3616cef5090cSJed Brown 
3617cef5090cSJed Brown    Notes:
3618cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3619cef5090cSJed Brown 
3620cef5090cSJed Brown    Options Database Key:
3621cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3622cef5090cSJed Brown 
3623cef5090cSJed Brown    Level: intermediate
3624cef5090cSJed Brown 
3625cef5090cSJed Brown .keywords: TS, set, maximum, number
3626cef5090cSJed Brown 
36275ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3628cef5090cSJed Brown @*/
3629cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3630cef5090cSJed Brown {
3631cef5090cSJed Brown   PetscFunctionBegin;
3632cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3633cef5090cSJed Brown   ts->max_snes_failures = fails;
3634cef5090cSJed Brown   PetscFunctionReturn(0);
3635cef5090cSJed Brown }
3636cef5090cSJed Brown 
3637cef5090cSJed Brown #undef __FUNCT__
3638cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3639cef5090cSJed Brown /*@
3640cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3641cef5090cSJed Brown 
3642cef5090cSJed Brown    Not Collective
3643cef5090cSJed Brown 
3644cef5090cSJed Brown    Input Parameter:
3645cef5090cSJed Brown +  ts - TS context
3646cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3647cef5090cSJed Brown 
3648cef5090cSJed Brown    Options Database Key:
3649cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3650cef5090cSJed Brown 
3651cef5090cSJed Brown    Level: intermediate
3652cef5090cSJed Brown 
3653cef5090cSJed Brown .keywords: TS, set, error
3654cef5090cSJed Brown 
36555ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3656cef5090cSJed Brown @*/
3657cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3658cef5090cSJed Brown {
3659cef5090cSJed Brown   PetscFunctionBegin;
3660cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3661cef5090cSJed Brown   ts->errorifstepfailed = err;
3662cef5090cSJed Brown   PetscFunctionReturn(0);
3663cef5090cSJed Brown }
3664cef5090cSJed Brown 
3665cef5090cSJed Brown #undef __FUNCT__
3666fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3667fb1732b5SBarry Smith /*@C
3668fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3669fb1732b5SBarry Smith 
3670fb1732b5SBarry Smith    Collective on TS
3671fb1732b5SBarry Smith 
3672fb1732b5SBarry Smith    Input Parameters:
3673fb1732b5SBarry Smith +  ts - the TS context
3674fb1732b5SBarry Smith .  step - current time-step
3675fb1732b5SBarry Smith .  ptime - current time
36760910c330SBarry Smith .  u - current state
3677fb1732b5SBarry Smith -  viewer - binary viewer
3678fb1732b5SBarry Smith 
3679fb1732b5SBarry Smith    Level: intermediate
3680fb1732b5SBarry Smith 
3681fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3682fb1732b5SBarry Smith 
3683fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3684fb1732b5SBarry Smith @*/
36850910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3686fb1732b5SBarry Smith {
3687fb1732b5SBarry Smith   PetscErrorCode ierr;
3688ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3689fb1732b5SBarry Smith 
3690fb1732b5SBarry Smith   PetscFunctionBegin;
36910910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3692ed81e22dSJed Brown   PetscFunctionReturn(0);
3693ed81e22dSJed Brown }
3694ed81e22dSJed Brown 
3695ed81e22dSJed Brown #undef __FUNCT__
3696ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3697ed81e22dSJed Brown /*@C
3698ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3699ed81e22dSJed Brown 
3700ed81e22dSJed Brown    Collective on TS
3701ed81e22dSJed Brown 
3702ed81e22dSJed Brown    Input Parameters:
3703ed81e22dSJed Brown +  ts - the TS context
3704ed81e22dSJed Brown .  step - current time-step
3705ed81e22dSJed Brown .  ptime - current time
37060910c330SBarry Smith .  u - current state
3707ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3708ed81e22dSJed Brown 
3709ed81e22dSJed Brown    Level: intermediate
3710ed81e22dSJed Brown 
3711ed81e22dSJed Brown    Notes:
3712ed81e22dSJed 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.
3713ed81e22dSJed Brown    These are named according to the file name template.
3714ed81e22dSJed Brown 
3715ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3716ed81e22dSJed Brown 
3717ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3718ed81e22dSJed Brown 
3719ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3720ed81e22dSJed Brown @*/
37210910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3722ed81e22dSJed Brown {
3723ed81e22dSJed Brown   PetscErrorCode ierr;
3724ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3725ed81e22dSJed Brown   PetscViewer    viewer;
3726ed81e22dSJed Brown 
3727ed81e22dSJed Brown   PetscFunctionBegin;
37288caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3729ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
37300910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3731ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3732ed81e22dSJed Brown   PetscFunctionReturn(0);
3733ed81e22dSJed Brown }
3734ed81e22dSJed Brown 
3735ed81e22dSJed Brown #undef __FUNCT__
3736ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3737ed81e22dSJed Brown /*@C
3738ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3739ed81e22dSJed Brown 
3740ed81e22dSJed Brown    Collective on TS
3741ed81e22dSJed Brown 
3742ed81e22dSJed Brown    Input Parameters:
3743ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3744ed81e22dSJed Brown 
3745ed81e22dSJed Brown    Level: intermediate
3746ed81e22dSJed Brown 
3747ed81e22dSJed Brown    Note:
3748ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3749ed81e22dSJed Brown 
3750ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3751ed81e22dSJed Brown 
3752ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3753ed81e22dSJed Brown @*/
3754ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3755ed81e22dSJed Brown {
3756ed81e22dSJed Brown   PetscErrorCode ierr;
3757ed81e22dSJed Brown 
3758ed81e22dSJed Brown   PetscFunctionBegin;
3759ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3760fb1732b5SBarry Smith   PetscFunctionReturn(0);
3761fb1732b5SBarry Smith }
3762fb1732b5SBarry Smith 
376384df9cb4SJed Brown #undef __FUNCT__
3764ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt"
376584df9cb4SJed Brown /*@
3766ad6bc421SBarry Smith    TSGetTSAdapt - Get the adaptive controller context for the current method
376784df9cb4SJed Brown 
3768ed81e22dSJed Brown    Collective on TS if controller has not been created yet
376984df9cb4SJed Brown 
377084df9cb4SJed Brown    Input Arguments:
3771ed81e22dSJed Brown .  ts - time stepping context
377284df9cb4SJed Brown 
377384df9cb4SJed Brown    Output Arguments:
3774ed81e22dSJed Brown .  adapt - adaptive controller
377584df9cb4SJed Brown 
377684df9cb4SJed Brown    Level: intermediate
377784df9cb4SJed Brown 
3778ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
377984df9cb4SJed Brown @*/
3780ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt)
378184df9cb4SJed Brown {
378284df9cb4SJed Brown   PetscErrorCode ierr;
378384df9cb4SJed Brown 
378484df9cb4SJed Brown   PetscFunctionBegin;
378584df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
378684df9cb4SJed Brown   PetscValidPointer(adapt,2);
378784df9cb4SJed Brown   if (!ts->adapt) {
378884df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
37891c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
37901c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
379184df9cb4SJed Brown   }
379284df9cb4SJed Brown   *adapt = ts->adapt;
379384df9cb4SJed Brown   PetscFunctionReturn(0);
379484df9cb4SJed Brown }
3795d6ebe24aSShri Abhyankar 
3796d6ebe24aSShri Abhyankar #undef __FUNCT__
37971c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
37981c3436cfSJed Brown /*@
37991c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
38001c3436cfSJed Brown 
38011c3436cfSJed Brown    Logically Collective
38021c3436cfSJed Brown 
38031c3436cfSJed Brown    Input Arguments:
38041c3436cfSJed Brown +  ts - time integration context
38051c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
38061c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
38071c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
38081c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
38091c3436cfSJed Brown 
38101c3436cfSJed Brown    Level: beginner
38111c3436cfSJed Brown 
3812c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
38131c3436cfSJed Brown @*/
38141c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
38151c3436cfSJed Brown {
38161c3436cfSJed Brown   PetscErrorCode ierr;
38171c3436cfSJed Brown 
38181c3436cfSJed Brown   PetscFunctionBegin;
3819c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
38201c3436cfSJed Brown   if (vatol) {
38211c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
38221c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
38231c3436cfSJed Brown     ts->vatol = vatol;
38241c3436cfSJed Brown   }
3825c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
38261c3436cfSJed Brown   if (vrtol) {
38271c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
38281c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
38291c3436cfSJed Brown     ts->vrtol = vrtol;
38301c3436cfSJed Brown   }
38311c3436cfSJed Brown   PetscFunctionReturn(0);
38321c3436cfSJed Brown }
38331c3436cfSJed Brown 
38341c3436cfSJed Brown #undef __FUNCT__
3835c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3836c5033834SJed Brown /*@
3837c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3838c5033834SJed Brown 
3839c5033834SJed Brown    Logically Collective
3840c5033834SJed Brown 
3841c5033834SJed Brown    Input Arguments:
3842c5033834SJed Brown .  ts - time integration context
3843c5033834SJed Brown 
3844c5033834SJed Brown    Output Arguments:
3845c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3846c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3847c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3848c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3849c5033834SJed Brown 
3850c5033834SJed Brown    Level: beginner
3851c5033834SJed Brown 
3852c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3853c5033834SJed Brown @*/
3854c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3855c5033834SJed Brown {
3856c5033834SJed Brown   PetscFunctionBegin;
3857c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3858c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3859c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3860c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3861c5033834SJed Brown   PetscFunctionReturn(0);
3862c5033834SJed Brown }
3863c5033834SJed Brown 
3864c5033834SJed Brown #undef __FUNCT__
38651c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
38661c3436cfSJed Brown /*@
38671c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
38681c3436cfSJed Brown 
38691c3436cfSJed Brown    Collective on TS
38701c3436cfSJed Brown 
38711c3436cfSJed Brown    Input Arguments:
38721c3436cfSJed Brown +  ts - time stepping context
38731c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
38741c3436cfSJed Brown 
38751c3436cfSJed Brown    Output Arguments:
38761c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
38771c3436cfSJed Brown 
38781c3436cfSJed Brown    Level: developer
38791c3436cfSJed Brown 
38801c3436cfSJed Brown .seealso: TSSetTolerances()
38811c3436cfSJed Brown @*/
38821c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
38831c3436cfSJed Brown {
38841c3436cfSJed Brown   PetscErrorCode    ierr;
38851c3436cfSJed Brown   PetscInt          i,n,N;
38860910c330SBarry Smith   const PetscScalar *u,*y;
38870910c330SBarry Smith   Vec               U;
38881c3436cfSJed Brown   PetscReal         sum,gsum;
38891c3436cfSJed Brown 
38901c3436cfSJed Brown   PetscFunctionBegin;
38911c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38921c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
38931c3436cfSJed Brown   PetscValidPointer(norm,3);
38940910c330SBarry Smith   U = ts->vec_sol;
38950910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
38960910c330SBarry Smith   if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
38971c3436cfSJed Brown 
38980910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
38990910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
39000910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
39011c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
39021c3436cfSJed Brown   sum = 0.;
3903ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3904ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3905ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3906ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3907ceefc113SJed Brown     for (i=0; i<n; i++) {
39080910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39090910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3910ceefc113SJed Brown     }
3911ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3912ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3913ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3914ceefc113SJed Brown     const PetscScalar *atol;
3915ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3916ceefc113SJed Brown     for (i=0; i<n; i++) {
39170910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39180910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3919ceefc113SJed Brown     }
3920ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3921ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3922ceefc113SJed Brown     const PetscScalar *rtol;
3923ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3924ceefc113SJed Brown     for (i=0; i<n; i++) {
39250910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39260910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3927ceefc113SJed Brown     }
3928ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3929ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
39301c3436cfSJed Brown     for (i=0; i<n; i++) {
39310910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39320910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
39331c3436cfSJed Brown     }
3934ceefc113SJed Brown   }
39350910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
39361c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
39371c3436cfSJed Brown 
39381c3436cfSJed Brown   ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
39391c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
39401c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
39411c3436cfSJed Brown   PetscFunctionReturn(0);
39421c3436cfSJed Brown }
39431c3436cfSJed Brown 
39441c3436cfSJed Brown #undef __FUNCT__
39458d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
39468d59e960SJed Brown /*@
39478d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
39488d59e960SJed Brown 
39498d59e960SJed Brown    Logically Collective on TS
39508d59e960SJed Brown 
39518d59e960SJed Brown    Input Arguments:
39528d59e960SJed Brown +  ts - time stepping context
39538d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
39548d59e960SJed Brown 
39558d59e960SJed Brown    Note:
39568d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
39578d59e960SJed Brown 
39588d59e960SJed Brown    Level: intermediate
39598d59e960SJed Brown 
39608d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
39618d59e960SJed Brown @*/
39628d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
39638d59e960SJed Brown {
39648d59e960SJed Brown   PetscFunctionBegin;
39658d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39668d59e960SJed Brown   ts->cfltime_local = cfltime;
39678d59e960SJed Brown   ts->cfltime = -1.;
39688d59e960SJed Brown   PetscFunctionReturn(0);
39698d59e960SJed Brown }
39708d59e960SJed Brown 
39718d59e960SJed Brown #undef __FUNCT__
39728d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
39738d59e960SJed Brown /*@
39748d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
39758d59e960SJed Brown 
39768d59e960SJed Brown    Collective on TS
39778d59e960SJed Brown 
39788d59e960SJed Brown    Input Arguments:
39798d59e960SJed Brown .  ts - time stepping context
39808d59e960SJed Brown 
39818d59e960SJed Brown    Output Arguments:
39828d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
39838d59e960SJed Brown 
39848d59e960SJed Brown    Level: advanced
39858d59e960SJed Brown 
39868d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
39878d59e960SJed Brown @*/
39888d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
39898d59e960SJed Brown {
39908d59e960SJed Brown   PetscErrorCode ierr;
39918d59e960SJed Brown 
39928d59e960SJed Brown   PetscFunctionBegin;
39938d59e960SJed Brown   if (ts->cfltime < 0) {
39948d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
39958d59e960SJed Brown   }
39968d59e960SJed Brown   *cfltime = ts->cfltime;
39978d59e960SJed Brown   PetscFunctionReturn(0);
39988d59e960SJed Brown }
39998d59e960SJed Brown 
40008d59e960SJed Brown #undef __FUNCT__
4001d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4002d6ebe24aSShri Abhyankar /*@
4003d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4004d6ebe24aSShri Abhyankar 
4005d6ebe24aSShri Abhyankar    Input Parameters:
4006d6ebe24aSShri Abhyankar .  ts   - the TS context.
4007d6ebe24aSShri Abhyankar .  xl   - lower bound.
4008d6ebe24aSShri Abhyankar .  xu   - upper bound.
4009d6ebe24aSShri Abhyankar 
4010d6ebe24aSShri Abhyankar    Notes:
4011d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
401233c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4013d6ebe24aSShri Abhyankar 
40142bd2b0e6SSatish Balay    Level: advanced
40152bd2b0e6SSatish Balay 
4016d6ebe24aSShri Abhyankar @*/
4017d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4018d6ebe24aSShri Abhyankar {
4019d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4020d6ebe24aSShri Abhyankar   SNES           snes;
4021d6ebe24aSShri Abhyankar 
4022d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4023d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4024d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4025d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4026d6ebe24aSShri Abhyankar }
4027d6ebe24aSShri Abhyankar 
4028325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4029c6db04a5SJed Brown #include <mex.h>
4030325fc9f4SBarry Smith 
4031325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4032325fc9f4SBarry Smith 
4033325fc9f4SBarry Smith #undef __FUNCT__
4034325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4035325fc9f4SBarry Smith /*
4036325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4037325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4038325fc9f4SBarry Smith 
4039325fc9f4SBarry Smith    Collective on TS
4040325fc9f4SBarry Smith 
4041325fc9f4SBarry Smith    Input Parameters:
4042325fc9f4SBarry Smith +  snes - the TS context
40430910c330SBarry Smith -  u - input vector
4044325fc9f4SBarry Smith 
4045325fc9f4SBarry Smith    Output Parameter:
4046325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4047325fc9f4SBarry Smith 
4048325fc9f4SBarry Smith    Notes:
4049325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4050325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4051325fc9f4SBarry Smith    themselves.
4052325fc9f4SBarry Smith 
4053325fc9f4SBarry Smith    Level: developer
4054325fc9f4SBarry Smith 
4055325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4056325fc9f4SBarry Smith 
4057325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4058325fc9f4SBarry Smith */
40590910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4060325fc9f4SBarry Smith {
4061325fc9f4SBarry Smith   PetscErrorCode   ierr;
4062325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
4063325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
4064325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
4065325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
4066325fc9f4SBarry Smith 
4067325fc9f4SBarry Smith   PetscFunctionBegin;
4068325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
40690910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
40700910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4071325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
40720910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4073325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4074325fc9f4SBarry Smith 
4075325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
40760910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
40770910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
40780910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4079325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4080325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4081325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4082325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4083325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4084325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4085325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4086325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4087325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4088325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4089325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4090325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4091325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4092325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4093325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4094325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4095325fc9f4SBarry Smith   PetscFunctionReturn(0);
4096325fc9f4SBarry Smith }
4097325fc9f4SBarry Smith 
4098325fc9f4SBarry Smith 
4099325fc9f4SBarry Smith #undef __FUNCT__
4100325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4101325fc9f4SBarry Smith /*
4102325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4103325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4104e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4105325fc9f4SBarry Smith 
4106325fc9f4SBarry Smith    Logically Collective on TS
4107325fc9f4SBarry Smith 
4108325fc9f4SBarry Smith    Input Parameters:
4109325fc9f4SBarry Smith +  ts - the TS context
4110325fc9f4SBarry Smith -  func - function evaluation routine
4111325fc9f4SBarry Smith 
4112325fc9f4SBarry Smith    Calling sequence of func:
41130910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4114325fc9f4SBarry Smith 
4115325fc9f4SBarry Smith    Level: beginner
4116325fc9f4SBarry Smith 
4117325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4118325fc9f4SBarry Smith 
4119325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4120325fc9f4SBarry Smith */
4121cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4122325fc9f4SBarry Smith {
4123325fc9f4SBarry Smith   PetscErrorCode  ierr;
4124325fc9f4SBarry Smith   TSMatlabContext *sctx;
4125325fc9f4SBarry Smith 
4126325fc9f4SBarry Smith   PetscFunctionBegin;
4127325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4128325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4129325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4130325fc9f4SBarry Smith   /*
4131325fc9f4SBarry Smith      This should work, but it doesn't
4132325fc9f4SBarry Smith   sctx->ctx = ctx;
4133325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4134325fc9f4SBarry Smith   */
4135325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4136cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4137325fc9f4SBarry Smith   PetscFunctionReturn(0);
4138325fc9f4SBarry Smith }
4139325fc9f4SBarry Smith 
4140325fc9f4SBarry Smith #undef __FUNCT__
4141325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4142325fc9f4SBarry Smith /*
4143325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4144325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4145325fc9f4SBarry Smith 
4146325fc9f4SBarry Smith    Collective on TS
4147325fc9f4SBarry Smith 
4148325fc9f4SBarry Smith    Input Parameters:
4149cdcf91faSSean Farley +  ts - the TS context
41500910c330SBarry Smith .  u - input vector
4151325fc9f4SBarry Smith .  A, B - the matrices
4152325fc9f4SBarry Smith -  ctx - user context
4153325fc9f4SBarry Smith 
4154325fc9f4SBarry Smith    Output Parameter:
4155325fc9f4SBarry Smith .  flag - structure of the matrix
4156325fc9f4SBarry Smith 
4157325fc9f4SBarry Smith    Level: developer
4158325fc9f4SBarry Smith 
4159325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4160325fc9f4SBarry Smith 
4161325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4162325fc9f4SBarry Smith @*/
41630910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4164325fc9f4SBarry Smith {
4165325fc9f4SBarry Smith   PetscErrorCode  ierr;
4166325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
4167325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
4168325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4169325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4170325fc9f4SBarry Smith 
4171325fc9f4SBarry Smith   PetscFunctionBegin;
4172cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41730910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4174325fc9f4SBarry Smith 
41750910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4176325fc9f4SBarry Smith 
4177cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
41780910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
41790910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
41800910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
41810910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4182325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4183325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4184325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4185325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4186325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4187325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4188325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4189325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4190325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4191325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4192325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4193325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4194325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4195325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4196325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4197325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4198325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4199325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4200325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4201325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4202325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4203325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4204325fc9f4SBarry Smith   PetscFunctionReturn(0);
4205325fc9f4SBarry Smith }
4206325fc9f4SBarry Smith 
4207325fc9f4SBarry Smith 
4208325fc9f4SBarry Smith #undef __FUNCT__
4209325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4210325fc9f4SBarry Smith /*
4211325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4212e3c5b3baSBarry 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
4213325fc9f4SBarry Smith 
4214325fc9f4SBarry Smith    Logically Collective on TS
4215325fc9f4SBarry Smith 
4216325fc9f4SBarry Smith    Input Parameters:
4217cdcf91faSSean Farley +  ts - the TS context
4218325fc9f4SBarry Smith .  A,B - Jacobian matrices
4219325fc9f4SBarry Smith .  func - function evaluation routine
4220325fc9f4SBarry Smith -  ctx - user context
4221325fc9f4SBarry Smith 
4222325fc9f4SBarry Smith    Calling sequence of func:
42230910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4224325fc9f4SBarry Smith 
4225325fc9f4SBarry Smith 
4226325fc9f4SBarry Smith    Level: developer
4227325fc9f4SBarry Smith 
4228325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4229325fc9f4SBarry Smith 
4230325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4231325fc9f4SBarry Smith */
4232cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4233325fc9f4SBarry Smith {
4234325fc9f4SBarry Smith   PetscErrorCode    ierr;
4235325fc9f4SBarry Smith   TSMatlabContext *sctx;
4236325fc9f4SBarry Smith 
4237325fc9f4SBarry Smith   PetscFunctionBegin;
4238325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4239325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4240325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4241325fc9f4SBarry Smith   /*
4242325fc9f4SBarry Smith      This should work, but it doesn't
4243325fc9f4SBarry Smith   sctx->ctx = ctx;
4244325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4245325fc9f4SBarry Smith   */
4246325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4247cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4248325fc9f4SBarry Smith   PetscFunctionReturn(0);
4249325fc9f4SBarry Smith }
4250325fc9f4SBarry Smith 
4251b5b1a830SBarry Smith #undef __FUNCT__
4252b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4253b5b1a830SBarry Smith /*
4254b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4255b5b1a830SBarry Smith 
4256b5b1a830SBarry Smith    Collective on TS
4257b5b1a830SBarry Smith 
4258b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4259b5b1a830SBarry Smith @*/
42600910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4261b5b1a830SBarry Smith {
4262b5b1a830SBarry Smith   PetscErrorCode  ierr;
4263b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
4264a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
4265b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4266b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4267b5b1a830SBarry Smith 
4268b5b1a830SBarry Smith   PetscFunctionBegin;
4269cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42700910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4271b5b1a830SBarry Smith 
4272cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
42730910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4274b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4275b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4276b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4277b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4278b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4279b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4280b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4281b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4282b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4283b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4284b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4285b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4286b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4287b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4288b5b1a830SBarry Smith   PetscFunctionReturn(0);
4289b5b1a830SBarry Smith }
4290b5b1a830SBarry Smith 
4291b5b1a830SBarry Smith 
4292b5b1a830SBarry Smith #undef __FUNCT__
4293b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4294b5b1a830SBarry Smith /*
4295b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4296b5b1a830SBarry Smith 
4297b5b1a830SBarry Smith    Level: developer
4298b5b1a830SBarry Smith 
4299b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4300b5b1a830SBarry Smith 
4301b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4302b5b1a830SBarry Smith */
4303cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4304b5b1a830SBarry Smith {
4305b5b1a830SBarry Smith   PetscErrorCode    ierr;
4306b5b1a830SBarry Smith   TSMatlabContext *sctx;
4307b5b1a830SBarry Smith 
4308b5b1a830SBarry Smith   PetscFunctionBegin;
4309b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4310b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4311b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4312b5b1a830SBarry Smith   /*
4313b5b1a830SBarry Smith      This should work, but it doesn't
4314b5b1a830SBarry Smith   sctx->ctx = ctx;
4315b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4316b5b1a830SBarry Smith   */
4317b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4318cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
4319b5b1a830SBarry Smith   PetscFunctionReturn(0);
4320b5b1a830SBarry Smith }
4321325fc9f4SBarry Smith #endif
4322b3603a34SBarry Smith 
43230b039ecaSBarry Smith 
43240b039ecaSBarry Smith 
4325b3603a34SBarry Smith #undef __FUNCT__
43264f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4327b3603a34SBarry Smith /*@C
43284f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4329b3603a34SBarry Smith        in a time based line graph
4330b3603a34SBarry Smith 
4331b3603a34SBarry Smith    Collective on TS
4332b3603a34SBarry Smith 
4333b3603a34SBarry Smith    Input Parameters:
4334b3603a34SBarry Smith +  ts - the TS context
4335b3603a34SBarry Smith .  step - current time-step
4336b3603a34SBarry Smith .  ptime - current time
4337b3603a34SBarry Smith -  lg - a line graph object
4338b3603a34SBarry Smith 
4339b3603a34SBarry Smith    Level: intermediate
4340b3603a34SBarry Smith 
43410b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4342b3603a34SBarry Smith 
4343b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4344b3603a34SBarry Smith 
4345b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4346b3603a34SBarry Smith @*/
43470910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4348b3603a34SBarry Smith {
4349b3603a34SBarry Smith   PetscErrorCode    ierr;
43500b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4351b3603a34SBarry Smith   const PetscScalar *yy;
435258ff32f7SBarry Smith   PetscInt          dim;
4353b3603a34SBarry Smith 
4354b3603a34SBarry Smith   PetscFunctionBegin;
435558ff32f7SBarry Smith   if (!step) {
4356a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4357a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4358a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
43590910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
43600b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
43610b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
436258ff32f7SBarry Smith   }
43630910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4364e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4365e3efe391SJed Brown   {
4366e3efe391SJed Brown     PetscReal *yreal;
4367e3efe391SJed Brown     PetscInt  i,n;
43680910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4369e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4370e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
43710b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4372e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4373e3efe391SJed Brown   }
4374e3efe391SJed Brown #else
43750b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4376e3efe391SJed Brown #endif
43770910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
43783fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
43790b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43803923b477SBarry Smith   }
4381b3603a34SBarry Smith   PetscFunctionReturn(0);
4382b3603a34SBarry Smith }
4383b3603a34SBarry Smith 
4384b3603a34SBarry Smith #undef __FUNCT__
43854f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4386ef20d060SBarry Smith /*@C
43874f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4388ef20d060SBarry Smith        in a time based line graph
4389ef20d060SBarry Smith 
4390ef20d060SBarry Smith    Collective on TS
4391ef20d060SBarry Smith 
4392ef20d060SBarry Smith    Input Parameters:
4393ef20d060SBarry Smith +  ts - the TS context
4394ef20d060SBarry Smith .  step - current time-step
4395ef20d060SBarry Smith .  ptime - current time
4396ef20d060SBarry Smith -  lg - a line graph object
4397ef20d060SBarry Smith 
4398ef20d060SBarry Smith    Level: intermediate
4399ef20d060SBarry Smith 
4400abd5a294SJed Brown    Notes:
4401abd5a294SJed Brown    Only for sequential solves.
4402abd5a294SJed Brown 
4403abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4404abd5a294SJed Brown 
4405abd5a294SJed Brown    Options Database Keys:
44064f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4407ef20d060SBarry Smith 
4408ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4409ef20d060SBarry Smith 
4410abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4411ef20d060SBarry Smith @*/
44120910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4413ef20d060SBarry Smith {
4414ef20d060SBarry Smith   PetscErrorCode    ierr;
44150b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4416ef20d060SBarry Smith   const PetscScalar *yy;
4417ef20d060SBarry Smith   Vec               y;
4418a9f9c1f6SBarry Smith   PetscInt          dim;
4419ef20d060SBarry Smith 
4420ef20d060SBarry Smith   PetscFunctionBegin;
4421a9f9c1f6SBarry Smith   if (!step) {
4422a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4423a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
44241ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
44250910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4426a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4427a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4428a9f9c1f6SBarry Smith   }
44290910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4430ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
44310910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4432ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4433e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4434e3efe391SJed Brown   {
4435e3efe391SJed Brown     PetscReal *yreal;
4436e3efe391SJed Brown     PetscInt  i,n;
4437e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4438e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4439e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
44400b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4441e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4442e3efe391SJed Brown   }
4443e3efe391SJed Brown #else
44440b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4445e3efe391SJed Brown #endif
4446ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4447ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
44483fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
44490b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
44503923b477SBarry Smith   }
4451ef20d060SBarry Smith   PetscFunctionReturn(0);
4452ef20d060SBarry Smith }
4453ef20d060SBarry Smith 
4454201da799SBarry Smith #undef __FUNCT__
4455201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4456201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4457201da799SBarry Smith {
4458201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4459201da799SBarry Smith   PetscReal      x = ptime,y;
4460201da799SBarry Smith   PetscErrorCode ierr;
4461201da799SBarry Smith   PetscInt       its;
4462201da799SBarry Smith 
4463201da799SBarry Smith   PetscFunctionBegin;
4464201da799SBarry Smith   if (!n) {
4465201da799SBarry Smith     PetscDrawAxis  axis;
4466201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4467201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4468201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4469201da799SBarry Smith     ctx->snes_its  = 0;
4470201da799SBarry Smith   }
4471201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4472201da799SBarry Smith   y    = its - ctx->snes_its;
4473201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
44743fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){
4475201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4476201da799SBarry Smith   }
4477201da799SBarry Smith   ctx->snes_its = its;
4478201da799SBarry Smith   PetscFunctionReturn(0);
4479201da799SBarry Smith }
4480201da799SBarry Smith 
4481201da799SBarry Smith #undef __FUNCT__
4482201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4483201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4484201da799SBarry Smith {
4485201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4486201da799SBarry Smith   PetscReal      x = ptime,y;
4487201da799SBarry Smith   PetscErrorCode ierr;
4488201da799SBarry Smith   PetscInt       its;
4489201da799SBarry Smith 
4490201da799SBarry Smith   PetscFunctionBegin;
4491201da799SBarry Smith   if (!n) {
4492201da799SBarry Smith     PetscDrawAxis  axis;
4493201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4494201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4495201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4496201da799SBarry Smith     ctx->ksp_its = 0;
4497201da799SBarry Smith   }
4498201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4499201da799SBarry Smith   y    = its - ctx->ksp_its;
4500201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
450199fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){
4502201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4503201da799SBarry Smith   }
4504201da799SBarry Smith   ctx->ksp_its = its;
4505201da799SBarry Smith   PetscFunctionReturn(0);
4506201da799SBarry Smith }
4507f9c1d6abSBarry Smith 
4508f9c1d6abSBarry Smith #undef __FUNCT__
4509f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4510f9c1d6abSBarry Smith /*@
4511f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4512f9c1d6abSBarry Smith 
4513f9c1d6abSBarry Smith    Collective on TS and Vec
4514f9c1d6abSBarry Smith 
4515f9c1d6abSBarry Smith    Input Parameters:
4516f9c1d6abSBarry Smith +  ts - the TS context
4517f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4518f9c1d6abSBarry Smith 
4519f9c1d6abSBarry Smith    Output Parameters:
4520f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4521f9c1d6abSBarry Smith 
4522f9c1d6abSBarry Smith    Level: developer
4523f9c1d6abSBarry Smith 
4524f9c1d6abSBarry Smith .keywords: TS, compute
4525f9c1d6abSBarry Smith 
4526f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4527f9c1d6abSBarry Smith @*/
4528f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4529f9c1d6abSBarry Smith {
4530f9c1d6abSBarry Smith   PetscErrorCode ierr;
4531f9c1d6abSBarry Smith 
4532f9c1d6abSBarry Smith   PetscFunctionBegin;
4533f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4534f9c1d6abSBarry Smith   if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4535f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4536f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4537f9c1d6abSBarry Smith }
4538