xref: /petsc/src/ts/interface/ts.c (revision 9804daf34a45a1225c622b14639ad2e72848b92f)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
4d763cef2SBarry Smith 
5d5ba7fb7SMatthew Knepley /* Logging support */
6d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
7166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
8d405a339SMatthew Knepley 
949354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1049354f04SShri Abhyankar 
114a2ae208SSatish Balay #undef __FUNCT__
12bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
13bdad233fSMatthew Knepley /*
14bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
15bdad233fSMatthew Knepley 
16bdad233fSMatthew Knepley   Collective on TS
17bdad233fSMatthew Knepley 
18bdad233fSMatthew Knepley   Input Parameter:
19bdad233fSMatthew Knepley . ts - The ts
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley   Level: intermediate
22bdad233fSMatthew Knepley 
23bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
24bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
25bdad233fSMatthew Knepley */
266849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
27bdad233fSMatthew Knepley {
28ace3abfcSBarry Smith   PetscBool      opt;
292fc52814SBarry Smith   const char     *defaultType;
30bdad233fSMatthew Knepley   char           typeName[256];
31dfbe8321SBarry Smith   PetscErrorCode ierr;
32bdad233fSMatthew Knepley 
33bdad233fSMatthew Knepley   PetscFunctionBegin;
34bbd56ea5SKarl Rupp   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
35bbd56ea5SKarl Rupp   else defaultType = TSEULER;
36bdad233fSMatthew Knepley 
370298fd71SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(NULL);CHKERRQ(ierr);}
38bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
39a7cc72afSBarry Smith   if (opt) {
40bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
41bdad233fSMatthew Knepley   } else {
42bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
43bdad233fSMatthew Knepley   }
44bdad233fSMatthew Knepley   PetscFunctionReturn(0);
45bdad233fSMatthew Knepley }
46bdad233fSMatthew Knepley 
47bdad233fSMatthew Knepley #undef __FUNCT__
48bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
49bdad233fSMatthew Knepley /*@
50bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
51bdad233fSMatthew Knepley 
52bdad233fSMatthew Knepley    Collective on TS
53bdad233fSMatthew Knepley 
54bdad233fSMatthew Knepley    Input Parameter:
55bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
56bdad233fSMatthew Knepley 
57bdad233fSMatthew Knepley    Options Database Keys:
584d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
59bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
603bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
61bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
62bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
63de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
64de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
65de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
66de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
67de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
68de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
69de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
70de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
71de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
7291b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
7391b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
7491b97e58SBarry Smith 
7591b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
76bdad233fSMatthew Knepley 
77bdad233fSMatthew Knepley    Level: beginner
78bdad233fSMatthew Knepley 
79bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
80bdad233fSMatthew Knepley 
81a313700dSBarry Smith .seealso: TSGetType()
82bdad233fSMatthew Knepley @*/
837087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
84bdad233fSMatthew Knepley {
85ace3abfcSBarry Smith   PetscBool              opt,flg;
86dfbe8321SBarry Smith   PetscErrorCode         ierr;
87649052a6SBarry Smith   PetscViewer            monviewer;
88eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
89089b2837SJed Brown   SNES                   snes;
901c3436cfSJed Brown   TSAdapt                adapt;
9131748224SBarry Smith   PetscReal              time_step;
9249354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
93d1212d36SBarry Smith   char                   dir[16];
94bdad233fSMatthew Knepley 
95bdad233fSMatthew Knepley   PetscFunctionBegin;
960700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
973194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
98a43b19c4SJed Brown   /* Handle TS type options */
99a43b19c4SJed Brown   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
100bdad233fSMatthew Knepley 
101bdad233fSMatthew Knepley   /* Handle generic TS options */
1020298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1030298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1040298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
10531748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
10631748224SBarry Smith   if (flg) {
10731748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
10831748224SBarry Smith   }
10949354f04SShri 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);
11049354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1110298fd71SBarry 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);
1120298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1130298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1140298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1150298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
116bdad233fSMatthew Knepley 
117bdad233fSMatthew Knepley   /* Monitor options */
118a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
119eabae89aSBarry Smith   if (flg) {
120ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
121649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
122bdad233fSMatthew Knepley   }
1235180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1245180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1255180491cSLisandro Dalcin 
1264f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
127a7cc72afSBarry Smith   if (opt) {
1280b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1293923b477SBarry Smith     PetscInt       howoften = 1;
130a80ad3e0SBarry Smith 
1310298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
132ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1334f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
134b3603a34SBarry Smith   }
1354f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
136b3603a34SBarry Smith   if (opt) {
1370b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1383923b477SBarry Smith     PetscInt       howoften = 1;
139b3603a34SBarry Smith 
1400298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
14122d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1424f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
143bdad233fSMatthew Knepley   }
1444f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
145ef20d060SBarry Smith   if (opt) {
1460b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1473923b477SBarry Smith     PetscInt       howoften = 1;
148ef20d060SBarry Smith 
1490298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
15022d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1514f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
152ef20d060SBarry Smith   }
153201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
154201da799SBarry Smith   if (opt) {
155201da799SBarry Smith     TSMonitorLGCtx ctx;
156201da799SBarry Smith     PetscInt       howoften = 1;
157201da799SBarry Smith 
1580298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
15922d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
160201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
161201da799SBarry Smith   }
162201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
163201da799SBarry Smith   if (opt) {
164201da799SBarry Smith     TSMonitorLGCtx ctx;
165201da799SBarry Smith     PetscInt       howoften = 1;
166201da799SBarry Smith 
1670298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
16822d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
169201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
170201da799SBarry Smith   }
1718189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1728189c53fSBarry Smith   if (opt) {
1738189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
1748189c53fSBarry Smith     PetscInt          howoften = 1;
1758189c53fSBarry Smith 
1760298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
17722d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1788189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
1798189c53fSBarry Smith   }
180ef20d060SBarry Smith   opt  = PETSC_FALSE;
1810dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
182a7cc72afSBarry Smith   if (opt) {
18383a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
18483a4ac43SBarry Smith     PetscInt         howoften = 1;
185a80ad3e0SBarry Smith 
1860298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
187ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
18883a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
189bdad233fSMatthew Knepley   }
190fb1732b5SBarry Smith   opt  = PETSC_FALSE;
1910dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
1923a471f94SBarry Smith   if (opt) {
19383a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
19483a4ac43SBarry Smith     PetscInt         howoften = 1;
1953a471f94SBarry Smith 
1960298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
197ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
19883a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
1993a471f94SBarry Smith   }
2003a471f94SBarry Smith   opt  = PETSC_FALSE;
20191b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
202fb1732b5SBarry Smith   if (flg) {
203fb1732b5SBarry Smith     PetscViewer ctx;
204fb1732b5SBarry Smith     if (monfilename[0]) {
205ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
206c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
207fb1732b5SBarry Smith     } else {
208ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2090298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
210fb1732b5SBarry Smith     }
211fb1732b5SBarry Smith   }
212ed81e22dSJed Brown   opt  = PETSC_FALSE;
21391b97e58SBarry 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);
214ed81e22dSJed Brown   if (flg) {
215ed81e22dSJed Brown     const char *ptr,*ptr2;
216ed81e22dSJed Brown     char       *filetemplate;
217ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
218ed81e22dSJed Brown     /* Do some cursory validation of the input. */
219ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
220ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
221ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
222ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
223ce94432eSBarry 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");
224ed81e22dSJed Brown       if (ptr2) break;
225ed81e22dSJed Brown     }
226ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
227ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
228ed81e22dSJed Brown   }
229bdad233fSMatthew Knepley 
230d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
231d1212d36SBarry Smith   if (flg) {
232d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
233d1212d36SBarry Smith     int                 ray = 0;
234d1212d36SBarry Smith     DMDADirection       ddir;
235d1212d36SBarry Smith     DM                  da;
236d1212d36SBarry Smith     PetscMPIInt         rank;
237d1212d36SBarry Smith 
238ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
239d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
240d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
241ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
242d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
243d1212d36SBarry Smith 
244d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
245d1212d36SBarry Smith     ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr);
246d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
247d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
248ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
249d1212d36SBarry Smith     if (!rank) {
250d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
251d1212d36SBarry Smith     }
252d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
253d1212d36SBarry Smith   }
254d1212d36SBarry Smith 
255ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&adapt);CHKERRQ(ierr);
2561c3436cfSJed Brown   ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2571c3436cfSJed Brown 
258d52bd9f3SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
259d52bd9f3SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
260d52bd9f3SBarry Smith 
261bdad233fSMatthew Knepley   /* Handle specific TS options */
262abc0a331SBarry Smith   if (ts->ops->setfromoptions) {
263bdad233fSMatthew Knepley     ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
264bdad233fSMatthew Knepley   }
2655d973c19SBarry Smith 
2665d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
2675d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
268bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
269bdad233fSMatthew Knepley   PetscFunctionReturn(0);
270bdad233fSMatthew Knepley }
271bdad233fSMatthew Knepley 
272bdad233fSMatthew Knepley #undef __FUNCT__
273cdcf91faSSean Farley #undef __FUNCT__
2744a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
275a7a1495cSBarry Smith /*@
2768c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
277a7a1495cSBarry Smith       set with TSSetRHSJacobian().
278a7a1495cSBarry Smith 
279a7a1495cSBarry Smith    Collective on TS and Vec
280a7a1495cSBarry Smith 
281a7a1495cSBarry Smith    Input Parameters:
282316643e7SJed Brown +  ts - the TS context
283a7a1495cSBarry Smith .  t - current timestep
2840910c330SBarry Smith -  U - input vector
285a7a1495cSBarry Smith 
286a7a1495cSBarry Smith    Output Parameters:
287a7a1495cSBarry Smith +  A - Jacobian matrix
288a7a1495cSBarry Smith .  B - optional preconditioning matrix
289a7a1495cSBarry Smith -  flag - flag indicating matrix structure
290a7a1495cSBarry Smith 
291a7a1495cSBarry Smith    Notes:
292a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
293a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
294a7a1495cSBarry Smith 
29594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
296a7a1495cSBarry Smith    flag parameter.
297a7a1495cSBarry Smith 
298a7a1495cSBarry Smith    Level: developer
299a7a1495cSBarry Smith 
300a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
301a7a1495cSBarry Smith 
30294b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
303a7a1495cSBarry Smith @*/
3040910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
305a7a1495cSBarry Smith {
306dfbe8321SBarry Smith   PetscErrorCode ierr;
3070910c330SBarry Smith   PetscInt       Ustate;
30824989b8cSPeter Brune   DM             dm;
309942e3340SBarry Smith   DMTS           tsdm;
31024989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
31124989b8cSPeter Brune   void           *ctx;
31224989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
313a7a1495cSBarry Smith 
314a7a1495cSBarry Smith   PetscFunctionBegin;
3150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3160910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3170910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
31824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
319942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
32024989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
3210298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
3220910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
3230910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
3240e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
3250e4ef248SJed Brown     PetscFunctionReturn(0);
326f8ede8e7SJed Brown   }
327d90be118SSean Farley 
328ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
329d90be118SSean Farley 
33024989b8cSPeter Brune   if (rhsjacobianfunc) {
3310910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
332a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
333a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3340910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
335a7a1495cSBarry Smith     PetscStackPop;
3360910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
337a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3380700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3390700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
340ef66eb69SBarry Smith   } else {
341214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
342214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
343214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
344ef66eb69SBarry Smith   }
3450e4ef248SJed Brown   ts->rhsjacobian.time       = t;
3460910c330SBarry Smith   ts->rhsjacobian.X          = U;
3470910c330SBarry Smith   ierr                       = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3480e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
349a7a1495cSBarry Smith   PetscFunctionReturn(0);
350a7a1495cSBarry Smith }
351a7a1495cSBarry Smith 
3524a2ae208SSatish Balay #undef __FUNCT__
3534a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
354316643e7SJed Brown /*@
355d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
356d763cef2SBarry Smith 
357316643e7SJed Brown    Collective on TS and Vec
358316643e7SJed Brown 
359316643e7SJed Brown    Input Parameters:
360316643e7SJed Brown +  ts - the TS context
361316643e7SJed Brown .  t - current time
3620910c330SBarry Smith -  U - state vector
363316643e7SJed Brown 
364316643e7SJed Brown    Output Parameter:
365316643e7SJed Brown .  y - right hand side
366316643e7SJed Brown 
367316643e7SJed Brown    Note:
368316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
369316643e7SJed Brown    is used internally within the nonlinear solvers.
370316643e7SJed Brown 
371316643e7SJed Brown    Level: developer
372316643e7SJed Brown 
373316643e7SJed Brown .keywords: TS, compute
374316643e7SJed Brown 
375316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
376316643e7SJed Brown @*/
3770910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
378d763cef2SBarry Smith {
379dfbe8321SBarry Smith   PetscErrorCode ierr;
38024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
38124989b8cSPeter Brune   TSIFunction    ifunction;
38224989b8cSPeter Brune   void           *ctx;
38324989b8cSPeter Brune   DM             dm;
38424989b8cSPeter Brune 
385d763cef2SBarry Smith   PetscFunctionBegin;
3860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3870910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3880700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
38924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
39024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
3910298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
392d763cef2SBarry Smith 
393ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
394d763cef2SBarry Smith 
3950910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
39624989b8cSPeter Brune   if (rhsfunction) {
397d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
3980910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
399d763cef2SBarry Smith     PetscStackPop;
400214bc6a2SJed Brown   } else {
401214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
402b2cd27e8SJed Brown   }
40344a41b28SSean Farley 
4040910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
405d763cef2SBarry Smith   PetscFunctionReturn(0);
406d763cef2SBarry Smith }
407d763cef2SBarry Smith 
4084a2ae208SSatish Balay #undef __FUNCT__
409ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
410ef20d060SBarry Smith /*@
411ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
412ef20d060SBarry Smith 
413ef20d060SBarry Smith    Collective on TS and Vec
414ef20d060SBarry Smith 
415ef20d060SBarry Smith    Input Parameters:
416ef20d060SBarry Smith +  ts - the TS context
417ef20d060SBarry Smith -  t - current time
418ef20d060SBarry Smith 
419ef20d060SBarry Smith    Output Parameter:
4200910c330SBarry Smith .  U - the solution
421ef20d060SBarry Smith 
422ef20d060SBarry Smith    Note:
423ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
424ef20d060SBarry Smith    is used internally within the nonlinear solvers.
425ef20d060SBarry Smith 
426ef20d060SBarry Smith    Level: developer
427ef20d060SBarry Smith 
428ef20d060SBarry Smith .keywords: TS, compute
429ef20d060SBarry Smith 
430abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
431ef20d060SBarry Smith @*/
4320910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
433ef20d060SBarry Smith {
434ef20d060SBarry Smith   PetscErrorCode     ierr;
435ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
436ef20d060SBarry Smith   void               *ctx;
437ef20d060SBarry Smith   DM                 dm;
438ef20d060SBarry Smith 
439ef20d060SBarry Smith   PetscFunctionBegin;
440ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4410910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
442ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
443ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
444ef20d060SBarry Smith 
445ef20d060SBarry Smith   if (solutionfunction) {
4469b7cd975SBarry Smith     PetscStackPush("TS user solution function");
4470910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
448ef20d060SBarry Smith     PetscStackPop;
449ef20d060SBarry Smith   }
450ef20d060SBarry Smith   PetscFunctionReturn(0);
451ef20d060SBarry Smith }
4529b7cd975SBarry Smith #undef __FUNCT__
4539b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
4549b7cd975SBarry Smith /*@
4559b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
4569b7cd975SBarry Smith 
4579b7cd975SBarry Smith    Collective on TS and Vec
4589b7cd975SBarry Smith 
4599b7cd975SBarry Smith    Input Parameters:
4609b7cd975SBarry Smith +  ts - the TS context
4619b7cd975SBarry Smith -  t - current time
4629b7cd975SBarry Smith 
4639b7cd975SBarry Smith    Output Parameter:
4649b7cd975SBarry Smith .  U - the function value
4659b7cd975SBarry Smith 
4669b7cd975SBarry Smith    Note:
4679b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
4689b7cd975SBarry Smith    is used internally within the nonlinear solvers.
4699b7cd975SBarry Smith 
4709b7cd975SBarry Smith    Level: developer
4719b7cd975SBarry Smith 
4729b7cd975SBarry Smith .keywords: TS, compute
4739b7cd975SBarry Smith 
4749b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
4759b7cd975SBarry Smith @*/
4769b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
4779b7cd975SBarry Smith {
4789b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
4799b7cd975SBarry Smith   void               *ctx;
4809b7cd975SBarry Smith   DM                 dm;
4819b7cd975SBarry Smith 
4829b7cd975SBarry Smith   PetscFunctionBegin;
4839b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4849b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4859b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4869b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
4879b7cd975SBarry Smith 
4889b7cd975SBarry Smith   if (forcing) {
4899b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
4909b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
4919b7cd975SBarry Smith     PetscStackPop;
4929b7cd975SBarry Smith   }
4939b7cd975SBarry Smith   PetscFunctionReturn(0);
4949b7cd975SBarry Smith }
495ef20d060SBarry Smith 
496ef20d060SBarry Smith #undef __FUNCT__
497214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
498214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
499214bc6a2SJed Brown {
5002dd45cf8SJed Brown   Vec            F;
501214bc6a2SJed Brown   PetscErrorCode ierr;
502214bc6a2SJed Brown 
503214bc6a2SJed Brown   PetscFunctionBegin;
5040298fd71SBarry Smith   *Frhs = NULL;
5050298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
506214bc6a2SJed Brown   if (!ts->Frhs) {
5072dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
508214bc6a2SJed Brown   }
509214bc6a2SJed Brown   *Frhs = ts->Frhs;
510214bc6a2SJed Brown   PetscFunctionReturn(0);
511214bc6a2SJed Brown }
512214bc6a2SJed Brown 
513214bc6a2SJed Brown #undef __FUNCT__
514214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
515214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
516214bc6a2SJed Brown {
517214bc6a2SJed Brown   Mat            A,B;
5182dd45cf8SJed Brown   PetscErrorCode ierr;
519214bc6a2SJed Brown 
520214bc6a2SJed Brown   PetscFunctionBegin;
5210298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
522214bc6a2SJed Brown   if (Arhs) {
523214bc6a2SJed Brown     if (!ts->Arhs) {
524214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
525214bc6a2SJed Brown     }
526214bc6a2SJed Brown     *Arhs = ts->Arhs;
527214bc6a2SJed Brown   }
528214bc6a2SJed Brown   if (Brhs) {
529214bc6a2SJed Brown     if (!ts->Brhs) {
530214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
531214bc6a2SJed Brown     }
532214bc6a2SJed Brown     *Brhs = ts->Brhs;
533214bc6a2SJed Brown   }
534214bc6a2SJed Brown   PetscFunctionReturn(0);
535214bc6a2SJed Brown }
536214bc6a2SJed Brown 
537214bc6a2SJed Brown #undef __FUNCT__
538316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
539316643e7SJed Brown /*@
5400910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
541316643e7SJed Brown 
542316643e7SJed Brown    Collective on TS and Vec
543316643e7SJed Brown 
544316643e7SJed Brown    Input Parameters:
545316643e7SJed Brown +  ts - the TS context
546316643e7SJed Brown .  t - current time
5470910c330SBarry Smith .  U - state vector
5480910c330SBarry Smith .  Udot - time derivative of state vector
549214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
550316643e7SJed Brown 
551316643e7SJed Brown    Output Parameter:
552316643e7SJed Brown .  Y - right hand side
553316643e7SJed Brown 
554316643e7SJed Brown    Note:
555316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
556316643e7SJed Brown    is used internally within the nonlinear solvers.
557316643e7SJed Brown 
558316643e7SJed Brown    If the user did did not write their equations in implicit form, this
559316643e7SJed Brown    function recasts them in implicit form.
560316643e7SJed Brown 
561316643e7SJed Brown    Level: developer
562316643e7SJed Brown 
563316643e7SJed Brown .keywords: TS, compute
564316643e7SJed Brown 
565316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
566316643e7SJed Brown @*/
5670910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
568316643e7SJed Brown {
569316643e7SJed Brown   PetscErrorCode ierr;
57024989b8cSPeter Brune   TSIFunction    ifunction;
57124989b8cSPeter Brune   TSRHSFunction  rhsfunction;
57224989b8cSPeter Brune   void           *ctx;
57324989b8cSPeter Brune   DM             dm;
574316643e7SJed Brown 
575316643e7SJed Brown   PetscFunctionBegin;
5760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5770910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5780910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
5790700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
580316643e7SJed Brown 
58124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
58224989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
5830298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
58424989b8cSPeter Brune 
585ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
586d90be118SSean Farley 
5870910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
58824989b8cSPeter Brune   if (ifunction) {
589316643e7SJed Brown     PetscStackPush("TS user implicit function");
5900910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
591316643e7SJed Brown     PetscStackPop;
592214bc6a2SJed Brown   }
593214bc6a2SJed Brown   if (imex) {
59424989b8cSPeter Brune     if (!ifunction) {
5950910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
5962dd45cf8SJed Brown     }
59724989b8cSPeter Brune   } else if (rhsfunction) {
59824989b8cSPeter Brune     if (ifunction) {
599214bc6a2SJed Brown       Vec Frhs;
600214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
6010910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
602214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
6032dd45cf8SJed Brown     } else {
6040910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
6050910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
606316643e7SJed Brown     }
6074a6899ffSJed Brown   }
6080910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
609316643e7SJed Brown   PetscFunctionReturn(0);
610316643e7SJed Brown }
611316643e7SJed Brown 
612316643e7SJed Brown #undef __FUNCT__
613316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
614316643e7SJed Brown /*@
615316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
616316643e7SJed Brown 
617316643e7SJed Brown    Collective on TS and Vec
618316643e7SJed Brown 
619316643e7SJed Brown    Input
620316643e7SJed Brown       Input Parameters:
621316643e7SJed Brown +  ts - the TS context
622316643e7SJed Brown .  t - current timestep
6230910c330SBarry Smith .  U - state vector
6240910c330SBarry Smith .  Udot - time derivative of state vector
625214bc6a2SJed Brown .  shift - shift to apply, see note below
626214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
627316643e7SJed Brown 
628316643e7SJed Brown    Output Parameters:
629316643e7SJed Brown +  A - Jacobian matrix
630316643e7SJed Brown .  B - optional preconditioning matrix
631316643e7SJed Brown -  flag - flag indicating matrix structure
632316643e7SJed Brown 
633316643e7SJed Brown    Notes:
6340910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
635316643e7SJed Brown 
6360910c330SBarry Smith    dF/dU + shift*dF/dUdot
637316643e7SJed Brown 
638316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
639316643e7SJed Brown    is used internally within the nonlinear solvers.
640316643e7SJed Brown 
641316643e7SJed Brown    Level: developer
642316643e7SJed Brown 
643316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
644316643e7SJed Brown 
645316643e7SJed Brown .seealso:  TSSetIJacobian()
64663495f91SJed Brown @*/
6470910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
648316643e7SJed Brown {
6490910c330SBarry Smith   PetscInt       Ustate, Udotstate;
650316643e7SJed Brown   PetscErrorCode ierr;
65124989b8cSPeter Brune   TSIJacobian    ijacobian;
65224989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
65324989b8cSPeter Brune   DM             dm;
65424989b8cSPeter Brune   void           *ctx;
655316643e7SJed Brown 
656316643e7SJed Brown   PetscFunctionBegin;
6570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6580910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6590910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
660316643e7SJed Brown   PetscValidPointer(A,6);
6610700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
662316643e7SJed Brown   PetscValidPointer(B,7);
6630700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
664316643e7SJed Brown   PetscValidPointer(flg,8);
66524989b8cSPeter Brune 
66624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
66724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
6680298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
66924989b8cSPeter Brune 
6700910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
6710910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
6720910c330SBarry 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))) {
6730026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
6740026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
6750026cea9SSean Farley     PetscFunctionReturn(0);
6760026cea9SSean Farley   }
677316643e7SJed Brown 
678ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
679316643e7SJed Brown 
6804e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
6810910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
68224989b8cSPeter Brune   if (ijacobian) {
6832dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
684316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
6850910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
686316643e7SJed Brown     PetscStackPop;
687214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
688214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
689214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
6904a6899ffSJed Brown   }
691214bc6a2SJed Brown   if (imex) {
692b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
693214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
6942dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
695214bc6a2SJed Brown       if (*A != *B) {
696214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
697214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
698214bc6a2SJed Brown       }
699214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
700214bc6a2SJed Brown     }
701214bc6a2SJed Brown   } else {
70224989b8cSPeter Brune     if (!ijacobian) {
7030910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr);
704214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
705214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
706316643e7SJed Brown       if (*A != *B) {
707316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
708316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
709316643e7SJed Brown       }
71024989b8cSPeter Brune     } else if (rhsjacobian) {
711214bc6a2SJed Brown       Mat          Arhs,Brhs;
712214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
713214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
7140910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
715214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
716214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
717214bc6a2SJed Brown       if (*A != *B) {
718214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
719214bc6a2SJed Brown       }
720214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
721214bc6a2SJed Brown     }
722316643e7SJed Brown   }
7230026cea9SSean Farley 
7240026cea9SSean Farley   ts->ijacobian.time = t;
7250910c330SBarry Smith   ts->ijacobian.X    = U;
7260910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
727bbd56ea5SKarl Rupp 
7280910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
7290910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
730bbd56ea5SKarl Rupp 
7310026cea9SSean Farley   ts->ijacobian.shift      = shift;
7320026cea9SSean Farley   ts->ijacobian.imex       = imex;
7330026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
734bbd56ea5SKarl Rupp 
7350910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
736316643e7SJed Brown   PetscFunctionReturn(0);
737316643e7SJed Brown }
738316643e7SJed Brown 
739316643e7SJed Brown #undef __FUNCT__
7404a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
741d763cef2SBarry Smith /*@C
742d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
743b5abc632SBarry Smith     where U_t = G(t,u).
744d763cef2SBarry Smith 
7453f9fe445SBarry Smith     Logically Collective on TS
746d763cef2SBarry Smith 
747d763cef2SBarry Smith     Input Parameters:
748d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
7490298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
750d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
751d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
7520298fd71SBarry Smith           function evaluation routine (may be NULL)
753d763cef2SBarry Smith 
754d763cef2SBarry Smith     Calling sequence of func:
75587828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
756d763cef2SBarry Smith 
757d763cef2SBarry Smith +   t - current timestep
758d763cef2SBarry Smith .   u - input vector
759d763cef2SBarry Smith .   F - function vector
760d763cef2SBarry Smith -   ctx - [optional] user-defined function context
761d763cef2SBarry Smith 
762d763cef2SBarry Smith     Level: beginner
763d763cef2SBarry Smith 
764d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
765d763cef2SBarry Smith 
766d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
767d763cef2SBarry Smith @*/
768089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
769d763cef2SBarry Smith {
770089b2837SJed Brown   PetscErrorCode ierr;
771089b2837SJed Brown   SNES           snes;
7720298fd71SBarry Smith   Vec            ralloc = NULL;
77324989b8cSPeter Brune   DM             dm;
774d763cef2SBarry Smith 
775089b2837SJed Brown   PetscFunctionBegin;
7760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
777ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
77824989b8cSPeter Brune 
77924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
78024989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
781089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
782e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
783e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
784e856ceecSJed Brown     r    = ralloc;
785e856ceecSJed Brown   }
786089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
787e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
788d763cef2SBarry Smith   PetscFunctionReturn(0);
789d763cef2SBarry Smith }
790d763cef2SBarry Smith 
7914a2ae208SSatish Balay #undef __FUNCT__
792ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
793ef20d060SBarry Smith /*@C
794abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
795ef20d060SBarry Smith 
796ef20d060SBarry Smith     Logically Collective on TS
797ef20d060SBarry Smith 
798ef20d060SBarry Smith     Input Parameters:
799ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
800ef20d060SBarry Smith .   f - routine for evaluating the solution
801ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
8020298fd71SBarry Smith           function evaluation routine (may be NULL)
803ef20d060SBarry Smith 
804ef20d060SBarry Smith     Calling sequence of func:
805ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
806ef20d060SBarry Smith 
807ef20d060SBarry Smith +   t - current timestep
808ef20d060SBarry Smith .   u - output vector
809ef20d060SBarry Smith -   ctx - [optional] user-defined function context
810ef20d060SBarry Smith 
811abd5a294SJed Brown     Notes:
812abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
813abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
814abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
815abd5a294SJed Brown 
8164f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
817abd5a294SJed Brown 
818ef20d060SBarry Smith     Level: beginner
819ef20d060SBarry Smith 
820ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
821ef20d060SBarry Smith 
8229b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
823ef20d060SBarry Smith @*/
824ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
825ef20d060SBarry Smith {
826ef20d060SBarry Smith   PetscErrorCode ierr;
827ef20d060SBarry Smith   DM             dm;
828ef20d060SBarry Smith 
829ef20d060SBarry Smith   PetscFunctionBegin;
830ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
831ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
832ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
833ef20d060SBarry Smith   PetscFunctionReturn(0);
834ef20d060SBarry Smith }
835ef20d060SBarry Smith 
836ef20d060SBarry Smith #undef __FUNCT__
8379b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
8389b7cd975SBarry Smith /*@C
8399b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
8409b7cd975SBarry Smith 
8419b7cd975SBarry Smith     Logically Collective on TS
8429b7cd975SBarry Smith 
8439b7cd975SBarry Smith     Input Parameters:
8449b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
8459b7cd975SBarry Smith .   f - routine for evaluating the forcing function
8469b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
8470298fd71SBarry Smith           function evaluation routine (may be NULL)
8489b7cd975SBarry Smith 
8499b7cd975SBarry Smith     Calling sequence of func:
8509b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
8519b7cd975SBarry Smith 
8529b7cd975SBarry Smith +   t - current timestep
8539b7cd975SBarry Smith .   u - output vector
8549b7cd975SBarry Smith -   ctx - [optional] user-defined function context
8559b7cd975SBarry Smith 
8569b7cd975SBarry Smith     Notes:
8579b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
8589b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
8599b7cd975SBarry Smith 
8609b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
8619b7cd975SBarry Smith 
8629b7cd975SBarry Smith     Level: beginner
8639b7cd975SBarry Smith 
8649b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
8659b7cd975SBarry Smith 
8669b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
8679b7cd975SBarry Smith @*/
8689b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
8699b7cd975SBarry Smith {
8709b7cd975SBarry Smith   PetscErrorCode ierr;
8719b7cd975SBarry Smith   DM             dm;
8729b7cd975SBarry Smith 
8739b7cd975SBarry Smith   PetscFunctionBegin;
8749b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8759b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
8769b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
8779b7cd975SBarry Smith   PetscFunctionReturn(0);
8789b7cd975SBarry Smith }
8799b7cd975SBarry Smith 
8809b7cd975SBarry Smith #undef __FUNCT__
8814a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
882d763cef2SBarry Smith /*@C
883d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
884b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
885d763cef2SBarry Smith 
8863f9fe445SBarry Smith    Logically Collective on TS
887d763cef2SBarry Smith 
888d763cef2SBarry Smith    Input Parameters:
889d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
890d763cef2SBarry Smith .  A   - Jacobian matrix
891d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
892d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
893d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
8940298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
895d763cef2SBarry Smith 
896d763cef2SBarry Smith    Calling sequence of func:
89787828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
898d763cef2SBarry Smith 
899d763cef2SBarry Smith +  t - current timestep
900d763cef2SBarry Smith .  u - input vector
901d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
902d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
903d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
90494b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
905d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
906d763cef2SBarry Smith 
907d763cef2SBarry Smith    Notes:
90894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
909d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
910d763cef2SBarry Smith 
911d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
912d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
91356335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
914d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
915d763cef2SBarry Smith    throughout the global iterations.
916d763cef2SBarry Smith 
917d763cef2SBarry Smith    Level: beginner
918d763cef2SBarry Smith 
919d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
920d763cef2SBarry Smith 
921ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
922d763cef2SBarry Smith 
923d763cef2SBarry Smith @*/
924089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
925d763cef2SBarry Smith {
926277b19d0SLisandro Dalcin   PetscErrorCode ierr;
927089b2837SJed Brown   SNES           snes;
92824989b8cSPeter Brune   DM             dm;
92924989b8cSPeter Brune   TSIJacobian    ijacobian;
930277b19d0SLisandro Dalcin 
931d763cef2SBarry Smith   PetscFunctionBegin;
9320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
933277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
934277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
935277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
936277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
937d763cef2SBarry Smith 
93824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
93924989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
9400298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
94124989b8cSPeter Brune 
942089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
9435f659677SPeter Brune   if (!ijacobian) {
944089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
9450e4ef248SJed Brown   }
9460e4ef248SJed Brown   if (A) {
9470e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
9480e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
949bbd56ea5SKarl Rupp 
9500e4ef248SJed Brown     ts->Arhs = A;
9510e4ef248SJed Brown   }
9520e4ef248SJed Brown   if (B) {
9530e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
9540e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
955bbd56ea5SKarl Rupp 
9560e4ef248SJed Brown     ts->Brhs = B;
9570e4ef248SJed Brown   }
958d763cef2SBarry Smith   PetscFunctionReturn(0);
959d763cef2SBarry Smith }
960d763cef2SBarry Smith 
961316643e7SJed Brown 
962316643e7SJed Brown #undef __FUNCT__
963316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
964316643e7SJed Brown /*@C
965b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
966316643e7SJed Brown 
9673f9fe445SBarry Smith    Logically Collective on TS
968316643e7SJed Brown 
969316643e7SJed Brown    Input Parameters:
970316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
9710298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
972316643e7SJed Brown .  f   - the function evaluation routine
9730298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
974316643e7SJed Brown 
975316643e7SJed Brown    Calling sequence of f:
976316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
977316643e7SJed Brown 
978316643e7SJed Brown +  t   - time at step/stage being solved
979316643e7SJed Brown .  u   - state vector
980316643e7SJed Brown .  u_t - time derivative of state vector
981316643e7SJed Brown .  F   - function vector
982316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
983316643e7SJed Brown 
984316643e7SJed Brown    Important:
985d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
986316643e7SJed Brown 
987316643e7SJed Brown    Level: beginner
988316643e7SJed Brown 
989316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
990316643e7SJed Brown 
991d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
992316643e7SJed Brown @*/
993089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
994316643e7SJed Brown {
995089b2837SJed Brown   PetscErrorCode ierr;
996089b2837SJed Brown   SNES           snes;
9970298fd71SBarry Smith   Vec            resalloc = NULL;
99824989b8cSPeter Brune   DM             dm;
999316643e7SJed Brown 
1000316643e7SJed Brown   PetscFunctionBegin;
10010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1002ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
100324989b8cSPeter Brune 
100424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
100524989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
100624989b8cSPeter Brune 
1007089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1008e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1009e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1010e856ceecSJed Brown     res  = resalloc;
1011e856ceecSJed Brown   }
1012089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1013e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1014089b2837SJed Brown   PetscFunctionReturn(0);
1015089b2837SJed Brown }
1016089b2837SJed Brown 
1017089b2837SJed Brown #undef __FUNCT__
1018089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1019089b2837SJed Brown /*@C
1020089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1021089b2837SJed Brown 
1022089b2837SJed Brown    Not Collective
1023089b2837SJed Brown 
1024089b2837SJed Brown    Input Parameter:
1025089b2837SJed Brown .  ts - the TS context
1026089b2837SJed Brown 
1027089b2837SJed Brown    Output Parameter:
10280298fd71SBarry Smith +  r - vector to hold residual (or NULL)
10290298fd71SBarry Smith .  func - the function to compute residual (or NULL)
10300298fd71SBarry Smith -  ctx - the function context (or NULL)
1031089b2837SJed Brown 
1032089b2837SJed Brown    Level: advanced
1033089b2837SJed Brown 
1034089b2837SJed Brown .keywords: TS, nonlinear, get, function
1035089b2837SJed Brown 
1036089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1037089b2837SJed Brown @*/
1038089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1039089b2837SJed Brown {
1040089b2837SJed Brown   PetscErrorCode ierr;
1041089b2837SJed Brown   SNES           snes;
104224989b8cSPeter Brune   DM             dm;
1043089b2837SJed Brown 
1044089b2837SJed Brown   PetscFunctionBegin;
1045089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1046089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10470298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
104824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
104924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1050089b2837SJed Brown   PetscFunctionReturn(0);
1051089b2837SJed Brown }
1052089b2837SJed Brown 
1053089b2837SJed Brown #undef __FUNCT__
1054089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1055089b2837SJed Brown /*@C
1056089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1057089b2837SJed Brown 
1058089b2837SJed Brown    Not Collective
1059089b2837SJed Brown 
1060089b2837SJed Brown    Input Parameter:
1061089b2837SJed Brown .  ts - the TS context
1062089b2837SJed Brown 
1063089b2837SJed Brown    Output Parameter:
10640298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
10650298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
10660298fd71SBarry Smith -  ctx - the function context (or NULL)
1067089b2837SJed Brown 
1068089b2837SJed Brown    Level: advanced
1069089b2837SJed Brown 
1070089b2837SJed Brown .keywords: TS, nonlinear, get, function
1071089b2837SJed Brown 
1072089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1073089b2837SJed Brown @*/
1074089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1075089b2837SJed Brown {
1076089b2837SJed Brown   PetscErrorCode ierr;
1077089b2837SJed Brown   SNES           snes;
107824989b8cSPeter Brune   DM             dm;
1079089b2837SJed Brown 
1080089b2837SJed Brown   PetscFunctionBegin;
1081089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1082089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10830298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
108424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
108524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1086316643e7SJed Brown   PetscFunctionReturn(0);
1087316643e7SJed Brown }
1088316643e7SJed Brown 
1089316643e7SJed Brown #undef __FUNCT__
1090316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1091316643e7SJed Brown /*@C
1092a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1093a4f0a591SBarry Smith         you provided with TSSetIFunction().
1094316643e7SJed Brown 
10953f9fe445SBarry Smith    Logically Collective on TS
1096316643e7SJed Brown 
1097316643e7SJed Brown    Input Parameters:
1098316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1099316643e7SJed Brown .  A   - Jacobian matrix
1100316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
1101316643e7SJed Brown .  f   - the Jacobian evaluation routine
11020298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1103316643e7SJed Brown 
1104316643e7SJed Brown    Calling sequence of f:
11051b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
1106316643e7SJed Brown 
1107316643e7SJed Brown +  t    - time at step/stage being solved
11081b4a444bSJed Brown .  U    - state vector
11091b4a444bSJed Brown .  U_t  - time derivative of state vector
1110316643e7SJed Brown .  a    - shift
1111b5abc632SBarry Smith .  A    - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1112316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
1113316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1114316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1115316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1116316643e7SJed Brown 
1117316643e7SJed Brown    Notes:
1118316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
1119316643e7SJed Brown 
1120a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1121b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1122a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1123a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1124a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1125a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1126a4f0a591SBarry Smith 
1127316643e7SJed Brown    Level: beginner
1128316643e7SJed Brown 
1129316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1130316643e7SJed Brown 
1131ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
1132316643e7SJed Brown 
1133316643e7SJed Brown @*/
11347087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1135316643e7SJed Brown {
1136316643e7SJed Brown   PetscErrorCode ierr;
1137089b2837SJed Brown   SNES           snes;
113824989b8cSPeter Brune   DM             dm;
1139316643e7SJed Brown 
1140316643e7SJed Brown   PetscFunctionBegin;
11410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11420700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
11430700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1144316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1145316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
114624989b8cSPeter Brune 
114724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
114824989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
114924989b8cSPeter Brune 
1150089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1151089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1152316643e7SJed Brown   PetscFunctionReturn(0);
1153316643e7SJed Brown }
1154316643e7SJed Brown 
11554a2ae208SSatish Balay #undef __FUNCT__
115655849f57SBarry Smith #define __FUNCT__ "TSLoad"
115755849f57SBarry Smith /*@C
115855849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
115955849f57SBarry Smith 
116055849f57SBarry Smith   Collective on PetscViewer
116155849f57SBarry Smith 
116255849f57SBarry Smith   Input Parameters:
116355849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
116455849f57SBarry Smith            some related function before a call to TSLoad().
116555849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
116655849f57SBarry Smith 
116755849f57SBarry Smith    Level: intermediate
116855849f57SBarry Smith 
116955849f57SBarry Smith   Notes:
117055849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
117155849f57SBarry Smith 
117255849f57SBarry Smith   Notes for advanced users:
117355849f57SBarry Smith   Most users should not need to know the details of the binary storage
117455849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
117555849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
117655849f57SBarry Smith   format is
117755849f57SBarry Smith .vb
117855849f57SBarry Smith      has not yet been determined
117955849f57SBarry Smith .ve
118055849f57SBarry Smith 
118155849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
118255849f57SBarry Smith @*/
1183f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
118455849f57SBarry Smith {
118555849f57SBarry Smith   PetscErrorCode ierr;
118655849f57SBarry Smith   PetscBool      isbinary;
118755849f57SBarry Smith   PetscInt       classid;
118855849f57SBarry Smith   char           type[256];
11892d53ad75SBarry Smith   DMTS           sdm;
1190ad6bc421SBarry Smith   DM             dm;
119155849f57SBarry Smith 
119255849f57SBarry Smith   PetscFunctionBegin;
1193f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
119455849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
119555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
119655849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
119755849f57SBarry Smith 
119855849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1199ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
120055849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1201f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1202f2c2a1b9SBarry Smith   if (ts->ops->load) {
1203f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1204f2c2a1b9SBarry Smith   }
1205ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1206ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1207ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1208f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1209f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
12102d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12112d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
121255849f57SBarry Smith   PetscFunctionReturn(0);
121355849f57SBarry Smith }
121455849f57SBarry Smith 
1215*9804daf3SBarry Smith #include <petscdraw.h>
121655849f57SBarry Smith #undef __FUNCT__
12174a2ae208SSatish Balay #define __FUNCT__ "TSView"
12187e2c5f70SBarry Smith /*@C
1219d763cef2SBarry Smith     TSView - Prints the TS data structure.
1220d763cef2SBarry Smith 
12214c49b128SBarry Smith     Collective on TS
1222d763cef2SBarry Smith 
1223d763cef2SBarry Smith     Input Parameters:
1224d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1225d763cef2SBarry Smith -   viewer - visualization context
1226d763cef2SBarry Smith 
1227d763cef2SBarry Smith     Options Database Key:
1228d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1229d763cef2SBarry Smith 
1230d763cef2SBarry Smith     Notes:
1231d763cef2SBarry Smith     The available visualization contexts include
1232b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1233b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1234d763cef2SBarry Smith          output where only the first processor opens
1235d763cef2SBarry Smith          the file.  All other processors send their
1236d763cef2SBarry Smith          data to the first processor to print.
1237d763cef2SBarry Smith 
1238d763cef2SBarry Smith     The user can open an alternative visualization context with
1239b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1240d763cef2SBarry Smith 
1241d763cef2SBarry Smith     Level: beginner
1242d763cef2SBarry Smith 
1243d763cef2SBarry Smith .keywords: TS, timestep, view
1244d763cef2SBarry Smith 
1245b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1246d763cef2SBarry Smith @*/
12477087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1248d763cef2SBarry Smith {
1249dfbe8321SBarry Smith   PetscErrorCode ierr;
125019fd82e9SBarry Smith   TSType         type;
12512b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
12522d53ad75SBarry Smith   DMTS           sdm;
1253d763cef2SBarry Smith 
1254d763cef2SBarry Smith   PetscFunctionBegin;
12550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12563050cee2SBarry Smith   if (!viewer) {
1257ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
12583050cee2SBarry Smith   }
12590700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1260c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1261fd16b177SBarry Smith 
1262251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1263251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
126455849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
12652b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
126632077d6dSBarry Smith   if (iascii) {
1267317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
126877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1269a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1270d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
12715ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1272c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1273d763cef2SBarry Smith     }
12745ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1275193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
12762d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12772d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1278d52bd9f3SBarry Smith     if (ts->ops->view) {
1279d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1280d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1281d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1282d52bd9f3SBarry Smith     }
12830f5bd95cSBarry Smith   } else if (isstring) {
1284a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1285b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
128655849f57SBarry Smith   } else if (isbinary) {
128755849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
128855849f57SBarry Smith     MPI_Comm    comm;
128955849f57SBarry Smith     PetscMPIInt rank;
129055849f57SBarry Smith     char        type[256];
129155849f57SBarry Smith 
129255849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
129355849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
129455849f57SBarry Smith     if (!rank) {
129555849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
129655849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
129755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
129855849f57SBarry Smith     }
129955849f57SBarry Smith     if (ts->ops->view) {
130055849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
130155849f57SBarry Smith     }
1302f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1303f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
13042d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13052d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
13062b0a91c0SBarry Smith   } else if (isdraw) {
13072b0a91c0SBarry Smith     PetscDraw draw;
13082b0a91c0SBarry Smith     char      str[36];
130989fd9fafSBarry Smith     PetscReal x,y,bottom,h;
13102b0a91c0SBarry Smith 
13112b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
13122b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
13132b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
13142b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
13150298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
131689fd9fafSBarry Smith     bottom = y - h;
13172b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
13182b0a91c0SBarry Smith     if (ts->ops->view) {
13192b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
13202b0a91c0SBarry Smith     }
13212b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1322d763cef2SBarry Smith   }
1323b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1324251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1325b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1326d763cef2SBarry Smith   PetscFunctionReturn(0);
1327d763cef2SBarry Smith }
1328d763cef2SBarry Smith 
1329d763cef2SBarry Smith 
13304a2ae208SSatish Balay #undef __FUNCT__
13314a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1332b07ff414SBarry Smith /*@
1333d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1334d763cef2SBarry Smith    the timesteppers.
1335d763cef2SBarry Smith 
13363f9fe445SBarry Smith    Logically Collective on TS
1337d763cef2SBarry Smith 
1338d763cef2SBarry Smith    Input Parameters:
1339d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1340d763cef2SBarry Smith -  usrP - optional user context
1341d763cef2SBarry Smith 
1342d763cef2SBarry Smith    Level: intermediate
1343d763cef2SBarry Smith 
1344d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1345d763cef2SBarry Smith 
1346d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1347d763cef2SBarry Smith @*/
13487087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1349d763cef2SBarry Smith {
1350d763cef2SBarry Smith   PetscFunctionBegin;
13510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1352d763cef2SBarry Smith   ts->user = usrP;
1353d763cef2SBarry Smith   PetscFunctionReturn(0);
1354d763cef2SBarry Smith }
1355d763cef2SBarry Smith 
13564a2ae208SSatish Balay #undef __FUNCT__
13574a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1358b07ff414SBarry Smith /*@
1359d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1360d763cef2SBarry Smith     timestepper.
1361d763cef2SBarry Smith 
1362d763cef2SBarry Smith     Not Collective
1363d763cef2SBarry Smith 
1364d763cef2SBarry Smith     Input Parameter:
1365d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1366d763cef2SBarry Smith 
1367d763cef2SBarry Smith     Output Parameter:
1368d763cef2SBarry Smith .   usrP - user context
1369d763cef2SBarry Smith 
1370d763cef2SBarry Smith     Level: intermediate
1371d763cef2SBarry Smith 
1372d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1373d763cef2SBarry Smith 
1374d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1375d763cef2SBarry Smith @*/
1376e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1377d763cef2SBarry Smith {
1378d763cef2SBarry Smith   PetscFunctionBegin;
13790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1380e71120c6SJed Brown   *(void**)usrP = ts->user;
1381d763cef2SBarry Smith   PetscFunctionReturn(0);
1382d763cef2SBarry Smith }
1383d763cef2SBarry Smith 
13844a2ae208SSatish Balay #undef __FUNCT__
13854a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1386d763cef2SBarry Smith /*@
1387b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1388d763cef2SBarry Smith 
1389d763cef2SBarry Smith    Not Collective
1390d763cef2SBarry Smith 
1391d763cef2SBarry Smith    Input Parameter:
1392d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1393d763cef2SBarry Smith 
1394d763cef2SBarry Smith    Output Parameter:
1395b8123daeSJed Brown .  iter - number of steps completed so far
1396d763cef2SBarry Smith 
1397d763cef2SBarry Smith    Level: intermediate
1398d763cef2SBarry Smith 
1399d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1400b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1401d763cef2SBarry Smith @*/
14027087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1403d763cef2SBarry Smith {
1404d763cef2SBarry Smith   PetscFunctionBegin;
14050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14064482741eSBarry Smith   PetscValidIntPointer(iter,2);
1407d763cef2SBarry Smith   *iter = ts->steps;
1408d763cef2SBarry Smith   PetscFunctionReturn(0);
1409d763cef2SBarry Smith }
1410d763cef2SBarry Smith 
14114a2ae208SSatish Balay #undef __FUNCT__
14124a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1413d763cef2SBarry Smith /*@
1414d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1415d763cef2SBarry Smith    as well as the initial time.
1416d763cef2SBarry Smith 
14173f9fe445SBarry Smith    Logically Collective on TS
1418d763cef2SBarry Smith 
1419d763cef2SBarry Smith    Input Parameters:
1420d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1421d763cef2SBarry Smith .  initial_time - the initial time
1422d763cef2SBarry Smith -  time_step - the size of the timestep
1423d763cef2SBarry Smith 
1424d763cef2SBarry Smith    Level: intermediate
1425d763cef2SBarry Smith 
1426d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1427d763cef2SBarry Smith 
1428d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1429d763cef2SBarry Smith @*/
14307087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1431d763cef2SBarry Smith {
1432e144a568SJed Brown   PetscErrorCode ierr;
1433e144a568SJed Brown 
1434d763cef2SBarry Smith   PetscFunctionBegin;
14350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1436e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1437d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1438d763cef2SBarry Smith   PetscFunctionReturn(0);
1439d763cef2SBarry Smith }
1440d763cef2SBarry Smith 
14414a2ae208SSatish Balay #undef __FUNCT__
14424a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1443d763cef2SBarry Smith /*@
1444d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1445d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1446d763cef2SBarry Smith 
14473f9fe445SBarry Smith    Logically Collective on TS
1448d763cef2SBarry Smith 
1449d763cef2SBarry Smith    Input Parameters:
1450d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1451d763cef2SBarry Smith -  time_step - the size of the timestep
1452d763cef2SBarry Smith 
1453d763cef2SBarry Smith    Level: intermediate
1454d763cef2SBarry Smith 
1455d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1456d763cef2SBarry Smith 
1457d763cef2SBarry Smith .keywords: TS, set, timestep
1458d763cef2SBarry Smith @*/
14597087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1460d763cef2SBarry Smith {
1461d763cef2SBarry Smith   PetscFunctionBegin;
14620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1463c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1464d763cef2SBarry Smith   ts->time_step      = time_step;
146531748224SBarry Smith   ts->time_step_orig = time_step;
1466d763cef2SBarry Smith   PetscFunctionReturn(0);
1467d763cef2SBarry Smith }
1468d763cef2SBarry Smith 
14694a2ae208SSatish Balay #undef __FUNCT__
1470a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1471a43b19c4SJed Brown /*@
147249354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
147349354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
147449354f04SShri Abhyankar      or just return at the final time TS computed.
1475a43b19c4SJed Brown 
1476a43b19c4SJed Brown   Logically Collective on TS
1477a43b19c4SJed Brown 
1478a43b19c4SJed Brown    Input Parameter:
1479a43b19c4SJed Brown +   ts - the time-step context
148049354f04SShri Abhyankar -   eftopt - exact final time option
1481a43b19c4SJed Brown 
1482a43b19c4SJed Brown    Level: beginner
1483a43b19c4SJed Brown 
1484a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1485a43b19c4SJed Brown @*/
148649354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1487a43b19c4SJed Brown {
1488a43b19c4SJed Brown   PetscFunctionBegin;
1489a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
149049354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
149149354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1492a43b19c4SJed Brown   PetscFunctionReturn(0);
1493a43b19c4SJed Brown }
1494a43b19c4SJed Brown 
1495a43b19c4SJed Brown #undef __FUNCT__
14964a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1497d763cef2SBarry Smith /*@
1498d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1499d763cef2SBarry Smith 
1500d763cef2SBarry Smith    Not Collective
1501d763cef2SBarry Smith 
1502d763cef2SBarry Smith    Input Parameter:
1503d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1504d763cef2SBarry Smith 
1505d763cef2SBarry Smith    Output Parameter:
1506d763cef2SBarry Smith .  dt - the current timestep size
1507d763cef2SBarry Smith 
1508d763cef2SBarry Smith    Level: intermediate
1509d763cef2SBarry Smith 
1510d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1511d763cef2SBarry Smith 
1512d763cef2SBarry Smith .keywords: TS, get, timestep
1513d763cef2SBarry Smith @*/
15147087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1515d763cef2SBarry Smith {
1516d763cef2SBarry Smith   PetscFunctionBegin;
15170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1518f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1519d763cef2SBarry Smith   *dt = ts->time_step;
1520d763cef2SBarry Smith   PetscFunctionReturn(0);
1521d763cef2SBarry Smith }
1522d763cef2SBarry Smith 
15234a2ae208SSatish Balay #undef __FUNCT__
15244a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1525d8e5e3e6SSatish Balay /*@
1526d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1527d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1528d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1529d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1530d763cef2SBarry Smith 
1531d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1532d763cef2SBarry Smith 
1533d763cef2SBarry Smith    Input Parameter:
1534d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1535d763cef2SBarry Smith 
1536d763cef2SBarry Smith    Output Parameter:
1537d763cef2SBarry Smith .  v - the vector containing the solution
1538d763cef2SBarry Smith 
1539d763cef2SBarry Smith    Level: intermediate
1540d763cef2SBarry Smith 
1541d763cef2SBarry Smith .seealso: TSGetTimeStep()
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1544d763cef2SBarry Smith @*/
15457087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1546d763cef2SBarry Smith {
1547d763cef2SBarry Smith   PetscFunctionBegin;
15480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15494482741eSBarry Smith   PetscValidPointer(v,2);
15508737fe31SLisandro Dalcin   *v = ts->vec_sol;
1551d763cef2SBarry Smith   PetscFunctionReturn(0);
1552d763cef2SBarry Smith }
1553d763cef2SBarry Smith 
1554bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
15554a2ae208SSatish Balay #undef __FUNCT__
1556bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1557d8e5e3e6SSatish Balay /*@
1558bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1559d763cef2SBarry Smith 
1560bdad233fSMatthew Knepley   Not collective
1561d763cef2SBarry Smith 
1562bdad233fSMatthew Knepley   Input Parameters:
1563bdad233fSMatthew Knepley + ts   - The TS
1564bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1565d763cef2SBarry Smith .vb
15660910c330SBarry Smith          U_t - A U = 0      (linear)
15670910c330SBarry Smith          U_t - A(t) U = 0   (linear)
15680910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1569d763cef2SBarry Smith .ve
1570d763cef2SBarry Smith 
1571d763cef2SBarry Smith    Level: beginner
1572d763cef2SBarry Smith 
1573bdad233fSMatthew Knepley .keywords: TS, problem type
1574bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1575d763cef2SBarry Smith @*/
15767087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1577a7cc72afSBarry Smith {
15789e2a6581SJed Brown   PetscErrorCode ierr;
15799e2a6581SJed Brown 
1580d763cef2SBarry Smith   PetscFunctionBegin;
15810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1582bdad233fSMatthew Knepley   ts->problem_type = type;
15839e2a6581SJed Brown   if (type == TS_LINEAR) {
15849e2a6581SJed Brown     SNES snes;
15859e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
15869e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
15879e2a6581SJed Brown   }
1588d763cef2SBarry Smith   PetscFunctionReturn(0);
1589d763cef2SBarry Smith }
1590d763cef2SBarry Smith 
1591bdad233fSMatthew Knepley #undef __FUNCT__
1592bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1593bdad233fSMatthew Knepley /*@C
1594bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1595bdad233fSMatthew Knepley 
1596bdad233fSMatthew Knepley   Not collective
1597bdad233fSMatthew Knepley 
1598bdad233fSMatthew Knepley   Input Parameter:
1599bdad233fSMatthew Knepley . ts   - The TS
1600bdad233fSMatthew Knepley 
1601bdad233fSMatthew Knepley   Output Parameter:
1602bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1603bdad233fSMatthew Knepley .vb
1604089b2837SJed Brown          M U_t = A U
1605089b2837SJed Brown          M(t) U_t = A(t) U
1606b5abc632SBarry Smith          F(t,U,U_t)
1607bdad233fSMatthew Knepley .ve
1608bdad233fSMatthew Knepley 
1609bdad233fSMatthew Knepley    Level: beginner
1610bdad233fSMatthew Knepley 
1611bdad233fSMatthew Knepley .keywords: TS, problem type
1612bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1613bdad233fSMatthew Knepley @*/
16147087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1615a7cc72afSBarry Smith {
1616bdad233fSMatthew Knepley   PetscFunctionBegin;
16170700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
16184482741eSBarry Smith   PetscValidIntPointer(type,2);
1619bdad233fSMatthew Knepley   *type = ts->problem_type;
1620bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1621bdad233fSMatthew Knepley }
1622d763cef2SBarry Smith 
16234a2ae208SSatish Balay #undef __FUNCT__
16244a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1625d763cef2SBarry Smith /*@
1626d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1627d763cef2SBarry Smith    of a timestepper.
1628d763cef2SBarry Smith 
1629d763cef2SBarry Smith    Collective on TS
1630d763cef2SBarry Smith 
1631d763cef2SBarry Smith    Input Parameter:
1632d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1633d763cef2SBarry Smith 
1634d763cef2SBarry Smith    Notes:
1635d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1636d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1637d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1638d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1639d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1640d763cef2SBarry Smith 
1641d763cef2SBarry Smith    Level: advanced
1642d763cef2SBarry Smith 
1643d763cef2SBarry Smith .keywords: TS, timestep, setup
1644d763cef2SBarry Smith 
1645d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1646d763cef2SBarry Smith @*/
16477087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1648d763cef2SBarry Smith {
1649dfbe8321SBarry Smith   PetscErrorCode ierr;
16506c6b9e74SPeter Brune   DM             dm;
16516c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
16526c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
16536c6b9e74SPeter Brune   TSIJacobian    ijac;
16546c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1655d763cef2SBarry Smith 
1656d763cef2SBarry Smith   PetscFunctionBegin;
16570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1658277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1659277b19d0SLisandro Dalcin 
16607adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
16619596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1662d763cef2SBarry Smith   }
1663277b19d0SLisandro Dalcin 
1664277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1665277b19d0SLisandro Dalcin 
1666ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr);
16671c3436cfSJed Brown 
1668277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1669000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1670277b19d0SLisandro Dalcin   }
1671277b19d0SLisandro Dalcin 
16726c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
16736c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
16746c6b9e74SPeter Brune    */
16756c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
16760298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
16776c6b9e74SPeter Brune   if (!func) {
16786c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
16796c6b9e74SPeter Brune   }
16806c6b9e74SPeter 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.
16816c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
16826c6b9e74SPeter Brune    */
16830298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
16840298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
16850298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
16866c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
16876c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
16886c6b9e74SPeter Brune   }
1689277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1690277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1691277b19d0SLisandro Dalcin }
1692277b19d0SLisandro Dalcin 
1693277b19d0SLisandro Dalcin #undef __FUNCT__
1694277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1695277b19d0SLisandro Dalcin /*@
1696277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1697277b19d0SLisandro Dalcin 
1698277b19d0SLisandro Dalcin    Collective on TS
1699277b19d0SLisandro Dalcin 
1700277b19d0SLisandro Dalcin    Input Parameter:
1701277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1702277b19d0SLisandro Dalcin 
1703277b19d0SLisandro Dalcin    Level: beginner
1704277b19d0SLisandro Dalcin 
1705277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1706277b19d0SLisandro Dalcin 
1707277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1708277b19d0SLisandro Dalcin @*/
1709277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1710277b19d0SLisandro Dalcin {
1711277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1712277b19d0SLisandro Dalcin 
1713277b19d0SLisandro Dalcin   PetscFunctionBegin;
1714277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1715277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1716277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1717277b19d0SLisandro Dalcin   }
1718277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1719bbd56ea5SKarl Rupp 
17204e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
17214e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1722214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
17236bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1724e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1725e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
172638637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1727bbd56ea5SKarl Rupp 
1728277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1729d763cef2SBarry Smith   PetscFunctionReturn(0);
1730d763cef2SBarry Smith }
1731d763cef2SBarry Smith 
17324a2ae208SSatish Balay #undef __FUNCT__
17334a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1734d8e5e3e6SSatish Balay /*@
1735d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1736d763cef2SBarry Smith    with TSCreate().
1737d763cef2SBarry Smith 
1738d763cef2SBarry Smith    Collective on TS
1739d763cef2SBarry Smith 
1740d763cef2SBarry Smith    Input Parameter:
1741d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1742d763cef2SBarry Smith 
1743d763cef2SBarry Smith    Level: beginner
1744d763cef2SBarry Smith 
1745d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1746d763cef2SBarry Smith 
1747d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1748d763cef2SBarry Smith @*/
17496bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1750d763cef2SBarry Smith {
17516849ba73SBarry Smith   PetscErrorCode ierr;
1752d763cef2SBarry Smith 
1753d763cef2SBarry Smith   PetscFunctionBegin;
17546bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
17556bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
17566bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1757d763cef2SBarry Smith 
17586bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1759277b19d0SLisandro Dalcin 
1760be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
17616bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
17626bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
17636d4c513bSLisandro Dalcin 
176484df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
17656bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
17666bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
17676bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
17686d4c513bSLisandro Dalcin 
1769a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1770d763cef2SBarry Smith   PetscFunctionReturn(0);
1771d763cef2SBarry Smith }
1772d763cef2SBarry Smith 
17734a2ae208SSatish Balay #undef __FUNCT__
17744a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1775d8e5e3e6SSatish Balay /*@
1776d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1777d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1778d763cef2SBarry Smith 
1779d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1780d763cef2SBarry Smith 
1781d763cef2SBarry Smith    Input Parameter:
1782d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1783d763cef2SBarry Smith 
1784d763cef2SBarry Smith    Output Parameter:
1785d763cef2SBarry Smith .  snes - the nonlinear solver context
1786d763cef2SBarry Smith 
1787d763cef2SBarry Smith    Notes:
1788d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1789d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
179094b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1791d763cef2SBarry Smith 
1792d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
17930298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
1794d763cef2SBarry Smith 
1795d763cef2SBarry Smith    Level: beginner
1796d763cef2SBarry Smith 
1797d763cef2SBarry Smith .keywords: timestep, get, SNES
1798d763cef2SBarry Smith @*/
17997087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1800d763cef2SBarry Smith {
1801d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1802d372ba47SLisandro Dalcin 
1803d763cef2SBarry Smith   PetscFunctionBegin;
18040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18054482741eSBarry Smith   PetscValidPointer(snes,2);
1806d372ba47SLisandro Dalcin   if (!ts->snes) {
1807ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
18080298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1809d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1810d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1811496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
18129e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
18139e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
18149e2a6581SJed Brown     }
1815d372ba47SLisandro Dalcin   }
1816d763cef2SBarry Smith   *snes = ts->snes;
1817d763cef2SBarry Smith   PetscFunctionReturn(0);
1818d763cef2SBarry Smith }
1819d763cef2SBarry Smith 
18204a2ae208SSatish Balay #undef __FUNCT__
1821deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1822deb2cd25SJed Brown /*@
1823deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1824deb2cd25SJed Brown 
1825deb2cd25SJed Brown    Collective
1826deb2cd25SJed Brown 
1827deb2cd25SJed Brown    Input Parameter:
1828deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1829deb2cd25SJed Brown -  snes - the nonlinear solver context
1830deb2cd25SJed Brown 
1831deb2cd25SJed Brown    Notes:
1832deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1833deb2cd25SJed Brown 
1834deb2cd25SJed Brown    Level: developer
1835deb2cd25SJed Brown 
1836deb2cd25SJed Brown .keywords: timestep, set, SNES
1837deb2cd25SJed Brown @*/
1838deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1839deb2cd25SJed Brown {
1840deb2cd25SJed Brown   PetscErrorCode ierr;
1841740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1842deb2cd25SJed Brown 
1843deb2cd25SJed Brown   PetscFunctionBegin;
1844deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1845deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1846deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1847deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1848bbd56ea5SKarl Rupp 
1849deb2cd25SJed Brown   ts->snes = snes;
1850bbd56ea5SKarl Rupp 
18510298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
18520298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
1853740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
18540298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1855740132f1SEmil Constantinescu   }
1856deb2cd25SJed Brown   PetscFunctionReturn(0);
1857deb2cd25SJed Brown }
1858deb2cd25SJed Brown 
1859deb2cd25SJed Brown #undef __FUNCT__
186094b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1861d8e5e3e6SSatish Balay /*@
186294b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1863d763cef2SBarry Smith    a TS (timestepper) context.
1864d763cef2SBarry Smith 
186594b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1866d763cef2SBarry Smith 
1867d763cef2SBarry Smith    Input Parameter:
1868d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1869d763cef2SBarry Smith 
1870d763cef2SBarry Smith    Output Parameter:
187194b7f48cSBarry Smith .  ksp - the nonlinear solver context
1872d763cef2SBarry Smith 
1873d763cef2SBarry Smith    Notes:
187494b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1875d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1876d763cef2SBarry Smith    KSP and PC contexts as well.
1877d763cef2SBarry Smith 
187894b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
18790298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
1880d763cef2SBarry Smith 
1881d763cef2SBarry Smith    Level: beginner
1882d763cef2SBarry Smith 
188394b7f48cSBarry Smith .keywords: timestep, get, KSP
1884d763cef2SBarry Smith @*/
18857087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1886d763cef2SBarry Smith {
1887d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1888089b2837SJed Brown   SNES           snes;
1889d372ba47SLisandro Dalcin 
1890d763cef2SBarry Smith   PetscFunctionBegin;
18910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18924482741eSBarry Smith   PetscValidPointer(ksp,2);
189317186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1894e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1895089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1896089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1897d763cef2SBarry Smith   PetscFunctionReturn(0);
1898d763cef2SBarry Smith }
1899d763cef2SBarry Smith 
1900d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1901d763cef2SBarry Smith 
19024a2ae208SSatish Balay #undef __FUNCT__
1903adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1904adb62b0dSMatthew Knepley /*@
1905adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1906adb62b0dSMatthew Knepley    maximum time for iteration.
1907adb62b0dSMatthew Knepley 
19083f9fe445SBarry Smith    Not Collective
1909adb62b0dSMatthew Knepley 
1910adb62b0dSMatthew Knepley    Input Parameters:
1911adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
19120298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
19130298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
1914adb62b0dSMatthew Knepley 
1915adb62b0dSMatthew Knepley    Level: intermediate
1916adb62b0dSMatthew Knepley 
1917adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1918adb62b0dSMatthew Knepley @*/
19197087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1920adb62b0dSMatthew Knepley {
1921adb62b0dSMatthew Knepley   PetscFunctionBegin;
19220700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1923abc0a331SBarry Smith   if (maxsteps) {
19244482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1925adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1926adb62b0dSMatthew Knepley   }
1927abc0a331SBarry Smith   if (maxtime) {
19284482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1929adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
1930adb62b0dSMatthew Knepley   }
1931adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1932adb62b0dSMatthew Knepley }
1933adb62b0dSMatthew Knepley 
1934adb62b0dSMatthew Knepley #undef __FUNCT__
19354a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1936d763cef2SBarry Smith /*@
1937d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1938d763cef2SBarry Smith    maximum time for iteration.
1939d763cef2SBarry Smith 
19403f9fe445SBarry Smith    Logically Collective on TS
1941d763cef2SBarry Smith 
1942d763cef2SBarry Smith    Input Parameters:
1943d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1944d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1945d763cef2SBarry Smith -  maxtime - final time to iterate to
1946d763cef2SBarry Smith 
1947d763cef2SBarry Smith    Options Database Keys:
1948d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
19493bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1950d763cef2SBarry Smith 
1951d763cef2SBarry Smith    Notes:
1952d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1953d763cef2SBarry Smith 
1954d763cef2SBarry Smith    Level: intermediate
1955d763cef2SBarry Smith 
1956d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1957a43b19c4SJed Brown 
1958a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1959d763cef2SBarry Smith @*/
19607087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1961d763cef2SBarry Smith {
1962d763cef2SBarry Smith   PetscFunctionBegin;
19630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1964c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1965c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
196639b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
196739b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
1968d763cef2SBarry Smith   PetscFunctionReturn(0);
1969d763cef2SBarry Smith }
1970d763cef2SBarry Smith 
19714a2ae208SSatish Balay #undef __FUNCT__
19724a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1973d763cef2SBarry Smith /*@
1974d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1975d763cef2SBarry Smith    for use by the TS routines.
1976d763cef2SBarry Smith 
19773f9fe445SBarry Smith    Logically Collective on TS and Vec
1978d763cef2SBarry Smith 
1979d763cef2SBarry Smith    Input Parameters:
1980d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
19810910c330SBarry Smith -  u - the solution vector
1982d763cef2SBarry Smith 
1983d763cef2SBarry Smith    Level: beginner
1984d763cef2SBarry Smith 
1985d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1986d763cef2SBarry Smith @*/
19870910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
1988d763cef2SBarry Smith {
19898737fe31SLisandro Dalcin   PetscErrorCode ierr;
1990496e6a7aSJed Brown   DM             dm;
19918737fe31SLisandro Dalcin 
1992d763cef2SBarry Smith   PetscFunctionBegin;
19930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19940910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
19950910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
19966bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1997bbd56ea5SKarl Rupp 
19980910c330SBarry Smith   ts->vec_sol = u;
1999bbd56ea5SKarl Rupp 
2000496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
20010910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2002d763cef2SBarry Smith   PetscFunctionReturn(0);
2003d763cef2SBarry Smith }
2004d763cef2SBarry Smith 
2005e74ef692SMatthew Knepley #undef __FUNCT__
2006e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2007ac226902SBarry Smith /*@C
2008000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
20093f2090d5SJed Brown   called once at the beginning of each time step.
2010000e7ae3SMatthew Knepley 
20113f9fe445SBarry Smith   Logically Collective on TS
2012000e7ae3SMatthew Knepley 
2013000e7ae3SMatthew Knepley   Input Parameters:
2014000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2015000e7ae3SMatthew Knepley - func - The function
2016000e7ae3SMatthew Knepley 
2017000e7ae3SMatthew Knepley   Calling sequence of func:
2018000e7ae3SMatthew Knepley . func (TS ts);
2019000e7ae3SMatthew Knepley 
2020000e7ae3SMatthew Knepley   Level: intermediate
2021000e7ae3SMatthew Knepley 
2022b8123daeSJed Brown   Note:
2023b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2024b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2025b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2026b8123daeSJed Brown 
2027000e7ae3SMatthew Knepley .keywords: TS, timestep
2028b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
2029000e7ae3SMatthew Knepley @*/
20307087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2031000e7ae3SMatthew Knepley {
2032000e7ae3SMatthew Knepley   PetscFunctionBegin;
20330700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2034000e7ae3SMatthew Knepley   ts->ops->prestep = func;
2035000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2036000e7ae3SMatthew Knepley }
2037000e7ae3SMatthew Knepley 
2038e74ef692SMatthew Knepley #undef __FUNCT__
20393f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
204009ee8438SJed Brown /*@
20413f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
20423f2090d5SJed Brown 
20433f2090d5SJed Brown   Collective on TS
20443f2090d5SJed Brown 
20453f2090d5SJed Brown   Input Parameters:
20463f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
20473f2090d5SJed Brown 
20483f2090d5SJed Brown   Notes:
20493f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
20503f2090d5SJed Brown   so most users would not generally call this routine themselves.
20513f2090d5SJed Brown 
20523f2090d5SJed Brown   Level: developer
20533f2090d5SJed Brown 
20543f2090d5SJed Brown .keywords: TS, timestep
2055b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
20563f2090d5SJed Brown @*/
20577087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
20583f2090d5SJed Brown {
20593f2090d5SJed Brown   PetscErrorCode ierr;
20603f2090d5SJed Brown 
20613f2090d5SJed Brown   PetscFunctionBegin;
20620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
206372ac3e02SJed Brown   if (ts->ops->prestep) {
20643f2090d5SJed Brown     PetscStackPush("TS PreStep function");
20653f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
20663f2090d5SJed Brown     PetscStackPop;
2067312ce896SJed Brown   }
20683f2090d5SJed Brown   PetscFunctionReturn(0);
20693f2090d5SJed Brown }
20703f2090d5SJed Brown 
20713f2090d5SJed Brown #undef __FUNCT__
2072b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2073b8123daeSJed Brown /*@C
2074b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2075b8123daeSJed Brown   called once at the beginning of each stage.
2076b8123daeSJed Brown 
2077b8123daeSJed Brown   Logically Collective on TS
2078b8123daeSJed Brown 
2079b8123daeSJed Brown   Input Parameters:
2080b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2081b8123daeSJed Brown - func - The function
2082b8123daeSJed Brown 
2083b8123daeSJed Brown   Calling sequence of func:
2084b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2085b8123daeSJed Brown 
2086b8123daeSJed Brown   Level: intermediate
2087b8123daeSJed Brown 
2088b8123daeSJed Brown   Note:
2089b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2090b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2091b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2092b8123daeSJed Brown 
2093b8123daeSJed Brown .keywords: TS, timestep
2094b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2095b8123daeSJed Brown @*/
2096b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2097b8123daeSJed Brown {
2098b8123daeSJed Brown   PetscFunctionBegin;
2099b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2100b8123daeSJed Brown   ts->ops->prestage = func;
2101b8123daeSJed Brown   PetscFunctionReturn(0);
2102b8123daeSJed Brown }
2103b8123daeSJed Brown 
2104b8123daeSJed Brown #undef __FUNCT__
2105b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2106b8123daeSJed Brown /*@
2107b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2108b8123daeSJed Brown 
2109b8123daeSJed Brown   Collective on TS
2110b8123daeSJed Brown 
2111b8123daeSJed Brown   Input Parameters:
2112b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2113b8123daeSJed Brown 
2114b8123daeSJed Brown   Notes:
2115b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2116b8123daeSJed Brown   most users would not generally call this routine themselves.
2117b8123daeSJed Brown 
2118b8123daeSJed Brown   Level: developer
2119b8123daeSJed Brown 
2120b8123daeSJed Brown .keywords: TS, timestep
2121b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2122b8123daeSJed Brown @*/
2123b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2124b8123daeSJed Brown {
2125b8123daeSJed Brown   PetscErrorCode ierr;
2126b8123daeSJed Brown 
2127b8123daeSJed Brown   PetscFunctionBegin;
2128b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2129b8123daeSJed Brown   if (ts->ops->prestage) {
2130b8123daeSJed Brown     PetscStackPush("TS PreStage function");
2131b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
2132b8123daeSJed Brown     PetscStackPop;
2133b8123daeSJed Brown   }
2134b8123daeSJed Brown   PetscFunctionReturn(0);
2135b8123daeSJed Brown }
2136b8123daeSJed Brown 
2137b8123daeSJed Brown #undef __FUNCT__
2138e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2139ac226902SBarry Smith /*@C
2140000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
21413f2090d5SJed Brown   called once at the end of each time step.
2142000e7ae3SMatthew Knepley 
21433f9fe445SBarry Smith   Logically Collective on TS
2144000e7ae3SMatthew Knepley 
2145000e7ae3SMatthew Knepley   Input Parameters:
2146000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2147000e7ae3SMatthew Knepley - func - The function
2148000e7ae3SMatthew Knepley 
2149000e7ae3SMatthew Knepley   Calling sequence of func:
2150b8123daeSJed Brown $ func (TS ts);
2151000e7ae3SMatthew Knepley 
2152000e7ae3SMatthew Knepley   Level: intermediate
2153000e7ae3SMatthew Knepley 
2154000e7ae3SMatthew Knepley .keywords: TS, timestep
2155b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2156000e7ae3SMatthew Knepley @*/
21577087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2158000e7ae3SMatthew Knepley {
2159000e7ae3SMatthew Knepley   PetscFunctionBegin;
21600700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2161000e7ae3SMatthew Knepley   ts->ops->poststep = func;
2162000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2163000e7ae3SMatthew Knepley }
2164000e7ae3SMatthew Knepley 
2165e74ef692SMatthew Knepley #undef __FUNCT__
21663f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
216709ee8438SJed Brown /*@
21683f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
21693f2090d5SJed Brown 
21703f2090d5SJed Brown   Collective on TS
21713f2090d5SJed Brown 
21723f2090d5SJed Brown   Input Parameters:
21733f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
21743f2090d5SJed Brown 
21753f2090d5SJed Brown   Notes:
21763f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
21773f2090d5SJed Brown   so most users would not generally call this routine themselves.
21783f2090d5SJed Brown 
21793f2090d5SJed Brown   Level: developer
21803f2090d5SJed Brown 
21813f2090d5SJed Brown .keywords: TS, timestep
21823f2090d5SJed Brown @*/
21837087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
21843f2090d5SJed Brown {
21853f2090d5SJed Brown   PetscErrorCode ierr;
21863f2090d5SJed Brown 
21873f2090d5SJed Brown   PetscFunctionBegin;
21880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
218972ac3e02SJed Brown   if (ts->ops->poststep) {
21903f2090d5SJed Brown     PetscStackPush("TS PostStep function");
21913f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
21923f2090d5SJed Brown     PetscStackPop;
219372ac3e02SJed Brown   }
21943f2090d5SJed Brown   PetscFunctionReturn(0);
21953f2090d5SJed Brown }
21963f2090d5SJed Brown 
2197d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2198d763cef2SBarry Smith 
21994a2ae208SSatish Balay #undef __FUNCT__
2200a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2201d763cef2SBarry Smith /*@C
2202a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2203d763cef2SBarry Smith    timestep to display the iteration's  progress.
2204d763cef2SBarry Smith 
22053f9fe445SBarry Smith    Logically Collective on TS
2206d763cef2SBarry Smith 
2207d763cef2SBarry Smith    Input Parameters:
2208d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2209e213d8f1SJed Brown .  monitor - monitoring routine
2210329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
22110298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2212b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
22130298fd71SBarry Smith           (may be NULL)
2214d763cef2SBarry Smith 
2215e213d8f1SJed Brown    Calling sequence of monitor:
22160910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2217d763cef2SBarry Smith 
2218d763cef2SBarry Smith +    ts - the TS context
221988c05cc5SBarry 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
222088c05cc5SBarry Smith                                been interpolated to)
22211f06c33eSBarry Smith .    time - current time
22220910c330SBarry Smith .    u - current iterate
2223d763cef2SBarry Smith -    mctx - [optional] monitoring context
2224d763cef2SBarry Smith 
2225d763cef2SBarry Smith    Notes:
2226d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2227d763cef2SBarry Smith    already has been loaded.
2228d763cef2SBarry Smith 
2229025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2230025f1a04SBarry Smith 
2231d763cef2SBarry Smith    Level: intermediate
2232d763cef2SBarry Smith 
2233d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2234d763cef2SBarry Smith 
2235a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2236d763cef2SBarry Smith @*/
2237c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2238d763cef2SBarry Smith {
2239d763cef2SBarry Smith   PetscFunctionBegin;
22400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
224117186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2242d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
22438704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2244d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2245d763cef2SBarry Smith   PetscFunctionReturn(0);
2246d763cef2SBarry Smith }
2247d763cef2SBarry Smith 
22484a2ae208SSatish Balay #undef __FUNCT__
2249a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2250d763cef2SBarry Smith /*@C
2251a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2252d763cef2SBarry Smith 
22533f9fe445SBarry Smith    Logically Collective on TS
2254d763cef2SBarry Smith 
2255d763cef2SBarry Smith    Input Parameters:
2256d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2257d763cef2SBarry Smith 
2258d763cef2SBarry Smith    Notes:
2259d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2260d763cef2SBarry Smith 
2261d763cef2SBarry Smith    Level: intermediate
2262d763cef2SBarry Smith 
2263d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2264d763cef2SBarry Smith 
2265a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2266d763cef2SBarry Smith @*/
22677087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2268d763cef2SBarry Smith {
2269d952e501SBarry Smith   PetscErrorCode ierr;
2270d952e501SBarry Smith   PetscInt       i;
2271d952e501SBarry Smith 
2272d763cef2SBarry Smith   PetscFunctionBegin;
22730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2274d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
22758704b422SBarry Smith     if (ts->monitordestroy[i]) {
22768704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2277d952e501SBarry Smith     }
2278d952e501SBarry Smith   }
2279d763cef2SBarry Smith   ts->numbermonitors = 0;
2280d763cef2SBarry Smith   PetscFunctionReturn(0);
2281d763cef2SBarry Smith }
2282d763cef2SBarry Smith 
22834a2ae208SSatish Balay #undef __FUNCT__
2284a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2285d8e5e3e6SSatish Balay /*@
2286a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
22875516499fSSatish Balay 
22885516499fSSatish Balay    Level: intermediate
228941251cbbSSatish Balay 
22905516499fSSatish Balay .keywords: TS, set, monitor
22915516499fSSatish Balay 
229241251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
229341251cbbSSatish Balay @*/
2294649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2295d763cef2SBarry Smith {
2296dfbe8321SBarry Smith   PetscErrorCode ierr;
2297ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2298d132466eSBarry Smith 
2299d763cef2SBarry Smith   PetscFunctionBegin;
2300649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2301971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2302649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2303d763cef2SBarry Smith   PetscFunctionReturn(0);
2304d763cef2SBarry Smith }
2305d763cef2SBarry Smith 
23064a2ae208SSatish Balay #undef __FUNCT__
2307cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2308cd652676SJed Brown /*@
2309cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2310cd652676SJed Brown 
2311cd652676SJed Brown    Logically Collective on TS
2312cd652676SJed Brown 
2313cd652676SJed Brown    Input Argument:
2314cd652676SJed Brown .  ts - time stepping context
2315cd652676SJed Brown 
2316cd652676SJed Brown    Output Argument:
2317cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2318cd652676SJed Brown 
2319cd652676SJed Brown    Level: intermediate
2320cd652676SJed Brown 
2321cd652676SJed Brown .keywords: TS, set
2322cd652676SJed Brown 
2323cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2324cd652676SJed Brown @*/
2325cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2326cd652676SJed Brown {
2327cd652676SJed Brown   PetscFunctionBegin;
2328cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2329cd652676SJed Brown   ts->retain_stages = flg;
2330cd652676SJed Brown   PetscFunctionReturn(0);
2331cd652676SJed Brown }
2332cd652676SJed Brown 
2333cd652676SJed Brown #undef __FUNCT__
2334cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2335cd652676SJed Brown /*@
2336cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2337cd652676SJed Brown 
2338cd652676SJed Brown    Collective on TS
2339cd652676SJed Brown 
2340cd652676SJed Brown    Input Argument:
2341cd652676SJed Brown +  ts - time stepping context
2342cd652676SJed Brown -  t - time to interpolate to
2343cd652676SJed Brown 
2344cd652676SJed Brown    Output Argument:
23450910c330SBarry Smith .  U - state at given time
2346cd652676SJed Brown 
2347cd652676SJed Brown    Notes:
2348cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2349cd652676SJed Brown 
2350cd652676SJed Brown    Level: intermediate
2351cd652676SJed Brown 
2352cd652676SJed Brown    Developer Notes:
2353cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2354cd652676SJed Brown 
2355cd652676SJed Brown .keywords: TS, set
2356cd652676SJed Brown 
2357cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2358cd652676SJed Brown @*/
23590910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2360cd652676SJed Brown {
2361cd652676SJed Brown   PetscErrorCode ierr;
2362cd652676SJed Brown 
2363cd652676SJed Brown   PetscFunctionBegin;
2364cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2365ce94432eSBarry 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);
2366ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
23670910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2368cd652676SJed Brown   PetscFunctionReturn(0);
2369cd652676SJed Brown }
2370cd652676SJed Brown 
2371cd652676SJed Brown #undef __FUNCT__
23724a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2373d763cef2SBarry Smith /*@
23746d9e5789SSean Farley    TSStep - Steps one time step
2375d763cef2SBarry Smith 
2376d763cef2SBarry Smith    Collective on TS
2377d763cef2SBarry Smith 
2378d763cef2SBarry Smith    Input Parameter:
2379d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2380d763cef2SBarry Smith 
23816d9e5789SSean Farley    Level: intermediate
2382d763cef2SBarry Smith 
2383b8123daeSJed Brown    Notes:
2384b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2385b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2386b8123daeSJed Brown 
238725cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
238825cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
238925cb2221SBarry Smith 
2390d763cef2SBarry Smith .keywords: TS, timestep, solve
2391d763cef2SBarry Smith 
239225cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2393d763cef2SBarry Smith @*/
2394193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2395d763cef2SBarry Smith {
2396362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2397dfbe8321SBarry Smith   PetscErrorCode ierr;
2398d763cef2SBarry Smith 
2399d763cef2SBarry Smith   PetscFunctionBegin;
24000700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2401d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2402d405a339SMatthew Knepley 
2403362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2404362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2405bbd56ea5SKarl Rupp 
2406d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2407193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2408d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2409bbd56ea5SKarl Rupp 
2410362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2411362cd11cSLisandro Dalcin 
2412362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2413cef5090cSJed Brown     if (ts->errorifstepfailed) {
2414cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2415ce94432eSBarry 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]);
2416ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2417cef5090cSJed Brown     }
2418362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2419db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2420db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2421362cd11cSLisandro Dalcin   }
2422d763cef2SBarry Smith   PetscFunctionReturn(0);
2423d763cef2SBarry Smith }
2424d763cef2SBarry Smith 
24254a2ae208SSatish Balay #undef __FUNCT__
242605175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
242705175c85SJed Brown /*@
242805175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
242905175c85SJed Brown 
24301c3436cfSJed Brown    Collective on TS
243105175c85SJed Brown 
243205175c85SJed Brown    Input Arguments:
24331c3436cfSJed Brown +  ts - time stepping context
24341c3436cfSJed Brown .  order - desired order of accuracy
24350298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
243605175c85SJed Brown 
243705175c85SJed Brown    Output Arguments:
24380910c330SBarry Smith .  U - state at the end of the current step
243905175c85SJed Brown 
244005175c85SJed Brown    Level: advanced
244105175c85SJed Brown 
2442108c343cSJed Brown    Notes:
2443108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2444108c343cSJed 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.
2445108c343cSJed Brown 
24461c3436cfSJed Brown .seealso: TSStep(), TSAdapt
244705175c85SJed Brown @*/
24480910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
244905175c85SJed Brown {
245005175c85SJed Brown   PetscErrorCode ierr;
245105175c85SJed Brown 
245205175c85SJed Brown   PetscFunctionBegin;
245305175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
245405175c85SJed Brown   PetscValidType(ts,1);
24550910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2456ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
24570910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
245805175c85SJed Brown   PetscFunctionReturn(0);
245905175c85SJed Brown }
246005175c85SJed Brown 
246105175c85SJed Brown #undef __FUNCT__
24626a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
24636a4d4014SLisandro Dalcin /*@
24646a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
24656a4d4014SLisandro Dalcin 
24666a4d4014SLisandro Dalcin    Collective on TS
24676a4d4014SLisandro Dalcin 
24686a4d4014SLisandro Dalcin    Input Parameter:
24696a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2470cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
24715a3a76d0SJed Brown 
24726a4d4014SLisandro Dalcin    Level: beginner
24736a4d4014SLisandro Dalcin 
24745a3a76d0SJed Brown    Notes:
24755a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
24765a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
24775a3a76d0SJed Brown    stepped over the final time.
24785a3a76d0SJed Brown 
24796a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
24806a4d4014SLisandro Dalcin 
24816a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
24826a4d4014SLisandro Dalcin @*/
2483cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
24846a4d4014SLisandro Dalcin {
24854d7d938eSLisandro Dalcin   PetscBool         flg;
24864d7d938eSLisandro Dalcin   PetscViewer       viewer;
24876a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2488cffb1e40SBarry Smith   PetscViewerFormat format;
2489f22f69f0SBarry Smith 
24906a4d4014SLisandro Dalcin   PetscFunctionBegin;
24910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2492f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
249349354f04SShri 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 */
24940910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
24955a3a76d0SJed Brown       Vec y;
24960910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
24975a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
24985a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
24995a3a76d0SJed Brown     }
2500f2c2a1b9SBarry Smith     if (u) {
25010910c330SBarry Smith       ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2502f2c2a1b9SBarry Smith     }
2503bbd56ea5SKarl Rupp   } else if (u) {
25040910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
25055a3a76d0SJed Brown   }
2506b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
25076a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2508193ac0bcSJed Brown   ts->steps             = 0;
25095ef26d82SJed Brown   ts->ksp_its           = 0;
25105ef26d82SJed Brown   ts->snes_its          = 0;
2511c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2512c610991cSLisandro Dalcin   ts->reject            = 0;
2513193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2514193ac0bcSJed Brown 
2515193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2516193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
25170910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2518bbd56ea5SKarl Rupp 
2519cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2520193ac0bcSJed Brown   } else {
25216a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2522362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2523db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2524db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2525e1a7a14fSJed Brown     while (!ts->reason) {
2526193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2527193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2528193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2529193ac0bcSJed Brown     }
253049354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
25310910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2532bbd56ea5SKarl Rupp 
2533cc708dedSBarry Smith       ts->solvetime = ts->max_time;
25340574a7fbSJed Brown     } else {
2535ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2536cc708dedSBarry Smith       ts->solvetime = ts->ptime;
25370574a7fbSJed Brown     }
2538193ac0bcSJed Brown   }
25393923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2540ce94432eSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
25414d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2542cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
25434d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2544cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2545cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
25462b0a91c0SBarry Smith   }
25476a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
25486a4d4014SLisandro Dalcin }
25496a4d4014SLisandro Dalcin 
25506a4d4014SLisandro Dalcin #undef __FUNCT__
25514a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2552228d79bcSJed Brown /*@
2553228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2554228d79bcSJed Brown 
2555228d79bcSJed Brown    Collective on TS
2556228d79bcSJed Brown 
2557228d79bcSJed Brown    Input Parameters:
2558228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2559228d79bcSJed Brown .  step - step number that has just completed
2560228d79bcSJed Brown .  ptime - model time of the state
25610910c330SBarry Smith -  u - state at the current model time
2562228d79bcSJed Brown 
2563228d79bcSJed Brown    Notes:
2564228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2565228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2566228d79bcSJed Brown 
2567228d79bcSJed Brown    Level: advanced
2568228d79bcSJed Brown 
2569228d79bcSJed Brown .keywords: TS, timestep
2570228d79bcSJed Brown @*/
25710910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2572d763cef2SBarry Smith {
25736849ba73SBarry Smith   PetscErrorCode ierr;
2574a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2575d763cef2SBarry Smith 
2576d763cef2SBarry Smith   PetscFunctionBegin;
2577d763cef2SBarry Smith   for (i=0; i<n; i++) {
25780910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2579d763cef2SBarry Smith   }
2580d763cef2SBarry Smith   PetscFunctionReturn(0);
2581d763cef2SBarry Smith }
2582d763cef2SBarry Smith 
2583d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
25840b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
25850b039ecaSBarry Smith   PetscDrawLG lg;
25860b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2587201da799SBarry Smith   PetscInt    ksp_its,snes_its;
25880b039ecaSBarry Smith };
25890b039ecaSBarry Smith 
2590d763cef2SBarry Smith 
25914a2ae208SSatish Balay #undef __FUNCT__
2592a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2593d763cef2SBarry Smith /*@C
2594a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2595a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2596d763cef2SBarry Smith 
2597d763cef2SBarry Smith    Collective on TS
2598d763cef2SBarry Smith 
2599d763cef2SBarry Smith    Input Parameters:
2600d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2601d763cef2SBarry Smith .  label - the title to put in the title bar
26027c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2603a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2604a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2605d763cef2SBarry Smith 
2606d763cef2SBarry Smith    Output Parameter:
26070b039ecaSBarry Smith .  ctx - the context
2608d763cef2SBarry Smith 
2609d763cef2SBarry Smith    Options Database Key:
26104f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
26114f09c107SBarry Smith .  -ts_monitor_lg_solution -
261299fdda47SBarry Smith .  -ts_monitor_lg_error -
261399fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
261499fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
261599fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2616d763cef2SBarry Smith 
2617d763cef2SBarry Smith    Notes:
2618a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2619d763cef2SBarry Smith 
2620d763cef2SBarry Smith    Level: intermediate
2621d763cef2SBarry Smith 
26227c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2623d763cef2SBarry Smith 
26244f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
26257c922b88SBarry Smith 
2626d763cef2SBarry Smith @*/
2627a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2628d763cef2SBarry Smith {
2629b0a32e0cSBarry Smith   PetscDraw      win;
2630dfbe8321SBarry Smith   PetscErrorCode ierr;
263199fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2632d763cef2SBarry Smith 
2633d763cef2SBarry Smith   PetscFunctionBegin;
2634201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2635a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
26363fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
26370b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
26380298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-lg_indicate_data_points",&flg,NULL);CHKERRQ(ierr);
263999fdda47SBarry Smith   if (flg) {
26400b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
264199fdda47SBarry Smith   }
26420b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2643a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2644d763cef2SBarry Smith   PetscFunctionReturn(0);
2645d763cef2SBarry Smith }
2646d763cef2SBarry Smith 
26474a2ae208SSatish Balay #undef __FUNCT__
26484f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
26494f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2650d763cef2SBarry Smith {
26510b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2652c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2653dfbe8321SBarry Smith   PetscErrorCode ierr;
2654d763cef2SBarry Smith 
2655d763cef2SBarry Smith   PetscFunctionBegin;
2656a9f9c1f6SBarry Smith   if (!n) {
2657a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2658a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2659a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2660a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2661a9f9c1f6SBarry Smith   }
2662c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
26630b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
266499fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))) {
26650b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
26663923b477SBarry Smith   }
2667d763cef2SBarry Smith   PetscFunctionReturn(0);
2668d763cef2SBarry Smith }
2669d763cef2SBarry Smith 
26704a2ae208SSatish Balay #undef __FUNCT__
2671a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2672d763cef2SBarry Smith /*@C
2673a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2674a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2675d763cef2SBarry Smith 
26760b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2677d763cef2SBarry Smith 
2678d763cef2SBarry Smith    Input Parameter:
26790b039ecaSBarry Smith .  ctx - the monitor context
2680d763cef2SBarry Smith 
2681d763cef2SBarry Smith    Level: intermediate
2682d763cef2SBarry Smith 
2683d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2684d763cef2SBarry Smith 
26854f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2686d763cef2SBarry Smith @*/
2687a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2688d763cef2SBarry Smith {
2689b0a32e0cSBarry Smith   PetscDraw      draw;
2690dfbe8321SBarry Smith   PetscErrorCode ierr;
2691d763cef2SBarry Smith 
2692d763cef2SBarry Smith   PetscFunctionBegin;
26930b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
26946bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
26950b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
26960b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2697d763cef2SBarry Smith   PetscFunctionReturn(0);
2698d763cef2SBarry Smith }
2699d763cef2SBarry Smith 
27004a2ae208SSatish Balay #undef __FUNCT__
27014a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2702d763cef2SBarry Smith /*@
2703b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2704d763cef2SBarry Smith 
2705d763cef2SBarry Smith    Not Collective
2706d763cef2SBarry Smith 
2707d763cef2SBarry Smith    Input Parameter:
2708d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2709d763cef2SBarry Smith 
2710d763cef2SBarry Smith    Output Parameter:
2711d763cef2SBarry Smith .  t  - the current time
2712d763cef2SBarry Smith 
2713d763cef2SBarry Smith    Level: beginner
2714d763cef2SBarry Smith 
2715b8123daeSJed Brown    Note:
2716b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2717b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2718b8123daeSJed Brown 
2719d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2720d763cef2SBarry Smith 
2721d763cef2SBarry Smith .keywords: TS, get, time
2722d763cef2SBarry Smith @*/
27237087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2724d763cef2SBarry Smith {
2725d763cef2SBarry Smith   PetscFunctionBegin;
27260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2727f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2728d763cef2SBarry Smith   *t = ts->ptime;
2729d763cef2SBarry Smith   PetscFunctionReturn(0);
2730d763cef2SBarry Smith }
2731d763cef2SBarry Smith 
27324a2ae208SSatish Balay #undef __FUNCT__
27336a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
27346a4d4014SLisandro Dalcin /*@
27356a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
27366a4d4014SLisandro Dalcin 
27373f9fe445SBarry Smith    Logically Collective on TS
27386a4d4014SLisandro Dalcin 
27396a4d4014SLisandro Dalcin    Input Parameters:
27406a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
27416a4d4014SLisandro Dalcin -  time - the time
27426a4d4014SLisandro Dalcin 
27436a4d4014SLisandro Dalcin    Level: intermediate
27446a4d4014SLisandro Dalcin 
27456a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
27466a4d4014SLisandro Dalcin 
27476a4d4014SLisandro Dalcin .keywords: TS, set, time
27486a4d4014SLisandro Dalcin @*/
27497087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
27506a4d4014SLisandro Dalcin {
27516a4d4014SLisandro Dalcin   PetscFunctionBegin;
27520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2753c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
27546a4d4014SLisandro Dalcin   ts->ptime = t;
27556a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
27566a4d4014SLisandro Dalcin }
27576a4d4014SLisandro Dalcin 
27586a4d4014SLisandro Dalcin #undef __FUNCT__
27594a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2760d763cef2SBarry Smith /*@C
2761d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2762d763cef2SBarry Smith    TS options in the database.
2763d763cef2SBarry Smith 
27643f9fe445SBarry Smith    Logically Collective on TS
2765d763cef2SBarry Smith 
2766d763cef2SBarry Smith    Input Parameter:
2767d763cef2SBarry Smith +  ts     - The TS context
2768d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2769d763cef2SBarry Smith 
2770d763cef2SBarry Smith    Notes:
2771d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2772d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2773d763cef2SBarry Smith    hyphen.
2774d763cef2SBarry Smith 
2775d763cef2SBarry Smith    Level: advanced
2776d763cef2SBarry Smith 
2777d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2778d763cef2SBarry Smith 
2779d763cef2SBarry Smith .seealso: TSSetFromOptions()
2780d763cef2SBarry Smith 
2781d763cef2SBarry Smith @*/
27827087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2783d763cef2SBarry Smith {
2784dfbe8321SBarry Smith   PetscErrorCode ierr;
2785089b2837SJed Brown   SNES           snes;
2786d763cef2SBarry Smith 
2787d763cef2SBarry Smith   PetscFunctionBegin;
27880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2789d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2790089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2791089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2792d763cef2SBarry Smith   PetscFunctionReturn(0);
2793d763cef2SBarry Smith }
2794d763cef2SBarry Smith 
2795d763cef2SBarry Smith 
27964a2ae208SSatish Balay #undef __FUNCT__
27974a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2798d763cef2SBarry Smith /*@C
2799d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2800d763cef2SBarry Smith    TS options in the database.
2801d763cef2SBarry Smith 
28023f9fe445SBarry Smith    Logically Collective on TS
2803d763cef2SBarry Smith 
2804d763cef2SBarry Smith    Input Parameter:
2805d763cef2SBarry Smith +  ts     - The TS context
2806d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2807d763cef2SBarry Smith 
2808d763cef2SBarry Smith    Notes:
2809d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2810d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2811d763cef2SBarry Smith    hyphen.
2812d763cef2SBarry Smith 
2813d763cef2SBarry Smith    Level: advanced
2814d763cef2SBarry Smith 
2815d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2816d763cef2SBarry Smith 
2817d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2818d763cef2SBarry Smith 
2819d763cef2SBarry Smith @*/
28207087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2821d763cef2SBarry Smith {
2822dfbe8321SBarry Smith   PetscErrorCode ierr;
2823089b2837SJed Brown   SNES           snes;
2824d763cef2SBarry Smith 
2825d763cef2SBarry Smith   PetscFunctionBegin;
28260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2827d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2828089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2829089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2830d763cef2SBarry Smith   PetscFunctionReturn(0);
2831d763cef2SBarry Smith }
2832d763cef2SBarry Smith 
28334a2ae208SSatish Balay #undef __FUNCT__
28344a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2835d763cef2SBarry Smith /*@C
2836d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2837d763cef2SBarry Smith    TS options in the database.
2838d763cef2SBarry Smith 
2839d763cef2SBarry Smith    Not Collective
2840d763cef2SBarry Smith 
2841d763cef2SBarry Smith    Input Parameter:
2842d763cef2SBarry Smith .  ts - The TS context
2843d763cef2SBarry Smith 
2844d763cef2SBarry Smith    Output Parameter:
2845d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2846d763cef2SBarry Smith 
2847d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2848d763cef2SBarry Smith    sufficient length to hold the prefix.
2849d763cef2SBarry Smith 
2850d763cef2SBarry Smith    Level: intermediate
2851d763cef2SBarry Smith 
2852d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2853d763cef2SBarry Smith 
2854d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2855d763cef2SBarry Smith @*/
28567087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2857d763cef2SBarry Smith {
2858dfbe8321SBarry Smith   PetscErrorCode ierr;
2859d763cef2SBarry Smith 
2860d763cef2SBarry Smith   PetscFunctionBegin;
28610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28624482741eSBarry Smith   PetscValidPointer(prefix,2);
2863d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2864d763cef2SBarry Smith   PetscFunctionReturn(0);
2865d763cef2SBarry Smith }
2866d763cef2SBarry Smith 
28674a2ae208SSatish Balay #undef __FUNCT__
28684a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2869d763cef2SBarry Smith /*@C
2870d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2871d763cef2SBarry Smith 
2872d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2873d763cef2SBarry Smith 
2874d763cef2SBarry Smith    Input Parameter:
2875d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2876d763cef2SBarry Smith 
2877d763cef2SBarry Smith    Output Parameters:
2878b5abc632SBarry Smith +  J   - The Jacobian J of F, where U_t = G(U,t)
2879d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2880089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2881d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2882d763cef2SBarry Smith 
28830298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
2884d763cef2SBarry Smith 
2885d763cef2SBarry Smith    Level: intermediate
2886d763cef2SBarry Smith 
288726d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2888d763cef2SBarry Smith 
2889d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2890d763cef2SBarry Smith @*/
2891089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2892d763cef2SBarry Smith {
2893089b2837SJed Brown   PetscErrorCode ierr;
2894089b2837SJed Brown   SNES           snes;
289524989b8cSPeter Brune   DM             dm;
2896089b2837SJed Brown 
2897d763cef2SBarry Smith   PetscFunctionBegin;
2898089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
28990298fd71SBarry Smith   ierr = SNESGetJacobian(snes,J,M,NULL,NULL);CHKERRQ(ierr);
290024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
290124989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2902d763cef2SBarry Smith   PetscFunctionReturn(0);
2903d763cef2SBarry Smith }
2904d763cef2SBarry Smith 
29051713a123SBarry Smith #undef __FUNCT__
29062eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
29072eca1d9cSJed Brown /*@C
29082eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
29092eca1d9cSJed Brown 
29102eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
29112eca1d9cSJed Brown 
29122eca1d9cSJed Brown    Input Parameter:
29132eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
29142eca1d9cSJed Brown 
29152eca1d9cSJed Brown    Output Parameters:
29162eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
29172eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
29182eca1d9cSJed Brown .  f   - The function to compute the matrices
29192eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
29202eca1d9cSJed Brown 
29210298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
29222eca1d9cSJed Brown 
29232eca1d9cSJed Brown    Level: advanced
29242eca1d9cSJed Brown 
29252eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
29262eca1d9cSJed Brown 
29272eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
29282eca1d9cSJed Brown @*/
29297087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
29302eca1d9cSJed Brown {
2931089b2837SJed Brown   PetscErrorCode ierr;
2932089b2837SJed Brown   SNES           snes;
293324989b8cSPeter Brune   DM             dm;
29340910c330SBarry Smith 
29352eca1d9cSJed Brown   PetscFunctionBegin;
2936089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2937f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
29380298fd71SBarry Smith   ierr = SNESGetJacobian(snes,A,B,NULL,NULL);CHKERRQ(ierr);
293924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
294024989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
29412eca1d9cSJed Brown   PetscFunctionReturn(0);
29422eca1d9cSJed Brown }
29432eca1d9cSJed Brown 
294483a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
29456083293cSBarry Smith   PetscViewer viewer;
29466083293cSBarry Smith   Vec         initialsolution;
29476083293cSBarry Smith   PetscBool   showinitial;
294883a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2949473a3ab2SBarry Smith   PetscBool   showtimestepandtime;
295083a4ac43SBarry Smith };
29516083293cSBarry Smith 
29522eca1d9cSJed Brown #undef __FUNCT__
295383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
29541713a123SBarry Smith /*@C
295583a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
29561713a123SBarry Smith    VecView() for the solution at each timestep
29571713a123SBarry Smith 
29581713a123SBarry Smith    Collective on TS
29591713a123SBarry Smith 
29601713a123SBarry Smith    Input Parameters:
29611713a123SBarry Smith +  ts - the TS context
29621713a123SBarry Smith .  step - current time-step
2963142b95e3SSatish Balay .  ptime - current time
29640298fd71SBarry Smith -  dummy - either a viewer or NULL
29651713a123SBarry Smith 
296699fdda47SBarry Smith    Options Database:
296799fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
296899fdda47SBarry Smith 
296999fdda47SBarry 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
297099fdda47SBarry Smith        will look bad
297199fdda47SBarry Smith 
29721713a123SBarry Smith    Level: intermediate
29731713a123SBarry Smith 
29741713a123SBarry Smith .keywords: TS,  vector, monitor, view
29751713a123SBarry Smith 
2976a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
29771713a123SBarry Smith @*/
29780910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
29791713a123SBarry Smith {
2980dfbe8321SBarry Smith   PetscErrorCode   ierr;
298183a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
2982473a3ab2SBarry Smith   PetscDraw        draw;
29831713a123SBarry Smith 
29841713a123SBarry Smith   PetscFunctionBegin;
29856083293cSBarry Smith   if (!step && ictx->showinitial) {
29866083293cSBarry Smith     if (!ictx->initialsolution) {
29870910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
29881713a123SBarry Smith     }
29890910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
29906083293cSBarry Smith   }
29913fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
29920dcf80beSBarry Smith 
29936083293cSBarry Smith   if (ictx->showinitial) {
29946083293cSBarry Smith     PetscReal pause;
29956083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
29966083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
29976083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
29986083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
29996083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
30006083293cSBarry Smith   }
30010910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3002473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3003473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3004473a3ab2SBarry Smith     char      time[32];
3005473a3ab2SBarry Smith     size_t    len;
3006473a3ab2SBarry Smith 
3007473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3008473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3009473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3010473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
30110298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3012473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3013473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3014473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3015473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3016473a3ab2SBarry Smith   }
3017473a3ab2SBarry Smith 
30186083293cSBarry Smith   if (ictx->showinitial) {
30196083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
30206083293cSBarry Smith   }
30211713a123SBarry Smith   PetscFunctionReturn(0);
30221713a123SBarry Smith }
30231713a123SBarry Smith 
30241713a123SBarry Smith 
30256c699258SBarry Smith #undef __FUNCT__
302683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
30276083293cSBarry Smith /*@C
302883a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
30296083293cSBarry Smith 
30306083293cSBarry Smith    Collective on TS
30316083293cSBarry Smith 
30326083293cSBarry Smith    Input Parameters:
30336083293cSBarry Smith .    ctx - the monitor context
30346083293cSBarry Smith 
30356083293cSBarry Smith    Level: intermediate
30366083293cSBarry Smith 
30376083293cSBarry Smith .keywords: TS,  vector, monitor, view
30386083293cSBarry Smith 
303983a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
30406083293cSBarry Smith @*/
304183a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
30426083293cSBarry Smith {
30436083293cSBarry Smith   PetscErrorCode ierr;
30446083293cSBarry Smith 
30456083293cSBarry Smith   PetscFunctionBegin;
304683a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
304783a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
304883a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
30496083293cSBarry Smith   PetscFunctionReturn(0);
30506083293cSBarry Smith }
30516083293cSBarry Smith 
30526083293cSBarry Smith #undef __FUNCT__
305383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
30546083293cSBarry Smith /*@C
305583a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
30566083293cSBarry Smith 
30576083293cSBarry Smith    Collective on TS
30586083293cSBarry Smith 
30596083293cSBarry Smith    Input Parameter:
30606083293cSBarry Smith .    ts - time-step context
30616083293cSBarry Smith 
30626083293cSBarry Smith    Output Patameter:
30636083293cSBarry Smith .    ctx - the monitor context
30646083293cSBarry Smith 
306599fdda47SBarry Smith    Options Database:
306699fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
306799fdda47SBarry Smith 
30686083293cSBarry Smith    Level: intermediate
30696083293cSBarry Smith 
30706083293cSBarry Smith .keywords: TS,  vector, monitor, view
30716083293cSBarry Smith 
307283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
30736083293cSBarry Smith @*/
307483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
30756083293cSBarry Smith {
30766083293cSBarry Smith   PetscErrorCode   ierr;
30776083293cSBarry Smith 
30786083293cSBarry Smith   PetscFunctionBegin;
307983a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
308083a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3081e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3082bbd56ea5SKarl Rupp 
308383a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3084473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
30850298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3086473a3ab2SBarry Smith 
3087473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
30880298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
30896083293cSBarry Smith   PetscFunctionReturn(0);
30906083293cSBarry Smith }
30916083293cSBarry Smith 
30926083293cSBarry Smith #undef __FUNCT__
309383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
30943a471f94SBarry Smith /*@C
309583a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
30963a471f94SBarry Smith    VecView() for the error at each timestep
30973a471f94SBarry Smith 
30983a471f94SBarry Smith    Collective on TS
30993a471f94SBarry Smith 
31003a471f94SBarry Smith    Input Parameters:
31013a471f94SBarry Smith +  ts - the TS context
31023a471f94SBarry Smith .  step - current time-step
31033a471f94SBarry Smith .  ptime - current time
31040298fd71SBarry Smith -  dummy - either a viewer or NULL
31053a471f94SBarry Smith 
31063a471f94SBarry Smith    Level: intermediate
31073a471f94SBarry Smith 
31083a471f94SBarry Smith .keywords: TS,  vector, monitor, view
31093a471f94SBarry Smith 
31103a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
31113a471f94SBarry Smith @*/
31120910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
31133a471f94SBarry Smith {
31143a471f94SBarry Smith   PetscErrorCode   ierr;
311583a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
311683a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
31173a471f94SBarry Smith   Vec              work;
31183a471f94SBarry Smith 
31193a471f94SBarry Smith   PetscFunctionBegin;
31203fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
31210910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
31223a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
31230910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
31243a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
31253a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
31263a471f94SBarry Smith   PetscFunctionReturn(0);
31273a471f94SBarry Smith }
31283a471f94SBarry Smith 
31292a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
31303a471f94SBarry Smith #undef __FUNCT__
31316c699258SBarry Smith #define __FUNCT__ "TSSetDM"
31326c699258SBarry Smith /*@
31336c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
31346c699258SBarry Smith 
31353f9fe445SBarry Smith    Logically Collective on TS and DM
31366c699258SBarry Smith 
31376c699258SBarry Smith    Input Parameters:
31386c699258SBarry Smith +  ts - the preconditioner context
31396c699258SBarry Smith -  dm - the dm
31406c699258SBarry Smith 
31416c699258SBarry Smith    Level: intermediate
31426c699258SBarry Smith 
31436c699258SBarry Smith 
31446c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
31456c699258SBarry Smith @*/
31467087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
31476c699258SBarry Smith {
31486c699258SBarry Smith   PetscErrorCode ierr;
3149089b2837SJed Brown   SNES           snes;
3150942e3340SBarry Smith   DMTS           tsdm;
31516c699258SBarry Smith 
31526c699258SBarry Smith   PetscFunctionBegin;
31530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
315470663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3155942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
31562a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3157942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3158942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
315924989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
316024989b8cSPeter Brune         tsdm->originaldm = dm;
316124989b8cSPeter Brune       }
316224989b8cSPeter Brune     }
31636bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
316424989b8cSPeter Brune   }
31656c699258SBarry Smith   ts->dm = dm;
3166bbd56ea5SKarl Rupp 
3167089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3168089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
31696c699258SBarry Smith   PetscFunctionReturn(0);
31706c699258SBarry Smith }
31716c699258SBarry Smith 
31726c699258SBarry Smith #undef __FUNCT__
31736c699258SBarry Smith #define __FUNCT__ "TSGetDM"
31746c699258SBarry Smith /*@
31756c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
31766c699258SBarry Smith 
31773f9fe445SBarry Smith    Not Collective
31786c699258SBarry Smith 
31796c699258SBarry Smith    Input Parameter:
31806c699258SBarry Smith . ts - the preconditioner context
31816c699258SBarry Smith 
31826c699258SBarry Smith    Output Parameter:
31836c699258SBarry Smith .  dm - the dm
31846c699258SBarry Smith 
31856c699258SBarry Smith    Level: intermediate
31866c699258SBarry Smith 
31876c699258SBarry Smith 
31886c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
31896c699258SBarry Smith @*/
31907087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
31916c699258SBarry Smith {
3192496e6a7aSJed Brown   PetscErrorCode ierr;
3193496e6a7aSJed Brown 
31946c699258SBarry Smith   PetscFunctionBegin;
31950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3196496e6a7aSJed Brown   if (!ts->dm) {
3197ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
3198496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3199496e6a7aSJed Brown   }
32006c699258SBarry Smith   *dm = ts->dm;
32016c699258SBarry Smith   PetscFunctionReturn(0);
32026c699258SBarry Smith }
32031713a123SBarry Smith 
32040f5c6efeSJed Brown #undef __FUNCT__
32050f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
32060f5c6efeSJed Brown /*@
32070f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
32080f5c6efeSJed Brown 
32093f9fe445SBarry Smith    Logically Collective on SNES
32100f5c6efeSJed Brown 
32110f5c6efeSJed Brown    Input Parameter:
3212d42a1c89SJed Brown + snes - nonlinear solver
32130910c330SBarry Smith . U - the current state at which to evaluate the residual
3214d42a1c89SJed Brown - ctx - user context, must be a TS
32150f5c6efeSJed Brown 
32160f5c6efeSJed Brown    Output Parameter:
32170f5c6efeSJed Brown . F - the nonlinear residual
32180f5c6efeSJed Brown 
32190f5c6efeSJed Brown    Notes:
32200f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
32210f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
32220f5c6efeSJed Brown 
32230f5c6efeSJed Brown    Level: advanced
32240f5c6efeSJed Brown 
32250f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
32260f5c6efeSJed Brown @*/
32270910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
32280f5c6efeSJed Brown {
32290f5c6efeSJed Brown   TS             ts = (TS)ctx;
32300f5c6efeSJed Brown   PetscErrorCode ierr;
32310f5c6efeSJed Brown 
32320f5c6efeSJed Brown   PetscFunctionBegin;
32330f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
32340910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
32350f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
32360f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
32370910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
32380f5c6efeSJed Brown   PetscFunctionReturn(0);
32390f5c6efeSJed Brown }
32400f5c6efeSJed Brown 
32410f5c6efeSJed Brown #undef __FUNCT__
32420f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
32430f5c6efeSJed Brown /*@
32440f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
32450f5c6efeSJed Brown 
32460f5c6efeSJed Brown    Collective on SNES
32470f5c6efeSJed Brown 
32480f5c6efeSJed Brown    Input Parameter:
32490f5c6efeSJed Brown + snes - nonlinear solver
32500910c330SBarry Smith . U - the current state at which to evaluate the residual
32510f5c6efeSJed Brown - ctx - user context, must be a TS
32520f5c6efeSJed Brown 
32530f5c6efeSJed Brown    Output Parameter:
32540f5c6efeSJed Brown + A - the Jacobian
32550f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
32560f5c6efeSJed Brown - flag - indicates any structure change in the matrix
32570f5c6efeSJed Brown 
32580f5c6efeSJed Brown    Notes:
32590f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
32600f5c6efeSJed Brown 
32610f5c6efeSJed Brown    Level: developer
32620f5c6efeSJed Brown 
32630f5c6efeSJed Brown .seealso: SNESSetJacobian()
32640f5c6efeSJed Brown @*/
32650910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
32660f5c6efeSJed Brown {
32670f5c6efeSJed Brown   TS             ts = (TS)ctx;
32680f5c6efeSJed Brown   PetscErrorCode ierr;
32690f5c6efeSJed Brown 
32700f5c6efeSJed Brown   PetscFunctionBegin;
32710f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
32720910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
32730f5c6efeSJed Brown   PetscValidPointer(A,3);
32740f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
32750f5c6efeSJed Brown   PetscValidPointer(B,4);
32760f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
32770f5c6efeSJed Brown   PetscValidPointer(flag,5);
32780f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
32790910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
32800f5c6efeSJed Brown   PetscFunctionReturn(0);
32810f5c6efeSJed Brown }
3282325fc9f4SBarry Smith 
32830e4ef248SJed Brown #undef __FUNCT__
32840e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
32850e4ef248SJed Brown /*@C
32860e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
32870e4ef248SJed Brown 
32880e4ef248SJed Brown    Collective on TS
32890e4ef248SJed Brown 
32900e4ef248SJed Brown    Input Arguments:
32910e4ef248SJed Brown +  ts - time stepping context
32920e4ef248SJed Brown .  t - time at which to evaluate
32930910c330SBarry Smith .  U - state at which to evaluate
32940e4ef248SJed Brown -  ctx - context
32950e4ef248SJed Brown 
32960e4ef248SJed Brown    Output Arguments:
32970e4ef248SJed Brown .  F - right hand side
32980e4ef248SJed Brown 
32990e4ef248SJed Brown    Level: intermediate
33000e4ef248SJed Brown 
33010e4ef248SJed Brown    Notes:
33020e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
33030e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
33040e4ef248SJed Brown 
33050e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
33060e4ef248SJed Brown @*/
33070910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
33080e4ef248SJed Brown {
33090e4ef248SJed Brown   PetscErrorCode ierr;
33100e4ef248SJed Brown   Mat            Arhs,Brhs;
33110e4ef248SJed Brown   MatStructure   flg2;
33120e4ef248SJed Brown 
33130e4ef248SJed Brown   PetscFunctionBegin;
33140e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
33150910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
33160910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
33170e4ef248SJed Brown   PetscFunctionReturn(0);
33180e4ef248SJed Brown }
33190e4ef248SJed Brown 
33200e4ef248SJed Brown #undef __FUNCT__
33210e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
33220e4ef248SJed Brown /*@C
33230e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
33240e4ef248SJed Brown 
33250e4ef248SJed Brown    Collective on TS
33260e4ef248SJed Brown 
33270e4ef248SJed Brown    Input Arguments:
33280e4ef248SJed Brown +  ts - time stepping context
33290e4ef248SJed Brown .  t - time at which to evaluate
33300910c330SBarry Smith .  U - state at which to evaluate
33310e4ef248SJed Brown -  ctx - context
33320e4ef248SJed Brown 
33330e4ef248SJed Brown    Output Arguments:
33340e4ef248SJed Brown +  A - pointer to operator
33350e4ef248SJed Brown .  B - pointer to preconditioning matrix
33360e4ef248SJed Brown -  flg - matrix structure flag
33370e4ef248SJed Brown 
33380e4ef248SJed Brown    Level: intermediate
33390e4ef248SJed Brown 
33400e4ef248SJed Brown    Notes:
33410e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
33420e4ef248SJed Brown 
33430e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
33440e4ef248SJed Brown @*/
33450910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
33460e4ef248SJed Brown {
33470e4ef248SJed Brown   PetscFunctionBegin;
33480e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
33490e4ef248SJed Brown   PetscFunctionReturn(0);
33500e4ef248SJed Brown }
33510e4ef248SJed Brown 
33520026cea9SSean Farley #undef __FUNCT__
33530026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
33540026cea9SSean Farley /*@C
33550026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
33560026cea9SSean Farley 
33570026cea9SSean Farley    Collective on TS
33580026cea9SSean Farley 
33590026cea9SSean Farley    Input Arguments:
33600026cea9SSean Farley +  ts - time stepping context
33610026cea9SSean Farley .  t - time at which to evaluate
33620910c330SBarry Smith .  U - state at which to evaluate
33630910c330SBarry Smith .  Udot - time derivative of state vector
33640026cea9SSean Farley -  ctx - context
33650026cea9SSean Farley 
33660026cea9SSean Farley    Output Arguments:
33670026cea9SSean Farley .  F - left hand side
33680026cea9SSean Farley 
33690026cea9SSean Farley    Level: intermediate
33700026cea9SSean Farley 
33710026cea9SSean Farley    Notes:
33720910c330SBarry 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
33730026cea9SSean Farley    user is required to write their own TSComputeIFunction.
33740026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
33750026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
33760026cea9SSean Farley 
33770026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
33780026cea9SSean Farley @*/
33790910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
33800026cea9SSean Farley {
33810026cea9SSean Farley   PetscErrorCode ierr;
33820026cea9SSean Farley   Mat            A,B;
33830026cea9SSean Farley   MatStructure   flg2;
33840026cea9SSean Farley 
33850026cea9SSean Farley   PetscFunctionBegin;
33860298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
33870910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
33880910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
33890026cea9SSean Farley   PetscFunctionReturn(0);
33900026cea9SSean Farley }
33910026cea9SSean Farley 
33920026cea9SSean Farley #undef __FUNCT__
33930026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
33940026cea9SSean Farley /*@C
339524e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
33960026cea9SSean Farley 
33970026cea9SSean Farley    Collective on TS
33980026cea9SSean Farley 
33990026cea9SSean Farley    Input Arguments:
34000026cea9SSean Farley +  ts - time stepping context
34010026cea9SSean Farley .  t - time at which to evaluate
34020910c330SBarry Smith .  U - state at which to evaluate
34030910c330SBarry Smith .  Udot - time derivative of state vector
34040026cea9SSean Farley .  shift - shift to apply
34050026cea9SSean Farley -  ctx - context
34060026cea9SSean Farley 
34070026cea9SSean Farley    Output Arguments:
34080026cea9SSean Farley +  A - pointer to operator
34090026cea9SSean Farley .  B - pointer to preconditioning matrix
34100026cea9SSean Farley -  flg - matrix structure flag
34110026cea9SSean Farley 
34120026cea9SSean Farley    Level: intermediate
34130026cea9SSean Farley 
34140026cea9SSean Farley    Notes:
34150026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
34160026cea9SSean Farley 
34170026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
34180026cea9SSean Farley @*/
34190910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
34200026cea9SSean Farley {
34210026cea9SSean Farley   PetscFunctionBegin;
34220026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
34230026cea9SSean Farley   PetscFunctionReturn(0);
34240026cea9SSean Farley }
3425e817cc15SEmil Constantinescu #undef __FUNCT__
3426e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3427e817cc15SEmil Constantinescu /*@
3428e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3429e817cc15SEmil Constantinescu 
3430e817cc15SEmil Constantinescu    Not Collective
3431e817cc15SEmil Constantinescu 
3432e817cc15SEmil Constantinescu    Input Parameter:
3433e817cc15SEmil Constantinescu .  ts - the TS context
3434e817cc15SEmil Constantinescu 
3435e817cc15SEmil Constantinescu    Output Parameter:
3436e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3437e817cc15SEmil Constantinescu 
3438e817cc15SEmil Constantinescu    Level: beginner
3439e817cc15SEmil Constantinescu 
3440e817cc15SEmil Constantinescu .keywords: TS, equation type
3441e817cc15SEmil Constantinescu 
3442e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3443e817cc15SEmil Constantinescu @*/
3444e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3445e817cc15SEmil Constantinescu {
3446e817cc15SEmil Constantinescu   PetscFunctionBegin;
3447e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3448e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3449e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3450e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3451e817cc15SEmil Constantinescu }
3452e817cc15SEmil Constantinescu 
3453e817cc15SEmil Constantinescu #undef __FUNCT__
3454e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3455e817cc15SEmil Constantinescu /*@
3456e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3457e817cc15SEmil Constantinescu 
3458e817cc15SEmil Constantinescu    Not Collective
3459e817cc15SEmil Constantinescu 
3460e817cc15SEmil Constantinescu    Input Parameter:
3461e817cc15SEmil Constantinescu +  ts - the TS context
3462e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3463e817cc15SEmil Constantinescu 
3464e817cc15SEmil Constantinescu    Level: advanced
3465e817cc15SEmil Constantinescu 
3466e817cc15SEmil Constantinescu .keywords: TS, equation type
3467e817cc15SEmil Constantinescu 
3468e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3469e817cc15SEmil Constantinescu @*/
3470e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3471e817cc15SEmil Constantinescu {
3472e817cc15SEmil Constantinescu   PetscFunctionBegin;
3473e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3474e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3475e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3476e817cc15SEmil Constantinescu }
34770026cea9SSean Farley 
34784af1b03aSJed Brown #undef __FUNCT__
34794af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
34804af1b03aSJed Brown /*@
34814af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
34824af1b03aSJed Brown 
34834af1b03aSJed Brown    Not Collective
34844af1b03aSJed Brown 
34854af1b03aSJed Brown    Input Parameter:
34864af1b03aSJed Brown .  ts - the TS context
34874af1b03aSJed Brown 
34884af1b03aSJed Brown    Output Parameter:
34894af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
34904af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
34914af1b03aSJed Brown 
3492487e0bb9SJed Brown    Level: beginner
34934af1b03aSJed Brown 
3494cd652676SJed Brown    Notes:
3495cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
34964af1b03aSJed Brown 
34974af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
34984af1b03aSJed Brown 
34994af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
35004af1b03aSJed Brown @*/
35014af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
35024af1b03aSJed Brown {
35034af1b03aSJed Brown   PetscFunctionBegin;
35044af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35054af1b03aSJed Brown   PetscValidPointer(reason,2);
35064af1b03aSJed Brown   *reason = ts->reason;
35074af1b03aSJed Brown   PetscFunctionReturn(0);
35084af1b03aSJed Brown }
35094af1b03aSJed Brown 
3510fb1732b5SBarry Smith #undef __FUNCT__
3511d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3512d6ad946cSShri Abhyankar /*@
3513d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3514d6ad946cSShri Abhyankar 
3515d6ad946cSShri Abhyankar    Not Collective
3516d6ad946cSShri Abhyankar 
3517d6ad946cSShri Abhyankar    Input Parameter:
3518d6ad946cSShri Abhyankar +  ts - the TS context
3519d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3520d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3521d6ad946cSShri Abhyankar 
3522f5abba47SShri Abhyankar    Level: advanced
3523d6ad946cSShri Abhyankar 
3524d6ad946cSShri Abhyankar    Notes:
3525d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3526d6ad946cSShri Abhyankar 
3527d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3528d6ad946cSShri Abhyankar 
3529d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3530d6ad946cSShri Abhyankar @*/
3531d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3532d6ad946cSShri Abhyankar {
3533d6ad946cSShri Abhyankar   PetscFunctionBegin;
3534d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3535d6ad946cSShri Abhyankar   ts->reason = reason;
3536d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3537d6ad946cSShri Abhyankar }
3538d6ad946cSShri Abhyankar 
3539d6ad946cSShri Abhyankar #undef __FUNCT__
3540cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3541cc708dedSBarry Smith /*@
3542cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3543cc708dedSBarry Smith 
3544cc708dedSBarry Smith    Not Collective
3545cc708dedSBarry Smith 
3546cc708dedSBarry Smith    Input Parameter:
3547cc708dedSBarry Smith .  ts - the TS context
3548cc708dedSBarry Smith 
3549cc708dedSBarry Smith    Output Parameter:
3550cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3551cc708dedSBarry Smith 
3552487e0bb9SJed Brown    Level: beginner
3553cc708dedSBarry Smith 
3554cc708dedSBarry Smith    Notes:
3555cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3556cc708dedSBarry Smith 
3557cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3558cc708dedSBarry Smith 
3559cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3560cc708dedSBarry Smith @*/
3561cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3562cc708dedSBarry Smith {
3563cc708dedSBarry Smith   PetscFunctionBegin;
3564cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3565cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3566cc708dedSBarry Smith   *ftime = ts->solvetime;
3567cc708dedSBarry Smith   PetscFunctionReturn(0);
3568cc708dedSBarry Smith }
3569cc708dedSBarry Smith 
3570cc708dedSBarry Smith #undef __FUNCT__
35715ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
35729f67acb7SJed Brown /*@
35735ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
35749f67acb7SJed Brown    used by the time integrator.
35759f67acb7SJed Brown 
35769f67acb7SJed Brown    Not Collective
35779f67acb7SJed Brown 
35789f67acb7SJed Brown    Input Parameter:
35799f67acb7SJed Brown .  ts - TS context
35809f67acb7SJed Brown 
35819f67acb7SJed Brown    Output Parameter:
35829f67acb7SJed Brown .  nits - number of nonlinear iterations
35839f67acb7SJed Brown 
35849f67acb7SJed Brown    Notes:
35859f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
35869f67acb7SJed Brown 
35879f67acb7SJed Brown    Level: intermediate
35889f67acb7SJed Brown 
35899f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
35909f67acb7SJed Brown 
35915ef26d82SJed Brown .seealso:  TSGetKSPIterations()
35929f67acb7SJed Brown @*/
35935ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
35949f67acb7SJed Brown {
35959f67acb7SJed Brown   PetscFunctionBegin;
35969f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35979f67acb7SJed Brown   PetscValidIntPointer(nits,2);
35985ef26d82SJed Brown   *nits = ts->snes_its;
35999f67acb7SJed Brown   PetscFunctionReturn(0);
36009f67acb7SJed Brown }
36019f67acb7SJed Brown 
36029f67acb7SJed Brown #undef __FUNCT__
36035ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
36049f67acb7SJed Brown /*@
36055ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
36069f67acb7SJed Brown    used by the time integrator.
36079f67acb7SJed Brown 
36089f67acb7SJed Brown    Not Collective
36099f67acb7SJed Brown 
36109f67acb7SJed Brown    Input Parameter:
36119f67acb7SJed Brown .  ts - TS context
36129f67acb7SJed Brown 
36139f67acb7SJed Brown    Output Parameter:
36149f67acb7SJed Brown .  lits - number of linear iterations
36159f67acb7SJed Brown 
36169f67acb7SJed Brown    Notes:
36179f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
36189f67acb7SJed Brown 
36199f67acb7SJed Brown    Level: intermediate
36209f67acb7SJed Brown 
36219f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
36229f67acb7SJed Brown 
36235ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
36249f67acb7SJed Brown @*/
36255ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
36269f67acb7SJed Brown {
36279f67acb7SJed Brown   PetscFunctionBegin;
36289f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36299f67acb7SJed Brown   PetscValidIntPointer(lits,2);
36305ef26d82SJed Brown   *lits = ts->ksp_its;
36319f67acb7SJed Brown   PetscFunctionReturn(0);
36329f67acb7SJed Brown }
36339f67acb7SJed Brown 
36349f67acb7SJed Brown #undef __FUNCT__
3635cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3636cef5090cSJed Brown /*@
3637cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3638cef5090cSJed Brown 
3639cef5090cSJed Brown    Not Collective
3640cef5090cSJed Brown 
3641cef5090cSJed Brown    Input Parameter:
3642cef5090cSJed Brown .  ts - TS context
3643cef5090cSJed Brown 
3644cef5090cSJed Brown    Output Parameter:
3645cef5090cSJed Brown .  rejects - number of steps rejected
3646cef5090cSJed Brown 
3647cef5090cSJed Brown    Notes:
3648cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3649cef5090cSJed Brown 
3650cef5090cSJed Brown    Level: intermediate
3651cef5090cSJed Brown 
3652cef5090cSJed Brown .keywords: TS, get, number
3653cef5090cSJed Brown 
36545ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3655cef5090cSJed Brown @*/
3656cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3657cef5090cSJed Brown {
3658cef5090cSJed Brown   PetscFunctionBegin;
3659cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3660cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3661cef5090cSJed Brown   *rejects = ts->reject;
3662cef5090cSJed Brown   PetscFunctionReturn(0);
3663cef5090cSJed Brown }
3664cef5090cSJed Brown 
3665cef5090cSJed Brown #undef __FUNCT__
3666cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3667cef5090cSJed Brown /*@
3668cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3669cef5090cSJed Brown 
3670cef5090cSJed Brown    Not Collective
3671cef5090cSJed Brown 
3672cef5090cSJed Brown    Input Parameter:
3673cef5090cSJed Brown .  ts - TS context
3674cef5090cSJed Brown 
3675cef5090cSJed Brown    Output Parameter:
3676cef5090cSJed Brown .  fails - number of failed nonlinear solves
3677cef5090cSJed Brown 
3678cef5090cSJed Brown    Notes:
3679cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3680cef5090cSJed Brown 
3681cef5090cSJed Brown    Level: intermediate
3682cef5090cSJed Brown 
3683cef5090cSJed Brown .keywords: TS, get, number
3684cef5090cSJed Brown 
36855ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3686cef5090cSJed Brown @*/
3687cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3688cef5090cSJed Brown {
3689cef5090cSJed Brown   PetscFunctionBegin;
3690cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3691cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3692cef5090cSJed Brown   *fails = ts->num_snes_failures;
3693cef5090cSJed Brown   PetscFunctionReturn(0);
3694cef5090cSJed Brown }
3695cef5090cSJed Brown 
3696cef5090cSJed Brown #undef __FUNCT__
3697cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3698cef5090cSJed Brown /*@
3699cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3700cef5090cSJed Brown 
3701cef5090cSJed Brown    Not Collective
3702cef5090cSJed Brown 
3703cef5090cSJed Brown    Input Parameter:
3704cef5090cSJed Brown +  ts - TS context
3705cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3706cef5090cSJed Brown 
3707cef5090cSJed Brown    Notes:
3708cef5090cSJed Brown    The counter is reset to zero for each step
3709cef5090cSJed Brown 
3710cef5090cSJed Brown    Options Database Key:
3711cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3712cef5090cSJed Brown 
3713cef5090cSJed Brown    Level: intermediate
3714cef5090cSJed Brown 
3715cef5090cSJed Brown .keywords: TS, set, maximum, number
3716cef5090cSJed Brown 
37175ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3718cef5090cSJed Brown @*/
3719cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3720cef5090cSJed Brown {
3721cef5090cSJed Brown   PetscFunctionBegin;
3722cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3723cef5090cSJed Brown   ts->max_reject = rejects;
3724cef5090cSJed Brown   PetscFunctionReturn(0);
3725cef5090cSJed Brown }
3726cef5090cSJed Brown 
3727cef5090cSJed Brown #undef __FUNCT__
3728cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3729cef5090cSJed Brown /*@
3730cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3731cef5090cSJed Brown 
3732cef5090cSJed Brown    Not Collective
3733cef5090cSJed Brown 
3734cef5090cSJed Brown    Input Parameter:
3735cef5090cSJed Brown +  ts - TS context
3736cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3737cef5090cSJed Brown 
3738cef5090cSJed Brown    Notes:
3739cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3740cef5090cSJed Brown 
3741cef5090cSJed Brown    Options Database Key:
3742cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3743cef5090cSJed Brown 
3744cef5090cSJed Brown    Level: intermediate
3745cef5090cSJed Brown 
3746cef5090cSJed Brown .keywords: TS, set, maximum, number
3747cef5090cSJed Brown 
37485ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3749cef5090cSJed Brown @*/
3750cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3751cef5090cSJed Brown {
3752cef5090cSJed Brown   PetscFunctionBegin;
3753cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3754cef5090cSJed Brown   ts->max_snes_failures = fails;
3755cef5090cSJed Brown   PetscFunctionReturn(0);
3756cef5090cSJed Brown }
3757cef5090cSJed Brown 
3758cef5090cSJed Brown #undef __FUNCT__
3759cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3760cef5090cSJed Brown /*@
3761cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3762cef5090cSJed Brown 
3763cef5090cSJed Brown    Not Collective
3764cef5090cSJed Brown 
3765cef5090cSJed Brown    Input Parameter:
3766cef5090cSJed Brown +  ts - TS context
3767cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3768cef5090cSJed Brown 
3769cef5090cSJed Brown    Options Database Key:
3770cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3771cef5090cSJed Brown 
3772cef5090cSJed Brown    Level: intermediate
3773cef5090cSJed Brown 
3774cef5090cSJed Brown .keywords: TS, set, error
3775cef5090cSJed Brown 
37765ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3777cef5090cSJed Brown @*/
3778cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3779cef5090cSJed Brown {
3780cef5090cSJed Brown   PetscFunctionBegin;
3781cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3782cef5090cSJed Brown   ts->errorifstepfailed = err;
3783cef5090cSJed Brown   PetscFunctionReturn(0);
3784cef5090cSJed Brown }
3785cef5090cSJed Brown 
3786cef5090cSJed Brown #undef __FUNCT__
3787fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3788fb1732b5SBarry Smith /*@C
3789fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3790fb1732b5SBarry Smith 
3791fb1732b5SBarry Smith    Collective on TS
3792fb1732b5SBarry Smith 
3793fb1732b5SBarry Smith    Input Parameters:
3794fb1732b5SBarry Smith +  ts - the TS context
3795fb1732b5SBarry Smith .  step - current time-step
3796fb1732b5SBarry Smith .  ptime - current time
37970910c330SBarry Smith .  u - current state
3798fb1732b5SBarry Smith -  viewer - binary viewer
3799fb1732b5SBarry Smith 
3800fb1732b5SBarry Smith    Level: intermediate
3801fb1732b5SBarry Smith 
3802fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3803fb1732b5SBarry Smith 
3804fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3805fb1732b5SBarry Smith @*/
38060910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3807fb1732b5SBarry Smith {
3808fb1732b5SBarry Smith   PetscErrorCode ierr;
3809ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3810fb1732b5SBarry Smith 
3811fb1732b5SBarry Smith   PetscFunctionBegin;
38120910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3813ed81e22dSJed Brown   PetscFunctionReturn(0);
3814ed81e22dSJed Brown }
3815ed81e22dSJed Brown 
3816ed81e22dSJed Brown #undef __FUNCT__
3817ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3818ed81e22dSJed Brown /*@C
3819ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3820ed81e22dSJed Brown 
3821ed81e22dSJed Brown    Collective on TS
3822ed81e22dSJed Brown 
3823ed81e22dSJed Brown    Input Parameters:
3824ed81e22dSJed Brown +  ts - the TS context
3825ed81e22dSJed Brown .  step - current time-step
3826ed81e22dSJed Brown .  ptime - current time
38270910c330SBarry Smith .  u - current state
3828ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3829ed81e22dSJed Brown 
3830ed81e22dSJed Brown    Level: intermediate
3831ed81e22dSJed Brown 
3832ed81e22dSJed Brown    Notes:
3833ed81e22dSJed 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.
3834ed81e22dSJed Brown    These are named according to the file name template.
3835ed81e22dSJed Brown 
3836ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3837ed81e22dSJed Brown 
3838ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3839ed81e22dSJed Brown 
3840ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3841ed81e22dSJed Brown @*/
38420910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3843ed81e22dSJed Brown {
3844ed81e22dSJed Brown   PetscErrorCode ierr;
3845ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3846ed81e22dSJed Brown   PetscViewer    viewer;
3847ed81e22dSJed Brown 
3848ed81e22dSJed Brown   PetscFunctionBegin;
38498caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3850ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
38510910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3852ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3853ed81e22dSJed Brown   PetscFunctionReturn(0);
3854ed81e22dSJed Brown }
3855ed81e22dSJed Brown 
3856ed81e22dSJed Brown #undef __FUNCT__
3857ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3858ed81e22dSJed Brown /*@C
3859ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3860ed81e22dSJed Brown 
3861ed81e22dSJed Brown    Collective on TS
3862ed81e22dSJed Brown 
3863ed81e22dSJed Brown    Input Parameters:
3864ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3865ed81e22dSJed Brown 
3866ed81e22dSJed Brown    Level: intermediate
3867ed81e22dSJed Brown 
3868ed81e22dSJed Brown    Note:
3869ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3870ed81e22dSJed Brown 
3871ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3872ed81e22dSJed Brown 
3873ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3874ed81e22dSJed Brown @*/
3875ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3876ed81e22dSJed Brown {
3877ed81e22dSJed Brown   PetscErrorCode ierr;
3878ed81e22dSJed Brown 
3879ed81e22dSJed Brown   PetscFunctionBegin;
3880ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3881fb1732b5SBarry Smith   PetscFunctionReturn(0);
3882fb1732b5SBarry Smith }
3883fb1732b5SBarry Smith 
388484df9cb4SJed Brown #undef __FUNCT__
3885ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt"
388684df9cb4SJed Brown /*@
3887ad6bc421SBarry Smith    TSGetTSAdapt - Get the adaptive controller context for the current method
388884df9cb4SJed Brown 
3889ed81e22dSJed Brown    Collective on TS if controller has not been created yet
389084df9cb4SJed Brown 
389184df9cb4SJed Brown    Input Arguments:
3892ed81e22dSJed Brown .  ts - time stepping context
389384df9cb4SJed Brown 
389484df9cb4SJed Brown    Output Arguments:
3895ed81e22dSJed Brown .  adapt - adaptive controller
389684df9cb4SJed Brown 
389784df9cb4SJed Brown    Level: intermediate
389884df9cb4SJed Brown 
3899ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
390084df9cb4SJed Brown @*/
3901ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt)
390284df9cb4SJed Brown {
390384df9cb4SJed Brown   PetscErrorCode ierr;
390484df9cb4SJed Brown 
390584df9cb4SJed Brown   PetscFunctionBegin;
390684df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
390784df9cb4SJed Brown   PetscValidPointer(adapt,2);
390884df9cb4SJed Brown   if (!ts->adapt) {
3909ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
39101c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
39111c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
391284df9cb4SJed Brown   }
391384df9cb4SJed Brown   *adapt = ts->adapt;
391484df9cb4SJed Brown   PetscFunctionReturn(0);
391584df9cb4SJed Brown }
3916d6ebe24aSShri Abhyankar 
3917d6ebe24aSShri Abhyankar #undef __FUNCT__
39181c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
39191c3436cfSJed Brown /*@
39201c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
39211c3436cfSJed Brown 
39221c3436cfSJed Brown    Logically Collective
39231c3436cfSJed Brown 
39241c3436cfSJed Brown    Input Arguments:
39251c3436cfSJed Brown +  ts - time integration context
39261c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
39270298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
39281c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
39290298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
39301c3436cfSJed Brown 
39311c3436cfSJed Brown    Level: beginner
39321c3436cfSJed Brown 
3933c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
39341c3436cfSJed Brown @*/
39351c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
39361c3436cfSJed Brown {
39371c3436cfSJed Brown   PetscErrorCode ierr;
39381c3436cfSJed Brown 
39391c3436cfSJed Brown   PetscFunctionBegin;
3940c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
39411c3436cfSJed Brown   if (vatol) {
39421c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
39431c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
3944bbd56ea5SKarl Rupp 
39451c3436cfSJed Brown     ts->vatol = vatol;
39461c3436cfSJed Brown   }
3947c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
39481c3436cfSJed Brown   if (vrtol) {
39491c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
39501c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
3951bbd56ea5SKarl Rupp 
39521c3436cfSJed Brown     ts->vrtol = vrtol;
39531c3436cfSJed Brown   }
39541c3436cfSJed Brown   PetscFunctionReturn(0);
39551c3436cfSJed Brown }
39561c3436cfSJed Brown 
39571c3436cfSJed Brown #undef __FUNCT__
3958c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3959c5033834SJed Brown /*@
3960c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3961c5033834SJed Brown 
3962c5033834SJed Brown    Logically Collective
3963c5033834SJed Brown 
3964c5033834SJed Brown    Input Arguments:
3965c5033834SJed Brown .  ts - time integration context
3966c5033834SJed Brown 
3967c5033834SJed Brown    Output Arguments:
39680298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
39690298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
39700298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
39710298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
3972c5033834SJed Brown 
3973c5033834SJed Brown    Level: beginner
3974c5033834SJed Brown 
3975c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3976c5033834SJed Brown @*/
3977c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3978c5033834SJed Brown {
3979c5033834SJed Brown   PetscFunctionBegin;
3980c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3981c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3982c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3983c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3984c5033834SJed Brown   PetscFunctionReturn(0);
3985c5033834SJed Brown }
3986c5033834SJed Brown 
3987c5033834SJed Brown #undef __FUNCT__
39881c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
39891c3436cfSJed Brown /*@
39901c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
39911c3436cfSJed Brown 
39921c3436cfSJed Brown    Collective on TS
39931c3436cfSJed Brown 
39941c3436cfSJed Brown    Input Arguments:
39951c3436cfSJed Brown +  ts - time stepping context
39961c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
39971c3436cfSJed Brown 
39981c3436cfSJed Brown    Output Arguments:
39991c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
40001c3436cfSJed Brown 
40011c3436cfSJed Brown    Level: developer
40021c3436cfSJed Brown 
40031c3436cfSJed Brown .seealso: TSSetTolerances()
40041c3436cfSJed Brown @*/
40051c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
40061c3436cfSJed Brown {
40071c3436cfSJed Brown   PetscErrorCode    ierr;
40081c3436cfSJed Brown   PetscInt          i,n,N;
40090910c330SBarry Smith   const PetscScalar *u,*y;
40100910c330SBarry Smith   Vec               U;
40111c3436cfSJed Brown   PetscReal         sum,gsum;
40121c3436cfSJed Brown 
40131c3436cfSJed Brown   PetscFunctionBegin;
40141c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40151c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
40161c3436cfSJed Brown   PetscValidPointer(norm,3);
40170910c330SBarry Smith   U = ts->vec_sol;
40180910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4019ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
40201c3436cfSJed Brown 
40210910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
40220910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
40230910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
40241c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
40251c3436cfSJed Brown   sum  = 0.;
4026ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4027ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4028ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4029ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4030ceefc113SJed Brown     for (i=0; i<n; i++) {
40310910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40320910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4033ceefc113SJed Brown     }
4034ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4035ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4036ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4037ceefc113SJed Brown     const PetscScalar *atol;
4038ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4039ceefc113SJed Brown     for (i=0; i<n; i++) {
40400910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40410910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4042ceefc113SJed Brown     }
4043ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4044ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4045ceefc113SJed Brown     const PetscScalar *rtol;
4046ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4047ceefc113SJed Brown     for (i=0; i<n; i++) {
40480910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40490910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4050ceefc113SJed Brown     }
4051ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4052ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
40531c3436cfSJed Brown     for (i=0; i<n; i++) {
40540910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40550910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
40561c3436cfSJed Brown     }
4057ceefc113SJed Brown   }
40580910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
40591c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
40601c3436cfSJed Brown 
4061ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
40621c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4063ce94432eSBarry Smith   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
40641c3436cfSJed Brown   PetscFunctionReturn(0);
40651c3436cfSJed Brown }
40661c3436cfSJed Brown 
40671c3436cfSJed Brown #undef __FUNCT__
40688d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
40698d59e960SJed Brown /*@
40708d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
40718d59e960SJed Brown 
40728d59e960SJed Brown    Logically Collective on TS
40738d59e960SJed Brown 
40748d59e960SJed Brown    Input Arguments:
40758d59e960SJed Brown +  ts - time stepping context
40768d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
40778d59e960SJed Brown 
40788d59e960SJed Brown    Note:
40798d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
40808d59e960SJed Brown 
40818d59e960SJed Brown    Level: intermediate
40828d59e960SJed Brown 
40838d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
40848d59e960SJed Brown @*/
40858d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
40868d59e960SJed Brown {
40878d59e960SJed Brown   PetscFunctionBegin;
40888d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40898d59e960SJed Brown   ts->cfltime_local = cfltime;
40908d59e960SJed Brown   ts->cfltime       = -1.;
40918d59e960SJed Brown   PetscFunctionReturn(0);
40928d59e960SJed Brown }
40938d59e960SJed Brown 
40948d59e960SJed Brown #undef __FUNCT__
40958d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
40968d59e960SJed Brown /*@
40978d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
40988d59e960SJed Brown 
40998d59e960SJed Brown    Collective on TS
41008d59e960SJed Brown 
41018d59e960SJed Brown    Input Arguments:
41028d59e960SJed Brown .  ts - time stepping context
41038d59e960SJed Brown 
41048d59e960SJed Brown    Output Arguments:
41058d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
41068d59e960SJed Brown 
41078d59e960SJed Brown    Level: advanced
41088d59e960SJed Brown 
41098d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
41108d59e960SJed Brown @*/
41118d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
41128d59e960SJed Brown {
41138d59e960SJed Brown   PetscErrorCode ierr;
41148d59e960SJed Brown 
41158d59e960SJed Brown   PetscFunctionBegin;
41168d59e960SJed Brown   if (ts->cfltime < 0) {
4117ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
41188d59e960SJed Brown   }
41198d59e960SJed Brown   *cfltime = ts->cfltime;
41208d59e960SJed Brown   PetscFunctionReturn(0);
41218d59e960SJed Brown }
41228d59e960SJed Brown 
41238d59e960SJed Brown #undef __FUNCT__
4124d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4125d6ebe24aSShri Abhyankar /*@
4126d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4127d6ebe24aSShri Abhyankar 
4128d6ebe24aSShri Abhyankar    Input Parameters:
4129d6ebe24aSShri Abhyankar .  ts   - the TS context.
4130d6ebe24aSShri Abhyankar .  xl   - lower bound.
4131d6ebe24aSShri Abhyankar .  xu   - upper bound.
4132d6ebe24aSShri Abhyankar 
4133d6ebe24aSShri Abhyankar    Notes:
4134d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
413533c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4136d6ebe24aSShri Abhyankar 
41372bd2b0e6SSatish Balay    Level: advanced
41382bd2b0e6SSatish Balay 
4139d6ebe24aSShri Abhyankar @*/
4140d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4141d6ebe24aSShri Abhyankar {
4142d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4143d6ebe24aSShri Abhyankar   SNES           snes;
4144d6ebe24aSShri Abhyankar 
4145d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4146d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4147d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4148d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4149d6ebe24aSShri Abhyankar }
4150d6ebe24aSShri Abhyankar 
4151325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4152c6db04a5SJed Brown #include <mex.h>
4153325fc9f4SBarry Smith 
4154325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4155325fc9f4SBarry Smith 
4156325fc9f4SBarry Smith #undef __FUNCT__
4157325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4158325fc9f4SBarry Smith /*
4159325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4160325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4161325fc9f4SBarry Smith 
4162325fc9f4SBarry Smith    Collective on TS
4163325fc9f4SBarry Smith 
4164325fc9f4SBarry Smith    Input Parameters:
4165325fc9f4SBarry Smith +  snes - the TS context
41660910c330SBarry Smith -  u - input vector
4167325fc9f4SBarry Smith 
4168325fc9f4SBarry Smith    Output Parameter:
4169325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4170325fc9f4SBarry Smith 
4171325fc9f4SBarry Smith    Notes:
4172325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4173325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4174325fc9f4SBarry Smith    themselves.
4175325fc9f4SBarry Smith 
4176325fc9f4SBarry Smith    Level: developer
4177325fc9f4SBarry Smith 
4178325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4179325fc9f4SBarry Smith 
4180325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4181325fc9f4SBarry Smith */
41820910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4183325fc9f4SBarry Smith {
4184325fc9f4SBarry Smith   PetscErrorCode  ierr;
4185325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4186325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4187325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4188325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4189325fc9f4SBarry Smith 
4190325fc9f4SBarry Smith   PetscFunctionBegin;
4191325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
41920910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
41930910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4194325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
41950910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4196325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4197325fc9f4SBarry Smith 
4198325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
41990910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
42000910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
42010910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4202bbd56ea5SKarl Rupp 
4203325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4204325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4205325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4206325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4207325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4208325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4209325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4210325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4211325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4212325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4213325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4214325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4215325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4216325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4217325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4218325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4219325fc9f4SBarry Smith   PetscFunctionReturn(0);
4220325fc9f4SBarry Smith }
4221325fc9f4SBarry Smith 
4222325fc9f4SBarry Smith 
4223325fc9f4SBarry Smith #undef __FUNCT__
4224325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4225325fc9f4SBarry Smith /*
4226325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4227325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4228e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4229325fc9f4SBarry Smith 
4230325fc9f4SBarry Smith    Logically Collective on TS
4231325fc9f4SBarry Smith 
4232325fc9f4SBarry Smith    Input Parameters:
4233325fc9f4SBarry Smith +  ts - the TS context
4234325fc9f4SBarry Smith -  func - function evaluation routine
4235325fc9f4SBarry Smith 
4236325fc9f4SBarry Smith    Calling sequence of func:
42370910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4238325fc9f4SBarry Smith 
4239325fc9f4SBarry Smith    Level: beginner
4240325fc9f4SBarry Smith 
4241325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4242325fc9f4SBarry Smith 
4243325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4244325fc9f4SBarry Smith */
4245cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4246325fc9f4SBarry Smith {
4247325fc9f4SBarry Smith   PetscErrorCode  ierr;
4248325fc9f4SBarry Smith   TSMatlabContext *sctx;
4249325fc9f4SBarry Smith 
4250325fc9f4SBarry Smith   PetscFunctionBegin;
4251325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4252325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4253325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4254325fc9f4SBarry Smith   /*
4255325fc9f4SBarry Smith      This should work, but it doesn't
4256325fc9f4SBarry Smith   sctx->ctx = ctx;
4257325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4258325fc9f4SBarry Smith   */
4259325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4260bbd56ea5SKarl Rupp 
42610298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4262325fc9f4SBarry Smith   PetscFunctionReturn(0);
4263325fc9f4SBarry Smith }
4264325fc9f4SBarry Smith 
4265325fc9f4SBarry Smith #undef __FUNCT__
4266325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4267325fc9f4SBarry Smith /*
4268325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4269325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4270325fc9f4SBarry Smith 
4271325fc9f4SBarry Smith    Collective on TS
4272325fc9f4SBarry Smith 
4273325fc9f4SBarry Smith    Input Parameters:
4274cdcf91faSSean Farley +  ts - the TS context
42750910c330SBarry Smith .  u - input vector
4276325fc9f4SBarry Smith .  A, B - the matrices
4277325fc9f4SBarry Smith -  ctx - user context
4278325fc9f4SBarry Smith 
4279325fc9f4SBarry Smith    Output Parameter:
4280325fc9f4SBarry Smith .  flag - structure of the matrix
4281325fc9f4SBarry Smith 
4282325fc9f4SBarry Smith    Level: developer
4283325fc9f4SBarry Smith 
4284325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4285325fc9f4SBarry Smith 
4286325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4287325fc9f4SBarry Smith @*/
42880910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4289325fc9f4SBarry Smith {
4290325fc9f4SBarry Smith   PetscErrorCode  ierr;
4291325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4292325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4293325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4294325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4295325fc9f4SBarry Smith 
4296325fc9f4SBarry Smith   PetscFunctionBegin;
4297cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42980910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4299325fc9f4SBarry Smith 
43000910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4301325fc9f4SBarry Smith 
4302cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
43030910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
43040910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
43050910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
43060910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4307bbd56ea5SKarl Rupp 
4308325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4309325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4310325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4311325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4312325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4313325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4314325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4315325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4316325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4317325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4318325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4319325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4320325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4321325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4322325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4323325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4324325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4325325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4326325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4327325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4328325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4329325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4330325fc9f4SBarry Smith   PetscFunctionReturn(0);
4331325fc9f4SBarry Smith }
4332325fc9f4SBarry Smith 
4333325fc9f4SBarry Smith 
4334325fc9f4SBarry Smith #undef __FUNCT__
4335325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4336325fc9f4SBarry Smith /*
4337325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4338e3c5b3baSBarry 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
4339325fc9f4SBarry Smith 
4340325fc9f4SBarry Smith    Logically Collective on TS
4341325fc9f4SBarry Smith 
4342325fc9f4SBarry Smith    Input Parameters:
4343cdcf91faSSean Farley +  ts - the TS context
4344325fc9f4SBarry Smith .  A,B - Jacobian matrices
4345325fc9f4SBarry Smith .  func - function evaluation routine
4346325fc9f4SBarry Smith -  ctx - user context
4347325fc9f4SBarry Smith 
4348325fc9f4SBarry Smith    Calling sequence of func:
43490910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4350325fc9f4SBarry Smith 
4351325fc9f4SBarry Smith 
4352325fc9f4SBarry Smith    Level: developer
4353325fc9f4SBarry Smith 
4354325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4355325fc9f4SBarry Smith 
4356325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4357325fc9f4SBarry Smith */
4358cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4359325fc9f4SBarry Smith {
4360325fc9f4SBarry Smith   PetscErrorCode  ierr;
4361325fc9f4SBarry Smith   TSMatlabContext *sctx;
4362325fc9f4SBarry Smith 
4363325fc9f4SBarry Smith   PetscFunctionBegin;
4364325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4365325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4366325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4367325fc9f4SBarry Smith   /*
4368325fc9f4SBarry Smith      This should work, but it doesn't
4369325fc9f4SBarry Smith   sctx->ctx = ctx;
4370325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4371325fc9f4SBarry Smith   */
4372325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4373bbd56ea5SKarl Rupp 
4374cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4375325fc9f4SBarry Smith   PetscFunctionReturn(0);
4376325fc9f4SBarry Smith }
4377325fc9f4SBarry Smith 
4378b5b1a830SBarry Smith #undef __FUNCT__
4379b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4380b5b1a830SBarry Smith /*
4381b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4382b5b1a830SBarry Smith 
4383b5b1a830SBarry Smith    Collective on TS
4384b5b1a830SBarry Smith 
4385b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4386b5b1a830SBarry Smith @*/
43870910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4388b5b1a830SBarry Smith {
4389b5b1a830SBarry Smith   PetscErrorCode  ierr;
4390b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4391a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4392b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4393b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4394b5b1a830SBarry Smith 
4395b5b1a830SBarry Smith   PetscFunctionBegin;
4396cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43970910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4398b5b1a830SBarry Smith 
4399cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
44000910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4401bbd56ea5SKarl Rupp 
4402b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4403b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4404b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4405b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4406b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4407b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4408b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4409b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4410b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4411b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4412b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4413b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4414b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4415b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4416b5b1a830SBarry Smith   PetscFunctionReturn(0);
4417b5b1a830SBarry Smith }
4418b5b1a830SBarry Smith 
4419b5b1a830SBarry Smith 
4420b5b1a830SBarry Smith #undef __FUNCT__
4421b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4422b5b1a830SBarry Smith /*
4423b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4424b5b1a830SBarry Smith 
4425b5b1a830SBarry Smith    Level: developer
4426b5b1a830SBarry Smith 
4427b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4428b5b1a830SBarry Smith 
4429b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4430b5b1a830SBarry Smith */
4431cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4432b5b1a830SBarry Smith {
4433b5b1a830SBarry Smith   PetscErrorCode  ierr;
4434b5b1a830SBarry Smith   TSMatlabContext *sctx;
4435b5b1a830SBarry Smith 
4436b5b1a830SBarry Smith   PetscFunctionBegin;
4437b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4438b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4439b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4440b5b1a830SBarry Smith   /*
4441b5b1a830SBarry Smith      This should work, but it doesn't
4442b5b1a830SBarry Smith   sctx->ctx = ctx;
4443b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4444b5b1a830SBarry Smith   */
4445b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4446bbd56ea5SKarl Rupp 
44470298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
4448b5b1a830SBarry Smith   PetscFunctionReturn(0);
4449b5b1a830SBarry Smith }
4450325fc9f4SBarry Smith #endif
4451b3603a34SBarry Smith 
44520b039ecaSBarry Smith 
44530b039ecaSBarry Smith 
4454b3603a34SBarry Smith #undef __FUNCT__
44554f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4456b3603a34SBarry Smith /*@C
44574f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4458b3603a34SBarry Smith        in a time based line graph
4459b3603a34SBarry Smith 
4460b3603a34SBarry Smith    Collective on TS
4461b3603a34SBarry Smith 
4462b3603a34SBarry Smith    Input Parameters:
4463b3603a34SBarry Smith +  ts - the TS context
4464b3603a34SBarry Smith .  step - current time-step
4465b3603a34SBarry Smith .  ptime - current time
4466b3603a34SBarry Smith -  lg - a line graph object
4467b3603a34SBarry Smith 
4468b3603a34SBarry Smith    Level: intermediate
4469b3603a34SBarry Smith 
44700b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4471b3603a34SBarry Smith 
4472b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4473b3603a34SBarry Smith 
4474b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4475b3603a34SBarry Smith @*/
44760910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4477b3603a34SBarry Smith {
4478b3603a34SBarry Smith   PetscErrorCode    ierr;
44790b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4480b3603a34SBarry Smith   const PetscScalar *yy;
448158ff32f7SBarry Smith   PetscInt          dim;
4482b3603a34SBarry Smith 
4483b3603a34SBarry Smith   PetscFunctionBegin;
448458ff32f7SBarry Smith   if (!step) {
4485a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4486a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4487a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
44880910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
44890b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
44900b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
449158ff32f7SBarry Smith   }
44920910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4493e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4494e3efe391SJed Brown   {
4495e3efe391SJed Brown     PetscReal *yreal;
4496e3efe391SJed Brown     PetscInt  i,n;
44970910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4498e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4499e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
45000b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4501e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4502e3efe391SJed Brown   }
4503e3efe391SJed Brown #else
45040b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4505e3efe391SJed Brown #endif
45060910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
45073fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
45080b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
45093923b477SBarry Smith   }
4510b3603a34SBarry Smith   PetscFunctionReturn(0);
4511b3603a34SBarry Smith }
4512b3603a34SBarry Smith 
4513b3603a34SBarry Smith #undef __FUNCT__
45144f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4515ef20d060SBarry Smith /*@C
45164f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4517ef20d060SBarry Smith        in a time based line graph
4518ef20d060SBarry Smith 
4519ef20d060SBarry Smith    Collective on TS
4520ef20d060SBarry Smith 
4521ef20d060SBarry Smith    Input Parameters:
4522ef20d060SBarry Smith +  ts - the TS context
4523ef20d060SBarry Smith .  step - current time-step
4524ef20d060SBarry Smith .  ptime - current time
4525ef20d060SBarry Smith -  lg - a line graph object
4526ef20d060SBarry Smith 
4527ef20d060SBarry Smith    Level: intermediate
4528ef20d060SBarry Smith 
4529abd5a294SJed Brown    Notes:
4530abd5a294SJed Brown    Only for sequential solves.
4531abd5a294SJed Brown 
4532abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4533abd5a294SJed Brown 
4534abd5a294SJed Brown    Options Database Keys:
45354f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4536ef20d060SBarry Smith 
4537ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4538ef20d060SBarry Smith 
4539abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4540ef20d060SBarry Smith @*/
45410910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4542ef20d060SBarry Smith {
4543ef20d060SBarry Smith   PetscErrorCode    ierr;
45440b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4545ef20d060SBarry Smith   const PetscScalar *yy;
4546ef20d060SBarry Smith   Vec               y;
4547a9f9c1f6SBarry Smith   PetscInt          dim;
4548ef20d060SBarry Smith 
4549ef20d060SBarry Smith   PetscFunctionBegin;
4550a9f9c1f6SBarry Smith   if (!step) {
4551a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4552a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
45531ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
45540910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4555a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4556a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4557a9f9c1f6SBarry Smith   }
45580910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4559ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
45600910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4561ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4562e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4563e3efe391SJed Brown   {
4564e3efe391SJed Brown     PetscReal *yreal;
4565e3efe391SJed Brown     PetscInt  i,n;
4566e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4567e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4568e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
45690b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4570e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4571e3efe391SJed Brown   }
4572e3efe391SJed Brown #else
45730b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4574e3efe391SJed Brown #endif
4575ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4576ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
45773fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
45780b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
45793923b477SBarry Smith   }
4580ef20d060SBarry Smith   PetscFunctionReturn(0);
4581ef20d060SBarry Smith }
4582ef20d060SBarry Smith 
4583201da799SBarry Smith #undef __FUNCT__
4584201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4585201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4586201da799SBarry Smith {
4587201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4588201da799SBarry Smith   PetscReal      x   = ptime,y;
4589201da799SBarry Smith   PetscErrorCode ierr;
4590201da799SBarry Smith   PetscInt       its;
4591201da799SBarry Smith 
4592201da799SBarry Smith   PetscFunctionBegin;
4593201da799SBarry Smith   if (!n) {
4594201da799SBarry Smith     PetscDrawAxis axis;
4595bbd56ea5SKarl Rupp 
4596201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4597201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4598201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4599bbd56ea5SKarl Rupp 
4600201da799SBarry Smith     ctx->snes_its = 0;
4601201da799SBarry Smith   }
4602201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4603201da799SBarry Smith   y    = its - ctx->snes_its;
4604201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
46053fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4606201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4607201da799SBarry Smith   }
4608201da799SBarry Smith   ctx->snes_its = its;
4609201da799SBarry Smith   PetscFunctionReturn(0);
4610201da799SBarry Smith }
4611201da799SBarry Smith 
4612201da799SBarry Smith #undef __FUNCT__
4613201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4614201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4615201da799SBarry Smith {
4616201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4617201da799SBarry Smith   PetscReal      x   = ptime,y;
4618201da799SBarry Smith   PetscErrorCode ierr;
4619201da799SBarry Smith   PetscInt       its;
4620201da799SBarry Smith 
4621201da799SBarry Smith   PetscFunctionBegin;
4622201da799SBarry Smith   if (!n) {
4623201da799SBarry Smith     PetscDrawAxis axis;
4624bbd56ea5SKarl Rupp 
4625201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4626201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4627201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4628bbd56ea5SKarl Rupp 
4629201da799SBarry Smith     ctx->ksp_its = 0;
4630201da799SBarry Smith   }
4631201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4632201da799SBarry Smith   y    = its - ctx->ksp_its;
4633201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
463499fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4635201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4636201da799SBarry Smith   }
4637201da799SBarry Smith   ctx->ksp_its = its;
4638201da799SBarry Smith   PetscFunctionReturn(0);
4639201da799SBarry Smith }
4640f9c1d6abSBarry Smith 
4641f9c1d6abSBarry Smith #undef __FUNCT__
4642f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4643f9c1d6abSBarry Smith /*@
4644f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4645f9c1d6abSBarry Smith 
4646f9c1d6abSBarry Smith    Collective on TS and Vec
4647f9c1d6abSBarry Smith 
4648f9c1d6abSBarry Smith    Input Parameters:
4649f9c1d6abSBarry Smith +  ts - the TS context
4650f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4651f9c1d6abSBarry Smith 
4652f9c1d6abSBarry Smith    Output Parameters:
4653f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4654f9c1d6abSBarry Smith 
4655f9c1d6abSBarry Smith    Level: developer
4656f9c1d6abSBarry Smith 
4657f9c1d6abSBarry Smith .keywords: TS, compute
4658f9c1d6abSBarry Smith 
4659f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4660f9c1d6abSBarry Smith @*/
4661f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4662f9c1d6abSBarry Smith {
4663f9c1d6abSBarry Smith   PetscErrorCode ierr;
4664f9c1d6abSBarry Smith 
4665f9c1d6abSBarry Smith   PetscFunctionBegin;
4666f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4667ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4668f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4669f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4670f9c1d6abSBarry Smith }
4671