xref: /petsc/src/ts/interface/ts.c (revision 1e25c2744a56dc779d05d2a7975a7f2272f1aa7b)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
4*1e25c274SJed Brown #include <petscdmda.h>
5d763cef2SBarry Smith 
6d5ba7fb7SMatthew Knepley /* Logging support */
7d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
8166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
9d405a339SMatthew Knepley 
1049354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1149354f04SShri Abhyankar 
124a2ae208SSatish Balay #undef __FUNCT__
13bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
14bdad233fSMatthew Knepley /*
15bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
16bdad233fSMatthew Knepley 
17bdad233fSMatthew Knepley   Collective on TS
18bdad233fSMatthew Knepley 
19bdad233fSMatthew Knepley   Input Parameter:
20bdad233fSMatthew Knepley . ts - The ts
21bdad233fSMatthew Knepley 
22bdad233fSMatthew Knepley   Level: intermediate
23bdad233fSMatthew Knepley 
24bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
25bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
26bdad233fSMatthew Knepley */
276849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
28bdad233fSMatthew Knepley {
29ace3abfcSBarry Smith   PetscBool      opt;
302fc52814SBarry Smith   const char     *defaultType;
31bdad233fSMatthew Knepley   char           typeName[256];
32dfbe8321SBarry Smith   PetscErrorCode ierr;
33bdad233fSMatthew Knepley 
34bdad233fSMatthew Knepley   PetscFunctionBegin;
35bbd56ea5SKarl Rupp   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
36bbd56ea5SKarl Rupp   else defaultType = TSEULER;
37bdad233fSMatthew Knepley 
380298fd71SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(NULL);CHKERRQ(ierr);}
39bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
40a7cc72afSBarry Smith   if (opt) {
41bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
42bdad233fSMatthew Knepley   } else {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   }
45bdad233fSMatthew Knepley   PetscFunctionReturn(0);
46bdad233fSMatthew Knepley }
47bdad233fSMatthew Knepley 
48bdad233fSMatthew Knepley #undef __FUNCT__
49bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
50bdad233fSMatthew Knepley /*@
51bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
52bdad233fSMatthew Knepley 
53bdad233fSMatthew Knepley    Collective on TS
54bdad233fSMatthew Knepley 
55bdad233fSMatthew Knepley    Input Parameter:
56bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
57bdad233fSMatthew Knepley 
58bdad233fSMatthew Knepley    Options Database Keys:
594d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
60bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
613bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
62bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
63bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
64de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
65de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
66de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
67de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
68de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
69de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
70de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
71de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
72de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
7391b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
7491b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
7591b97e58SBarry Smith 
7691b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
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 */
1030298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1040298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1050298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,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);}
1120298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr);
1130298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1140298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1150298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1160298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,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) {
121ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),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 
1320298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
133ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),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 
1410298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,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 
1500298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,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 
1590298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,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 
1680298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,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 
1770298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,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 
1870298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
188ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),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 
1970298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
198ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),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;
20291b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_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]) {
206ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
207c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
208fb1732b5SBarry Smith     } else {
209ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2100298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
211fb1732b5SBarry Smith     }
212fb1732b5SBarry Smith   }
213ed81e22dSJed Brown   opt  = PETSC_FALSE;
21491b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
215ed81e22dSJed Brown   if (flg) {
216ed81e22dSJed Brown     const char *ptr,*ptr2;
217ed81e22dSJed Brown     char       *filetemplate;
218ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
219ed81e22dSJed Brown     /* Do some cursory validation of the input. */
220ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
221ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
222ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
223ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
224ce94432eSBarry Smith       if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
225ed81e22dSJed Brown       if (ptr2) break;
226ed81e22dSJed Brown     }
227ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
228ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
229ed81e22dSJed Brown   }
230bdad233fSMatthew Knepley 
231d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
232d1212d36SBarry Smith   if (flg) {
233d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
234d1212d36SBarry Smith     int                 ray = 0;
235d1212d36SBarry Smith     DMDADirection       ddir;
236d1212d36SBarry Smith     DM                  da;
237d1212d36SBarry Smith     PetscMPIInt         rank;
238d1212d36SBarry Smith 
239ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
240d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
241d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
242ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
243d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
244d1212d36SBarry Smith 
245d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
246d1212d36SBarry Smith     ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr);
247d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
248d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
249ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
250d1212d36SBarry Smith     if (!rank) {
251d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
252d1212d36SBarry Smith     }
253d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
254d1212d36SBarry Smith   }
255d1212d36SBarry Smith 
256ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&adapt);CHKERRQ(ierr);
2571c3436cfSJed Brown   ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2581c3436cfSJed Brown 
259d52bd9f3SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
260d52bd9f3SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
261d52bd9f3SBarry Smith 
262bdad233fSMatthew Knepley   /* Handle specific TS options */
263abc0a331SBarry Smith   if (ts->ops->setfromoptions) {
264bdad233fSMatthew Knepley     ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
265bdad233fSMatthew Knepley   }
2665d973c19SBarry Smith 
2675d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
2685d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
269bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
270bdad233fSMatthew Knepley   PetscFunctionReturn(0);
271bdad233fSMatthew Knepley }
272bdad233fSMatthew Knepley 
273bdad233fSMatthew Knepley #undef __FUNCT__
274cdcf91faSSean Farley #undef __FUNCT__
2754a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
276a7a1495cSBarry Smith /*@
2778c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
278a7a1495cSBarry Smith       set with TSSetRHSJacobian().
279a7a1495cSBarry Smith 
280a7a1495cSBarry Smith    Collective on TS and Vec
281a7a1495cSBarry Smith 
282a7a1495cSBarry Smith    Input Parameters:
283316643e7SJed Brown +  ts - the TS context
284a7a1495cSBarry Smith .  t - current timestep
2850910c330SBarry Smith -  U - input vector
286a7a1495cSBarry Smith 
287a7a1495cSBarry Smith    Output Parameters:
288a7a1495cSBarry Smith +  A - Jacobian matrix
289a7a1495cSBarry Smith .  B - optional preconditioning matrix
290a7a1495cSBarry Smith -  flag - flag indicating matrix structure
291a7a1495cSBarry Smith 
292a7a1495cSBarry Smith    Notes:
293a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
294a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
295a7a1495cSBarry Smith 
29694b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
297a7a1495cSBarry Smith    flag parameter.
298a7a1495cSBarry Smith 
299a7a1495cSBarry Smith    Level: developer
300a7a1495cSBarry Smith 
301a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
302a7a1495cSBarry Smith 
30394b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
304a7a1495cSBarry Smith @*/
3050910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
306a7a1495cSBarry Smith {
307dfbe8321SBarry Smith   PetscErrorCode ierr;
3080910c330SBarry Smith   PetscInt       Ustate;
30924989b8cSPeter Brune   DM             dm;
310942e3340SBarry Smith   DMTS           tsdm;
31124989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
31224989b8cSPeter Brune   void           *ctx;
31324989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
314a7a1495cSBarry Smith 
315a7a1495cSBarry Smith   PetscFunctionBegin;
3160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3170910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3180910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
31924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
320942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
32124989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
3220298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
3230910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
3240910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
3250e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
3260e4ef248SJed Brown     PetscFunctionReturn(0);
327f8ede8e7SJed Brown   }
328d90be118SSean Farley 
329ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
330d90be118SSean Farley 
33124989b8cSPeter Brune   if (rhsjacobianfunc) {
3320910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
333a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
334a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3350910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
336a7a1495cSBarry Smith     PetscStackPop;
3370910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
338a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3390700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3400700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
341ef66eb69SBarry Smith   } else {
342214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
343214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
344214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
345ef66eb69SBarry Smith   }
3460e4ef248SJed Brown   ts->rhsjacobian.time       = t;
3470910c330SBarry Smith   ts->rhsjacobian.X          = U;
3480910c330SBarry Smith   ierr                       = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3490e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
350a7a1495cSBarry Smith   PetscFunctionReturn(0);
351a7a1495cSBarry Smith }
352a7a1495cSBarry Smith 
3534a2ae208SSatish Balay #undef __FUNCT__
3544a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
355316643e7SJed Brown /*@
356d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
357d763cef2SBarry Smith 
358316643e7SJed Brown    Collective on TS and Vec
359316643e7SJed Brown 
360316643e7SJed Brown    Input Parameters:
361316643e7SJed Brown +  ts - the TS context
362316643e7SJed Brown .  t - current time
3630910c330SBarry Smith -  U - state vector
364316643e7SJed Brown 
365316643e7SJed Brown    Output Parameter:
366316643e7SJed Brown .  y - right hand side
367316643e7SJed Brown 
368316643e7SJed Brown    Note:
369316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
370316643e7SJed Brown    is used internally within the nonlinear solvers.
371316643e7SJed Brown 
372316643e7SJed Brown    Level: developer
373316643e7SJed Brown 
374316643e7SJed Brown .keywords: TS, compute
375316643e7SJed Brown 
376316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
377316643e7SJed Brown @*/
3780910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
379d763cef2SBarry Smith {
380dfbe8321SBarry Smith   PetscErrorCode ierr;
38124989b8cSPeter Brune   TSRHSFunction  rhsfunction;
38224989b8cSPeter Brune   TSIFunction    ifunction;
38324989b8cSPeter Brune   void           *ctx;
38424989b8cSPeter Brune   DM             dm;
38524989b8cSPeter Brune 
386d763cef2SBarry Smith   PetscFunctionBegin;
3870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3880910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3890700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
39024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
39124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
3920298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
393d763cef2SBarry Smith 
394ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
395d763cef2SBarry Smith 
3960910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
39724989b8cSPeter Brune   if (rhsfunction) {
398d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
3990910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
400d763cef2SBarry Smith     PetscStackPop;
401214bc6a2SJed Brown   } else {
402214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
403b2cd27e8SJed Brown   }
40444a41b28SSean Farley 
4050910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
406d763cef2SBarry Smith   PetscFunctionReturn(0);
407d763cef2SBarry Smith }
408d763cef2SBarry Smith 
4094a2ae208SSatish Balay #undef __FUNCT__
410ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
411ef20d060SBarry Smith /*@
412ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
413ef20d060SBarry Smith 
414ef20d060SBarry Smith    Collective on TS and Vec
415ef20d060SBarry Smith 
416ef20d060SBarry Smith    Input Parameters:
417ef20d060SBarry Smith +  ts - the TS context
418ef20d060SBarry Smith -  t - current time
419ef20d060SBarry Smith 
420ef20d060SBarry Smith    Output Parameter:
4210910c330SBarry Smith .  U - the solution
422ef20d060SBarry Smith 
423ef20d060SBarry Smith    Note:
424ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
425ef20d060SBarry Smith    is used internally within the nonlinear solvers.
426ef20d060SBarry Smith 
427ef20d060SBarry Smith    Level: developer
428ef20d060SBarry Smith 
429ef20d060SBarry Smith .keywords: TS, compute
430ef20d060SBarry Smith 
431abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
432ef20d060SBarry Smith @*/
4330910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
434ef20d060SBarry Smith {
435ef20d060SBarry Smith   PetscErrorCode     ierr;
436ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
437ef20d060SBarry Smith   void               *ctx;
438ef20d060SBarry Smith   DM                 dm;
439ef20d060SBarry Smith 
440ef20d060SBarry Smith   PetscFunctionBegin;
441ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4420910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
443ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
444ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
445ef20d060SBarry Smith 
446ef20d060SBarry Smith   if (solutionfunction) {
4479b7cd975SBarry Smith     PetscStackPush("TS user solution function");
4480910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
449ef20d060SBarry Smith     PetscStackPop;
450ef20d060SBarry Smith   }
451ef20d060SBarry Smith   PetscFunctionReturn(0);
452ef20d060SBarry Smith }
4539b7cd975SBarry Smith #undef __FUNCT__
4549b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
4559b7cd975SBarry Smith /*@
4569b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
4579b7cd975SBarry Smith 
4589b7cd975SBarry Smith    Collective on TS and Vec
4599b7cd975SBarry Smith 
4609b7cd975SBarry Smith    Input Parameters:
4619b7cd975SBarry Smith +  ts - the TS context
4629b7cd975SBarry Smith -  t - current time
4639b7cd975SBarry Smith 
4649b7cd975SBarry Smith    Output Parameter:
4659b7cd975SBarry Smith .  U - the function value
4669b7cd975SBarry Smith 
4679b7cd975SBarry Smith    Note:
4689b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
4699b7cd975SBarry Smith    is used internally within the nonlinear solvers.
4709b7cd975SBarry Smith 
4719b7cd975SBarry Smith    Level: developer
4729b7cd975SBarry Smith 
4739b7cd975SBarry Smith .keywords: TS, compute
4749b7cd975SBarry Smith 
4759b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
4769b7cd975SBarry Smith @*/
4779b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
4789b7cd975SBarry Smith {
4799b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
4809b7cd975SBarry Smith   void               *ctx;
4819b7cd975SBarry Smith   DM                 dm;
4829b7cd975SBarry Smith 
4839b7cd975SBarry Smith   PetscFunctionBegin;
4849b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4859b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4869b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4879b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
4889b7cd975SBarry Smith 
4899b7cd975SBarry Smith   if (forcing) {
4909b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
4919b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
4929b7cd975SBarry Smith     PetscStackPop;
4939b7cd975SBarry Smith   }
4949b7cd975SBarry Smith   PetscFunctionReturn(0);
4959b7cd975SBarry Smith }
496ef20d060SBarry Smith 
497ef20d060SBarry Smith #undef __FUNCT__
498214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
499214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
500214bc6a2SJed Brown {
5012dd45cf8SJed Brown   Vec            F;
502214bc6a2SJed Brown   PetscErrorCode ierr;
503214bc6a2SJed Brown 
504214bc6a2SJed Brown   PetscFunctionBegin;
5050298fd71SBarry Smith   *Frhs = NULL;
5060298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
507214bc6a2SJed Brown   if (!ts->Frhs) {
5082dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
509214bc6a2SJed Brown   }
510214bc6a2SJed Brown   *Frhs = ts->Frhs;
511214bc6a2SJed Brown   PetscFunctionReturn(0);
512214bc6a2SJed Brown }
513214bc6a2SJed Brown 
514214bc6a2SJed Brown #undef __FUNCT__
515214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
516214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
517214bc6a2SJed Brown {
518214bc6a2SJed Brown   Mat            A,B;
5192dd45cf8SJed Brown   PetscErrorCode ierr;
520214bc6a2SJed Brown 
521214bc6a2SJed Brown   PetscFunctionBegin;
5220298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
523214bc6a2SJed Brown   if (Arhs) {
524214bc6a2SJed Brown     if (!ts->Arhs) {
525214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
526214bc6a2SJed Brown     }
527214bc6a2SJed Brown     *Arhs = ts->Arhs;
528214bc6a2SJed Brown   }
529214bc6a2SJed Brown   if (Brhs) {
530214bc6a2SJed Brown     if (!ts->Brhs) {
531214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
532214bc6a2SJed Brown     }
533214bc6a2SJed Brown     *Brhs = ts->Brhs;
534214bc6a2SJed Brown   }
535214bc6a2SJed Brown   PetscFunctionReturn(0);
536214bc6a2SJed Brown }
537214bc6a2SJed Brown 
538214bc6a2SJed Brown #undef __FUNCT__
539316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
540316643e7SJed Brown /*@
5410910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
542316643e7SJed Brown 
543316643e7SJed Brown    Collective on TS and Vec
544316643e7SJed Brown 
545316643e7SJed Brown    Input Parameters:
546316643e7SJed Brown +  ts - the TS context
547316643e7SJed Brown .  t - current time
5480910c330SBarry Smith .  U - state vector
5490910c330SBarry Smith .  Udot - time derivative of state vector
550214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
551316643e7SJed Brown 
552316643e7SJed Brown    Output Parameter:
553316643e7SJed Brown .  Y - right hand side
554316643e7SJed Brown 
555316643e7SJed Brown    Note:
556316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
557316643e7SJed Brown    is used internally within the nonlinear solvers.
558316643e7SJed Brown 
559316643e7SJed Brown    If the user did did not write their equations in implicit form, this
560316643e7SJed Brown    function recasts them in implicit form.
561316643e7SJed Brown 
562316643e7SJed Brown    Level: developer
563316643e7SJed Brown 
564316643e7SJed Brown .keywords: TS, compute
565316643e7SJed Brown 
566316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
567316643e7SJed Brown @*/
5680910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
569316643e7SJed Brown {
570316643e7SJed Brown   PetscErrorCode ierr;
57124989b8cSPeter Brune   TSIFunction    ifunction;
57224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
57324989b8cSPeter Brune   void           *ctx;
57424989b8cSPeter Brune   DM             dm;
575316643e7SJed Brown 
576316643e7SJed Brown   PetscFunctionBegin;
5770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5780910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5790910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
5800700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
581316643e7SJed Brown 
58224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
58324989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
5840298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
58524989b8cSPeter Brune 
586ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
587d90be118SSean Farley 
5880910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
58924989b8cSPeter Brune   if (ifunction) {
590316643e7SJed Brown     PetscStackPush("TS user implicit function");
5910910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
592316643e7SJed Brown     PetscStackPop;
593214bc6a2SJed Brown   }
594214bc6a2SJed Brown   if (imex) {
59524989b8cSPeter Brune     if (!ifunction) {
5960910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
5972dd45cf8SJed Brown     }
59824989b8cSPeter Brune   } else if (rhsfunction) {
59924989b8cSPeter Brune     if (ifunction) {
600214bc6a2SJed Brown       Vec Frhs;
601214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
6020910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
603214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
6042dd45cf8SJed Brown     } else {
6050910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
6060910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
607316643e7SJed Brown     }
6084a6899ffSJed Brown   }
6090910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
610316643e7SJed Brown   PetscFunctionReturn(0);
611316643e7SJed Brown }
612316643e7SJed Brown 
613316643e7SJed Brown #undef __FUNCT__
614316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
615316643e7SJed Brown /*@
616316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
617316643e7SJed Brown 
618316643e7SJed Brown    Collective on TS and Vec
619316643e7SJed Brown 
620316643e7SJed Brown    Input
621316643e7SJed Brown       Input Parameters:
622316643e7SJed Brown +  ts - the TS context
623316643e7SJed Brown .  t - current timestep
6240910c330SBarry Smith .  U - state vector
6250910c330SBarry Smith .  Udot - time derivative of state vector
626214bc6a2SJed Brown .  shift - shift to apply, see note below
627214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
628316643e7SJed Brown 
629316643e7SJed Brown    Output Parameters:
630316643e7SJed Brown +  A - Jacobian matrix
631316643e7SJed Brown .  B - optional preconditioning matrix
632316643e7SJed Brown -  flag - flag indicating matrix structure
633316643e7SJed Brown 
634316643e7SJed Brown    Notes:
6350910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
636316643e7SJed Brown 
6370910c330SBarry Smith    dF/dU + shift*dF/dUdot
638316643e7SJed Brown 
639316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
640316643e7SJed Brown    is used internally within the nonlinear solvers.
641316643e7SJed Brown 
642316643e7SJed Brown    Level: developer
643316643e7SJed Brown 
644316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
645316643e7SJed Brown 
646316643e7SJed Brown .seealso:  TSSetIJacobian()
64763495f91SJed Brown @*/
6480910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
649316643e7SJed Brown {
6500910c330SBarry Smith   PetscInt       Ustate, Udotstate;
651316643e7SJed Brown   PetscErrorCode ierr;
65224989b8cSPeter Brune   TSIJacobian    ijacobian;
65324989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
65424989b8cSPeter Brune   DM             dm;
65524989b8cSPeter Brune   void           *ctx;
656316643e7SJed Brown 
657316643e7SJed Brown   PetscFunctionBegin;
6580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6590910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6600910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
661316643e7SJed Brown   PetscValidPointer(A,6);
6620700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
663316643e7SJed Brown   PetscValidPointer(B,7);
6640700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
665316643e7SJed Brown   PetscValidPointer(flg,8);
66624989b8cSPeter Brune 
66724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
66824989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
6690298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
67024989b8cSPeter Brune 
6710910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
6720910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
6730910c330SBarry 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))) {
6740026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
6750026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
6760026cea9SSean Farley     PetscFunctionReturn(0);
6770026cea9SSean Farley   }
678316643e7SJed Brown 
679ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
680316643e7SJed Brown 
6814e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
6820910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
68324989b8cSPeter Brune   if (ijacobian) {
6842dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
685316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
6860910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
687316643e7SJed Brown     PetscStackPop;
688214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
689214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
690214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
6914a6899ffSJed Brown   }
692214bc6a2SJed Brown   if (imex) {
693b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
694214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
6952dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
696214bc6a2SJed Brown       if (*A != *B) {
697214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
698214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
699214bc6a2SJed Brown       }
700214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
701214bc6a2SJed Brown     }
702214bc6a2SJed Brown   } else {
70324989b8cSPeter Brune     if (!ijacobian) {
7040910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr);
705214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
706214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
707316643e7SJed Brown       if (*A != *B) {
708316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
709316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
710316643e7SJed Brown       }
71124989b8cSPeter Brune     } else if (rhsjacobian) {
712214bc6a2SJed Brown       Mat          Arhs,Brhs;
713214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
714214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
7150910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
716214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
717214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
718214bc6a2SJed Brown       if (*A != *B) {
719214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
720214bc6a2SJed Brown       }
721214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
722214bc6a2SJed Brown     }
723316643e7SJed Brown   }
7240026cea9SSean Farley 
7250026cea9SSean Farley   ts->ijacobian.time = t;
7260910c330SBarry Smith   ts->ijacobian.X    = U;
7270910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
728bbd56ea5SKarl Rupp 
7290910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
7300910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
731bbd56ea5SKarl Rupp 
7320026cea9SSean Farley   ts->ijacobian.shift      = shift;
7330026cea9SSean Farley   ts->ijacobian.imex       = imex;
7340026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
735bbd56ea5SKarl Rupp 
7360910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
737316643e7SJed Brown   PetscFunctionReturn(0);
738316643e7SJed Brown }
739316643e7SJed Brown 
740316643e7SJed Brown #undef __FUNCT__
7414a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
742d763cef2SBarry Smith /*@C
743d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
744b5abc632SBarry Smith     where U_t = G(t,u).
745d763cef2SBarry Smith 
7463f9fe445SBarry Smith     Logically Collective on TS
747d763cef2SBarry Smith 
748d763cef2SBarry Smith     Input Parameters:
749d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
7500298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
751d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
752d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
7530298fd71SBarry Smith           function evaluation routine (may be NULL)
754d763cef2SBarry Smith 
755d763cef2SBarry Smith     Calling sequence of func:
75687828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
757d763cef2SBarry Smith 
758d763cef2SBarry Smith +   t - current timestep
759d763cef2SBarry Smith .   u - input vector
760d763cef2SBarry Smith .   F - function vector
761d763cef2SBarry Smith -   ctx - [optional] user-defined function context
762d763cef2SBarry Smith 
763d763cef2SBarry Smith     Level: beginner
764d763cef2SBarry Smith 
765d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
766d763cef2SBarry Smith 
767d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
768d763cef2SBarry Smith @*/
769089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
770d763cef2SBarry Smith {
771089b2837SJed Brown   PetscErrorCode ierr;
772089b2837SJed Brown   SNES           snes;
7730298fd71SBarry Smith   Vec            ralloc = NULL;
77424989b8cSPeter Brune   DM             dm;
775d763cef2SBarry Smith 
776089b2837SJed Brown   PetscFunctionBegin;
7770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
778ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
77924989b8cSPeter Brune 
78024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
78124989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
782089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
783e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
784e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
785e856ceecSJed Brown     r    = ralloc;
786e856ceecSJed Brown   }
787089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
788e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
789d763cef2SBarry Smith   PetscFunctionReturn(0);
790d763cef2SBarry Smith }
791d763cef2SBarry Smith 
7924a2ae208SSatish Balay #undef __FUNCT__
793ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
794ef20d060SBarry Smith /*@C
795abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
796ef20d060SBarry Smith 
797ef20d060SBarry Smith     Logically Collective on TS
798ef20d060SBarry Smith 
799ef20d060SBarry Smith     Input Parameters:
800ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
801ef20d060SBarry Smith .   f - routine for evaluating the solution
802ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
8030298fd71SBarry Smith           function evaluation routine (may be NULL)
804ef20d060SBarry Smith 
805ef20d060SBarry Smith     Calling sequence of func:
806ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
807ef20d060SBarry Smith 
808ef20d060SBarry Smith +   t - current timestep
809ef20d060SBarry Smith .   u - output vector
810ef20d060SBarry Smith -   ctx - [optional] user-defined function context
811ef20d060SBarry Smith 
812abd5a294SJed Brown     Notes:
813abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
814abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
815abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
816abd5a294SJed Brown 
8174f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
818abd5a294SJed Brown 
819ef20d060SBarry Smith     Level: beginner
820ef20d060SBarry Smith 
821ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
822ef20d060SBarry Smith 
8239b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
824ef20d060SBarry Smith @*/
825ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
826ef20d060SBarry Smith {
827ef20d060SBarry Smith   PetscErrorCode ierr;
828ef20d060SBarry Smith   DM             dm;
829ef20d060SBarry Smith 
830ef20d060SBarry Smith   PetscFunctionBegin;
831ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
832ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
833ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
834ef20d060SBarry Smith   PetscFunctionReturn(0);
835ef20d060SBarry Smith }
836ef20d060SBarry Smith 
837ef20d060SBarry Smith #undef __FUNCT__
8389b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
8399b7cd975SBarry Smith /*@C
8409b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
8419b7cd975SBarry Smith 
8429b7cd975SBarry Smith     Logically Collective on TS
8439b7cd975SBarry Smith 
8449b7cd975SBarry Smith     Input Parameters:
8459b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
8469b7cd975SBarry Smith .   f - routine for evaluating the forcing function
8479b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
8480298fd71SBarry Smith           function evaluation routine (may be NULL)
8499b7cd975SBarry Smith 
8509b7cd975SBarry Smith     Calling sequence of func:
8519b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
8529b7cd975SBarry Smith 
8539b7cd975SBarry Smith +   t - current timestep
8549b7cd975SBarry Smith .   u - output vector
8559b7cd975SBarry Smith -   ctx - [optional] user-defined function context
8569b7cd975SBarry Smith 
8579b7cd975SBarry Smith     Notes:
8589b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
8599b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
8609b7cd975SBarry Smith 
8619b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
8629b7cd975SBarry Smith 
8639b7cd975SBarry Smith     Level: beginner
8649b7cd975SBarry Smith 
8659b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
8669b7cd975SBarry Smith 
8679b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
8689b7cd975SBarry Smith @*/
8699b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
8709b7cd975SBarry Smith {
8719b7cd975SBarry Smith   PetscErrorCode ierr;
8729b7cd975SBarry Smith   DM             dm;
8739b7cd975SBarry Smith 
8749b7cd975SBarry Smith   PetscFunctionBegin;
8759b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8769b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
8779b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
8789b7cd975SBarry Smith   PetscFunctionReturn(0);
8799b7cd975SBarry Smith }
8809b7cd975SBarry Smith 
8819b7cd975SBarry Smith #undef __FUNCT__
8824a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
883d763cef2SBarry Smith /*@C
884d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
885b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
886d763cef2SBarry Smith 
8873f9fe445SBarry Smith    Logically Collective on TS
888d763cef2SBarry Smith 
889d763cef2SBarry Smith    Input Parameters:
890d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
891d763cef2SBarry Smith .  A   - Jacobian matrix
892d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
893d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
894d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
8950298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
896d763cef2SBarry Smith 
897d763cef2SBarry Smith    Calling sequence of func:
89887828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
899d763cef2SBarry Smith 
900d763cef2SBarry Smith +  t - current timestep
901d763cef2SBarry Smith .  u - input vector
902d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
903d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
904d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
90594b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
906d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
907d763cef2SBarry Smith 
908d763cef2SBarry Smith    Notes:
90994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
910d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
911d763cef2SBarry Smith 
912d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
913d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
91456335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
915d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
916d763cef2SBarry Smith    throughout the global iterations.
917d763cef2SBarry Smith 
918d763cef2SBarry Smith    Level: beginner
919d763cef2SBarry Smith 
920d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
921d763cef2SBarry Smith 
922ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
923d763cef2SBarry Smith 
924d763cef2SBarry Smith @*/
925089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
926d763cef2SBarry Smith {
927277b19d0SLisandro Dalcin   PetscErrorCode ierr;
928089b2837SJed Brown   SNES           snes;
92924989b8cSPeter Brune   DM             dm;
93024989b8cSPeter Brune   TSIJacobian    ijacobian;
931277b19d0SLisandro Dalcin 
932d763cef2SBarry Smith   PetscFunctionBegin;
9330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
934277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
935277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
936277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
937277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
938d763cef2SBarry Smith 
93924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
94024989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
9410298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
94224989b8cSPeter Brune 
943089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
9445f659677SPeter Brune   if (!ijacobian) {
945089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
9460e4ef248SJed Brown   }
9470e4ef248SJed Brown   if (A) {
9480e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
9490e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
950bbd56ea5SKarl Rupp 
9510e4ef248SJed Brown     ts->Arhs = A;
9520e4ef248SJed Brown   }
9530e4ef248SJed Brown   if (B) {
9540e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
9550e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
956bbd56ea5SKarl Rupp 
9570e4ef248SJed Brown     ts->Brhs = B;
9580e4ef248SJed Brown   }
959d763cef2SBarry Smith   PetscFunctionReturn(0);
960d763cef2SBarry Smith }
961d763cef2SBarry Smith 
962316643e7SJed Brown 
963316643e7SJed Brown #undef __FUNCT__
964316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
965316643e7SJed Brown /*@C
966b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
967316643e7SJed Brown 
9683f9fe445SBarry Smith    Logically Collective on TS
969316643e7SJed Brown 
970316643e7SJed Brown    Input Parameters:
971316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
9720298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
973316643e7SJed Brown .  f   - the function evaluation routine
9740298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
975316643e7SJed Brown 
976316643e7SJed Brown    Calling sequence of f:
977316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
978316643e7SJed Brown 
979316643e7SJed Brown +  t   - time at step/stage being solved
980316643e7SJed Brown .  u   - state vector
981316643e7SJed Brown .  u_t - time derivative of state vector
982316643e7SJed Brown .  F   - function vector
983316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
984316643e7SJed Brown 
985316643e7SJed Brown    Important:
986d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
987316643e7SJed Brown 
988316643e7SJed Brown    Level: beginner
989316643e7SJed Brown 
990316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
991316643e7SJed Brown 
992d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
993316643e7SJed Brown @*/
994089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
995316643e7SJed Brown {
996089b2837SJed Brown   PetscErrorCode ierr;
997089b2837SJed Brown   SNES           snes;
9980298fd71SBarry Smith   Vec            resalloc = NULL;
99924989b8cSPeter Brune   DM             dm;
1000316643e7SJed Brown 
1001316643e7SJed Brown   PetscFunctionBegin;
10020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1003ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
100424989b8cSPeter Brune 
100524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
100624989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
100724989b8cSPeter Brune 
1008089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1009e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1010e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1011e856ceecSJed Brown     res  = resalloc;
1012e856ceecSJed Brown   }
1013089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1014e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1015089b2837SJed Brown   PetscFunctionReturn(0);
1016089b2837SJed Brown }
1017089b2837SJed Brown 
1018089b2837SJed Brown #undef __FUNCT__
1019089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1020089b2837SJed Brown /*@C
1021089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1022089b2837SJed Brown 
1023089b2837SJed Brown    Not Collective
1024089b2837SJed Brown 
1025089b2837SJed Brown    Input Parameter:
1026089b2837SJed Brown .  ts - the TS context
1027089b2837SJed Brown 
1028089b2837SJed Brown    Output Parameter:
10290298fd71SBarry Smith +  r - vector to hold residual (or NULL)
10300298fd71SBarry Smith .  func - the function to compute residual (or NULL)
10310298fd71SBarry Smith -  ctx - the function context (or NULL)
1032089b2837SJed Brown 
1033089b2837SJed Brown    Level: advanced
1034089b2837SJed Brown 
1035089b2837SJed Brown .keywords: TS, nonlinear, get, function
1036089b2837SJed Brown 
1037089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1038089b2837SJed Brown @*/
1039089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1040089b2837SJed Brown {
1041089b2837SJed Brown   PetscErrorCode ierr;
1042089b2837SJed Brown   SNES           snes;
104324989b8cSPeter Brune   DM             dm;
1044089b2837SJed Brown 
1045089b2837SJed Brown   PetscFunctionBegin;
1046089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1047089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10480298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
104924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
105024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1051089b2837SJed Brown   PetscFunctionReturn(0);
1052089b2837SJed Brown }
1053089b2837SJed Brown 
1054089b2837SJed Brown #undef __FUNCT__
1055089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1056089b2837SJed Brown /*@C
1057089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1058089b2837SJed Brown 
1059089b2837SJed Brown    Not Collective
1060089b2837SJed Brown 
1061089b2837SJed Brown    Input Parameter:
1062089b2837SJed Brown .  ts - the TS context
1063089b2837SJed Brown 
1064089b2837SJed Brown    Output Parameter:
10650298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
10660298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
10670298fd71SBarry Smith -  ctx - the function context (or NULL)
1068089b2837SJed Brown 
1069089b2837SJed Brown    Level: advanced
1070089b2837SJed Brown 
1071089b2837SJed Brown .keywords: TS, nonlinear, get, function
1072089b2837SJed Brown 
1073089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1074089b2837SJed Brown @*/
1075089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1076089b2837SJed Brown {
1077089b2837SJed Brown   PetscErrorCode ierr;
1078089b2837SJed Brown   SNES           snes;
107924989b8cSPeter Brune   DM             dm;
1080089b2837SJed Brown 
1081089b2837SJed Brown   PetscFunctionBegin;
1082089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1083089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10840298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
108524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
108624989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1087316643e7SJed Brown   PetscFunctionReturn(0);
1088316643e7SJed Brown }
1089316643e7SJed Brown 
1090316643e7SJed Brown #undef __FUNCT__
1091316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1092316643e7SJed Brown /*@C
1093a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1094a4f0a591SBarry Smith         you provided with TSSetIFunction().
1095316643e7SJed Brown 
10963f9fe445SBarry Smith    Logically Collective on TS
1097316643e7SJed Brown 
1098316643e7SJed Brown    Input Parameters:
1099316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1100316643e7SJed Brown .  A   - Jacobian matrix
1101316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
1102316643e7SJed Brown .  f   - the Jacobian evaluation routine
11030298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1104316643e7SJed Brown 
1105316643e7SJed Brown    Calling sequence of f:
11061b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
1107316643e7SJed Brown 
1108316643e7SJed Brown +  t    - time at step/stage being solved
11091b4a444bSJed Brown .  U    - state vector
11101b4a444bSJed Brown .  U_t  - time derivative of state vector
1111316643e7SJed Brown .  a    - shift
1112b5abc632SBarry Smith .  A    - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1113316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
1114316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1115316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1116316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1117316643e7SJed Brown 
1118316643e7SJed Brown    Notes:
1119316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
1120316643e7SJed Brown 
1121a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1122b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1123a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1124a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1125a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1126a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1127a4f0a591SBarry Smith 
1128316643e7SJed Brown    Level: beginner
1129316643e7SJed Brown 
1130316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1131316643e7SJed Brown 
1132ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
1133316643e7SJed Brown 
1134316643e7SJed Brown @*/
11357087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1136316643e7SJed Brown {
1137316643e7SJed Brown   PetscErrorCode ierr;
1138089b2837SJed Brown   SNES           snes;
113924989b8cSPeter Brune   DM             dm;
1140316643e7SJed Brown 
1141316643e7SJed Brown   PetscFunctionBegin;
11420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11430700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
11440700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1145316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1146316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
114724989b8cSPeter Brune 
114824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
114924989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
115024989b8cSPeter Brune 
1151089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1152089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1153316643e7SJed Brown   PetscFunctionReturn(0);
1154316643e7SJed Brown }
1155316643e7SJed Brown 
11564a2ae208SSatish Balay #undef __FUNCT__
115755849f57SBarry Smith #define __FUNCT__ "TSLoad"
115855849f57SBarry Smith /*@C
115955849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
116055849f57SBarry Smith 
116155849f57SBarry Smith   Collective on PetscViewer
116255849f57SBarry Smith 
116355849f57SBarry Smith   Input Parameters:
116455849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
116555849f57SBarry Smith            some related function before a call to TSLoad().
116655849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
116755849f57SBarry Smith 
116855849f57SBarry Smith    Level: intermediate
116955849f57SBarry Smith 
117055849f57SBarry Smith   Notes:
117155849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
117255849f57SBarry Smith 
117355849f57SBarry Smith   Notes for advanced users:
117455849f57SBarry Smith   Most users should not need to know the details of the binary storage
117555849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
117655849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
117755849f57SBarry Smith   format is
117855849f57SBarry Smith .vb
117955849f57SBarry Smith      has not yet been determined
118055849f57SBarry Smith .ve
118155849f57SBarry Smith 
118255849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
118355849f57SBarry Smith @*/
1184f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
118555849f57SBarry Smith {
118655849f57SBarry Smith   PetscErrorCode ierr;
118755849f57SBarry Smith   PetscBool      isbinary;
118855849f57SBarry Smith   PetscInt       classid;
118955849f57SBarry Smith   char           type[256];
11902d53ad75SBarry Smith   DMTS           sdm;
1191ad6bc421SBarry Smith   DM             dm;
119255849f57SBarry Smith 
119355849f57SBarry Smith   PetscFunctionBegin;
1194f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
119555849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
119655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
119755849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
119855849f57SBarry Smith 
119955849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1200ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
120155849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1202f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1203f2c2a1b9SBarry Smith   if (ts->ops->load) {
1204f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1205f2c2a1b9SBarry Smith   }
1206ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1207ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1208ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1209f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1210f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
12112d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12122d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
121355849f57SBarry Smith   PetscFunctionReturn(0);
121455849f57SBarry Smith }
121555849f57SBarry Smith 
12169804daf3SBarry Smith #include <petscdraw.h>
121755849f57SBarry Smith #undef __FUNCT__
12184a2ae208SSatish Balay #define __FUNCT__ "TSView"
12197e2c5f70SBarry Smith /*@C
1220d763cef2SBarry Smith     TSView - Prints the TS data structure.
1221d763cef2SBarry Smith 
12224c49b128SBarry Smith     Collective on TS
1223d763cef2SBarry Smith 
1224d763cef2SBarry Smith     Input Parameters:
1225d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1226d763cef2SBarry Smith -   viewer - visualization context
1227d763cef2SBarry Smith 
1228d763cef2SBarry Smith     Options Database Key:
1229d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1230d763cef2SBarry Smith 
1231d763cef2SBarry Smith     Notes:
1232d763cef2SBarry Smith     The available visualization contexts include
1233b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1234b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1235d763cef2SBarry Smith          output where only the first processor opens
1236d763cef2SBarry Smith          the file.  All other processors send their
1237d763cef2SBarry Smith          data to the first processor to print.
1238d763cef2SBarry Smith 
1239d763cef2SBarry Smith     The user can open an alternative visualization context with
1240b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1241d763cef2SBarry Smith 
1242d763cef2SBarry Smith     Level: beginner
1243d763cef2SBarry Smith 
1244d763cef2SBarry Smith .keywords: TS, timestep, view
1245d763cef2SBarry Smith 
1246b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1247d763cef2SBarry Smith @*/
12487087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1249d763cef2SBarry Smith {
1250dfbe8321SBarry Smith   PetscErrorCode ierr;
125119fd82e9SBarry Smith   TSType         type;
12522b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
12532d53ad75SBarry Smith   DMTS           sdm;
1254d763cef2SBarry Smith 
1255d763cef2SBarry Smith   PetscFunctionBegin;
12560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12573050cee2SBarry Smith   if (!viewer) {
1258ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
12593050cee2SBarry Smith   }
12600700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1261c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1262fd16b177SBarry Smith 
1263251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1264251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
126555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
12662b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
126732077d6dSBarry Smith   if (iascii) {
1268317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
126977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1270a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1271d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
12725ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1273c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1274d763cef2SBarry Smith     }
12755ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1276193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
12772d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12782d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1279d52bd9f3SBarry Smith     if (ts->ops->view) {
1280d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1281d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1282d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1283d52bd9f3SBarry Smith     }
12840f5bd95cSBarry Smith   } else if (isstring) {
1285a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1286b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
128755849f57SBarry Smith   } else if (isbinary) {
128855849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
128955849f57SBarry Smith     MPI_Comm    comm;
129055849f57SBarry Smith     PetscMPIInt rank;
129155849f57SBarry Smith     char        type[256];
129255849f57SBarry Smith 
129355849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
129455849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
129555849f57SBarry Smith     if (!rank) {
129655849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
129755849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
129855849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
129955849f57SBarry Smith     }
130055849f57SBarry Smith     if (ts->ops->view) {
130155849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
130255849f57SBarry Smith     }
1303f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1304f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
13052d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13062d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
13072b0a91c0SBarry Smith   } else if (isdraw) {
13082b0a91c0SBarry Smith     PetscDraw draw;
13092b0a91c0SBarry Smith     char      str[36];
131089fd9fafSBarry Smith     PetscReal x,y,bottom,h;
13112b0a91c0SBarry Smith 
13122b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
13132b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
13142b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
13152b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
13160298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
131789fd9fafSBarry Smith     bottom = y - h;
13182b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
13192b0a91c0SBarry Smith     if (ts->ops->view) {
13202b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
13212b0a91c0SBarry Smith     }
13222b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1323d763cef2SBarry Smith   }
1324b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1325251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1326b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1327d763cef2SBarry Smith   PetscFunctionReturn(0);
1328d763cef2SBarry Smith }
1329d763cef2SBarry Smith 
1330d763cef2SBarry Smith 
13314a2ae208SSatish Balay #undef __FUNCT__
13324a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1333b07ff414SBarry Smith /*@
1334d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1335d763cef2SBarry Smith    the timesteppers.
1336d763cef2SBarry Smith 
13373f9fe445SBarry Smith    Logically Collective on TS
1338d763cef2SBarry Smith 
1339d763cef2SBarry Smith    Input Parameters:
1340d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1341d763cef2SBarry Smith -  usrP - optional user context
1342d763cef2SBarry Smith 
1343d763cef2SBarry Smith    Level: intermediate
1344d763cef2SBarry Smith 
1345d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1346d763cef2SBarry Smith 
1347d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1348d763cef2SBarry Smith @*/
13497087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1350d763cef2SBarry Smith {
1351d763cef2SBarry Smith   PetscFunctionBegin;
13520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1353d763cef2SBarry Smith   ts->user = usrP;
1354d763cef2SBarry Smith   PetscFunctionReturn(0);
1355d763cef2SBarry Smith }
1356d763cef2SBarry Smith 
13574a2ae208SSatish Balay #undef __FUNCT__
13584a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1359b07ff414SBarry Smith /*@
1360d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1361d763cef2SBarry Smith     timestepper.
1362d763cef2SBarry Smith 
1363d763cef2SBarry Smith     Not Collective
1364d763cef2SBarry Smith 
1365d763cef2SBarry Smith     Input Parameter:
1366d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1367d763cef2SBarry Smith 
1368d763cef2SBarry Smith     Output Parameter:
1369d763cef2SBarry Smith .   usrP - user context
1370d763cef2SBarry Smith 
1371d763cef2SBarry Smith     Level: intermediate
1372d763cef2SBarry Smith 
1373d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1374d763cef2SBarry Smith 
1375d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1376d763cef2SBarry Smith @*/
1377e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1378d763cef2SBarry Smith {
1379d763cef2SBarry Smith   PetscFunctionBegin;
13800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1381e71120c6SJed Brown   *(void**)usrP = ts->user;
1382d763cef2SBarry Smith   PetscFunctionReturn(0);
1383d763cef2SBarry Smith }
1384d763cef2SBarry Smith 
13854a2ae208SSatish Balay #undef __FUNCT__
13864a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1387d763cef2SBarry Smith /*@
1388b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1389d763cef2SBarry Smith 
1390d763cef2SBarry Smith    Not Collective
1391d763cef2SBarry Smith 
1392d763cef2SBarry Smith    Input Parameter:
1393d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1394d763cef2SBarry Smith 
1395d763cef2SBarry Smith    Output Parameter:
1396b8123daeSJed Brown .  iter - number of steps completed so far
1397d763cef2SBarry Smith 
1398d763cef2SBarry Smith    Level: intermediate
1399d763cef2SBarry Smith 
1400d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1401b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1402d763cef2SBarry Smith @*/
14037087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1404d763cef2SBarry Smith {
1405d763cef2SBarry Smith   PetscFunctionBegin;
14060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14074482741eSBarry Smith   PetscValidIntPointer(iter,2);
1408d763cef2SBarry Smith   *iter = ts->steps;
1409d763cef2SBarry Smith   PetscFunctionReturn(0);
1410d763cef2SBarry Smith }
1411d763cef2SBarry Smith 
14124a2ae208SSatish Balay #undef __FUNCT__
14134a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1414d763cef2SBarry Smith /*@
1415d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1416d763cef2SBarry Smith    as well as the initial time.
1417d763cef2SBarry Smith 
14183f9fe445SBarry Smith    Logically Collective on TS
1419d763cef2SBarry Smith 
1420d763cef2SBarry Smith    Input Parameters:
1421d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1422d763cef2SBarry Smith .  initial_time - the initial time
1423d763cef2SBarry Smith -  time_step - the size of the timestep
1424d763cef2SBarry Smith 
1425d763cef2SBarry Smith    Level: intermediate
1426d763cef2SBarry Smith 
1427d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1428d763cef2SBarry Smith 
1429d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1430d763cef2SBarry Smith @*/
14317087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1432d763cef2SBarry Smith {
1433e144a568SJed Brown   PetscErrorCode ierr;
1434e144a568SJed Brown 
1435d763cef2SBarry Smith   PetscFunctionBegin;
14360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1437e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1438d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1439d763cef2SBarry Smith   PetscFunctionReturn(0);
1440d763cef2SBarry Smith }
1441d763cef2SBarry Smith 
14424a2ae208SSatish Balay #undef __FUNCT__
14434a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1444d763cef2SBarry Smith /*@
1445d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1446d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1447d763cef2SBarry Smith 
14483f9fe445SBarry Smith    Logically Collective on TS
1449d763cef2SBarry Smith 
1450d763cef2SBarry Smith    Input Parameters:
1451d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1452d763cef2SBarry Smith -  time_step - the size of the timestep
1453d763cef2SBarry Smith 
1454d763cef2SBarry Smith    Level: intermediate
1455d763cef2SBarry Smith 
1456d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1457d763cef2SBarry Smith 
1458d763cef2SBarry Smith .keywords: TS, set, timestep
1459d763cef2SBarry Smith @*/
14607087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1461d763cef2SBarry Smith {
1462d763cef2SBarry Smith   PetscFunctionBegin;
14630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1464c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1465d763cef2SBarry Smith   ts->time_step      = time_step;
146631748224SBarry Smith   ts->time_step_orig = time_step;
1467d763cef2SBarry Smith   PetscFunctionReturn(0);
1468d763cef2SBarry Smith }
1469d763cef2SBarry Smith 
14704a2ae208SSatish Balay #undef __FUNCT__
1471a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1472a43b19c4SJed Brown /*@
147349354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
147449354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
147549354f04SShri Abhyankar      or just return at the final time TS computed.
1476a43b19c4SJed Brown 
1477a43b19c4SJed Brown   Logically Collective on TS
1478a43b19c4SJed Brown 
1479a43b19c4SJed Brown    Input Parameter:
1480a43b19c4SJed Brown +   ts - the time-step context
148149354f04SShri Abhyankar -   eftopt - exact final time option
1482a43b19c4SJed Brown 
1483a43b19c4SJed Brown    Level: beginner
1484a43b19c4SJed Brown 
1485a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1486a43b19c4SJed Brown @*/
148749354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1488a43b19c4SJed Brown {
1489a43b19c4SJed Brown   PetscFunctionBegin;
1490a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
149149354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
149249354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1493a43b19c4SJed Brown   PetscFunctionReturn(0);
1494a43b19c4SJed Brown }
1495a43b19c4SJed Brown 
1496a43b19c4SJed Brown #undef __FUNCT__
14974a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1498d763cef2SBarry Smith /*@
1499d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1500d763cef2SBarry Smith 
1501d763cef2SBarry Smith    Not Collective
1502d763cef2SBarry Smith 
1503d763cef2SBarry Smith    Input Parameter:
1504d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1505d763cef2SBarry Smith 
1506d763cef2SBarry Smith    Output Parameter:
1507d763cef2SBarry Smith .  dt - the current timestep size
1508d763cef2SBarry Smith 
1509d763cef2SBarry Smith    Level: intermediate
1510d763cef2SBarry Smith 
1511d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1512d763cef2SBarry Smith 
1513d763cef2SBarry Smith .keywords: TS, get, timestep
1514d763cef2SBarry Smith @*/
15157087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1516d763cef2SBarry Smith {
1517d763cef2SBarry Smith   PetscFunctionBegin;
15180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1519f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1520d763cef2SBarry Smith   *dt = ts->time_step;
1521d763cef2SBarry Smith   PetscFunctionReturn(0);
1522d763cef2SBarry Smith }
1523d763cef2SBarry Smith 
15244a2ae208SSatish Balay #undef __FUNCT__
15254a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1526d8e5e3e6SSatish Balay /*@
1527d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1528d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1529d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1530d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1531d763cef2SBarry Smith 
1532d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1533d763cef2SBarry Smith 
1534d763cef2SBarry Smith    Input Parameter:
1535d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1536d763cef2SBarry Smith 
1537d763cef2SBarry Smith    Output Parameter:
1538d763cef2SBarry Smith .  v - the vector containing the solution
1539d763cef2SBarry Smith 
1540d763cef2SBarry Smith    Level: intermediate
1541d763cef2SBarry Smith 
1542d763cef2SBarry Smith .seealso: TSGetTimeStep()
1543d763cef2SBarry Smith 
1544d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1545d763cef2SBarry Smith @*/
15467087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1547d763cef2SBarry Smith {
1548d763cef2SBarry Smith   PetscFunctionBegin;
15490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15504482741eSBarry Smith   PetscValidPointer(v,2);
15518737fe31SLisandro Dalcin   *v = ts->vec_sol;
1552d763cef2SBarry Smith   PetscFunctionReturn(0);
1553d763cef2SBarry Smith }
1554d763cef2SBarry Smith 
1555bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
15564a2ae208SSatish Balay #undef __FUNCT__
1557bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1558d8e5e3e6SSatish Balay /*@
1559bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1560d763cef2SBarry Smith 
1561bdad233fSMatthew Knepley   Not collective
1562d763cef2SBarry Smith 
1563bdad233fSMatthew Knepley   Input Parameters:
1564bdad233fSMatthew Knepley + ts   - The TS
1565bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1566d763cef2SBarry Smith .vb
15670910c330SBarry Smith          U_t - A U = 0      (linear)
15680910c330SBarry Smith          U_t - A(t) U = 0   (linear)
15690910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1570d763cef2SBarry Smith .ve
1571d763cef2SBarry Smith 
1572d763cef2SBarry Smith    Level: beginner
1573d763cef2SBarry Smith 
1574bdad233fSMatthew Knepley .keywords: TS, problem type
1575bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1576d763cef2SBarry Smith @*/
15777087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1578a7cc72afSBarry Smith {
15799e2a6581SJed Brown   PetscErrorCode ierr;
15809e2a6581SJed Brown 
1581d763cef2SBarry Smith   PetscFunctionBegin;
15820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1583bdad233fSMatthew Knepley   ts->problem_type = type;
15849e2a6581SJed Brown   if (type == TS_LINEAR) {
15859e2a6581SJed Brown     SNES snes;
15869e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
15879e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
15889e2a6581SJed Brown   }
1589d763cef2SBarry Smith   PetscFunctionReturn(0);
1590d763cef2SBarry Smith }
1591d763cef2SBarry Smith 
1592bdad233fSMatthew Knepley #undef __FUNCT__
1593bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1594bdad233fSMatthew Knepley /*@C
1595bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1596bdad233fSMatthew Knepley 
1597bdad233fSMatthew Knepley   Not collective
1598bdad233fSMatthew Knepley 
1599bdad233fSMatthew Knepley   Input Parameter:
1600bdad233fSMatthew Knepley . ts   - The TS
1601bdad233fSMatthew Knepley 
1602bdad233fSMatthew Knepley   Output Parameter:
1603bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1604bdad233fSMatthew Knepley .vb
1605089b2837SJed Brown          M U_t = A U
1606089b2837SJed Brown          M(t) U_t = A(t) U
1607b5abc632SBarry Smith          F(t,U,U_t)
1608bdad233fSMatthew Knepley .ve
1609bdad233fSMatthew Knepley 
1610bdad233fSMatthew Knepley    Level: beginner
1611bdad233fSMatthew Knepley 
1612bdad233fSMatthew Knepley .keywords: TS, problem type
1613bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1614bdad233fSMatthew Knepley @*/
16157087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1616a7cc72afSBarry Smith {
1617bdad233fSMatthew Knepley   PetscFunctionBegin;
16180700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
16194482741eSBarry Smith   PetscValidIntPointer(type,2);
1620bdad233fSMatthew Knepley   *type = ts->problem_type;
1621bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1622bdad233fSMatthew Knepley }
1623d763cef2SBarry Smith 
16244a2ae208SSatish Balay #undef __FUNCT__
16254a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1626d763cef2SBarry Smith /*@
1627d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1628d763cef2SBarry Smith    of a timestepper.
1629d763cef2SBarry Smith 
1630d763cef2SBarry Smith    Collective on TS
1631d763cef2SBarry Smith 
1632d763cef2SBarry Smith    Input Parameter:
1633d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1634d763cef2SBarry Smith 
1635d763cef2SBarry Smith    Notes:
1636d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1637d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1638d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1639d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1640d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1641d763cef2SBarry Smith 
1642d763cef2SBarry Smith    Level: advanced
1643d763cef2SBarry Smith 
1644d763cef2SBarry Smith .keywords: TS, timestep, setup
1645d763cef2SBarry Smith 
1646d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1647d763cef2SBarry Smith @*/
16487087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1649d763cef2SBarry Smith {
1650dfbe8321SBarry Smith   PetscErrorCode ierr;
16516c6b9e74SPeter Brune   DM             dm;
16526c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
16536c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
16546c6b9e74SPeter Brune   TSIJacobian    ijac;
16556c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1656d763cef2SBarry Smith 
1657d763cef2SBarry Smith   PetscFunctionBegin;
16580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1659277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1660277b19d0SLisandro Dalcin 
16617adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
16629596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1663d763cef2SBarry Smith   }
1664277b19d0SLisandro Dalcin 
1665277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1666277b19d0SLisandro Dalcin 
1667ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr);
16681c3436cfSJed Brown 
1669277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1670000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1671277b19d0SLisandro Dalcin   }
1672277b19d0SLisandro Dalcin 
16736c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
16746c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
16756c6b9e74SPeter Brune    */
16766c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
16770298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
16786c6b9e74SPeter Brune   if (!func) {
16796c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
16806c6b9e74SPeter Brune   }
16816c6b9e74SPeter 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.
16826c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
16836c6b9e74SPeter Brune    */
16840298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
16850298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
16860298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
16876c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
16886c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
16896c6b9e74SPeter Brune   }
1690277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1691277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1692277b19d0SLisandro Dalcin }
1693277b19d0SLisandro Dalcin 
1694277b19d0SLisandro Dalcin #undef __FUNCT__
1695277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1696277b19d0SLisandro Dalcin /*@
1697277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1698277b19d0SLisandro Dalcin 
1699277b19d0SLisandro Dalcin    Collective on TS
1700277b19d0SLisandro Dalcin 
1701277b19d0SLisandro Dalcin    Input Parameter:
1702277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1703277b19d0SLisandro Dalcin 
1704277b19d0SLisandro Dalcin    Level: beginner
1705277b19d0SLisandro Dalcin 
1706277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1707277b19d0SLisandro Dalcin 
1708277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1709277b19d0SLisandro Dalcin @*/
1710277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1711277b19d0SLisandro Dalcin {
1712277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1713277b19d0SLisandro Dalcin 
1714277b19d0SLisandro Dalcin   PetscFunctionBegin;
1715277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1716277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1717277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1718277b19d0SLisandro Dalcin   }
1719277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1720bbd56ea5SKarl Rupp 
17214e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
17224e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1723214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
17246bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1725e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1726e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
172738637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1728bbd56ea5SKarl Rupp 
1729277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1730d763cef2SBarry Smith   PetscFunctionReturn(0);
1731d763cef2SBarry Smith }
1732d763cef2SBarry Smith 
17334a2ae208SSatish Balay #undef __FUNCT__
17344a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1735d8e5e3e6SSatish Balay /*@
1736d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1737d763cef2SBarry Smith    with TSCreate().
1738d763cef2SBarry Smith 
1739d763cef2SBarry Smith    Collective on TS
1740d763cef2SBarry Smith 
1741d763cef2SBarry Smith    Input Parameter:
1742d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1743d763cef2SBarry Smith 
1744d763cef2SBarry Smith    Level: beginner
1745d763cef2SBarry Smith 
1746d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1747d763cef2SBarry Smith 
1748d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1749d763cef2SBarry Smith @*/
17506bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1751d763cef2SBarry Smith {
17526849ba73SBarry Smith   PetscErrorCode ierr;
1753d763cef2SBarry Smith 
1754d763cef2SBarry Smith   PetscFunctionBegin;
17556bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
17566bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
17576bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1758d763cef2SBarry Smith 
17596bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1760277b19d0SLisandro Dalcin 
1761be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
17626bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
17636bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
17646d4c513bSLisandro Dalcin 
176584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
17666bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
17676bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
17686bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
17696d4c513bSLisandro Dalcin 
1770a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1771d763cef2SBarry Smith   PetscFunctionReturn(0);
1772d763cef2SBarry Smith }
1773d763cef2SBarry Smith 
17744a2ae208SSatish Balay #undef __FUNCT__
17754a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1776d8e5e3e6SSatish Balay /*@
1777d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1778d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1779d763cef2SBarry Smith 
1780d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1781d763cef2SBarry Smith 
1782d763cef2SBarry Smith    Input Parameter:
1783d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1784d763cef2SBarry Smith 
1785d763cef2SBarry Smith    Output Parameter:
1786d763cef2SBarry Smith .  snes - the nonlinear solver context
1787d763cef2SBarry Smith 
1788d763cef2SBarry Smith    Notes:
1789d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1790d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
179194b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1792d763cef2SBarry Smith 
1793d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
17940298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
1795d763cef2SBarry Smith 
1796d763cef2SBarry Smith    Level: beginner
1797d763cef2SBarry Smith 
1798d763cef2SBarry Smith .keywords: timestep, get, SNES
1799d763cef2SBarry Smith @*/
18007087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1801d763cef2SBarry Smith {
1802d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1803d372ba47SLisandro Dalcin 
1804d763cef2SBarry Smith   PetscFunctionBegin;
18050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18064482741eSBarry Smith   PetscValidPointer(snes,2);
1807d372ba47SLisandro Dalcin   if (!ts->snes) {
1808ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
18090298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1810d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1811d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1812496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
18139e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
18149e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
18159e2a6581SJed Brown     }
1816d372ba47SLisandro Dalcin   }
1817d763cef2SBarry Smith   *snes = ts->snes;
1818d763cef2SBarry Smith   PetscFunctionReturn(0);
1819d763cef2SBarry Smith }
1820d763cef2SBarry Smith 
18214a2ae208SSatish Balay #undef __FUNCT__
1822deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1823deb2cd25SJed Brown /*@
1824deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1825deb2cd25SJed Brown 
1826deb2cd25SJed Brown    Collective
1827deb2cd25SJed Brown 
1828deb2cd25SJed Brown    Input Parameter:
1829deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1830deb2cd25SJed Brown -  snes - the nonlinear solver context
1831deb2cd25SJed Brown 
1832deb2cd25SJed Brown    Notes:
1833deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1834deb2cd25SJed Brown 
1835deb2cd25SJed Brown    Level: developer
1836deb2cd25SJed Brown 
1837deb2cd25SJed Brown .keywords: timestep, set, SNES
1838deb2cd25SJed Brown @*/
1839deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1840deb2cd25SJed Brown {
1841deb2cd25SJed Brown   PetscErrorCode ierr;
1842740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1843deb2cd25SJed Brown 
1844deb2cd25SJed Brown   PetscFunctionBegin;
1845deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1846deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1847deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1848deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1849bbd56ea5SKarl Rupp 
1850deb2cd25SJed Brown   ts->snes = snes;
1851bbd56ea5SKarl Rupp 
18520298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
18530298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
1854740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
18550298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1856740132f1SEmil Constantinescu   }
1857deb2cd25SJed Brown   PetscFunctionReturn(0);
1858deb2cd25SJed Brown }
1859deb2cd25SJed Brown 
1860deb2cd25SJed Brown #undef __FUNCT__
186194b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1862d8e5e3e6SSatish Balay /*@
186394b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1864d763cef2SBarry Smith    a TS (timestepper) context.
1865d763cef2SBarry Smith 
186694b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1867d763cef2SBarry Smith 
1868d763cef2SBarry Smith    Input Parameter:
1869d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1870d763cef2SBarry Smith 
1871d763cef2SBarry Smith    Output Parameter:
187294b7f48cSBarry Smith .  ksp - the nonlinear solver context
1873d763cef2SBarry Smith 
1874d763cef2SBarry Smith    Notes:
187594b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1876d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1877d763cef2SBarry Smith    KSP and PC contexts as well.
1878d763cef2SBarry Smith 
187994b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
18800298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
1881d763cef2SBarry Smith 
1882d763cef2SBarry Smith    Level: beginner
1883d763cef2SBarry Smith 
188494b7f48cSBarry Smith .keywords: timestep, get, KSP
1885d763cef2SBarry Smith @*/
18867087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1887d763cef2SBarry Smith {
1888d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1889089b2837SJed Brown   SNES           snes;
1890d372ba47SLisandro Dalcin 
1891d763cef2SBarry Smith   PetscFunctionBegin;
18920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18934482741eSBarry Smith   PetscValidPointer(ksp,2);
189417186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1895e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1896089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1897089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1898d763cef2SBarry Smith   PetscFunctionReturn(0);
1899d763cef2SBarry Smith }
1900d763cef2SBarry Smith 
1901d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1902d763cef2SBarry Smith 
19034a2ae208SSatish Balay #undef __FUNCT__
1904adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1905adb62b0dSMatthew Knepley /*@
1906adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1907adb62b0dSMatthew Knepley    maximum time for iteration.
1908adb62b0dSMatthew Knepley 
19093f9fe445SBarry Smith    Not Collective
1910adb62b0dSMatthew Knepley 
1911adb62b0dSMatthew Knepley    Input Parameters:
1912adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
19130298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
19140298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
1915adb62b0dSMatthew Knepley 
1916adb62b0dSMatthew Knepley    Level: intermediate
1917adb62b0dSMatthew Knepley 
1918adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1919adb62b0dSMatthew Knepley @*/
19207087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1921adb62b0dSMatthew Knepley {
1922adb62b0dSMatthew Knepley   PetscFunctionBegin;
19230700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1924abc0a331SBarry Smith   if (maxsteps) {
19254482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1926adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1927adb62b0dSMatthew Knepley   }
1928abc0a331SBarry Smith   if (maxtime) {
19294482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1930adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
1931adb62b0dSMatthew Knepley   }
1932adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1933adb62b0dSMatthew Knepley }
1934adb62b0dSMatthew Knepley 
1935adb62b0dSMatthew Knepley #undef __FUNCT__
19364a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1937d763cef2SBarry Smith /*@
1938d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1939d763cef2SBarry Smith    maximum time for iteration.
1940d763cef2SBarry Smith 
19413f9fe445SBarry Smith    Logically Collective on TS
1942d763cef2SBarry Smith 
1943d763cef2SBarry Smith    Input Parameters:
1944d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1945d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1946d763cef2SBarry Smith -  maxtime - final time to iterate to
1947d763cef2SBarry Smith 
1948d763cef2SBarry Smith    Options Database Keys:
1949d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
19503bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1951d763cef2SBarry Smith 
1952d763cef2SBarry Smith    Notes:
1953d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1954d763cef2SBarry Smith 
1955d763cef2SBarry Smith    Level: intermediate
1956d763cef2SBarry Smith 
1957d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1958a43b19c4SJed Brown 
1959a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1960d763cef2SBarry Smith @*/
19617087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1962d763cef2SBarry Smith {
1963d763cef2SBarry Smith   PetscFunctionBegin;
19640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1965c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1966c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
196739b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
196839b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
1969d763cef2SBarry Smith   PetscFunctionReturn(0);
1970d763cef2SBarry Smith }
1971d763cef2SBarry Smith 
19724a2ae208SSatish Balay #undef __FUNCT__
19734a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1974d763cef2SBarry Smith /*@
1975d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1976d763cef2SBarry Smith    for use by the TS routines.
1977d763cef2SBarry Smith 
19783f9fe445SBarry Smith    Logically Collective on TS and Vec
1979d763cef2SBarry Smith 
1980d763cef2SBarry Smith    Input Parameters:
1981d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
19820910c330SBarry Smith -  u - the solution vector
1983d763cef2SBarry Smith 
1984d763cef2SBarry Smith    Level: beginner
1985d763cef2SBarry Smith 
1986d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1987d763cef2SBarry Smith @*/
19880910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
1989d763cef2SBarry Smith {
19908737fe31SLisandro Dalcin   PetscErrorCode ierr;
1991496e6a7aSJed Brown   DM             dm;
19928737fe31SLisandro Dalcin 
1993d763cef2SBarry Smith   PetscFunctionBegin;
19940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19950910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
19960910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
19976bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1998bbd56ea5SKarl Rupp 
19990910c330SBarry Smith   ts->vec_sol = u;
2000bbd56ea5SKarl Rupp 
2001496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
20020910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2003d763cef2SBarry Smith   PetscFunctionReturn(0);
2004d763cef2SBarry Smith }
2005d763cef2SBarry Smith 
2006e74ef692SMatthew Knepley #undef __FUNCT__
2007e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2008ac226902SBarry Smith /*@C
2009000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
20103f2090d5SJed Brown   called once at the beginning of each time step.
2011000e7ae3SMatthew Knepley 
20123f9fe445SBarry Smith   Logically Collective on TS
2013000e7ae3SMatthew Knepley 
2014000e7ae3SMatthew Knepley   Input Parameters:
2015000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2016000e7ae3SMatthew Knepley - func - The function
2017000e7ae3SMatthew Knepley 
2018000e7ae3SMatthew Knepley   Calling sequence of func:
2019000e7ae3SMatthew Knepley . func (TS ts);
2020000e7ae3SMatthew Knepley 
2021000e7ae3SMatthew Knepley   Level: intermediate
2022000e7ae3SMatthew Knepley 
2023b8123daeSJed Brown   Note:
2024b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2025b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2026b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2027b8123daeSJed Brown 
2028000e7ae3SMatthew Knepley .keywords: TS, timestep
2029b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
2030000e7ae3SMatthew Knepley @*/
20317087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2032000e7ae3SMatthew Knepley {
2033000e7ae3SMatthew Knepley   PetscFunctionBegin;
20340700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2035000e7ae3SMatthew Knepley   ts->ops->prestep = func;
2036000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2037000e7ae3SMatthew Knepley }
2038000e7ae3SMatthew Knepley 
2039e74ef692SMatthew Knepley #undef __FUNCT__
20403f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
204109ee8438SJed Brown /*@
20423f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
20433f2090d5SJed Brown 
20443f2090d5SJed Brown   Collective on TS
20453f2090d5SJed Brown 
20463f2090d5SJed Brown   Input Parameters:
20473f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
20483f2090d5SJed Brown 
20493f2090d5SJed Brown   Notes:
20503f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
20513f2090d5SJed Brown   so most users would not generally call this routine themselves.
20523f2090d5SJed Brown 
20533f2090d5SJed Brown   Level: developer
20543f2090d5SJed Brown 
20553f2090d5SJed Brown .keywords: TS, timestep
2056b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
20573f2090d5SJed Brown @*/
20587087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
20593f2090d5SJed Brown {
20603f2090d5SJed Brown   PetscErrorCode ierr;
20613f2090d5SJed Brown 
20623f2090d5SJed Brown   PetscFunctionBegin;
20630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
206472ac3e02SJed Brown   if (ts->ops->prestep) {
20653f2090d5SJed Brown     PetscStackPush("TS PreStep function");
20663f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
20673f2090d5SJed Brown     PetscStackPop;
2068312ce896SJed Brown   }
20693f2090d5SJed Brown   PetscFunctionReturn(0);
20703f2090d5SJed Brown }
20713f2090d5SJed Brown 
20723f2090d5SJed Brown #undef __FUNCT__
2073b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2074b8123daeSJed Brown /*@C
2075b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2076b8123daeSJed Brown   called once at the beginning of each stage.
2077b8123daeSJed Brown 
2078b8123daeSJed Brown   Logically Collective on TS
2079b8123daeSJed Brown 
2080b8123daeSJed Brown   Input Parameters:
2081b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2082b8123daeSJed Brown - func - The function
2083b8123daeSJed Brown 
2084b8123daeSJed Brown   Calling sequence of func:
2085b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2086b8123daeSJed Brown 
2087b8123daeSJed Brown   Level: intermediate
2088b8123daeSJed Brown 
2089b8123daeSJed Brown   Note:
2090b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2091b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2092b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2093b8123daeSJed Brown 
2094b8123daeSJed Brown .keywords: TS, timestep
2095b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2096b8123daeSJed Brown @*/
2097b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2098b8123daeSJed Brown {
2099b8123daeSJed Brown   PetscFunctionBegin;
2100b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2101b8123daeSJed Brown   ts->ops->prestage = func;
2102b8123daeSJed Brown   PetscFunctionReturn(0);
2103b8123daeSJed Brown }
2104b8123daeSJed Brown 
2105b8123daeSJed Brown #undef __FUNCT__
2106b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2107b8123daeSJed Brown /*@
2108b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2109b8123daeSJed Brown 
2110b8123daeSJed Brown   Collective on TS
2111b8123daeSJed Brown 
2112b8123daeSJed Brown   Input Parameters:
2113b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2114b8123daeSJed Brown 
2115b8123daeSJed Brown   Notes:
2116b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2117b8123daeSJed Brown   most users would not generally call this routine themselves.
2118b8123daeSJed Brown 
2119b8123daeSJed Brown   Level: developer
2120b8123daeSJed Brown 
2121b8123daeSJed Brown .keywords: TS, timestep
2122b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2123b8123daeSJed Brown @*/
2124b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2125b8123daeSJed Brown {
2126b8123daeSJed Brown   PetscErrorCode ierr;
2127b8123daeSJed Brown 
2128b8123daeSJed Brown   PetscFunctionBegin;
2129b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2130b8123daeSJed Brown   if (ts->ops->prestage) {
2131b8123daeSJed Brown     PetscStackPush("TS PreStage function");
2132b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
2133b8123daeSJed Brown     PetscStackPop;
2134b8123daeSJed Brown   }
2135b8123daeSJed Brown   PetscFunctionReturn(0);
2136b8123daeSJed Brown }
2137b8123daeSJed Brown 
2138b8123daeSJed Brown #undef __FUNCT__
2139e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2140ac226902SBarry Smith /*@C
2141000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
21423f2090d5SJed Brown   called once at the end of each time step.
2143000e7ae3SMatthew Knepley 
21443f9fe445SBarry Smith   Logically Collective on TS
2145000e7ae3SMatthew Knepley 
2146000e7ae3SMatthew Knepley   Input Parameters:
2147000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2148000e7ae3SMatthew Knepley - func - The function
2149000e7ae3SMatthew Knepley 
2150000e7ae3SMatthew Knepley   Calling sequence of func:
2151b8123daeSJed Brown $ func (TS ts);
2152000e7ae3SMatthew Knepley 
2153000e7ae3SMatthew Knepley   Level: intermediate
2154000e7ae3SMatthew Knepley 
2155000e7ae3SMatthew Knepley .keywords: TS, timestep
2156b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2157000e7ae3SMatthew Knepley @*/
21587087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2159000e7ae3SMatthew Knepley {
2160000e7ae3SMatthew Knepley   PetscFunctionBegin;
21610700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2162000e7ae3SMatthew Knepley   ts->ops->poststep = func;
2163000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2164000e7ae3SMatthew Knepley }
2165000e7ae3SMatthew Knepley 
2166e74ef692SMatthew Knepley #undef __FUNCT__
21673f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
216809ee8438SJed Brown /*@
21693f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
21703f2090d5SJed Brown 
21713f2090d5SJed Brown   Collective on TS
21723f2090d5SJed Brown 
21733f2090d5SJed Brown   Input Parameters:
21743f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
21753f2090d5SJed Brown 
21763f2090d5SJed Brown   Notes:
21773f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
21783f2090d5SJed Brown   so most users would not generally call this routine themselves.
21793f2090d5SJed Brown 
21803f2090d5SJed Brown   Level: developer
21813f2090d5SJed Brown 
21823f2090d5SJed Brown .keywords: TS, timestep
21833f2090d5SJed Brown @*/
21847087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
21853f2090d5SJed Brown {
21863f2090d5SJed Brown   PetscErrorCode ierr;
21873f2090d5SJed Brown 
21883f2090d5SJed Brown   PetscFunctionBegin;
21890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
219072ac3e02SJed Brown   if (ts->ops->poststep) {
21913f2090d5SJed Brown     PetscStackPush("TS PostStep function");
21923f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
21933f2090d5SJed Brown     PetscStackPop;
219472ac3e02SJed Brown   }
21953f2090d5SJed Brown   PetscFunctionReturn(0);
21963f2090d5SJed Brown }
21973f2090d5SJed Brown 
2198d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2199d763cef2SBarry Smith 
22004a2ae208SSatish Balay #undef __FUNCT__
2201a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2202d763cef2SBarry Smith /*@C
2203a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2204d763cef2SBarry Smith    timestep to display the iteration's  progress.
2205d763cef2SBarry Smith 
22063f9fe445SBarry Smith    Logically Collective on TS
2207d763cef2SBarry Smith 
2208d763cef2SBarry Smith    Input Parameters:
2209d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2210e213d8f1SJed Brown .  monitor - monitoring routine
2211329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
22120298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2213b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
22140298fd71SBarry Smith           (may be NULL)
2215d763cef2SBarry Smith 
2216e213d8f1SJed Brown    Calling sequence of monitor:
22170910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2218d763cef2SBarry Smith 
2219d763cef2SBarry Smith +    ts - the TS context
222088c05cc5SBarry 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
222188c05cc5SBarry Smith                                been interpolated to)
22221f06c33eSBarry Smith .    time - current time
22230910c330SBarry Smith .    u - current iterate
2224d763cef2SBarry Smith -    mctx - [optional] monitoring context
2225d763cef2SBarry Smith 
2226d763cef2SBarry Smith    Notes:
2227d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2228d763cef2SBarry Smith    already has been loaded.
2229d763cef2SBarry Smith 
2230025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2231025f1a04SBarry Smith 
2232d763cef2SBarry Smith    Level: intermediate
2233d763cef2SBarry Smith 
2234d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2235d763cef2SBarry Smith 
2236a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2237d763cef2SBarry Smith @*/
2238c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2239d763cef2SBarry Smith {
2240d763cef2SBarry Smith   PetscFunctionBegin;
22410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
224217186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2243d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
22448704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2245d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2246d763cef2SBarry Smith   PetscFunctionReturn(0);
2247d763cef2SBarry Smith }
2248d763cef2SBarry Smith 
22494a2ae208SSatish Balay #undef __FUNCT__
2250a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2251d763cef2SBarry Smith /*@C
2252a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2253d763cef2SBarry Smith 
22543f9fe445SBarry Smith    Logically Collective on TS
2255d763cef2SBarry Smith 
2256d763cef2SBarry Smith    Input Parameters:
2257d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2258d763cef2SBarry Smith 
2259d763cef2SBarry Smith    Notes:
2260d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2261d763cef2SBarry Smith 
2262d763cef2SBarry Smith    Level: intermediate
2263d763cef2SBarry Smith 
2264d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2265d763cef2SBarry Smith 
2266a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2267d763cef2SBarry Smith @*/
22687087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2269d763cef2SBarry Smith {
2270d952e501SBarry Smith   PetscErrorCode ierr;
2271d952e501SBarry Smith   PetscInt       i;
2272d952e501SBarry Smith 
2273d763cef2SBarry Smith   PetscFunctionBegin;
22740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2275d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
22768704b422SBarry Smith     if (ts->monitordestroy[i]) {
22778704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2278d952e501SBarry Smith     }
2279d952e501SBarry Smith   }
2280d763cef2SBarry Smith   ts->numbermonitors = 0;
2281d763cef2SBarry Smith   PetscFunctionReturn(0);
2282d763cef2SBarry Smith }
2283d763cef2SBarry Smith 
22844a2ae208SSatish Balay #undef __FUNCT__
2285a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2286d8e5e3e6SSatish Balay /*@
2287a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
22885516499fSSatish Balay 
22895516499fSSatish Balay    Level: intermediate
229041251cbbSSatish Balay 
22915516499fSSatish Balay .keywords: TS, set, monitor
22925516499fSSatish Balay 
229341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
229441251cbbSSatish Balay @*/
2295649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2296d763cef2SBarry Smith {
2297dfbe8321SBarry Smith   PetscErrorCode ierr;
2298ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2299d132466eSBarry Smith 
2300d763cef2SBarry Smith   PetscFunctionBegin;
2301649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2302971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2303649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2304d763cef2SBarry Smith   PetscFunctionReturn(0);
2305d763cef2SBarry Smith }
2306d763cef2SBarry Smith 
23074a2ae208SSatish Balay #undef __FUNCT__
2308cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2309cd652676SJed Brown /*@
2310cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2311cd652676SJed Brown 
2312cd652676SJed Brown    Logically Collective on TS
2313cd652676SJed Brown 
2314cd652676SJed Brown    Input Argument:
2315cd652676SJed Brown .  ts - time stepping context
2316cd652676SJed Brown 
2317cd652676SJed Brown    Output Argument:
2318cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2319cd652676SJed Brown 
2320cd652676SJed Brown    Level: intermediate
2321cd652676SJed Brown 
2322cd652676SJed Brown .keywords: TS, set
2323cd652676SJed Brown 
2324cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2325cd652676SJed Brown @*/
2326cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2327cd652676SJed Brown {
2328cd652676SJed Brown   PetscFunctionBegin;
2329cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2330cd652676SJed Brown   ts->retain_stages = flg;
2331cd652676SJed Brown   PetscFunctionReturn(0);
2332cd652676SJed Brown }
2333cd652676SJed Brown 
2334cd652676SJed Brown #undef __FUNCT__
2335cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2336cd652676SJed Brown /*@
2337cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2338cd652676SJed Brown 
2339cd652676SJed Brown    Collective on TS
2340cd652676SJed Brown 
2341cd652676SJed Brown    Input Argument:
2342cd652676SJed Brown +  ts - time stepping context
2343cd652676SJed Brown -  t - time to interpolate to
2344cd652676SJed Brown 
2345cd652676SJed Brown    Output Argument:
23460910c330SBarry Smith .  U - state at given time
2347cd652676SJed Brown 
2348cd652676SJed Brown    Notes:
2349cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2350cd652676SJed Brown 
2351cd652676SJed Brown    Level: intermediate
2352cd652676SJed Brown 
2353cd652676SJed Brown    Developer Notes:
2354cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2355cd652676SJed Brown 
2356cd652676SJed Brown .keywords: TS, set
2357cd652676SJed Brown 
2358cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2359cd652676SJed Brown @*/
23600910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2361cd652676SJed Brown {
2362cd652676SJed Brown   PetscErrorCode ierr;
2363cd652676SJed Brown 
2364cd652676SJed Brown   PetscFunctionBegin;
2365cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2366ce94432eSBarry Smith   if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime);
2367ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
23680910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2369cd652676SJed Brown   PetscFunctionReturn(0);
2370cd652676SJed Brown }
2371cd652676SJed Brown 
2372cd652676SJed Brown #undef __FUNCT__
23734a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2374d763cef2SBarry Smith /*@
23756d9e5789SSean Farley    TSStep - Steps one time step
2376d763cef2SBarry Smith 
2377d763cef2SBarry Smith    Collective on TS
2378d763cef2SBarry Smith 
2379d763cef2SBarry Smith    Input Parameter:
2380d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2381d763cef2SBarry Smith 
23826d9e5789SSean Farley    Level: intermediate
2383d763cef2SBarry Smith 
2384b8123daeSJed Brown    Notes:
2385b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2386b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2387b8123daeSJed Brown 
238825cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
238925cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
239025cb2221SBarry Smith 
2391d763cef2SBarry Smith .keywords: TS, timestep, solve
2392d763cef2SBarry Smith 
239325cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2394d763cef2SBarry Smith @*/
2395193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2396d763cef2SBarry Smith {
2397362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2398dfbe8321SBarry Smith   PetscErrorCode ierr;
2399d763cef2SBarry Smith 
2400d763cef2SBarry Smith   PetscFunctionBegin;
24010700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2402d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2403d405a339SMatthew Knepley 
2404362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2405362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2406bbd56ea5SKarl Rupp 
2407d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2408193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2409d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2410bbd56ea5SKarl Rupp 
2411362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2412362cd11cSLisandro Dalcin 
2413362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2414cef5090cSJed Brown     if (ts->errorifstepfailed) {
2415cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2416ce94432eSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
2417ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2418cef5090cSJed Brown     }
2419362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2420db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2421db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2422362cd11cSLisandro Dalcin   }
2423d763cef2SBarry Smith   PetscFunctionReturn(0);
2424d763cef2SBarry Smith }
2425d763cef2SBarry Smith 
24264a2ae208SSatish Balay #undef __FUNCT__
242705175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
242805175c85SJed Brown /*@
242905175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
243005175c85SJed Brown 
24311c3436cfSJed Brown    Collective on TS
243205175c85SJed Brown 
243305175c85SJed Brown    Input Arguments:
24341c3436cfSJed Brown +  ts - time stepping context
24351c3436cfSJed Brown .  order - desired order of accuracy
24360298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
243705175c85SJed Brown 
243805175c85SJed Brown    Output Arguments:
24390910c330SBarry Smith .  U - state at the end of the current step
244005175c85SJed Brown 
244105175c85SJed Brown    Level: advanced
244205175c85SJed Brown 
2443108c343cSJed Brown    Notes:
2444108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2445108c343cSJed 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.
2446108c343cSJed Brown 
24471c3436cfSJed Brown .seealso: TSStep(), TSAdapt
244805175c85SJed Brown @*/
24490910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
245005175c85SJed Brown {
245105175c85SJed Brown   PetscErrorCode ierr;
245205175c85SJed Brown 
245305175c85SJed Brown   PetscFunctionBegin;
245405175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
245505175c85SJed Brown   PetscValidType(ts,1);
24560910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2457ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
24580910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
245905175c85SJed Brown   PetscFunctionReturn(0);
246005175c85SJed Brown }
246105175c85SJed Brown 
246205175c85SJed Brown #undef __FUNCT__
24636a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
24646a4d4014SLisandro Dalcin /*@
24656a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
24666a4d4014SLisandro Dalcin 
24676a4d4014SLisandro Dalcin    Collective on TS
24686a4d4014SLisandro Dalcin 
24696a4d4014SLisandro Dalcin    Input Parameter:
24706a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2471cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
24725a3a76d0SJed Brown 
24736a4d4014SLisandro Dalcin    Level: beginner
24746a4d4014SLisandro Dalcin 
24755a3a76d0SJed Brown    Notes:
24765a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
24775a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
24785a3a76d0SJed Brown    stepped over the final time.
24795a3a76d0SJed Brown 
24806a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
24816a4d4014SLisandro Dalcin 
24826a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
24836a4d4014SLisandro Dalcin @*/
2484cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
24856a4d4014SLisandro Dalcin {
24864d7d938eSLisandro Dalcin   PetscBool         flg;
24874d7d938eSLisandro Dalcin   PetscViewer       viewer;
24886a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2489cffb1e40SBarry Smith   PetscViewerFormat format;
2490f22f69f0SBarry Smith 
24916a4d4014SLisandro Dalcin   PetscFunctionBegin;
24920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2493f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
249449354f04SShri 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 */
24950910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
24965a3a76d0SJed Brown       Vec y;
24970910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
24985a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
24995a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
25005a3a76d0SJed Brown     }
2501f2c2a1b9SBarry Smith     if (u) {
25020910c330SBarry Smith       ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2503f2c2a1b9SBarry Smith     }
2504bbd56ea5SKarl Rupp   } else if (u) {
25050910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
25065a3a76d0SJed Brown   }
2507b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
25086a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2509193ac0bcSJed Brown   ts->steps             = 0;
25105ef26d82SJed Brown   ts->ksp_its           = 0;
25115ef26d82SJed Brown   ts->snes_its          = 0;
2512c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2513c610991cSLisandro Dalcin   ts->reject            = 0;
2514193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2515193ac0bcSJed Brown 
2516193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2517193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
25180910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2519bbd56ea5SKarl Rupp 
2520cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2521193ac0bcSJed Brown   } else {
25226a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2523362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2524db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2525db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2526e1a7a14fSJed Brown     while (!ts->reason) {
2527193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2528193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2529193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2530193ac0bcSJed Brown     }
253149354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
25320910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2533bbd56ea5SKarl Rupp 
2534cc708dedSBarry Smith       ts->solvetime = ts->max_time;
25350574a7fbSJed Brown     } else {
2536ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2537cc708dedSBarry Smith       ts->solvetime = ts->ptime;
25380574a7fbSJed Brown     }
2539193ac0bcSJed Brown   }
25403923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2541ce94432eSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
25424d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2543cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
25444d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2545cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2546cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
25472b0a91c0SBarry Smith   }
25486a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
25496a4d4014SLisandro Dalcin }
25506a4d4014SLisandro Dalcin 
25516a4d4014SLisandro Dalcin #undef __FUNCT__
25524a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2553228d79bcSJed Brown /*@
2554228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2555228d79bcSJed Brown 
2556228d79bcSJed Brown    Collective on TS
2557228d79bcSJed Brown 
2558228d79bcSJed Brown    Input Parameters:
2559228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2560228d79bcSJed Brown .  step - step number that has just completed
2561228d79bcSJed Brown .  ptime - model time of the state
25620910c330SBarry Smith -  u - state at the current model time
2563228d79bcSJed Brown 
2564228d79bcSJed Brown    Notes:
2565228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2566228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2567228d79bcSJed Brown 
2568228d79bcSJed Brown    Level: advanced
2569228d79bcSJed Brown 
2570228d79bcSJed Brown .keywords: TS, timestep
2571228d79bcSJed Brown @*/
25720910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2573d763cef2SBarry Smith {
25746849ba73SBarry Smith   PetscErrorCode ierr;
2575a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2576d763cef2SBarry Smith 
2577d763cef2SBarry Smith   PetscFunctionBegin;
2578d763cef2SBarry Smith   for (i=0; i<n; i++) {
25790910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2580d763cef2SBarry Smith   }
2581d763cef2SBarry Smith   PetscFunctionReturn(0);
2582d763cef2SBarry Smith }
2583d763cef2SBarry Smith 
2584d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
25850b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
25860b039ecaSBarry Smith   PetscDrawLG lg;
25870b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2588201da799SBarry Smith   PetscInt    ksp_its,snes_its;
25890b039ecaSBarry Smith };
25900b039ecaSBarry Smith 
2591d763cef2SBarry Smith 
25924a2ae208SSatish Balay #undef __FUNCT__
2593a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2594d763cef2SBarry Smith /*@C
2595a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2596a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2597d763cef2SBarry Smith 
2598d763cef2SBarry Smith    Collective on TS
2599d763cef2SBarry Smith 
2600d763cef2SBarry Smith    Input Parameters:
2601d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2602d763cef2SBarry Smith .  label - the title to put in the title bar
26037c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2604a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2605a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2606d763cef2SBarry Smith 
2607d763cef2SBarry Smith    Output Parameter:
26080b039ecaSBarry Smith .  ctx - the context
2609d763cef2SBarry Smith 
2610d763cef2SBarry Smith    Options Database Key:
26114f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
26124f09c107SBarry Smith .  -ts_monitor_lg_solution -
261399fdda47SBarry Smith .  -ts_monitor_lg_error -
261499fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
261599fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
261699fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2617d763cef2SBarry Smith 
2618d763cef2SBarry Smith    Notes:
2619a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2620d763cef2SBarry Smith 
2621d763cef2SBarry Smith    Level: intermediate
2622d763cef2SBarry Smith 
26237c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2624d763cef2SBarry Smith 
26254f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
26267c922b88SBarry Smith 
2627d763cef2SBarry Smith @*/
2628a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2629d763cef2SBarry Smith {
2630b0a32e0cSBarry Smith   PetscDraw      win;
2631dfbe8321SBarry Smith   PetscErrorCode ierr;
263299fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2633d763cef2SBarry Smith 
2634d763cef2SBarry Smith   PetscFunctionBegin;
2635201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2636a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
26373fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
26380b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
26390298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-lg_indicate_data_points",&flg,NULL);CHKERRQ(ierr);
264099fdda47SBarry Smith   if (flg) {
26410b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
264299fdda47SBarry Smith   }
26430b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2644a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2645d763cef2SBarry Smith   PetscFunctionReturn(0);
2646d763cef2SBarry Smith }
2647d763cef2SBarry Smith 
26484a2ae208SSatish Balay #undef __FUNCT__
26494f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
26504f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2651d763cef2SBarry Smith {
26520b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2653c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2654dfbe8321SBarry Smith   PetscErrorCode ierr;
2655d763cef2SBarry Smith 
2656d763cef2SBarry Smith   PetscFunctionBegin;
2657a9f9c1f6SBarry Smith   if (!n) {
2658a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2659a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2660a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2661a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2662a9f9c1f6SBarry Smith   }
2663c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
26640b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
266599fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))) {
26660b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
26673923b477SBarry Smith   }
2668d763cef2SBarry Smith   PetscFunctionReturn(0);
2669d763cef2SBarry Smith }
2670d763cef2SBarry Smith 
26714a2ae208SSatish Balay #undef __FUNCT__
2672a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2673d763cef2SBarry Smith /*@C
2674a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2675a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2676d763cef2SBarry Smith 
26770b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2678d763cef2SBarry Smith 
2679d763cef2SBarry Smith    Input Parameter:
26800b039ecaSBarry Smith .  ctx - the monitor context
2681d763cef2SBarry Smith 
2682d763cef2SBarry Smith    Level: intermediate
2683d763cef2SBarry Smith 
2684d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2685d763cef2SBarry Smith 
26864f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2687d763cef2SBarry Smith @*/
2688a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2689d763cef2SBarry Smith {
2690b0a32e0cSBarry Smith   PetscDraw      draw;
2691dfbe8321SBarry Smith   PetscErrorCode ierr;
2692d763cef2SBarry Smith 
2693d763cef2SBarry Smith   PetscFunctionBegin;
26940b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
26956bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
26960b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
26970b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2698d763cef2SBarry Smith   PetscFunctionReturn(0);
2699d763cef2SBarry Smith }
2700d763cef2SBarry Smith 
27014a2ae208SSatish Balay #undef __FUNCT__
27024a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2703d763cef2SBarry Smith /*@
2704b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2705d763cef2SBarry Smith 
2706d763cef2SBarry Smith    Not Collective
2707d763cef2SBarry Smith 
2708d763cef2SBarry Smith    Input Parameter:
2709d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2710d763cef2SBarry Smith 
2711d763cef2SBarry Smith    Output Parameter:
2712d763cef2SBarry Smith .  t  - the current time
2713d763cef2SBarry Smith 
2714d763cef2SBarry Smith    Level: beginner
2715d763cef2SBarry Smith 
2716b8123daeSJed Brown    Note:
2717b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2718b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2719b8123daeSJed Brown 
2720d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2721d763cef2SBarry Smith 
2722d763cef2SBarry Smith .keywords: TS, get, time
2723d763cef2SBarry Smith @*/
27247087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2725d763cef2SBarry Smith {
2726d763cef2SBarry Smith   PetscFunctionBegin;
27270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2728f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2729d763cef2SBarry Smith   *t = ts->ptime;
2730d763cef2SBarry Smith   PetscFunctionReturn(0);
2731d763cef2SBarry Smith }
2732d763cef2SBarry Smith 
27334a2ae208SSatish Balay #undef __FUNCT__
27346a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
27356a4d4014SLisandro Dalcin /*@
27366a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
27376a4d4014SLisandro Dalcin 
27383f9fe445SBarry Smith    Logically Collective on TS
27396a4d4014SLisandro Dalcin 
27406a4d4014SLisandro Dalcin    Input Parameters:
27416a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
27426a4d4014SLisandro Dalcin -  time - the time
27436a4d4014SLisandro Dalcin 
27446a4d4014SLisandro Dalcin    Level: intermediate
27456a4d4014SLisandro Dalcin 
27466a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
27476a4d4014SLisandro Dalcin 
27486a4d4014SLisandro Dalcin .keywords: TS, set, time
27496a4d4014SLisandro Dalcin @*/
27507087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
27516a4d4014SLisandro Dalcin {
27526a4d4014SLisandro Dalcin   PetscFunctionBegin;
27530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2754c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
27556a4d4014SLisandro Dalcin   ts->ptime = t;
27566a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
27576a4d4014SLisandro Dalcin }
27586a4d4014SLisandro Dalcin 
27596a4d4014SLisandro Dalcin #undef __FUNCT__
27604a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2761d763cef2SBarry Smith /*@C
2762d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2763d763cef2SBarry Smith    TS options in the database.
2764d763cef2SBarry Smith 
27653f9fe445SBarry Smith    Logically Collective on TS
2766d763cef2SBarry Smith 
2767d763cef2SBarry Smith    Input Parameter:
2768d763cef2SBarry Smith +  ts     - The TS context
2769d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2770d763cef2SBarry Smith 
2771d763cef2SBarry Smith    Notes:
2772d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2773d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2774d763cef2SBarry Smith    hyphen.
2775d763cef2SBarry Smith 
2776d763cef2SBarry Smith    Level: advanced
2777d763cef2SBarry Smith 
2778d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2779d763cef2SBarry Smith 
2780d763cef2SBarry Smith .seealso: TSSetFromOptions()
2781d763cef2SBarry Smith 
2782d763cef2SBarry Smith @*/
27837087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2784d763cef2SBarry Smith {
2785dfbe8321SBarry Smith   PetscErrorCode ierr;
2786089b2837SJed Brown   SNES           snes;
2787d763cef2SBarry Smith 
2788d763cef2SBarry Smith   PetscFunctionBegin;
27890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2790d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2791089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2792089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2793d763cef2SBarry Smith   PetscFunctionReturn(0);
2794d763cef2SBarry Smith }
2795d763cef2SBarry Smith 
2796d763cef2SBarry Smith 
27974a2ae208SSatish Balay #undef __FUNCT__
27984a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2799d763cef2SBarry Smith /*@C
2800d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2801d763cef2SBarry Smith    TS options in the database.
2802d763cef2SBarry Smith 
28033f9fe445SBarry Smith    Logically Collective on TS
2804d763cef2SBarry Smith 
2805d763cef2SBarry Smith    Input Parameter:
2806d763cef2SBarry Smith +  ts     - The TS context
2807d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2808d763cef2SBarry Smith 
2809d763cef2SBarry Smith    Notes:
2810d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2811d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2812d763cef2SBarry Smith    hyphen.
2813d763cef2SBarry Smith 
2814d763cef2SBarry Smith    Level: advanced
2815d763cef2SBarry Smith 
2816d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2817d763cef2SBarry Smith 
2818d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2819d763cef2SBarry Smith 
2820d763cef2SBarry Smith @*/
28217087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2822d763cef2SBarry Smith {
2823dfbe8321SBarry Smith   PetscErrorCode ierr;
2824089b2837SJed Brown   SNES           snes;
2825d763cef2SBarry Smith 
2826d763cef2SBarry Smith   PetscFunctionBegin;
28270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2828d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2829089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2830089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2831d763cef2SBarry Smith   PetscFunctionReturn(0);
2832d763cef2SBarry Smith }
2833d763cef2SBarry Smith 
28344a2ae208SSatish Balay #undef __FUNCT__
28354a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2836d763cef2SBarry Smith /*@C
2837d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2838d763cef2SBarry Smith    TS options in the database.
2839d763cef2SBarry Smith 
2840d763cef2SBarry Smith    Not Collective
2841d763cef2SBarry Smith 
2842d763cef2SBarry Smith    Input Parameter:
2843d763cef2SBarry Smith .  ts - The TS context
2844d763cef2SBarry Smith 
2845d763cef2SBarry Smith    Output Parameter:
2846d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2847d763cef2SBarry Smith 
2848d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2849d763cef2SBarry Smith    sufficient length to hold the prefix.
2850d763cef2SBarry Smith 
2851d763cef2SBarry Smith    Level: intermediate
2852d763cef2SBarry Smith 
2853d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2854d763cef2SBarry Smith 
2855d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2856d763cef2SBarry Smith @*/
28577087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2858d763cef2SBarry Smith {
2859dfbe8321SBarry Smith   PetscErrorCode ierr;
2860d763cef2SBarry Smith 
2861d763cef2SBarry Smith   PetscFunctionBegin;
28620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28634482741eSBarry Smith   PetscValidPointer(prefix,2);
2864d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2865d763cef2SBarry Smith   PetscFunctionReturn(0);
2866d763cef2SBarry Smith }
2867d763cef2SBarry Smith 
28684a2ae208SSatish Balay #undef __FUNCT__
28694a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2870d763cef2SBarry Smith /*@C
2871d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2872d763cef2SBarry Smith 
2873d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2874d763cef2SBarry Smith 
2875d763cef2SBarry Smith    Input Parameter:
2876d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2877d763cef2SBarry Smith 
2878d763cef2SBarry Smith    Output Parameters:
2879b5abc632SBarry Smith +  J   - The Jacobian J of F, where U_t = G(U,t)
2880d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2881089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2882d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2883d763cef2SBarry Smith 
28840298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
2885d763cef2SBarry Smith 
2886d763cef2SBarry Smith    Level: intermediate
2887d763cef2SBarry Smith 
288826d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2889d763cef2SBarry Smith 
2890d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2891d763cef2SBarry Smith @*/
2892089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2893d763cef2SBarry Smith {
2894089b2837SJed Brown   PetscErrorCode ierr;
2895089b2837SJed Brown   SNES           snes;
289624989b8cSPeter Brune   DM             dm;
2897089b2837SJed Brown 
2898d763cef2SBarry Smith   PetscFunctionBegin;
2899089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
29000298fd71SBarry Smith   ierr = SNESGetJacobian(snes,J,M,NULL,NULL);CHKERRQ(ierr);
290124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
290224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2903d763cef2SBarry Smith   PetscFunctionReturn(0);
2904d763cef2SBarry Smith }
2905d763cef2SBarry Smith 
29061713a123SBarry Smith #undef __FUNCT__
29072eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
29082eca1d9cSJed Brown /*@C
29092eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
29102eca1d9cSJed Brown 
29112eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
29122eca1d9cSJed Brown 
29132eca1d9cSJed Brown    Input Parameter:
29142eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
29152eca1d9cSJed Brown 
29162eca1d9cSJed Brown    Output Parameters:
29172eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
29182eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
29192eca1d9cSJed Brown .  f   - The function to compute the matrices
29202eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
29212eca1d9cSJed Brown 
29220298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
29232eca1d9cSJed Brown 
29242eca1d9cSJed Brown    Level: advanced
29252eca1d9cSJed Brown 
29262eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
29272eca1d9cSJed Brown 
29282eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
29292eca1d9cSJed Brown @*/
29307087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
29312eca1d9cSJed Brown {
2932089b2837SJed Brown   PetscErrorCode ierr;
2933089b2837SJed Brown   SNES           snes;
293424989b8cSPeter Brune   DM             dm;
29350910c330SBarry Smith 
29362eca1d9cSJed Brown   PetscFunctionBegin;
2937089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2938f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
29390298fd71SBarry Smith   ierr = SNESGetJacobian(snes,A,B,NULL,NULL);CHKERRQ(ierr);
294024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
294124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
29422eca1d9cSJed Brown   PetscFunctionReturn(0);
29432eca1d9cSJed Brown }
29442eca1d9cSJed Brown 
294583a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
29466083293cSBarry Smith   PetscViewer viewer;
29476083293cSBarry Smith   Vec         initialsolution;
29486083293cSBarry Smith   PetscBool   showinitial;
294983a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2950473a3ab2SBarry Smith   PetscBool   showtimestepandtime;
295183a4ac43SBarry Smith };
29526083293cSBarry Smith 
29532eca1d9cSJed Brown #undef __FUNCT__
295483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
29551713a123SBarry Smith /*@C
295683a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
29571713a123SBarry Smith    VecView() for the solution at each timestep
29581713a123SBarry Smith 
29591713a123SBarry Smith    Collective on TS
29601713a123SBarry Smith 
29611713a123SBarry Smith    Input Parameters:
29621713a123SBarry Smith +  ts - the TS context
29631713a123SBarry Smith .  step - current time-step
2964142b95e3SSatish Balay .  ptime - current time
29650298fd71SBarry Smith -  dummy - either a viewer or NULL
29661713a123SBarry Smith 
296799fdda47SBarry Smith    Options Database:
296899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
296999fdda47SBarry Smith 
297099fdda47SBarry 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
297199fdda47SBarry Smith        will look bad
297299fdda47SBarry Smith 
29731713a123SBarry Smith    Level: intermediate
29741713a123SBarry Smith 
29751713a123SBarry Smith .keywords: TS,  vector, monitor, view
29761713a123SBarry Smith 
2977a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
29781713a123SBarry Smith @*/
29790910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
29801713a123SBarry Smith {
2981dfbe8321SBarry Smith   PetscErrorCode   ierr;
298283a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
2983473a3ab2SBarry Smith   PetscDraw        draw;
29841713a123SBarry Smith 
29851713a123SBarry Smith   PetscFunctionBegin;
29866083293cSBarry Smith   if (!step && ictx->showinitial) {
29876083293cSBarry Smith     if (!ictx->initialsolution) {
29880910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
29891713a123SBarry Smith     }
29900910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
29916083293cSBarry Smith   }
29923fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
29930dcf80beSBarry Smith 
29946083293cSBarry Smith   if (ictx->showinitial) {
29956083293cSBarry Smith     PetscReal pause;
29966083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
29976083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
29986083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
29996083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
30006083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
30016083293cSBarry Smith   }
30020910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3003473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3004473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3005473a3ab2SBarry Smith     char      time[32];
3006473a3ab2SBarry Smith     size_t    len;
3007473a3ab2SBarry Smith 
3008473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3009473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3010473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3011473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
30120298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3013473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3014473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3015473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3016473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3017473a3ab2SBarry Smith   }
3018473a3ab2SBarry Smith 
30196083293cSBarry Smith   if (ictx->showinitial) {
30206083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
30216083293cSBarry Smith   }
30221713a123SBarry Smith   PetscFunctionReturn(0);
30231713a123SBarry Smith }
30241713a123SBarry Smith 
30251713a123SBarry Smith 
30266c699258SBarry Smith #undef __FUNCT__
302783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
30286083293cSBarry Smith /*@C
302983a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
30306083293cSBarry Smith 
30316083293cSBarry Smith    Collective on TS
30326083293cSBarry Smith 
30336083293cSBarry Smith    Input Parameters:
30346083293cSBarry Smith .    ctx - the monitor context
30356083293cSBarry Smith 
30366083293cSBarry Smith    Level: intermediate
30376083293cSBarry Smith 
30386083293cSBarry Smith .keywords: TS,  vector, monitor, view
30396083293cSBarry Smith 
304083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
30416083293cSBarry Smith @*/
304283a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
30436083293cSBarry Smith {
30446083293cSBarry Smith   PetscErrorCode ierr;
30456083293cSBarry Smith 
30466083293cSBarry Smith   PetscFunctionBegin;
304783a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
304883a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
304983a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
30506083293cSBarry Smith   PetscFunctionReturn(0);
30516083293cSBarry Smith }
30526083293cSBarry Smith 
30536083293cSBarry Smith #undef __FUNCT__
305483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
30556083293cSBarry Smith /*@C
305683a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
30576083293cSBarry Smith 
30586083293cSBarry Smith    Collective on TS
30596083293cSBarry Smith 
30606083293cSBarry Smith    Input Parameter:
30616083293cSBarry Smith .    ts - time-step context
30626083293cSBarry Smith 
30636083293cSBarry Smith    Output Patameter:
30646083293cSBarry Smith .    ctx - the monitor context
30656083293cSBarry Smith 
306699fdda47SBarry Smith    Options Database:
306799fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
306899fdda47SBarry Smith 
30696083293cSBarry Smith    Level: intermediate
30706083293cSBarry Smith 
30716083293cSBarry Smith .keywords: TS,  vector, monitor, view
30726083293cSBarry Smith 
307383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
30746083293cSBarry Smith @*/
307583a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
30766083293cSBarry Smith {
30776083293cSBarry Smith   PetscErrorCode   ierr;
30786083293cSBarry Smith 
30796083293cSBarry Smith   PetscFunctionBegin;
308083a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
308183a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3082e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3083bbd56ea5SKarl Rupp 
308483a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3085473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
30860298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3087473a3ab2SBarry Smith 
3088473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
30890298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
30906083293cSBarry Smith   PetscFunctionReturn(0);
30916083293cSBarry Smith }
30926083293cSBarry Smith 
30936083293cSBarry Smith #undef __FUNCT__
309483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
30953a471f94SBarry Smith /*@C
309683a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
30973a471f94SBarry Smith    VecView() for the error at each timestep
30983a471f94SBarry Smith 
30993a471f94SBarry Smith    Collective on TS
31003a471f94SBarry Smith 
31013a471f94SBarry Smith    Input Parameters:
31023a471f94SBarry Smith +  ts - the TS context
31033a471f94SBarry Smith .  step - current time-step
31043a471f94SBarry Smith .  ptime - current time
31050298fd71SBarry Smith -  dummy - either a viewer or NULL
31063a471f94SBarry Smith 
31073a471f94SBarry Smith    Level: intermediate
31083a471f94SBarry Smith 
31093a471f94SBarry Smith .keywords: TS,  vector, monitor, view
31103a471f94SBarry Smith 
31113a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
31123a471f94SBarry Smith @*/
31130910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
31143a471f94SBarry Smith {
31153a471f94SBarry Smith   PetscErrorCode   ierr;
311683a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
311783a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
31183a471f94SBarry Smith   Vec              work;
31193a471f94SBarry Smith 
31203a471f94SBarry Smith   PetscFunctionBegin;
31213fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
31220910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
31233a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
31240910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
31253a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
31263a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
31273a471f94SBarry Smith   PetscFunctionReturn(0);
31283a471f94SBarry Smith }
31293a471f94SBarry Smith 
31302a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
31313a471f94SBarry Smith #undef __FUNCT__
31326c699258SBarry Smith #define __FUNCT__ "TSSetDM"
31336c699258SBarry Smith /*@
31346c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
31356c699258SBarry Smith 
31363f9fe445SBarry Smith    Logically Collective on TS and DM
31376c699258SBarry Smith 
31386c699258SBarry Smith    Input Parameters:
31396c699258SBarry Smith +  ts - the preconditioner context
31406c699258SBarry Smith -  dm - the dm
31416c699258SBarry Smith 
31426c699258SBarry Smith    Level: intermediate
31436c699258SBarry Smith 
31446c699258SBarry Smith 
31456c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
31466c699258SBarry Smith @*/
31477087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
31486c699258SBarry Smith {
31496c699258SBarry Smith   PetscErrorCode ierr;
3150089b2837SJed Brown   SNES           snes;
3151942e3340SBarry Smith   DMTS           tsdm;
31526c699258SBarry Smith 
31536c699258SBarry Smith   PetscFunctionBegin;
31540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
315570663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3156942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
31572a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3158942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3159942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
316024989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
316124989b8cSPeter Brune         tsdm->originaldm = dm;
316224989b8cSPeter Brune       }
316324989b8cSPeter Brune     }
31646bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
316524989b8cSPeter Brune   }
31666c699258SBarry Smith   ts->dm = dm;
3167bbd56ea5SKarl Rupp 
3168089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3169089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
31706c699258SBarry Smith   PetscFunctionReturn(0);
31716c699258SBarry Smith }
31726c699258SBarry Smith 
31736c699258SBarry Smith #undef __FUNCT__
31746c699258SBarry Smith #define __FUNCT__ "TSGetDM"
31756c699258SBarry Smith /*@
31766c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
31776c699258SBarry Smith 
31783f9fe445SBarry Smith    Not Collective
31796c699258SBarry Smith 
31806c699258SBarry Smith    Input Parameter:
31816c699258SBarry Smith . ts - the preconditioner context
31826c699258SBarry Smith 
31836c699258SBarry Smith    Output Parameter:
31846c699258SBarry Smith .  dm - the dm
31856c699258SBarry Smith 
31866c699258SBarry Smith    Level: intermediate
31876c699258SBarry Smith 
31886c699258SBarry Smith 
31896c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
31906c699258SBarry Smith @*/
31917087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
31926c699258SBarry Smith {
3193496e6a7aSJed Brown   PetscErrorCode ierr;
3194496e6a7aSJed Brown 
31956c699258SBarry Smith   PetscFunctionBegin;
31960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3197496e6a7aSJed Brown   if (!ts->dm) {
3198ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
3199496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3200496e6a7aSJed Brown   }
32016c699258SBarry Smith   *dm = ts->dm;
32026c699258SBarry Smith   PetscFunctionReturn(0);
32036c699258SBarry Smith }
32041713a123SBarry Smith 
32050f5c6efeSJed Brown #undef __FUNCT__
32060f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
32070f5c6efeSJed Brown /*@
32080f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
32090f5c6efeSJed Brown 
32103f9fe445SBarry Smith    Logically Collective on SNES
32110f5c6efeSJed Brown 
32120f5c6efeSJed Brown    Input Parameter:
3213d42a1c89SJed Brown + snes - nonlinear solver
32140910c330SBarry Smith . U - the current state at which to evaluate the residual
3215d42a1c89SJed Brown - ctx - user context, must be a TS
32160f5c6efeSJed Brown 
32170f5c6efeSJed Brown    Output Parameter:
32180f5c6efeSJed Brown . F - the nonlinear residual
32190f5c6efeSJed Brown 
32200f5c6efeSJed Brown    Notes:
32210f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
32220f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
32230f5c6efeSJed Brown 
32240f5c6efeSJed Brown    Level: advanced
32250f5c6efeSJed Brown 
32260f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
32270f5c6efeSJed Brown @*/
32280910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
32290f5c6efeSJed Brown {
32300f5c6efeSJed Brown   TS             ts = (TS)ctx;
32310f5c6efeSJed Brown   PetscErrorCode ierr;
32320f5c6efeSJed Brown 
32330f5c6efeSJed Brown   PetscFunctionBegin;
32340f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
32350910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
32360f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
32370f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
32380910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
32390f5c6efeSJed Brown   PetscFunctionReturn(0);
32400f5c6efeSJed Brown }
32410f5c6efeSJed Brown 
32420f5c6efeSJed Brown #undef __FUNCT__
32430f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
32440f5c6efeSJed Brown /*@
32450f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
32460f5c6efeSJed Brown 
32470f5c6efeSJed Brown    Collective on SNES
32480f5c6efeSJed Brown 
32490f5c6efeSJed Brown    Input Parameter:
32500f5c6efeSJed Brown + snes - nonlinear solver
32510910c330SBarry Smith . U - the current state at which to evaluate the residual
32520f5c6efeSJed Brown - ctx - user context, must be a TS
32530f5c6efeSJed Brown 
32540f5c6efeSJed Brown    Output Parameter:
32550f5c6efeSJed Brown + A - the Jacobian
32560f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
32570f5c6efeSJed Brown - flag - indicates any structure change in the matrix
32580f5c6efeSJed Brown 
32590f5c6efeSJed Brown    Notes:
32600f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
32610f5c6efeSJed Brown 
32620f5c6efeSJed Brown    Level: developer
32630f5c6efeSJed Brown 
32640f5c6efeSJed Brown .seealso: SNESSetJacobian()
32650f5c6efeSJed Brown @*/
32660910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
32670f5c6efeSJed Brown {
32680f5c6efeSJed Brown   TS             ts = (TS)ctx;
32690f5c6efeSJed Brown   PetscErrorCode ierr;
32700f5c6efeSJed Brown 
32710f5c6efeSJed Brown   PetscFunctionBegin;
32720f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
32730910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
32740f5c6efeSJed Brown   PetscValidPointer(A,3);
32750f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
32760f5c6efeSJed Brown   PetscValidPointer(B,4);
32770f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
32780f5c6efeSJed Brown   PetscValidPointer(flag,5);
32790f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
32800910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
32810f5c6efeSJed Brown   PetscFunctionReturn(0);
32820f5c6efeSJed Brown }
3283325fc9f4SBarry Smith 
32840e4ef248SJed Brown #undef __FUNCT__
32850e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
32860e4ef248SJed Brown /*@C
32870e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
32880e4ef248SJed Brown 
32890e4ef248SJed Brown    Collective on TS
32900e4ef248SJed Brown 
32910e4ef248SJed Brown    Input Arguments:
32920e4ef248SJed Brown +  ts - time stepping context
32930e4ef248SJed Brown .  t - time at which to evaluate
32940910c330SBarry Smith .  U - state at which to evaluate
32950e4ef248SJed Brown -  ctx - context
32960e4ef248SJed Brown 
32970e4ef248SJed Brown    Output Arguments:
32980e4ef248SJed Brown .  F - right hand side
32990e4ef248SJed Brown 
33000e4ef248SJed Brown    Level: intermediate
33010e4ef248SJed Brown 
33020e4ef248SJed Brown    Notes:
33030e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
33040e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
33050e4ef248SJed Brown 
33060e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
33070e4ef248SJed Brown @*/
33080910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
33090e4ef248SJed Brown {
33100e4ef248SJed Brown   PetscErrorCode ierr;
33110e4ef248SJed Brown   Mat            Arhs,Brhs;
33120e4ef248SJed Brown   MatStructure   flg2;
33130e4ef248SJed Brown 
33140e4ef248SJed Brown   PetscFunctionBegin;
33150e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
33160910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
33170910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
33180e4ef248SJed Brown   PetscFunctionReturn(0);
33190e4ef248SJed Brown }
33200e4ef248SJed Brown 
33210e4ef248SJed Brown #undef __FUNCT__
33220e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
33230e4ef248SJed Brown /*@C
33240e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
33250e4ef248SJed Brown 
33260e4ef248SJed Brown    Collective on TS
33270e4ef248SJed Brown 
33280e4ef248SJed Brown    Input Arguments:
33290e4ef248SJed Brown +  ts - time stepping context
33300e4ef248SJed Brown .  t - time at which to evaluate
33310910c330SBarry Smith .  U - state at which to evaluate
33320e4ef248SJed Brown -  ctx - context
33330e4ef248SJed Brown 
33340e4ef248SJed Brown    Output Arguments:
33350e4ef248SJed Brown +  A - pointer to operator
33360e4ef248SJed Brown .  B - pointer to preconditioning matrix
33370e4ef248SJed Brown -  flg - matrix structure flag
33380e4ef248SJed Brown 
33390e4ef248SJed Brown    Level: intermediate
33400e4ef248SJed Brown 
33410e4ef248SJed Brown    Notes:
33420e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
33430e4ef248SJed Brown 
33440e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
33450e4ef248SJed Brown @*/
33460910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
33470e4ef248SJed Brown {
33480e4ef248SJed Brown   PetscFunctionBegin;
33490e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
33500e4ef248SJed Brown   PetscFunctionReturn(0);
33510e4ef248SJed Brown }
33520e4ef248SJed Brown 
33530026cea9SSean Farley #undef __FUNCT__
33540026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
33550026cea9SSean Farley /*@C
33560026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
33570026cea9SSean Farley 
33580026cea9SSean Farley    Collective on TS
33590026cea9SSean Farley 
33600026cea9SSean Farley    Input Arguments:
33610026cea9SSean Farley +  ts - time stepping context
33620026cea9SSean Farley .  t - time at which to evaluate
33630910c330SBarry Smith .  U - state at which to evaluate
33640910c330SBarry Smith .  Udot - time derivative of state vector
33650026cea9SSean Farley -  ctx - context
33660026cea9SSean Farley 
33670026cea9SSean Farley    Output Arguments:
33680026cea9SSean Farley .  F - left hand side
33690026cea9SSean Farley 
33700026cea9SSean Farley    Level: intermediate
33710026cea9SSean Farley 
33720026cea9SSean Farley    Notes:
33730910c330SBarry 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
33740026cea9SSean Farley    user is required to write their own TSComputeIFunction.
33750026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
33760026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
33770026cea9SSean Farley 
33780026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
33790026cea9SSean Farley @*/
33800910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
33810026cea9SSean Farley {
33820026cea9SSean Farley   PetscErrorCode ierr;
33830026cea9SSean Farley   Mat            A,B;
33840026cea9SSean Farley   MatStructure   flg2;
33850026cea9SSean Farley 
33860026cea9SSean Farley   PetscFunctionBegin;
33870298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
33880910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
33890910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
33900026cea9SSean Farley   PetscFunctionReturn(0);
33910026cea9SSean Farley }
33920026cea9SSean Farley 
33930026cea9SSean Farley #undef __FUNCT__
33940026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
33950026cea9SSean Farley /*@C
339624e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
33970026cea9SSean Farley 
33980026cea9SSean Farley    Collective on TS
33990026cea9SSean Farley 
34000026cea9SSean Farley    Input Arguments:
34010026cea9SSean Farley +  ts - time stepping context
34020026cea9SSean Farley .  t - time at which to evaluate
34030910c330SBarry Smith .  U - state at which to evaluate
34040910c330SBarry Smith .  Udot - time derivative of state vector
34050026cea9SSean Farley .  shift - shift to apply
34060026cea9SSean Farley -  ctx - context
34070026cea9SSean Farley 
34080026cea9SSean Farley    Output Arguments:
34090026cea9SSean Farley +  A - pointer to operator
34100026cea9SSean Farley .  B - pointer to preconditioning matrix
34110026cea9SSean Farley -  flg - matrix structure flag
34120026cea9SSean Farley 
34130026cea9SSean Farley    Level: intermediate
34140026cea9SSean Farley 
34150026cea9SSean Farley    Notes:
34160026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
34170026cea9SSean Farley 
34180026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
34190026cea9SSean Farley @*/
34200910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
34210026cea9SSean Farley {
34220026cea9SSean Farley   PetscFunctionBegin;
34230026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
34240026cea9SSean Farley   PetscFunctionReturn(0);
34250026cea9SSean Farley }
3426e817cc15SEmil Constantinescu #undef __FUNCT__
3427e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3428e817cc15SEmil Constantinescu /*@
3429e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3430e817cc15SEmil Constantinescu 
3431e817cc15SEmil Constantinescu    Not Collective
3432e817cc15SEmil Constantinescu 
3433e817cc15SEmil Constantinescu    Input Parameter:
3434e817cc15SEmil Constantinescu .  ts - the TS context
3435e817cc15SEmil Constantinescu 
3436e817cc15SEmil Constantinescu    Output Parameter:
3437e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3438e817cc15SEmil Constantinescu 
3439e817cc15SEmil Constantinescu    Level: beginner
3440e817cc15SEmil Constantinescu 
3441e817cc15SEmil Constantinescu .keywords: TS, equation type
3442e817cc15SEmil Constantinescu 
3443e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3444e817cc15SEmil Constantinescu @*/
3445e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3446e817cc15SEmil Constantinescu {
3447e817cc15SEmil Constantinescu   PetscFunctionBegin;
3448e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3449e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3450e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3451e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3452e817cc15SEmil Constantinescu }
3453e817cc15SEmil Constantinescu 
3454e817cc15SEmil Constantinescu #undef __FUNCT__
3455e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3456e817cc15SEmil Constantinescu /*@
3457e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3458e817cc15SEmil Constantinescu 
3459e817cc15SEmil Constantinescu    Not Collective
3460e817cc15SEmil Constantinescu 
3461e817cc15SEmil Constantinescu    Input Parameter:
3462e817cc15SEmil Constantinescu +  ts - the TS context
3463e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3464e817cc15SEmil Constantinescu 
3465e817cc15SEmil Constantinescu    Level: advanced
3466e817cc15SEmil Constantinescu 
3467e817cc15SEmil Constantinescu .keywords: TS, equation type
3468e817cc15SEmil Constantinescu 
3469e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3470e817cc15SEmil Constantinescu @*/
3471e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3472e817cc15SEmil Constantinescu {
3473e817cc15SEmil Constantinescu   PetscFunctionBegin;
3474e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3475e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3476e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3477e817cc15SEmil Constantinescu }
34780026cea9SSean Farley 
34794af1b03aSJed Brown #undef __FUNCT__
34804af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
34814af1b03aSJed Brown /*@
34824af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
34834af1b03aSJed Brown 
34844af1b03aSJed Brown    Not Collective
34854af1b03aSJed Brown 
34864af1b03aSJed Brown    Input Parameter:
34874af1b03aSJed Brown .  ts - the TS context
34884af1b03aSJed Brown 
34894af1b03aSJed Brown    Output Parameter:
34904af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
34914af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
34924af1b03aSJed Brown 
3493487e0bb9SJed Brown    Level: beginner
34944af1b03aSJed Brown 
3495cd652676SJed Brown    Notes:
3496cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
34974af1b03aSJed Brown 
34984af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
34994af1b03aSJed Brown 
35004af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
35014af1b03aSJed Brown @*/
35024af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
35034af1b03aSJed Brown {
35044af1b03aSJed Brown   PetscFunctionBegin;
35054af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35064af1b03aSJed Brown   PetscValidPointer(reason,2);
35074af1b03aSJed Brown   *reason = ts->reason;
35084af1b03aSJed Brown   PetscFunctionReturn(0);
35094af1b03aSJed Brown }
35104af1b03aSJed Brown 
3511fb1732b5SBarry Smith #undef __FUNCT__
3512d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3513d6ad946cSShri Abhyankar /*@
3514d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3515d6ad946cSShri Abhyankar 
3516d6ad946cSShri Abhyankar    Not Collective
3517d6ad946cSShri Abhyankar 
3518d6ad946cSShri Abhyankar    Input Parameter:
3519d6ad946cSShri Abhyankar +  ts - the TS context
3520d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3521d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3522d6ad946cSShri Abhyankar 
3523f5abba47SShri Abhyankar    Level: advanced
3524d6ad946cSShri Abhyankar 
3525d6ad946cSShri Abhyankar    Notes:
3526d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3527d6ad946cSShri Abhyankar 
3528d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3529d6ad946cSShri Abhyankar 
3530d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3531d6ad946cSShri Abhyankar @*/
3532d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3533d6ad946cSShri Abhyankar {
3534d6ad946cSShri Abhyankar   PetscFunctionBegin;
3535d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3536d6ad946cSShri Abhyankar   ts->reason = reason;
3537d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3538d6ad946cSShri Abhyankar }
3539d6ad946cSShri Abhyankar 
3540d6ad946cSShri Abhyankar #undef __FUNCT__
3541cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3542cc708dedSBarry Smith /*@
3543cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3544cc708dedSBarry Smith 
3545cc708dedSBarry Smith    Not Collective
3546cc708dedSBarry Smith 
3547cc708dedSBarry Smith    Input Parameter:
3548cc708dedSBarry Smith .  ts - the TS context
3549cc708dedSBarry Smith 
3550cc708dedSBarry Smith    Output Parameter:
3551cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3552cc708dedSBarry Smith 
3553487e0bb9SJed Brown    Level: beginner
3554cc708dedSBarry Smith 
3555cc708dedSBarry Smith    Notes:
3556cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3557cc708dedSBarry Smith 
3558cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3559cc708dedSBarry Smith 
3560cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3561cc708dedSBarry Smith @*/
3562cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3563cc708dedSBarry Smith {
3564cc708dedSBarry Smith   PetscFunctionBegin;
3565cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3566cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3567cc708dedSBarry Smith   *ftime = ts->solvetime;
3568cc708dedSBarry Smith   PetscFunctionReturn(0);
3569cc708dedSBarry Smith }
3570cc708dedSBarry Smith 
3571cc708dedSBarry Smith #undef __FUNCT__
35725ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
35739f67acb7SJed Brown /*@
35745ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
35759f67acb7SJed Brown    used by the time integrator.
35769f67acb7SJed Brown 
35779f67acb7SJed Brown    Not Collective
35789f67acb7SJed Brown 
35799f67acb7SJed Brown    Input Parameter:
35809f67acb7SJed Brown .  ts - TS context
35819f67acb7SJed Brown 
35829f67acb7SJed Brown    Output Parameter:
35839f67acb7SJed Brown .  nits - number of nonlinear iterations
35849f67acb7SJed Brown 
35859f67acb7SJed Brown    Notes:
35869f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
35879f67acb7SJed Brown 
35889f67acb7SJed Brown    Level: intermediate
35899f67acb7SJed Brown 
35909f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
35919f67acb7SJed Brown 
35925ef26d82SJed Brown .seealso:  TSGetKSPIterations()
35939f67acb7SJed Brown @*/
35945ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
35959f67acb7SJed Brown {
35969f67acb7SJed Brown   PetscFunctionBegin;
35979f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35989f67acb7SJed Brown   PetscValidIntPointer(nits,2);
35995ef26d82SJed Brown   *nits = ts->snes_its;
36009f67acb7SJed Brown   PetscFunctionReturn(0);
36019f67acb7SJed Brown }
36029f67acb7SJed Brown 
36039f67acb7SJed Brown #undef __FUNCT__
36045ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
36059f67acb7SJed Brown /*@
36065ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
36079f67acb7SJed Brown    used by the time integrator.
36089f67acb7SJed Brown 
36099f67acb7SJed Brown    Not Collective
36109f67acb7SJed Brown 
36119f67acb7SJed Brown    Input Parameter:
36129f67acb7SJed Brown .  ts - TS context
36139f67acb7SJed Brown 
36149f67acb7SJed Brown    Output Parameter:
36159f67acb7SJed Brown .  lits - number of linear iterations
36169f67acb7SJed Brown 
36179f67acb7SJed Brown    Notes:
36189f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
36199f67acb7SJed Brown 
36209f67acb7SJed Brown    Level: intermediate
36219f67acb7SJed Brown 
36229f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
36239f67acb7SJed Brown 
36245ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
36259f67acb7SJed Brown @*/
36265ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
36279f67acb7SJed Brown {
36289f67acb7SJed Brown   PetscFunctionBegin;
36299f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36309f67acb7SJed Brown   PetscValidIntPointer(lits,2);
36315ef26d82SJed Brown   *lits = ts->ksp_its;
36329f67acb7SJed Brown   PetscFunctionReturn(0);
36339f67acb7SJed Brown }
36349f67acb7SJed Brown 
36359f67acb7SJed Brown #undef __FUNCT__
3636cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3637cef5090cSJed Brown /*@
3638cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3639cef5090cSJed Brown 
3640cef5090cSJed Brown    Not Collective
3641cef5090cSJed Brown 
3642cef5090cSJed Brown    Input Parameter:
3643cef5090cSJed Brown .  ts - TS context
3644cef5090cSJed Brown 
3645cef5090cSJed Brown    Output Parameter:
3646cef5090cSJed Brown .  rejects - number of steps rejected
3647cef5090cSJed Brown 
3648cef5090cSJed Brown    Notes:
3649cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3650cef5090cSJed Brown 
3651cef5090cSJed Brown    Level: intermediate
3652cef5090cSJed Brown 
3653cef5090cSJed Brown .keywords: TS, get, number
3654cef5090cSJed Brown 
36555ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3656cef5090cSJed Brown @*/
3657cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3658cef5090cSJed Brown {
3659cef5090cSJed Brown   PetscFunctionBegin;
3660cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3661cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3662cef5090cSJed Brown   *rejects = ts->reject;
3663cef5090cSJed Brown   PetscFunctionReturn(0);
3664cef5090cSJed Brown }
3665cef5090cSJed Brown 
3666cef5090cSJed Brown #undef __FUNCT__
3667cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3668cef5090cSJed Brown /*@
3669cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3670cef5090cSJed Brown 
3671cef5090cSJed Brown    Not Collective
3672cef5090cSJed Brown 
3673cef5090cSJed Brown    Input Parameter:
3674cef5090cSJed Brown .  ts - TS context
3675cef5090cSJed Brown 
3676cef5090cSJed Brown    Output Parameter:
3677cef5090cSJed Brown .  fails - number of failed nonlinear solves
3678cef5090cSJed Brown 
3679cef5090cSJed Brown    Notes:
3680cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3681cef5090cSJed Brown 
3682cef5090cSJed Brown    Level: intermediate
3683cef5090cSJed Brown 
3684cef5090cSJed Brown .keywords: TS, get, number
3685cef5090cSJed Brown 
36865ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3687cef5090cSJed Brown @*/
3688cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3689cef5090cSJed Brown {
3690cef5090cSJed Brown   PetscFunctionBegin;
3691cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3692cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3693cef5090cSJed Brown   *fails = ts->num_snes_failures;
3694cef5090cSJed Brown   PetscFunctionReturn(0);
3695cef5090cSJed Brown }
3696cef5090cSJed Brown 
3697cef5090cSJed Brown #undef __FUNCT__
3698cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3699cef5090cSJed Brown /*@
3700cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3701cef5090cSJed Brown 
3702cef5090cSJed Brown    Not Collective
3703cef5090cSJed Brown 
3704cef5090cSJed Brown    Input Parameter:
3705cef5090cSJed Brown +  ts - TS context
3706cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3707cef5090cSJed Brown 
3708cef5090cSJed Brown    Notes:
3709cef5090cSJed Brown    The counter is reset to zero for each step
3710cef5090cSJed Brown 
3711cef5090cSJed Brown    Options Database Key:
3712cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3713cef5090cSJed Brown 
3714cef5090cSJed Brown    Level: intermediate
3715cef5090cSJed Brown 
3716cef5090cSJed Brown .keywords: TS, set, maximum, number
3717cef5090cSJed Brown 
37185ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3719cef5090cSJed Brown @*/
3720cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3721cef5090cSJed Brown {
3722cef5090cSJed Brown   PetscFunctionBegin;
3723cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3724cef5090cSJed Brown   ts->max_reject = rejects;
3725cef5090cSJed Brown   PetscFunctionReturn(0);
3726cef5090cSJed Brown }
3727cef5090cSJed Brown 
3728cef5090cSJed Brown #undef __FUNCT__
3729cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3730cef5090cSJed Brown /*@
3731cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3732cef5090cSJed Brown 
3733cef5090cSJed Brown    Not Collective
3734cef5090cSJed Brown 
3735cef5090cSJed Brown    Input Parameter:
3736cef5090cSJed Brown +  ts - TS context
3737cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3738cef5090cSJed Brown 
3739cef5090cSJed Brown    Notes:
3740cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3741cef5090cSJed Brown 
3742cef5090cSJed Brown    Options Database Key:
3743cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3744cef5090cSJed Brown 
3745cef5090cSJed Brown    Level: intermediate
3746cef5090cSJed Brown 
3747cef5090cSJed Brown .keywords: TS, set, maximum, number
3748cef5090cSJed Brown 
37495ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3750cef5090cSJed Brown @*/
3751cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3752cef5090cSJed Brown {
3753cef5090cSJed Brown   PetscFunctionBegin;
3754cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3755cef5090cSJed Brown   ts->max_snes_failures = fails;
3756cef5090cSJed Brown   PetscFunctionReturn(0);
3757cef5090cSJed Brown }
3758cef5090cSJed Brown 
3759cef5090cSJed Brown #undef __FUNCT__
3760cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3761cef5090cSJed Brown /*@
3762cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3763cef5090cSJed Brown 
3764cef5090cSJed Brown    Not Collective
3765cef5090cSJed Brown 
3766cef5090cSJed Brown    Input Parameter:
3767cef5090cSJed Brown +  ts - TS context
3768cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3769cef5090cSJed Brown 
3770cef5090cSJed Brown    Options Database Key:
3771cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3772cef5090cSJed Brown 
3773cef5090cSJed Brown    Level: intermediate
3774cef5090cSJed Brown 
3775cef5090cSJed Brown .keywords: TS, set, error
3776cef5090cSJed Brown 
37775ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3778cef5090cSJed Brown @*/
3779cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3780cef5090cSJed Brown {
3781cef5090cSJed Brown   PetscFunctionBegin;
3782cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3783cef5090cSJed Brown   ts->errorifstepfailed = err;
3784cef5090cSJed Brown   PetscFunctionReturn(0);
3785cef5090cSJed Brown }
3786cef5090cSJed Brown 
3787cef5090cSJed Brown #undef __FUNCT__
3788fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3789fb1732b5SBarry Smith /*@C
3790fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3791fb1732b5SBarry Smith 
3792fb1732b5SBarry Smith    Collective on TS
3793fb1732b5SBarry Smith 
3794fb1732b5SBarry Smith    Input Parameters:
3795fb1732b5SBarry Smith +  ts - the TS context
3796fb1732b5SBarry Smith .  step - current time-step
3797fb1732b5SBarry Smith .  ptime - current time
37980910c330SBarry Smith .  u - current state
3799fb1732b5SBarry Smith -  viewer - binary viewer
3800fb1732b5SBarry Smith 
3801fb1732b5SBarry Smith    Level: intermediate
3802fb1732b5SBarry Smith 
3803fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3804fb1732b5SBarry Smith 
3805fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3806fb1732b5SBarry Smith @*/
38070910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3808fb1732b5SBarry Smith {
3809fb1732b5SBarry Smith   PetscErrorCode ierr;
3810ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3811fb1732b5SBarry Smith 
3812fb1732b5SBarry Smith   PetscFunctionBegin;
38130910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3814ed81e22dSJed Brown   PetscFunctionReturn(0);
3815ed81e22dSJed Brown }
3816ed81e22dSJed Brown 
3817ed81e22dSJed Brown #undef __FUNCT__
3818ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3819ed81e22dSJed Brown /*@C
3820ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3821ed81e22dSJed Brown 
3822ed81e22dSJed Brown    Collective on TS
3823ed81e22dSJed Brown 
3824ed81e22dSJed Brown    Input Parameters:
3825ed81e22dSJed Brown +  ts - the TS context
3826ed81e22dSJed Brown .  step - current time-step
3827ed81e22dSJed Brown .  ptime - current time
38280910c330SBarry Smith .  u - current state
3829ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3830ed81e22dSJed Brown 
3831ed81e22dSJed Brown    Level: intermediate
3832ed81e22dSJed Brown 
3833ed81e22dSJed Brown    Notes:
3834ed81e22dSJed 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.
3835ed81e22dSJed Brown    These are named according to the file name template.
3836ed81e22dSJed Brown 
3837ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3838ed81e22dSJed Brown 
3839ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3840ed81e22dSJed Brown 
3841ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3842ed81e22dSJed Brown @*/
38430910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3844ed81e22dSJed Brown {
3845ed81e22dSJed Brown   PetscErrorCode ierr;
3846ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3847ed81e22dSJed Brown   PetscViewer    viewer;
3848ed81e22dSJed Brown 
3849ed81e22dSJed Brown   PetscFunctionBegin;
38508caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3851ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
38520910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3853ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3854ed81e22dSJed Brown   PetscFunctionReturn(0);
3855ed81e22dSJed Brown }
3856ed81e22dSJed Brown 
3857ed81e22dSJed Brown #undef __FUNCT__
3858ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3859ed81e22dSJed Brown /*@C
3860ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3861ed81e22dSJed Brown 
3862ed81e22dSJed Brown    Collective on TS
3863ed81e22dSJed Brown 
3864ed81e22dSJed Brown    Input Parameters:
3865ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3866ed81e22dSJed Brown 
3867ed81e22dSJed Brown    Level: intermediate
3868ed81e22dSJed Brown 
3869ed81e22dSJed Brown    Note:
3870ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3871ed81e22dSJed Brown 
3872ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3873ed81e22dSJed Brown 
3874ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3875ed81e22dSJed Brown @*/
3876ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3877ed81e22dSJed Brown {
3878ed81e22dSJed Brown   PetscErrorCode ierr;
3879ed81e22dSJed Brown 
3880ed81e22dSJed Brown   PetscFunctionBegin;
3881ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3882fb1732b5SBarry Smith   PetscFunctionReturn(0);
3883fb1732b5SBarry Smith }
3884fb1732b5SBarry Smith 
388584df9cb4SJed Brown #undef __FUNCT__
3886ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt"
388784df9cb4SJed Brown /*@
3888ad6bc421SBarry Smith    TSGetTSAdapt - Get the adaptive controller context for the current method
388984df9cb4SJed Brown 
3890ed81e22dSJed Brown    Collective on TS if controller has not been created yet
389184df9cb4SJed Brown 
389284df9cb4SJed Brown    Input Arguments:
3893ed81e22dSJed Brown .  ts - time stepping context
389484df9cb4SJed Brown 
389584df9cb4SJed Brown    Output Arguments:
3896ed81e22dSJed Brown .  adapt - adaptive controller
389784df9cb4SJed Brown 
389884df9cb4SJed Brown    Level: intermediate
389984df9cb4SJed Brown 
3900ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
390184df9cb4SJed Brown @*/
3902ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt)
390384df9cb4SJed Brown {
390484df9cb4SJed Brown   PetscErrorCode ierr;
390584df9cb4SJed Brown 
390684df9cb4SJed Brown   PetscFunctionBegin;
390784df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
390884df9cb4SJed Brown   PetscValidPointer(adapt,2);
390984df9cb4SJed Brown   if (!ts->adapt) {
3910ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
39111c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
39121c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
391384df9cb4SJed Brown   }
391484df9cb4SJed Brown   *adapt = ts->adapt;
391584df9cb4SJed Brown   PetscFunctionReturn(0);
391684df9cb4SJed Brown }
3917d6ebe24aSShri Abhyankar 
3918d6ebe24aSShri Abhyankar #undef __FUNCT__
39191c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
39201c3436cfSJed Brown /*@
39211c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
39221c3436cfSJed Brown 
39231c3436cfSJed Brown    Logically Collective
39241c3436cfSJed Brown 
39251c3436cfSJed Brown    Input Arguments:
39261c3436cfSJed Brown +  ts - time integration context
39271c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
39280298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
39291c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
39300298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
39311c3436cfSJed Brown 
39321c3436cfSJed Brown    Level: beginner
39331c3436cfSJed Brown 
3934c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
39351c3436cfSJed Brown @*/
39361c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
39371c3436cfSJed Brown {
39381c3436cfSJed Brown   PetscErrorCode ierr;
39391c3436cfSJed Brown 
39401c3436cfSJed Brown   PetscFunctionBegin;
3941c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
39421c3436cfSJed Brown   if (vatol) {
39431c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
39441c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
3945bbd56ea5SKarl Rupp 
39461c3436cfSJed Brown     ts->vatol = vatol;
39471c3436cfSJed Brown   }
3948c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
39491c3436cfSJed Brown   if (vrtol) {
39501c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
39511c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
3952bbd56ea5SKarl Rupp 
39531c3436cfSJed Brown     ts->vrtol = vrtol;
39541c3436cfSJed Brown   }
39551c3436cfSJed Brown   PetscFunctionReturn(0);
39561c3436cfSJed Brown }
39571c3436cfSJed Brown 
39581c3436cfSJed Brown #undef __FUNCT__
3959c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3960c5033834SJed Brown /*@
3961c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3962c5033834SJed Brown 
3963c5033834SJed Brown    Logically Collective
3964c5033834SJed Brown 
3965c5033834SJed Brown    Input Arguments:
3966c5033834SJed Brown .  ts - time integration context
3967c5033834SJed Brown 
3968c5033834SJed Brown    Output Arguments:
39690298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
39700298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
39710298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
39720298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
3973c5033834SJed Brown 
3974c5033834SJed Brown    Level: beginner
3975c5033834SJed Brown 
3976c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3977c5033834SJed Brown @*/
3978c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3979c5033834SJed Brown {
3980c5033834SJed Brown   PetscFunctionBegin;
3981c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3982c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3983c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3984c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3985c5033834SJed Brown   PetscFunctionReturn(0);
3986c5033834SJed Brown }
3987c5033834SJed Brown 
3988c5033834SJed Brown #undef __FUNCT__
39891c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
39901c3436cfSJed Brown /*@
39911c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
39921c3436cfSJed Brown 
39931c3436cfSJed Brown    Collective on TS
39941c3436cfSJed Brown 
39951c3436cfSJed Brown    Input Arguments:
39961c3436cfSJed Brown +  ts - time stepping context
39971c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
39981c3436cfSJed Brown 
39991c3436cfSJed Brown    Output Arguments:
40001c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
40011c3436cfSJed Brown 
40021c3436cfSJed Brown    Level: developer
40031c3436cfSJed Brown 
40041c3436cfSJed Brown .seealso: TSSetTolerances()
40051c3436cfSJed Brown @*/
40061c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
40071c3436cfSJed Brown {
40081c3436cfSJed Brown   PetscErrorCode    ierr;
40091c3436cfSJed Brown   PetscInt          i,n,N;
40100910c330SBarry Smith   const PetscScalar *u,*y;
40110910c330SBarry Smith   Vec               U;
40121c3436cfSJed Brown   PetscReal         sum,gsum;
40131c3436cfSJed Brown 
40141c3436cfSJed Brown   PetscFunctionBegin;
40151c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40161c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
40171c3436cfSJed Brown   PetscValidPointer(norm,3);
40180910c330SBarry Smith   U = ts->vec_sol;
40190910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4020ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
40211c3436cfSJed Brown 
40220910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
40230910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
40240910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
40251c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
40261c3436cfSJed Brown   sum  = 0.;
4027ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4028ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4029ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4030ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4031ceefc113SJed Brown     for (i=0; i<n; i++) {
40320910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40330910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4034ceefc113SJed Brown     }
4035ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4036ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4037ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4038ceefc113SJed Brown     const PetscScalar *atol;
4039ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4040ceefc113SJed Brown     for (i=0; i<n; i++) {
40410910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40420910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4043ceefc113SJed Brown     }
4044ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4045ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4046ceefc113SJed Brown     const PetscScalar *rtol;
4047ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4048ceefc113SJed Brown     for (i=0; i<n; i++) {
40490910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40500910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4051ceefc113SJed Brown     }
4052ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4053ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
40541c3436cfSJed Brown     for (i=0; i<n; i++) {
40550910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40560910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
40571c3436cfSJed Brown     }
4058ceefc113SJed Brown   }
40590910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
40601c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
40611c3436cfSJed Brown 
4062ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
40631c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4064ce94432eSBarry Smith   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
40651c3436cfSJed Brown   PetscFunctionReturn(0);
40661c3436cfSJed Brown }
40671c3436cfSJed Brown 
40681c3436cfSJed Brown #undef __FUNCT__
40698d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
40708d59e960SJed Brown /*@
40718d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
40728d59e960SJed Brown 
40738d59e960SJed Brown    Logically Collective on TS
40748d59e960SJed Brown 
40758d59e960SJed Brown    Input Arguments:
40768d59e960SJed Brown +  ts - time stepping context
40778d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
40788d59e960SJed Brown 
40798d59e960SJed Brown    Note:
40808d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
40818d59e960SJed Brown 
40828d59e960SJed Brown    Level: intermediate
40838d59e960SJed Brown 
40848d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
40858d59e960SJed Brown @*/
40868d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
40878d59e960SJed Brown {
40888d59e960SJed Brown   PetscFunctionBegin;
40898d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40908d59e960SJed Brown   ts->cfltime_local = cfltime;
40918d59e960SJed Brown   ts->cfltime       = -1.;
40928d59e960SJed Brown   PetscFunctionReturn(0);
40938d59e960SJed Brown }
40948d59e960SJed Brown 
40958d59e960SJed Brown #undef __FUNCT__
40968d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
40978d59e960SJed Brown /*@
40988d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
40998d59e960SJed Brown 
41008d59e960SJed Brown    Collective on TS
41018d59e960SJed Brown 
41028d59e960SJed Brown    Input Arguments:
41038d59e960SJed Brown .  ts - time stepping context
41048d59e960SJed Brown 
41058d59e960SJed Brown    Output Arguments:
41068d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
41078d59e960SJed Brown 
41088d59e960SJed Brown    Level: advanced
41098d59e960SJed Brown 
41108d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
41118d59e960SJed Brown @*/
41128d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
41138d59e960SJed Brown {
41148d59e960SJed Brown   PetscErrorCode ierr;
41158d59e960SJed Brown 
41168d59e960SJed Brown   PetscFunctionBegin;
41178d59e960SJed Brown   if (ts->cfltime < 0) {
4118ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
41198d59e960SJed Brown   }
41208d59e960SJed Brown   *cfltime = ts->cfltime;
41218d59e960SJed Brown   PetscFunctionReturn(0);
41228d59e960SJed Brown }
41238d59e960SJed Brown 
41248d59e960SJed Brown #undef __FUNCT__
4125d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4126d6ebe24aSShri Abhyankar /*@
4127d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4128d6ebe24aSShri Abhyankar 
4129d6ebe24aSShri Abhyankar    Input Parameters:
4130d6ebe24aSShri Abhyankar .  ts   - the TS context.
4131d6ebe24aSShri Abhyankar .  xl   - lower bound.
4132d6ebe24aSShri Abhyankar .  xu   - upper bound.
4133d6ebe24aSShri Abhyankar 
4134d6ebe24aSShri Abhyankar    Notes:
4135d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
413633c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4137d6ebe24aSShri Abhyankar 
41382bd2b0e6SSatish Balay    Level: advanced
41392bd2b0e6SSatish Balay 
4140d6ebe24aSShri Abhyankar @*/
4141d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4142d6ebe24aSShri Abhyankar {
4143d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4144d6ebe24aSShri Abhyankar   SNES           snes;
4145d6ebe24aSShri Abhyankar 
4146d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4147d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4148d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4149d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4150d6ebe24aSShri Abhyankar }
4151d6ebe24aSShri Abhyankar 
4152325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4153c6db04a5SJed Brown #include <mex.h>
4154325fc9f4SBarry Smith 
4155325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4156325fc9f4SBarry Smith 
4157325fc9f4SBarry Smith #undef __FUNCT__
4158325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4159325fc9f4SBarry Smith /*
4160325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4161325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4162325fc9f4SBarry Smith 
4163325fc9f4SBarry Smith    Collective on TS
4164325fc9f4SBarry Smith 
4165325fc9f4SBarry Smith    Input Parameters:
4166325fc9f4SBarry Smith +  snes - the TS context
41670910c330SBarry Smith -  u - input vector
4168325fc9f4SBarry Smith 
4169325fc9f4SBarry Smith    Output Parameter:
4170325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4171325fc9f4SBarry Smith 
4172325fc9f4SBarry Smith    Notes:
4173325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4174325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4175325fc9f4SBarry Smith    themselves.
4176325fc9f4SBarry Smith 
4177325fc9f4SBarry Smith    Level: developer
4178325fc9f4SBarry Smith 
4179325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4180325fc9f4SBarry Smith 
4181325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4182325fc9f4SBarry Smith */
41830910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4184325fc9f4SBarry Smith {
4185325fc9f4SBarry Smith   PetscErrorCode  ierr;
4186325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4187325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4188325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4189325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4190325fc9f4SBarry Smith 
4191325fc9f4SBarry Smith   PetscFunctionBegin;
4192325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
41930910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
41940910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4195325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
41960910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4197325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4198325fc9f4SBarry Smith 
4199325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
42000910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
42010910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
42020910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4203bbd56ea5SKarl Rupp 
4204325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4205325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4206325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4207325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4208325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4209325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4210325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4211325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4212325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4213325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4214325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4215325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4216325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4217325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4218325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4219325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4220325fc9f4SBarry Smith   PetscFunctionReturn(0);
4221325fc9f4SBarry Smith }
4222325fc9f4SBarry Smith 
4223325fc9f4SBarry Smith 
4224325fc9f4SBarry Smith #undef __FUNCT__
4225325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4226325fc9f4SBarry Smith /*
4227325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4228325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4229e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4230325fc9f4SBarry Smith 
4231325fc9f4SBarry Smith    Logically Collective on TS
4232325fc9f4SBarry Smith 
4233325fc9f4SBarry Smith    Input Parameters:
4234325fc9f4SBarry Smith +  ts - the TS context
4235325fc9f4SBarry Smith -  func - function evaluation routine
4236325fc9f4SBarry Smith 
4237325fc9f4SBarry Smith    Calling sequence of func:
42380910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4239325fc9f4SBarry Smith 
4240325fc9f4SBarry Smith    Level: beginner
4241325fc9f4SBarry Smith 
4242325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4243325fc9f4SBarry Smith 
4244325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4245325fc9f4SBarry Smith */
4246cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4247325fc9f4SBarry Smith {
4248325fc9f4SBarry Smith   PetscErrorCode  ierr;
4249325fc9f4SBarry Smith   TSMatlabContext *sctx;
4250325fc9f4SBarry Smith 
4251325fc9f4SBarry Smith   PetscFunctionBegin;
4252325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4253325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4254325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4255325fc9f4SBarry Smith   /*
4256325fc9f4SBarry Smith      This should work, but it doesn't
4257325fc9f4SBarry Smith   sctx->ctx = ctx;
4258325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4259325fc9f4SBarry Smith   */
4260325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4261bbd56ea5SKarl Rupp 
42620298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4263325fc9f4SBarry Smith   PetscFunctionReturn(0);
4264325fc9f4SBarry Smith }
4265325fc9f4SBarry Smith 
4266325fc9f4SBarry Smith #undef __FUNCT__
4267325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4268325fc9f4SBarry Smith /*
4269325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4270325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4271325fc9f4SBarry Smith 
4272325fc9f4SBarry Smith    Collective on TS
4273325fc9f4SBarry Smith 
4274325fc9f4SBarry Smith    Input Parameters:
4275cdcf91faSSean Farley +  ts - the TS context
42760910c330SBarry Smith .  u - input vector
4277325fc9f4SBarry Smith .  A, B - the matrices
4278325fc9f4SBarry Smith -  ctx - user context
4279325fc9f4SBarry Smith 
4280325fc9f4SBarry Smith    Output Parameter:
4281325fc9f4SBarry Smith .  flag - structure of the matrix
4282325fc9f4SBarry Smith 
4283325fc9f4SBarry Smith    Level: developer
4284325fc9f4SBarry Smith 
4285325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4286325fc9f4SBarry Smith 
4287325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4288325fc9f4SBarry Smith @*/
42890910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4290325fc9f4SBarry Smith {
4291325fc9f4SBarry Smith   PetscErrorCode  ierr;
4292325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4293325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4294325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4295325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4296325fc9f4SBarry Smith 
4297325fc9f4SBarry Smith   PetscFunctionBegin;
4298cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42990910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4300325fc9f4SBarry Smith 
43010910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4302325fc9f4SBarry Smith 
4303cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
43040910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
43050910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
43060910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
43070910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4308bbd56ea5SKarl Rupp 
4309325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4310325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4311325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4312325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4313325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4314325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4315325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4316325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4317325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4318325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4319325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4320325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4321325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4322325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4323325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4324325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4325325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4326325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4327325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4328325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4329325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4330325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4331325fc9f4SBarry Smith   PetscFunctionReturn(0);
4332325fc9f4SBarry Smith }
4333325fc9f4SBarry Smith 
4334325fc9f4SBarry Smith 
4335325fc9f4SBarry Smith #undef __FUNCT__
4336325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4337325fc9f4SBarry Smith /*
4338325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4339e3c5b3baSBarry 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
4340325fc9f4SBarry Smith 
4341325fc9f4SBarry Smith    Logically Collective on TS
4342325fc9f4SBarry Smith 
4343325fc9f4SBarry Smith    Input Parameters:
4344cdcf91faSSean Farley +  ts - the TS context
4345325fc9f4SBarry Smith .  A,B - Jacobian matrices
4346325fc9f4SBarry Smith .  func - function evaluation routine
4347325fc9f4SBarry Smith -  ctx - user context
4348325fc9f4SBarry Smith 
4349325fc9f4SBarry Smith    Calling sequence of func:
43500910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4351325fc9f4SBarry Smith 
4352325fc9f4SBarry Smith 
4353325fc9f4SBarry Smith    Level: developer
4354325fc9f4SBarry Smith 
4355325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4356325fc9f4SBarry Smith 
4357325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4358325fc9f4SBarry Smith */
4359cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4360325fc9f4SBarry Smith {
4361325fc9f4SBarry Smith   PetscErrorCode  ierr;
4362325fc9f4SBarry Smith   TSMatlabContext *sctx;
4363325fc9f4SBarry Smith 
4364325fc9f4SBarry Smith   PetscFunctionBegin;
4365325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4366325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4367325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4368325fc9f4SBarry Smith   /*
4369325fc9f4SBarry Smith      This should work, but it doesn't
4370325fc9f4SBarry Smith   sctx->ctx = ctx;
4371325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4372325fc9f4SBarry Smith   */
4373325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4374bbd56ea5SKarl Rupp 
4375cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4376325fc9f4SBarry Smith   PetscFunctionReturn(0);
4377325fc9f4SBarry Smith }
4378325fc9f4SBarry Smith 
4379b5b1a830SBarry Smith #undef __FUNCT__
4380b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4381b5b1a830SBarry Smith /*
4382b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4383b5b1a830SBarry Smith 
4384b5b1a830SBarry Smith    Collective on TS
4385b5b1a830SBarry Smith 
4386b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4387b5b1a830SBarry Smith @*/
43880910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4389b5b1a830SBarry Smith {
4390b5b1a830SBarry Smith   PetscErrorCode  ierr;
4391b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4392a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4393b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4394b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4395b5b1a830SBarry Smith 
4396b5b1a830SBarry Smith   PetscFunctionBegin;
4397cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43980910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4399b5b1a830SBarry Smith 
4400cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
44010910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4402bbd56ea5SKarl Rupp 
4403b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4404b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4405b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4406b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4407b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4408b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4409b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4410b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4411b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4412b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4413b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4414b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4415b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4416b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4417b5b1a830SBarry Smith   PetscFunctionReturn(0);
4418b5b1a830SBarry Smith }
4419b5b1a830SBarry Smith 
4420b5b1a830SBarry Smith 
4421b5b1a830SBarry Smith #undef __FUNCT__
4422b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4423b5b1a830SBarry Smith /*
4424b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4425b5b1a830SBarry Smith 
4426b5b1a830SBarry Smith    Level: developer
4427b5b1a830SBarry Smith 
4428b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4429b5b1a830SBarry Smith 
4430b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4431b5b1a830SBarry Smith */
4432cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4433b5b1a830SBarry Smith {
4434b5b1a830SBarry Smith   PetscErrorCode  ierr;
4435b5b1a830SBarry Smith   TSMatlabContext *sctx;
4436b5b1a830SBarry Smith 
4437b5b1a830SBarry Smith   PetscFunctionBegin;
4438b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4439b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4440b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4441b5b1a830SBarry Smith   /*
4442b5b1a830SBarry Smith      This should work, but it doesn't
4443b5b1a830SBarry Smith   sctx->ctx = ctx;
4444b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4445b5b1a830SBarry Smith   */
4446b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4447bbd56ea5SKarl Rupp 
44480298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
4449b5b1a830SBarry Smith   PetscFunctionReturn(0);
4450b5b1a830SBarry Smith }
4451325fc9f4SBarry Smith #endif
4452b3603a34SBarry Smith 
44530b039ecaSBarry Smith 
44540b039ecaSBarry Smith 
4455b3603a34SBarry Smith #undef __FUNCT__
44564f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4457b3603a34SBarry Smith /*@C
44584f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4459b3603a34SBarry Smith        in a time based line graph
4460b3603a34SBarry Smith 
4461b3603a34SBarry Smith    Collective on TS
4462b3603a34SBarry Smith 
4463b3603a34SBarry Smith    Input Parameters:
4464b3603a34SBarry Smith +  ts - the TS context
4465b3603a34SBarry Smith .  step - current time-step
4466b3603a34SBarry Smith .  ptime - current time
4467b3603a34SBarry Smith -  lg - a line graph object
4468b3603a34SBarry Smith 
4469b3603a34SBarry Smith    Level: intermediate
4470b3603a34SBarry Smith 
44710b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4472b3603a34SBarry Smith 
4473b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4474b3603a34SBarry Smith 
4475b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4476b3603a34SBarry Smith @*/
44770910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4478b3603a34SBarry Smith {
4479b3603a34SBarry Smith   PetscErrorCode    ierr;
44800b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4481b3603a34SBarry Smith   const PetscScalar *yy;
448258ff32f7SBarry Smith   PetscInt          dim;
4483b3603a34SBarry Smith 
4484b3603a34SBarry Smith   PetscFunctionBegin;
448558ff32f7SBarry Smith   if (!step) {
4486a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4487a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4488a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
44890910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
44900b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
44910b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
449258ff32f7SBarry Smith   }
44930910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4494e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4495e3efe391SJed Brown   {
4496e3efe391SJed Brown     PetscReal *yreal;
4497e3efe391SJed Brown     PetscInt  i,n;
44980910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4499e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4500e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
45010b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4502e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4503e3efe391SJed Brown   }
4504e3efe391SJed Brown #else
45050b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4506e3efe391SJed Brown #endif
45070910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
45083fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
45090b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
45103923b477SBarry Smith   }
4511b3603a34SBarry Smith   PetscFunctionReturn(0);
4512b3603a34SBarry Smith }
4513b3603a34SBarry Smith 
4514b3603a34SBarry Smith #undef __FUNCT__
45154f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4516ef20d060SBarry Smith /*@C
45174f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4518ef20d060SBarry Smith        in a time based line graph
4519ef20d060SBarry Smith 
4520ef20d060SBarry Smith    Collective on TS
4521ef20d060SBarry Smith 
4522ef20d060SBarry Smith    Input Parameters:
4523ef20d060SBarry Smith +  ts - the TS context
4524ef20d060SBarry Smith .  step - current time-step
4525ef20d060SBarry Smith .  ptime - current time
4526ef20d060SBarry Smith -  lg - a line graph object
4527ef20d060SBarry Smith 
4528ef20d060SBarry Smith    Level: intermediate
4529ef20d060SBarry Smith 
4530abd5a294SJed Brown    Notes:
4531abd5a294SJed Brown    Only for sequential solves.
4532abd5a294SJed Brown 
4533abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4534abd5a294SJed Brown 
4535abd5a294SJed Brown    Options Database Keys:
45364f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4537ef20d060SBarry Smith 
4538ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4539ef20d060SBarry Smith 
4540abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4541ef20d060SBarry Smith @*/
45420910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4543ef20d060SBarry Smith {
4544ef20d060SBarry Smith   PetscErrorCode    ierr;
45450b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4546ef20d060SBarry Smith   const PetscScalar *yy;
4547ef20d060SBarry Smith   Vec               y;
4548a9f9c1f6SBarry Smith   PetscInt          dim;
4549ef20d060SBarry Smith 
4550ef20d060SBarry Smith   PetscFunctionBegin;
4551a9f9c1f6SBarry Smith   if (!step) {
4552a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4553a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
45541ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
45550910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4556a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4557a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4558a9f9c1f6SBarry Smith   }
45590910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4560ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
45610910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4562ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4563e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4564e3efe391SJed Brown   {
4565e3efe391SJed Brown     PetscReal *yreal;
4566e3efe391SJed Brown     PetscInt  i,n;
4567e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4568e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4569e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
45700b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4571e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4572e3efe391SJed Brown   }
4573e3efe391SJed Brown #else
45740b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4575e3efe391SJed Brown #endif
4576ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4577ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
45783fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
45790b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
45803923b477SBarry Smith   }
4581ef20d060SBarry Smith   PetscFunctionReturn(0);
4582ef20d060SBarry Smith }
4583ef20d060SBarry Smith 
4584201da799SBarry Smith #undef __FUNCT__
4585201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4586201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4587201da799SBarry Smith {
4588201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4589201da799SBarry Smith   PetscReal      x   = ptime,y;
4590201da799SBarry Smith   PetscErrorCode ierr;
4591201da799SBarry Smith   PetscInt       its;
4592201da799SBarry Smith 
4593201da799SBarry Smith   PetscFunctionBegin;
4594201da799SBarry Smith   if (!n) {
4595201da799SBarry Smith     PetscDrawAxis axis;
4596bbd56ea5SKarl Rupp 
4597201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4598201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4599201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4600bbd56ea5SKarl Rupp 
4601201da799SBarry Smith     ctx->snes_its = 0;
4602201da799SBarry Smith   }
4603201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4604201da799SBarry Smith   y    = its - ctx->snes_its;
4605201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
46063fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4607201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4608201da799SBarry Smith   }
4609201da799SBarry Smith   ctx->snes_its = its;
4610201da799SBarry Smith   PetscFunctionReturn(0);
4611201da799SBarry Smith }
4612201da799SBarry Smith 
4613201da799SBarry Smith #undef __FUNCT__
4614201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4615201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4616201da799SBarry Smith {
4617201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4618201da799SBarry Smith   PetscReal      x   = ptime,y;
4619201da799SBarry Smith   PetscErrorCode ierr;
4620201da799SBarry Smith   PetscInt       its;
4621201da799SBarry Smith 
4622201da799SBarry Smith   PetscFunctionBegin;
4623201da799SBarry Smith   if (!n) {
4624201da799SBarry Smith     PetscDrawAxis axis;
4625bbd56ea5SKarl Rupp 
4626201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4627201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4628201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4629bbd56ea5SKarl Rupp 
4630201da799SBarry Smith     ctx->ksp_its = 0;
4631201da799SBarry Smith   }
4632201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4633201da799SBarry Smith   y    = its - ctx->ksp_its;
4634201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
463599fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4636201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4637201da799SBarry Smith   }
4638201da799SBarry Smith   ctx->ksp_its = its;
4639201da799SBarry Smith   PetscFunctionReturn(0);
4640201da799SBarry Smith }
4641f9c1d6abSBarry Smith 
4642f9c1d6abSBarry Smith #undef __FUNCT__
4643f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4644f9c1d6abSBarry Smith /*@
4645f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4646f9c1d6abSBarry Smith 
4647f9c1d6abSBarry Smith    Collective on TS and Vec
4648f9c1d6abSBarry Smith 
4649f9c1d6abSBarry Smith    Input Parameters:
4650f9c1d6abSBarry Smith +  ts - the TS context
4651f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4652f9c1d6abSBarry Smith 
4653f9c1d6abSBarry Smith    Output Parameters:
4654f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4655f9c1d6abSBarry Smith 
4656f9c1d6abSBarry Smith    Level: developer
4657f9c1d6abSBarry Smith 
4658f9c1d6abSBarry Smith .keywords: TS, compute
4659f9c1d6abSBarry Smith 
4660f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4661f9c1d6abSBarry Smith @*/
4662f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4663f9c1d6abSBarry Smith {
4664f9c1d6abSBarry Smith   PetscErrorCode ierr;
4665f9c1d6abSBarry Smith 
4666f9c1d6abSBarry Smith   PetscFunctionBegin;
4667f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4668ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4669f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4670f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4671f9c1d6abSBarry Smith }
4672