xref: /petsc/src/ts/interface/ts.c (revision ce94432eddcd14845bc7e8083b7f8ea723b9bf7d)
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) {
120*ce94432eSBarry 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);
132*ce94432eSBarry 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);
187*ce94432eSBarry 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);
197*ce94432eSBarry 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]) {
205*ce94432eSBarry 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 {
208*ce94432eSBarry 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;
217*ce94432eSBarry 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);
220*ce94432eSBarry 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);
223*ce94432eSBarry 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 
238*ce94432eSBarry 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;
241*ce94432eSBarry 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);
248*ce94432eSBarry 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 
328*ce94432eSBarry 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 
393*ce94432eSBarry 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 
585*ce94432eSBarry 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 
678*ce94432eSBarry 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);
1199*ce94432eSBarry 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   }
1205*ce94432eSBarry 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 
121555849f57SBarry Smith #undef __FUNCT__
12164a2ae208SSatish Balay #define __FUNCT__ "TSView"
12177e2c5f70SBarry Smith /*@C
1218d763cef2SBarry Smith     TSView - Prints the TS data structure.
1219d763cef2SBarry Smith 
12204c49b128SBarry Smith     Collective on TS
1221d763cef2SBarry Smith 
1222d763cef2SBarry Smith     Input Parameters:
1223d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1224d763cef2SBarry Smith -   viewer - visualization context
1225d763cef2SBarry Smith 
1226d763cef2SBarry Smith     Options Database Key:
1227d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1228d763cef2SBarry Smith 
1229d763cef2SBarry Smith     Notes:
1230d763cef2SBarry Smith     The available visualization contexts include
1231b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1232b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1233d763cef2SBarry Smith          output where only the first processor opens
1234d763cef2SBarry Smith          the file.  All other processors send their
1235d763cef2SBarry Smith          data to the first processor to print.
1236d763cef2SBarry Smith 
1237d763cef2SBarry Smith     The user can open an alternative visualization context with
1238b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1239d763cef2SBarry Smith 
1240d763cef2SBarry Smith     Level: beginner
1241d763cef2SBarry Smith 
1242d763cef2SBarry Smith .keywords: TS, timestep, view
1243d763cef2SBarry Smith 
1244b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1245d763cef2SBarry Smith @*/
12467087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1247d763cef2SBarry Smith {
1248dfbe8321SBarry Smith   PetscErrorCode ierr;
124919fd82e9SBarry Smith   TSType         type;
12502b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
12512d53ad75SBarry Smith   DMTS           sdm;
1252d763cef2SBarry Smith 
1253d763cef2SBarry Smith   PetscFunctionBegin;
12540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12553050cee2SBarry Smith   if (!viewer) {
1256*ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
12573050cee2SBarry Smith   }
12580700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1259c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1260fd16b177SBarry Smith 
1261251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1262251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
126355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
12642b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
126532077d6dSBarry Smith   if (iascii) {
1266317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
126777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1268a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1269d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
12705ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1271c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1272d763cef2SBarry Smith     }
12735ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1274193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
12752d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12762d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1277d52bd9f3SBarry Smith     if (ts->ops->view) {
1278d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1279d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1280d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1281d52bd9f3SBarry Smith     }
12820f5bd95cSBarry Smith   } else if (isstring) {
1283a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1284b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
128555849f57SBarry Smith   } else if (isbinary) {
128655849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
128755849f57SBarry Smith     MPI_Comm    comm;
128855849f57SBarry Smith     PetscMPIInt rank;
128955849f57SBarry Smith     char        type[256];
129055849f57SBarry Smith 
129155849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
129255849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
129355849f57SBarry Smith     if (!rank) {
129455849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
129555849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
129655849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
129755849f57SBarry Smith     }
129855849f57SBarry Smith     if (ts->ops->view) {
129955849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
130055849f57SBarry Smith     }
1301f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1302f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
13032d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13042d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
13052b0a91c0SBarry Smith   } else if (isdraw) {
13062b0a91c0SBarry Smith     PetscDraw draw;
13072b0a91c0SBarry Smith     char      str[36];
130889fd9fafSBarry Smith     PetscReal x,y,bottom,h;
13092b0a91c0SBarry Smith 
13102b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
13112b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
13122b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
13132b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
13140298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
131589fd9fafSBarry Smith     bottom = y - h;
13162b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
13172b0a91c0SBarry Smith     if (ts->ops->view) {
13182b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
13192b0a91c0SBarry Smith     }
13202b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1321d763cef2SBarry Smith   }
1322b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1323251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1324b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1325d763cef2SBarry Smith   PetscFunctionReturn(0);
1326d763cef2SBarry Smith }
1327d763cef2SBarry Smith 
1328d763cef2SBarry Smith 
13294a2ae208SSatish Balay #undef __FUNCT__
13304a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1331b07ff414SBarry Smith /*@
1332d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1333d763cef2SBarry Smith    the timesteppers.
1334d763cef2SBarry Smith 
13353f9fe445SBarry Smith    Logically Collective on TS
1336d763cef2SBarry Smith 
1337d763cef2SBarry Smith    Input Parameters:
1338d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1339d763cef2SBarry Smith -  usrP - optional user context
1340d763cef2SBarry Smith 
1341d763cef2SBarry Smith    Level: intermediate
1342d763cef2SBarry Smith 
1343d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1344d763cef2SBarry Smith 
1345d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1346d763cef2SBarry Smith @*/
13477087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1348d763cef2SBarry Smith {
1349d763cef2SBarry Smith   PetscFunctionBegin;
13500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1351d763cef2SBarry Smith   ts->user = usrP;
1352d763cef2SBarry Smith   PetscFunctionReturn(0);
1353d763cef2SBarry Smith }
1354d763cef2SBarry Smith 
13554a2ae208SSatish Balay #undef __FUNCT__
13564a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1357b07ff414SBarry Smith /*@
1358d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1359d763cef2SBarry Smith     timestepper.
1360d763cef2SBarry Smith 
1361d763cef2SBarry Smith     Not Collective
1362d763cef2SBarry Smith 
1363d763cef2SBarry Smith     Input Parameter:
1364d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1365d763cef2SBarry Smith 
1366d763cef2SBarry Smith     Output Parameter:
1367d763cef2SBarry Smith .   usrP - user context
1368d763cef2SBarry Smith 
1369d763cef2SBarry Smith     Level: intermediate
1370d763cef2SBarry Smith 
1371d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1372d763cef2SBarry Smith 
1373d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1374d763cef2SBarry Smith @*/
1375e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1376d763cef2SBarry Smith {
1377d763cef2SBarry Smith   PetscFunctionBegin;
13780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1379e71120c6SJed Brown   *(void**)usrP = ts->user;
1380d763cef2SBarry Smith   PetscFunctionReturn(0);
1381d763cef2SBarry Smith }
1382d763cef2SBarry Smith 
13834a2ae208SSatish Balay #undef __FUNCT__
13844a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1385d763cef2SBarry Smith /*@
1386b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1387d763cef2SBarry Smith 
1388d763cef2SBarry Smith    Not Collective
1389d763cef2SBarry Smith 
1390d763cef2SBarry Smith    Input Parameter:
1391d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1392d763cef2SBarry Smith 
1393d763cef2SBarry Smith    Output Parameter:
1394b8123daeSJed Brown .  iter - number of steps completed so far
1395d763cef2SBarry Smith 
1396d763cef2SBarry Smith    Level: intermediate
1397d763cef2SBarry Smith 
1398d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1399b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1400d763cef2SBarry Smith @*/
14017087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1402d763cef2SBarry Smith {
1403d763cef2SBarry Smith   PetscFunctionBegin;
14040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14054482741eSBarry Smith   PetscValidIntPointer(iter,2);
1406d763cef2SBarry Smith   *iter = ts->steps;
1407d763cef2SBarry Smith   PetscFunctionReturn(0);
1408d763cef2SBarry Smith }
1409d763cef2SBarry Smith 
14104a2ae208SSatish Balay #undef __FUNCT__
14114a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1412d763cef2SBarry Smith /*@
1413d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1414d763cef2SBarry Smith    as well as the initial time.
1415d763cef2SBarry Smith 
14163f9fe445SBarry Smith    Logically Collective on TS
1417d763cef2SBarry Smith 
1418d763cef2SBarry Smith    Input Parameters:
1419d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1420d763cef2SBarry Smith .  initial_time - the initial time
1421d763cef2SBarry Smith -  time_step - the size of the timestep
1422d763cef2SBarry Smith 
1423d763cef2SBarry Smith    Level: intermediate
1424d763cef2SBarry Smith 
1425d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1426d763cef2SBarry Smith 
1427d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1428d763cef2SBarry Smith @*/
14297087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1430d763cef2SBarry Smith {
1431e144a568SJed Brown   PetscErrorCode ierr;
1432e144a568SJed Brown 
1433d763cef2SBarry Smith   PetscFunctionBegin;
14340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1435e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1436d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1437d763cef2SBarry Smith   PetscFunctionReturn(0);
1438d763cef2SBarry Smith }
1439d763cef2SBarry Smith 
14404a2ae208SSatish Balay #undef __FUNCT__
14414a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1442d763cef2SBarry Smith /*@
1443d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1444d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1445d763cef2SBarry Smith 
14463f9fe445SBarry Smith    Logically Collective on TS
1447d763cef2SBarry Smith 
1448d763cef2SBarry Smith    Input Parameters:
1449d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1450d763cef2SBarry Smith -  time_step - the size of the timestep
1451d763cef2SBarry Smith 
1452d763cef2SBarry Smith    Level: intermediate
1453d763cef2SBarry Smith 
1454d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1455d763cef2SBarry Smith 
1456d763cef2SBarry Smith .keywords: TS, set, timestep
1457d763cef2SBarry Smith @*/
14587087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1459d763cef2SBarry Smith {
1460d763cef2SBarry Smith   PetscFunctionBegin;
14610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1462c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1463d763cef2SBarry Smith   ts->time_step      = time_step;
146431748224SBarry Smith   ts->time_step_orig = time_step;
1465d763cef2SBarry Smith   PetscFunctionReturn(0);
1466d763cef2SBarry Smith }
1467d763cef2SBarry Smith 
14684a2ae208SSatish Balay #undef __FUNCT__
1469a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1470a43b19c4SJed Brown /*@
147149354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
147249354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
147349354f04SShri Abhyankar      or just return at the final time TS computed.
1474a43b19c4SJed Brown 
1475a43b19c4SJed Brown   Logically Collective on TS
1476a43b19c4SJed Brown 
1477a43b19c4SJed Brown    Input Parameter:
1478a43b19c4SJed Brown +   ts - the time-step context
147949354f04SShri Abhyankar -   eftopt - exact final time option
1480a43b19c4SJed Brown 
1481a43b19c4SJed Brown    Level: beginner
1482a43b19c4SJed Brown 
1483a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1484a43b19c4SJed Brown @*/
148549354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1486a43b19c4SJed Brown {
1487a43b19c4SJed Brown   PetscFunctionBegin;
1488a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
148949354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
149049354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1491a43b19c4SJed Brown   PetscFunctionReturn(0);
1492a43b19c4SJed Brown }
1493a43b19c4SJed Brown 
1494a43b19c4SJed Brown #undef __FUNCT__
14954a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1496d763cef2SBarry Smith /*@
1497d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1498d763cef2SBarry Smith 
1499d763cef2SBarry Smith    Not Collective
1500d763cef2SBarry Smith 
1501d763cef2SBarry Smith    Input Parameter:
1502d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1503d763cef2SBarry Smith 
1504d763cef2SBarry Smith    Output Parameter:
1505d763cef2SBarry Smith .  dt - the current timestep size
1506d763cef2SBarry Smith 
1507d763cef2SBarry Smith    Level: intermediate
1508d763cef2SBarry Smith 
1509d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1510d763cef2SBarry Smith 
1511d763cef2SBarry Smith .keywords: TS, get, timestep
1512d763cef2SBarry Smith @*/
15137087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1514d763cef2SBarry Smith {
1515d763cef2SBarry Smith   PetscFunctionBegin;
15160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1517f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1518d763cef2SBarry Smith   *dt = ts->time_step;
1519d763cef2SBarry Smith   PetscFunctionReturn(0);
1520d763cef2SBarry Smith }
1521d763cef2SBarry Smith 
15224a2ae208SSatish Balay #undef __FUNCT__
15234a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1524d8e5e3e6SSatish Balay /*@
1525d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1526d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1527d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1528d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1529d763cef2SBarry Smith 
1530d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1531d763cef2SBarry Smith 
1532d763cef2SBarry Smith    Input Parameter:
1533d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1534d763cef2SBarry Smith 
1535d763cef2SBarry Smith    Output Parameter:
1536d763cef2SBarry Smith .  v - the vector containing the solution
1537d763cef2SBarry Smith 
1538d763cef2SBarry Smith    Level: intermediate
1539d763cef2SBarry Smith 
1540d763cef2SBarry Smith .seealso: TSGetTimeStep()
1541d763cef2SBarry Smith 
1542d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1543d763cef2SBarry Smith @*/
15447087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1545d763cef2SBarry Smith {
1546d763cef2SBarry Smith   PetscFunctionBegin;
15470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15484482741eSBarry Smith   PetscValidPointer(v,2);
15498737fe31SLisandro Dalcin   *v = ts->vec_sol;
1550d763cef2SBarry Smith   PetscFunctionReturn(0);
1551d763cef2SBarry Smith }
1552d763cef2SBarry Smith 
1553bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
15544a2ae208SSatish Balay #undef __FUNCT__
1555bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1556d8e5e3e6SSatish Balay /*@
1557bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1558d763cef2SBarry Smith 
1559bdad233fSMatthew Knepley   Not collective
1560d763cef2SBarry Smith 
1561bdad233fSMatthew Knepley   Input Parameters:
1562bdad233fSMatthew Knepley + ts   - The TS
1563bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1564d763cef2SBarry Smith .vb
15650910c330SBarry Smith          U_t - A U = 0      (linear)
15660910c330SBarry Smith          U_t - A(t) U = 0   (linear)
15670910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1568d763cef2SBarry Smith .ve
1569d763cef2SBarry Smith 
1570d763cef2SBarry Smith    Level: beginner
1571d763cef2SBarry Smith 
1572bdad233fSMatthew Knepley .keywords: TS, problem type
1573bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1574d763cef2SBarry Smith @*/
15757087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1576a7cc72afSBarry Smith {
15779e2a6581SJed Brown   PetscErrorCode ierr;
15789e2a6581SJed Brown 
1579d763cef2SBarry Smith   PetscFunctionBegin;
15800700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1581bdad233fSMatthew Knepley   ts->problem_type = type;
15829e2a6581SJed Brown   if (type == TS_LINEAR) {
15839e2a6581SJed Brown     SNES snes;
15849e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
15859e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
15869e2a6581SJed Brown   }
1587d763cef2SBarry Smith   PetscFunctionReturn(0);
1588d763cef2SBarry Smith }
1589d763cef2SBarry Smith 
1590bdad233fSMatthew Knepley #undef __FUNCT__
1591bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1592bdad233fSMatthew Knepley /*@C
1593bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1594bdad233fSMatthew Knepley 
1595bdad233fSMatthew Knepley   Not collective
1596bdad233fSMatthew Knepley 
1597bdad233fSMatthew Knepley   Input Parameter:
1598bdad233fSMatthew Knepley . ts   - The TS
1599bdad233fSMatthew Knepley 
1600bdad233fSMatthew Knepley   Output Parameter:
1601bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1602bdad233fSMatthew Knepley .vb
1603089b2837SJed Brown          M U_t = A U
1604089b2837SJed Brown          M(t) U_t = A(t) U
1605b5abc632SBarry Smith          F(t,U,U_t)
1606bdad233fSMatthew Knepley .ve
1607bdad233fSMatthew Knepley 
1608bdad233fSMatthew Knepley    Level: beginner
1609bdad233fSMatthew Knepley 
1610bdad233fSMatthew Knepley .keywords: TS, problem type
1611bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1612bdad233fSMatthew Knepley @*/
16137087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1614a7cc72afSBarry Smith {
1615bdad233fSMatthew Knepley   PetscFunctionBegin;
16160700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
16174482741eSBarry Smith   PetscValidIntPointer(type,2);
1618bdad233fSMatthew Knepley   *type = ts->problem_type;
1619bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1620bdad233fSMatthew Knepley }
1621d763cef2SBarry Smith 
16224a2ae208SSatish Balay #undef __FUNCT__
16234a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1624d763cef2SBarry Smith /*@
1625d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1626d763cef2SBarry Smith    of a timestepper.
1627d763cef2SBarry Smith 
1628d763cef2SBarry Smith    Collective on TS
1629d763cef2SBarry Smith 
1630d763cef2SBarry Smith    Input Parameter:
1631d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1632d763cef2SBarry Smith 
1633d763cef2SBarry Smith    Notes:
1634d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1635d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1636d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1637d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1638d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1639d763cef2SBarry Smith 
1640d763cef2SBarry Smith    Level: advanced
1641d763cef2SBarry Smith 
1642d763cef2SBarry Smith .keywords: TS, timestep, setup
1643d763cef2SBarry Smith 
1644d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1645d763cef2SBarry Smith @*/
16467087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1647d763cef2SBarry Smith {
1648dfbe8321SBarry Smith   PetscErrorCode ierr;
16496c6b9e74SPeter Brune   DM             dm;
16506c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
16516c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
16526c6b9e74SPeter Brune   TSIJacobian    ijac;
16536c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1654d763cef2SBarry Smith 
1655d763cef2SBarry Smith   PetscFunctionBegin;
16560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1657277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1658277b19d0SLisandro Dalcin 
16597adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
16609596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1661d763cef2SBarry Smith   }
1662277b19d0SLisandro Dalcin 
1663277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1664277b19d0SLisandro Dalcin 
1665ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr);
16661c3436cfSJed Brown 
1667277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1668000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1669277b19d0SLisandro Dalcin   }
1670277b19d0SLisandro Dalcin 
16716c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
16726c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
16736c6b9e74SPeter Brune    */
16746c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
16750298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
16766c6b9e74SPeter Brune   if (!func) {
16776c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
16786c6b9e74SPeter Brune   }
16796c6b9e74SPeter 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.
16806c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
16816c6b9e74SPeter Brune    */
16820298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
16830298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
16840298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
16856c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
16866c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
16876c6b9e74SPeter Brune   }
1688277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1689277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1690277b19d0SLisandro Dalcin }
1691277b19d0SLisandro Dalcin 
1692277b19d0SLisandro Dalcin #undef __FUNCT__
1693277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1694277b19d0SLisandro Dalcin /*@
1695277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1696277b19d0SLisandro Dalcin 
1697277b19d0SLisandro Dalcin    Collective on TS
1698277b19d0SLisandro Dalcin 
1699277b19d0SLisandro Dalcin    Input Parameter:
1700277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1701277b19d0SLisandro Dalcin 
1702277b19d0SLisandro Dalcin    Level: beginner
1703277b19d0SLisandro Dalcin 
1704277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1705277b19d0SLisandro Dalcin 
1706277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1707277b19d0SLisandro Dalcin @*/
1708277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1709277b19d0SLisandro Dalcin {
1710277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1711277b19d0SLisandro Dalcin 
1712277b19d0SLisandro Dalcin   PetscFunctionBegin;
1713277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1714277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1715277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1716277b19d0SLisandro Dalcin   }
1717277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1718bbd56ea5SKarl Rupp 
17194e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
17204e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1721214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
17226bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1723e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1724e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
172538637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1726bbd56ea5SKarl Rupp 
1727277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1728d763cef2SBarry Smith   PetscFunctionReturn(0);
1729d763cef2SBarry Smith }
1730d763cef2SBarry Smith 
17314a2ae208SSatish Balay #undef __FUNCT__
17324a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1733d8e5e3e6SSatish Balay /*@
1734d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1735d763cef2SBarry Smith    with TSCreate().
1736d763cef2SBarry Smith 
1737d763cef2SBarry Smith    Collective on TS
1738d763cef2SBarry Smith 
1739d763cef2SBarry Smith    Input Parameter:
1740d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1741d763cef2SBarry Smith 
1742d763cef2SBarry Smith    Level: beginner
1743d763cef2SBarry Smith 
1744d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1745d763cef2SBarry Smith 
1746d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1747d763cef2SBarry Smith @*/
17486bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1749d763cef2SBarry Smith {
17506849ba73SBarry Smith   PetscErrorCode ierr;
1751d763cef2SBarry Smith 
1752d763cef2SBarry Smith   PetscFunctionBegin;
17536bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
17546bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
17556bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1756d763cef2SBarry Smith 
17576bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1758277b19d0SLisandro Dalcin 
1759be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
17606bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
17616bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
17626d4c513bSLisandro Dalcin 
176384df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
17646bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
17656bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
17666bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
17676d4c513bSLisandro Dalcin 
1768a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1769d763cef2SBarry Smith   PetscFunctionReturn(0);
1770d763cef2SBarry Smith }
1771d763cef2SBarry Smith 
17724a2ae208SSatish Balay #undef __FUNCT__
17734a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1774d8e5e3e6SSatish Balay /*@
1775d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1776d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1777d763cef2SBarry Smith 
1778d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1779d763cef2SBarry Smith 
1780d763cef2SBarry Smith    Input Parameter:
1781d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1782d763cef2SBarry Smith 
1783d763cef2SBarry Smith    Output Parameter:
1784d763cef2SBarry Smith .  snes - the nonlinear solver context
1785d763cef2SBarry Smith 
1786d763cef2SBarry Smith    Notes:
1787d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1788d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
178994b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1790d763cef2SBarry Smith 
1791d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
17920298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
1793d763cef2SBarry Smith 
1794d763cef2SBarry Smith    Level: beginner
1795d763cef2SBarry Smith 
1796d763cef2SBarry Smith .keywords: timestep, get, SNES
1797d763cef2SBarry Smith @*/
17987087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1799d763cef2SBarry Smith {
1800d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1801d372ba47SLisandro Dalcin 
1802d763cef2SBarry Smith   PetscFunctionBegin;
18030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18044482741eSBarry Smith   PetscValidPointer(snes,2);
1805d372ba47SLisandro Dalcin   if (!ts->snes) {
1806*ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
18070298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1808d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1809d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1810496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
18119e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
18129e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
18139e2a6581SJed Brown     }
1814d372ba47SLisandro Dalcin   }
1815d763cef2SBarry Smith   *snes = ts->snes;
1816d763cef2SBarry Smith   PetscFunctionReturn(0);
1817d763cef2SBarry Smith }
1818d763cef2SBarry Smith 
18194a2ae208SSatish Balay #undef __FUNCT__
1820deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1821deb2cd25SJed Brown /*@
1822deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1823deb2cd25SJed Brown 
1824deb2cd25SJed Brown    Collective
1825deb2cd25SJed Brown 
1826deb2cd25SJed Brown    Input Parameter:
1827deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1828deb2cd25SJed Brown -  snes - the nonlinear solver context
1829deb2cd25SJed Brown 
1830deb2cd25SJed Brown    Notes:
1831deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1832deb2cd25SJed Brown 
1833deb2cd25SJed Brown    Level: developer
1834deb2cd25SJed Brown 
1835deb2cd25SJed Brown .keywords: timestep, set, SNES
1836deb2cd25SJed Brown @*/
1837deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1838deb2cd25SJed Brown {
1839deb2cd25SJed Brown   PetscErrorCode ierr;
1840740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1841deb2cd25SJed Brown 
1842deb2cd25SJed Brown   PetscFunctionBegin;
1843deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1844deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1845deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1846deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1847bbd56ea5SKarl Rupp 
1848deb2cd25SJed Brown   ts->snes = snes;
1849bbd56ea5SKarl Rupp 
18500298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
18510298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
1852740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
18530298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1854740132f1SEmil Constantinescu   }
1855deb2cd25SJed Brown   PetscFunctionReturn(0);
1856deb2cd25SJed Brown }
1857deb2cd25SJed Brown 
1858deb2cd25SJed Brown #undef __FUNCT__
185994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1860d8e5e3e6SSatish Balay /*@
186194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1862d763cef2SBarry Smith    a TS (timestepper) context.
1863d763cef2SBarry Smith 
186494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1865d763cef2SBarry Smith 
1866d763cef2SBarry Smith    Input Parameter:
1867d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1868d763cef2SBarry Smith 
1869d763cef2SBarry Smith    Output Parameter:
187094b7f48cSBarry Smith .  ksp - the nonlinear solver context
1871d763cef2SBarry Smith 
1872d763cef2SBarry Smith    Notes:
187394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1874d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1875d763cef2SBarry Smith    KSP and PC contexts as well.
1876d763cef2SBarry Smith 
187794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
18780298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
1879d763cef2SBarry Smith 
1880d763cef2SBarry Smith    Level: beginner
1881d763cef2SBarry Smith 
188294b7f48cSBarry Smith .keywords: timestep, get, KSP
1883d763cef2SBarry Smith @*/
18847087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1885d763cef2SBarry Smith {
1886d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1887089b2837SJed Brown   SNES           snes;
1888d372ba47SLisandro Dalcin 
1889d763cef2SBarry Smith   PetscFunctionBegin;
18900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18914482741eSBarry Smith   PetscValidPointer(ksp,2);
189217186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1893e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1894089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1895089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1896d763cef2SBarry Smith   PetscFunctionReturn(0);
1897d763cef2SBarry Smith }
1898d763cef2SBarry Smith 
1899d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1900d763cef2SBarry Smith 
19014a2ae208SSatish Balay #undef __FUNCT__
1902adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1903adb62b0dSMatthew Knepley /*@
1904adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1905adb62b0dSMatthew Knepley    maximum time for iteration.
1906adb62b0dSMatthew Knepley 
19073f9fe445SBarry Smith    Not Collective
1908adb62b0dSMatthew Knepley 
1909adb62b0dSMatthew Knepley    Input Parameters:
1910adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
19110298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
19120298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
1913adb62b0dSMatthew Knepley 
1914adb62b0dSMatthew Knepley    Level: intermediate
1915adb62b0dSMatthew Knepley 
1916adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1917adb62b0dSMatthew Knepley @*/
19187087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1919adb62b0dSMatthew Knepley {
1920adb62b0dSMatthew Knepley   PetscFunctionBegin;
19210700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1922abc0a331SBarry Smith   if (maxsteps) {
19234482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1924adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1925adb62b0dSMatthew Knepley   }
1926abc0a331SBarry Smith   if (maxtime) {
19274482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1928adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
1929adb62b0dSMatthew Knepley   }
1930adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1931adb62b0dSMatthew Knepley }
1932adb62b0dSMatthew Knepley 
1933adb62b0dSMatthew Knepley #undef __FUNCT__
19344a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1935d763cef2SBarry Smith /*@
1936d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1937d763cef2SBarry Smith    maximum time for iteration.
1938d763cef2SBarry Smith 
19393f9fe445SBarry Smith    Logically Collective on TS
1940d763cef2SBarry Smith 
1941d763cef2SBarry Smith    Input Parameters:
1942d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1943d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1944d763cef2SBarry Smith -  maxtime - final time to iterate to
1945d763cef2SBarry Smith 
1946d763cef2SBarry Smith    Options Database Keys:
1947d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
19483bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1949d763cef2SBarry Smith 
1950d763cef2SBarry Smith    Notes:
1951d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1952d763cef2SBarry Smith 
1953d763cef2SBarry Smith    Level: intermediate
1954d763cef2SBarry Smith 
1955d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1956a43b19c4SJed Brown 
1957a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1958d763cef2SBarry Smith @*/
19597087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1960d763cef2SBarry Smith {
1961d763cef2SBarry Smith   PetscFunctionBegin;
19620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1963c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1964c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
196539b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
196639b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
1967d763cef2SBarry Smith   PetscFunctionReturn(0);
1968d763cef2SBarry Smith }
1969d763cef2SBarry Smith 
19704a2ae208SSatish Balay #undef __FUNCT__
19714a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1972d763cef2SBarry Smith /*@
1973d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1974d763cef2SBarry Smith    for use by the TS routines.
1975d763cef2SBarry Smith 
19763f9fe445SBarry Smith    Logically Collective on TS and Vec
1977d763cef2SBarry Smith 
1978d763cef2SBarry Smith    Input Parameters:
1979d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
19800910c330SBarry Smith -  u - the solution vector
1981d763cef2SBarry Smith 
1982d763cef2SBarry Smith    Level: beginner
1983d763cef2SBarry Smith 
1984d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1985d763cef2SBarry Smith @*/
19860910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
1987d763cef2SBarry Smith {
19888737fe31SLisandro Dalcin   PetscErrorCode ierr;
1989496e6a7aSJed Brown   DM             dm;
19908737fe31SLisandro Dalcin 
1991d763cef2SBarry Smith   PetscFunctionBegin;
19920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19930910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
19940910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
19956bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1996bbd56ea5SKarl Rupp 
19970910c330SBarry Smith   ts->vec_sol = u;
1998bbd56ea5SKarl Rupp 
1999496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
20000910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2001d763cef2SBarry Smith   PetscFunctionReturn(0);
2002d763cef2SBarry Smith }
2003d763cef2SBarry Smith 
2004e74ef692SMatthew Knepley #undef __FUNCT__
2005e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2006ac226902SBarry Smith /*@C
2007000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
20083f2090d5SJed Brown   called once at the beginning of each time step.
2009000e7ae3SMatthew Knepley 
20103f9fe445SBarry Smith   Logically Collective on TS
2011000e7ae3SMatthew Knepley 
2012000e7ae3SMatthew Knepley   Input Parameters:
2013000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2014000e7ae3SMatthew Knepley - func - The function
2015000e7ae3SMatthew Knepley 
2016000e7ae3SMatthew Knepley   Calling sequence of func:
2017000e7ae3SMatthew Knepley . func (TS ts);
2018000e7ae3SMatthew Knepley 
2019000e7ae3SMatthew Knepley   Level: intermediate
2020000e7ae3SMatthew Knepley 
2021b8123daeSJed Brown   Note:
2022b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2023b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2024b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2025b8123daeSJed Brown 
2026000e7ae3SMatthew Knepley .keywords: TS, timestep
2027b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
2028000e7ae3SMatthew Knepley @*/
20297087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2030000e7ae3SMatthew Knepley {
2031000e7ae3SMatthew Knepley   PetscFunctionBegin;
20320700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2033000e7ae3SMatthew Knepley   ts->ops->prestep = func;
2034000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2035000e7ae3SMatthew Knepley }
2036000e7ae3SMatthew Knepley 
2037e74ef692SMatthew Knepley #undef __FUNCT__
20383f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
203909ee8438SJed Brown /*@
20403f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
20413f2090d5SJed Brown 
20423f2090d5SJed Brown   Collective on TS
20433f2090d5SJed Brown 
20443f2090d5SJed Brown   Input Parameters:
20453f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
20463f2090d5SJed Brown 
20473f2090d5SJed Brown   Notes:
20483f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
20493f2090d5SJed Brown   so most users would not generally call this routine themselves.
20503f2090d5SJed Brown 
20513f2090d5SJed Brown   Level: developer
20523f2090d5SJed Brown 
20533f2090d5SJed Brown .keywords: TS, timestep
2054b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
20553f2090d5SJed Brown @*/
20567087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
20573f2090d5SJed Brown {
20583f2090d5SJed Brown   PetscErrorCode ierr;
20593f2090d5SJed Brown 
20603f2090d5SJed Brown   PetscFunctionBegin;
20610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
206272ac3e02SJed Brown   if (ts->ops->prestep) {
20633f2090d5SJed Brown     PetscStackPush("TS PreStep function");
20643f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
20653f2090d5SJed Brown     PetscStackPop;
2066312ce896SJed Brown   }
20673f2090d5SJed Brown   PetscFunctionReturn(0);
20683f2090d5SJed Brown }
20693f2090d5SJed Brown 
20703f2090d5SJed Brown #undef __FUNCT__
2071b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2072b8123daeSJed Brown /*@C
2073b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2074b8123daeSJed Brown   called once at the beginning of each stage.
2075b8123daeSJed Brown 
2076b8123daeSJed Brown   Logically Collective on TS
2077b8123daeSJed Brown 
2078b8123daeSJed Brown   Input Parameters:
2079b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2080b8123daeSJed Brown - func - The function
2081b8123daeSJed Brown 
2082b8123daeSJed Brown   Calling sequence of func:
2083b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2084b8123daeSJed Brown 
2085b8123daeSJed Brown   Level: intermediate
2086b8123daeSJed Brown 
2087b8123daeSJed Brown   Note:
2088b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2089b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2090b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2091b8123daeSJed Brown 
2092b8123daeSJed Brown .keywords: TS, timestep
2093b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2094b8123daeSJed Brown @*/
2095b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2096b8123daeSJed Brown {
2097b8123daeSJed Brown   PetscFunctionBegin;
2098b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2099b8123daeSJed Brown   ts->ops->prestage = func;
2100b8123daeSJed Brown   PetscFunctionReturn(0);
2101b8123daeSJed Brown }
2102b8123daeSJed Brown 
2103b8123daeSJed Brown #undef __FUNCT__
2104b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2105b8123daeSJed Brown /*@
2106b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2107b8123daeSJed Brown 
2108b8123daeSJed Brown   Collective on TS
2109b8123daeSJed Brown 
2110b8123daeSJed Brown   Input Parameters:
2111b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2112b8123daeSJed Brown 
2113b8123daeSJed Brown   Notes:
2114b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2115b8123daeSJed Brown   most users would not generally call this routine themselves.
2116b8123daeSJed Brown 
2117b8123daeSJed Brown   Level: developer
2118b8123daeSJed Brown 
2119b8123daeSJed Brown .keywords: TS, timestep
2120b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2121b8123daeSJed Brown @*/
2122b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2123b8123daeSJed Brown {
2124b8123daeSJed Brown   PetscErrorCode ierr;
2125b8123daeSJed Brown 
2126b8123daeSJed Brown   PetscFunctionBegin;
2127b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2128b8123daeSJed Brown   if (ts->ops->prestage) {
2129b8123daeSJed Brown     PetscStackPush("TS PreStage function");
2130b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
2131b8123daeSJed Brown     PetscStackPop;
2132b8123daeSJed Brown   }
2133b8123daeSJed Brown   PetscFunctionReturn(0);
2134b8123daeSJed Brown }
2135b8123daeSJed Brown 
2136b8123daeSJed Brown #undef __FUNCT__
2137e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2138ac226902SBarry Smith /*@C
2139000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
21403f2090d5SJed Brown   called once at the end of each time step.
2141000e7ae3SMatthew Knepley 
21423f9fe445SBarry Smith   Logically Collective on TS
2143000e7ae3SMatthew Knepley 
2144000e7ae3SMatthew Knepley   Input Parameters:
2145000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2146000e7ae3SMatthew Knepley - func - The function
2147000e7ae3SMatthew Knepley 
2148000e7ae3SMatthew Knepley   Calling sequence of func:
2149b8123daeSJed Brown $ func (TS ts);
2150000e7ae3SMatthew Knepley 
2151000e7ae3SMatthew Knepley   Level: intermediate
2152000e7ae3SMatthew Knepley 
2153000e7ae3SMatthew Knepley .keywords: TS, timestep
2154b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2155000e7ae3SMatthew Knepley @*/
21567087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2157000e7ae3SMatthew Knepley {
2158000e7ae3SMatthew Knepley   PetscFunctionBegin;
21590700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2160000e7ae3SMatthew Knepley   ts->ops->poststep = func;
2161000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2162000e7ae3SMatthew Knepley }
2163000e7ae3SMatthew Knepley 
2164e74ef692SMatthew Knepley #undef __FUNCT__
21653f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
216609ee8438SJed Brown /*@
21673f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
21683f2090d5SJed Brown 
21693f2090d5SJed Brown   Collective on TS
21703f2090d5SJed Brown 
21713f2090d5SJed Brown   Input Parameters:
21723f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
21733f2090d5SJed Brown 
21743f2090d5SJed Brown   Notes:
21753f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
21763f2090d5SJed Brown   so most users would not generally call this routine themselves.
21773f2090d5SJed Brown 
21783f2090d5SJed Brown   Level: developer
21793f2090d5SJed Brown 
21803f2090d5SJed Brown .keywords: TS, timestep
21813f2090d5SJed Brown @*/
21827087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
21833f2090d5SJed Brown {
21843f2090d5SJed Brown   PetscErrorCode ierr;
21853f2090d5SJed Brown 
21863f2090d5SJed Brown   PetscFunctionBegin;
21870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
218872ac3e02SJed Brown   if (ts->ops->poststep) {
21893f2090d5SJed Brown     PetscStackPush("TS PostStep function");
21903f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
21913f2090d5SJed Brown     PetscStackPop;
219272ac3e02SJed Brown   }
21933f2090d5SJed Brown   PetscFunctionReturn(0);
21943f2090d5SJed Brown }
21953f2090d5SJed Brown 
2196d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2197d763cef2SBarry Smith 
21984a2ae208SSatish Balay #undef __FUNCT__
2199a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2200d763cef2SBarry Smith /*@C
2201a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2202d763cef2SBarry Smith    timestep to display the iteration's  progress.
2203d763cef2SBarry Smith 
22043f9fe445SBarry Smith    Logically Collective on TS
2205d763cef2SBarry Smith 
2206d763cef2SBarry Smith    Input Parameters:
2207d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2208e213d8f1SJed Brown .  monitor - monitoring routine
2209329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
22100298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2211b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
22120298fd71SBarry Smith           (may be NULL)
2213d763cef2SBarry Smith 
2214e213d8f1SJed Brown    Calling sequence of monitor:
22150910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2216d763cef2SBarry Smith 
2217d763cef2SBarry Smith +    ts - the TS context
221888c05cc5SBarry 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
221988c05cc5SBarry Smith                                been interpolated to)
22201f06c33eSBarry Smith .    time - current time
22210910c330SBarry Smith .    u - current iterate
2222d763cef2SBarry Smith -    mctx - [optional] monitoring context
2223d763cef2SBarry Smith 
2224d763cef2SBarry Smith    Notes:
2225d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2226d763cef2SBarry Smith    already has been loaded.
2227d763cef2SBarry Smith 
2228025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2229025f1a04SBarry Smith 
2230d763cef2SBarry Smith    Level: intermediate
2231d763cef2SBarry Smith 
2232d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2233d763cef2SBarry Smith 
2234a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2235d763cef2SBarry Smith @*/
2236c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2237d763cef2SBarry Smith {
2238d763cef2SBarry Smith   PetscFunctionBegin;
22390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
224017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2241d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
22428704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2243d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2244d763cef2SBarry Smith   PetscFunctionReturn(0);
2245d763cef2SBarry Smith }
2246d763cef2SBarry Smith 
22474a2ae208SSatish Balay #undef __FUNCT__
2248a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2249d763cef2SBarry Smith /*@C
2250a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2251d763cef2SBarry Smith 
22523f9fe445SBarry Smith    Logically Collective on TS
2253d763cef2SBarry Smith 
2254d763cef2SBarry Smith    Input Parameters:
2255d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2256d763cef2SBarry Smith 
2257d763cef2SBarry Smith    Notes:
2258d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2259d763cef2SBarry Smith 
2260d763cef2SBarry Smith    Level: intermediate
2261d763cef2SBarry Smith 
2262d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2263d763cef2SBarry Smith 
2264a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2265d763cef2SBarry Smith @*/
22667087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2267d763cef2SBarry Smith {
2268d952e501SBarry Smith   PetscErrorCode ierr;
2269d952e501SBarry Smith   PetscInt       i;
2270d952e501SBarry Smith 
2271d763cef2SBarry Smith   PetscFunctionBegin;
22720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2273d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
22748704b422SBarry Smith     if (ts->monitordestroy[i]) {
22758704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2276d952e501SBarry Smith     }
2277d952e501SBarry Smith   }
2278d763cef2SBarry Smith   ts->numbermonitors = 0;
2279d763cef2SBarry Smith   PetscFunctionReturn(0);
2280d763cef2SBarry Smith }
2281d763cef2SBarry Smith 
22824a2ae208SSatish Balay #undef __FUNCT__
2283a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2284d8e5e3e6SSatish Balay /*@
2285a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
22865516499fSSatish Balay 
22875516499fSSatish Balay    Level: intermediate
228841251cbbSSatish Balay 
22895516499fSSatish Balay .keywords: TS, set, monitor
22905516499fSSatish Balay 
229141251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
229241251cbbSSatish Balay @*/
2293649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2294d763cef2SBarry Smith {
2295dfbe8321SBarry Smith   PetscErrorCode ierr;
2296*ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2297d132466eSBarry Smith 
2298d763cef2SBarry Smith   PetscFunctionBegin;
2299649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2300971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2301649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2302d763cef2SBarry Smith   PetscFunctionReturn(0);
2303d763cef2SBarry Smith }
2304d763cef2SBarry Smith 
23054a2ae208SSatish Balay #undef __FUNCT__
2306cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2307cd652676SJed Brown /*@
2308cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2309cd652676SJed Brown 
2310cd652676SJed Brown    Logically Collective on TS
2311cd652676SJed Brown 
2312cd652676SJed Brown    Input Argument:
2313cd652676SJed Brown .  ts - time stepping context
2314cd652676SJed Brown 
2315cd652676SJed Brown    Output Argument:
2316cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2317cd652676SJed Brown 
2318cd652676SJed Brown    Level: intermediate
2319cd652676SJed Brown 
2320cd652676SJed Brown .keywords: TS, set
2321cd652676SJed Brown 
2322cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2323cd652676SJed Brown @*/
2324cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2325cd652676SJed Brown {
2326cd652676SJed Brown   PetscFunctionBegin;
2327cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2328cd652676SJed Brown   ts->retain_stages = flg;
2329cd652676SJed Brown   PetscFunctionReturn(0);
2330cd652676SJed Brown }
2331cd652676SJed Brown 
2332cd652676SJed Brown #undef __FUNCT__
2333cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2334cd652676SJed Brown /*@
2335cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2336cd652676SJed Brown 
2337cd652676SJed Brown    Collective on TS
2338cd652676SJed Brown 
2339cd652676SJed Brown    Input Argument:
2340cd652676SJed Brown +  ts - time stepping context
2341cd652676SJed Brown -  t - time to interpolate to
2342cd652676SJed Brown 
2343cd652676SJed Brown    Output Argument:
23440910c330SBarry Smith .  U - state at given time
2345cd652676SJed Brown 
2346cd652676SJed Brown    Notes:
2347cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2348cd652676SJed Brown 
2349cd652676SJed Brown    Level: intermediate
2350cd652676SJed Brown 
2351cd652676SJed Brown    Developer Notes:
2352cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2353cd652676SJed Brown 
2354cd652676SJed Brown .keywords: TS, set
2355cd652676SJed Brown 
2356cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2357cd652676SJed Brown @*/
23580910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2359cd652676SJed Brown {
2360cd652676SJed Brown   PetscErrorCode ierr;
2361cd652676SJed Brown 
2362cd652676SJed Brown   PetscFunctionBegin;
2363cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2364*ce94432eSBarry 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);
2365*ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
23660910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2367cd652676SJed Brown   PetscFunctionReturn(0);
2368cd652676SJed Brown }
2369cd652676SJed Brown 
2370cd652676SJed Brown #undef __FUNCT__
23714a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2372d763cef2SBarry Smith /*@
23736d9e5789SSean Farley    TSStep - Steps one time step
2374d763cef2SBarry Smith 
2375d763cef2SBarry Smith    Collective on TS
2376d763cef2SBarry Smith 
2377d763cef2SBarry Smith    Input Parameter:
2378d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2379d763cef2SBarry Smith 
23806d9e5789SSean Farley    Level: intermediate
2381d763cef2SBarry Smith 
2382b8123daeSJed Brown    Notes:
2383b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2384b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2385b8123daeSJed Brown 
238625cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
238725cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
238825cb2221SBarry Smith 
2389d763cef2SBarry Smith .keywords: TS, timestep, solve
2390d763cef2SBarry Smith 
239125cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2392d763cef2SBarry Smith @*/
2393193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2394d763cef2SBarry Smith {
2395362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2396dfbe8321SBarry Smith   PetscErrorCode ierr;
2397d763cef2SBarry Smith 
2398d763cef2SBarry Smith   PetscFunctionBegin;
23990700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2400d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2401d405a339SMatthew Knepley 
2402362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2403362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2404bbd56ea5SKarl Rupp 
2405d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2406193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2407d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2408bbd56ea5SKarl Rupp 
2409362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2410362cd11cSLisandro Dalcin 
2411362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2412cef5090cSJed Brown     if (ts->errorifstepfailed) {
2413cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2414*ce94432eSBarry 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]);
2415*ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2416cef5090cSJed Brown     }
2417362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2418db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2419db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2420362cd11cSLisandro Dalcin   }
2421d763cef2SBarry Smith   PetscFunctionReturn(0);
2422d763cef2SBarry Smith }
2423d763cef2SBarry Smith 
24244a2ae208SSatish Balay #undef __FUNCT__
242505175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
242605175c85SJed Brown /*@
242705175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
242805175c85SJed Brown 
24291c3436cfSJed Brown    Collective on TS
243005175c85SJed Brown 
243105175c85SJed Brown    Input Arguments:
24321c3436cfSJed Brown +  ts - time stepping context
24331c3436cfSJed Brown .  order - desired order of accuracy
24340298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
243505175c85SJed Brown 
243605175c85SJed Brown    Output Arguments:
24370910c330SBarry Smith .  U - state at the end of the current step
243805175c85SJed Brown 
243905175c85SJed Brown    Level: advanced
244005175c85SJed Brown 
2441108c343cSJed Brown    Notes:
2442108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2443108c343cSJed 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.
2444108c343cSJed Brown 
24451c3436cfSJed Brown .seealso: TSStep(), TSAdapt
244605175c85SJed Brown @*/
24470910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
244805175c85SJed Brown {
244905175c85SJed Brown   PetscErrorCode ierr;
245005175c85SJed Brown 
245105175c85SJed Brown   PetscFunctionBegin;
245205175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
245305175c85SJed Brown   PetscValidType(ts,1);
24540910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2455*ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
24560910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
245705175c85SJed Brown   PetscFunctionReturn(0);
245805175c85SJed Brown }
245905175c85SJed Brown 
246005175c85SJed Brown #undef __FUNCT__
24616a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
24626a4d4014SLisandro Dalcin /*@
24636a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
24646a4d4014SLisandro Dalcin 
24656a4d4014SLisandro Dalcin    Collective on TS
24666a4d4014SLisandro Dalcin 
24676a4d4014SLisandro Dalcin    Input Parameter:
24686a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2469cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
24705a3a76d0SJed Brown 
24716a4d4014SLisandro Dalcin    Level: beginner
24726a4d4014SLisandro Dalcin 
24735a3a76d0SJed Brown    Notes:
24745a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
24755a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
24765a3a76d0SJed Brown    stepped over the final time.
24775a3a76d0SJed Brown 
24786a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
24796a4d4014SLisandro Dalcin 
24806a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
24816a4d4014SLisandro Dalcin @*/
2482cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
24836a4d4014SLisandro Dalcin {
24844d7d938eSLisandro Dalcin   PetscBool         flg;
24854d7d938eSLisandro Dalcin   PetscViewer       viewer;
24866a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2487cffb1e40SBarry Smith   PetscViewerFormat format;
2488f22f69f0SBarry Smith 
24896a4d4014SLisandro Dalcin   PetscFunctionBegin;
24900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2491f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
249249354f04SShri 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 */
24930910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
24945a3a76d0SJed Brown       Vec y;
24950910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
24965a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
24975a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
24985a3a76d0SJed Brown     }
2499f2c2a1b9SBarry Smith     if (u) {
25000910c330SBarry Smith       ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2501f2c2a1b9SBarry Smith     }
2502bbd56ea5SKarl Rupp   } else if (u) {
25030910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
25045a3a76d0SJed Brown   }
2505b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
25066a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2507193ac0bcSJed Brown   ts->steps             = 0;
25085ef26d82SJed Brown   ts->ksp_its           = 0;
25095ef26d82SJed Brown   ts->snes_its          = 0;
2510c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2511c610991cSLisandro Dalcin   ts->reject            = 0;
2512193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2513193ac0bcSJed Brown 
2514193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2515193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
25160910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2517bbd56ea5SKarl Rupp 
2518cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2519193ac0bcSJed Brown   } else {
25206a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2521362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2522db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2523db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2524e1a7a14fSJed Brown     while (!ts->reason) {
2525193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2526193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2527193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2528193ac0bcSJed Brown     }
252949354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
25300910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2531bbd56ea5SKarl Rupp 
2532cc708dedSBarry Smith       ts->solvetime = ts->max_time;
25330574a7fbSJed Brown     } else {
2534ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2535cc708dedSBarry Smith       ts->solvetime = ts->ptime;
25360574a7fbSJed Brown     }
2537193ac0bcSJed Brown   }
25383923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2539*ce94432eSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
25404d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2541cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
25424d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2543cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2544cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
25452b0a91c0SBarry Smith   }
25466a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
25476a4d4014SLisandro Dalcin }
25486a4d4014SLisandro Dalcin 
25496a4d4014SLisandro Dalcin #undef __FUNCT__
25504a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2551228d79bcSJed Brown /*@
2552228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2553228d79bcSJed Brown 
2554228d79bcSJed Brown    Collective on TS
2555228d79bcSJed Brown 
2556228d79bcSJed Brown    Input Parameters:
2557228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2558228d79bcSJed Brown .  step - step number that has just completed
2559228d79bcSJed Brown .  ptime - model time of the state
25600910c330SBarry Smith -  u - state at the current model time
2561228d79bcSJed Brown 
2562228d79bcSJed Brown    Notes:
2563228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2564228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2565228d79bcSJed Brown 
2566228d79bcSJed Brown    Level: advanced
2567228d79bcSJed Brown 
2568228d79bcSJed Brown .keywords: TS, timestep
2569228d79bcSJed Brown @*/
25700910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2571d763cef2SBarry Smith {
25726849ba73SBarry Smith   PetscErrorCode ierr;
2573a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2574d763cef2SBarry Smith 
2575d763cef2SBarry Smith   PetscFunctionBegin;
2576d763cef2SBarry Smith   for (i=0; i<n; i++) {
25770910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2578d763cef2SBarry Smith   }
2579d763cef2SBarry Smith   PetscFunctionReturn(0);
2580d763cef2SBarry Smith }
2581d763cef2SBarry Smith 
2582d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
25830b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
25840b039ecaSBarry Smith   PetscDrawLG lg;
25850b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2586201da799SBarry Smith   PetscInt    ksp_its,snes_its;
25870b039ecaSBarry Smith };
25880b039ecaSBarry Smith 
2589d763cef2SBarry Smith 
25904a2ae208SSatish Balay #undef __FUNCT__
2591a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2592d763cef2SBarry Smith /*@C
2593a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2594a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2595d763cef2SBarry Smith 
2596d763cef2SBarry Smith    Collective on TS
2597d763cef2SBarry Smith 
2598d763cef2SBarry Smith    Input Parameters:
2599d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2600d763cef2SBarry Smith .  label - the title to put in the title bar
26017c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2602a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2603a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2604d763cef2SBarry Smith 
2605d763cef2SBarry Smith    Output Parameter:
26060b039ecaSBarry Smith .  ctx - the context
2607d763cef2SBarry Smith 
2608d763cef2SBarry Smith    Options Database Key:
26094f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
26104f09c107SBarry Smith .  -ts_monitor_lg_solution -
261199fdda47SBarry Smith .  -ts_monitor_lg_error -
261299fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
261399fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
261499fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2615d763cef2SBarry Smith 
2616d763cef2SBarry Smith    Notes:
2617a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2618d763cef2SBarry Smith 
2619d763cef2SBarry Smith    Level: intermediate
2620d763cef2SBarry Smith 
26217c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2622d763cef2SBarry Smith 
26234f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
26247c922b88SBarry Smith 
2625d763cef2SBarry Smith @*/
2626a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2627d763cef2SBarry Smith {
2628b0a32e0cSBarry Smith   PetscDraw      win;
2629dfbe8321SBarry Smith   PetscErrorCode ierr;
263099fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2631d763cef2SBarry Smith 
2632d763cef2SBarry Smith   PetscFunctionBegin;
2633201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2634a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
26353fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
26360b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
26370298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-lg_indicate_data_points",&flg,NULL);CHKERRQ(ierr);
263899fdda47SBarry Smith   if (flg) {
26390b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
264099fdda47SBarry Smith   }
26410b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2642a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2643d763cef2SBarry Smith   PetscFunctionReturn(0);
2644d763cef2SBarry Smith }
2645d763cef2SBarry Smith 
26464a2ae208SSatish Balay #undef __FUNCT__
26474f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
26484f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2649d763cef2SBarry Smith {
26500b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2651c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2652dfbe8321SBarry Smith   PetscErrorCode ierr;
2653d763cef2SBarry Smith 
2654d763cef2SBarry Smith   PetscFunctionBegin;
2655a9f9c1f6SBarry Smith   if (!n) {
2656a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2657a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2658a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2659a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2660a9f9c1f6SBarry Smith   }
2661c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
26620b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
266399fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))) {
26640b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
26653923b477SBarry Smith   }
2666d763cef2SBarry Smith   PetscFunctionReturn(0);
2667d763cef2SBarry Smith }
2668d763cef2SBarry Smith 
26694a2ae208SSatish Balay #undef __FUNCT__
2670a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2671d763cef2SBarry Smith /*@C
2672a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2673a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2674d763cef2SBarry Smith 
26750b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2676d763cef2SBarry Smith 
2677d763cef2SBarry Smith    Input Parameter:
26780b039ecaSBarry Smith .  ctx - the monitor context
2679d763cef2SBarry Smith 
2680d763cef2SBarry Smith    Level: intermediate
2681d763cef2SBarry Smith 
2682d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2683d763cef2SBarry Smith 
26844f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2685d763cef2SBarry Smith @*/
2686a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2687d763cef2SBarry Smith {
2688b0a32e0cSBarry Smith   PetscDraw      draw;
2689dfbe8321SBarry Smith   PetscErrorCode ierr;
2690d763cef2SBarry Smith 
2691d763cef2SBarry Smith   PetscFunctionBegin;
26920b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
26936bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
26940b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
26950b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2696d763cef2SBarry Smith   PetscFunctionReturn(0);
2697d763cef2SBarry Smith }
2698d763cef2SBarry Smith 
26994a2ae208SSatish Balay #undef __FUNCT__
27004a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2701d763cef2SBarry Smith /*@
2702b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2703d763cef2SBarry Smith 
2704d763cef2SBarry Smith    Not Collective
2705d763cef2SBarry Smith 
2706d763cef2SBarry Smith    Input Parameter:
2707d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2708d763cef2SBarry Smith 
2709d763cef2SBarry Smith    Output Parameter:
2710d763cef2SBarry Smith .  t  - the current time
2711d763cef2SBarry Smith 
2712d763cef2SBarry Smith    Level: beginner
2713d763cef2SBarry Smith 
2714b8123daeSJed Brown    Note:
2715b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2716b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2717b8123daeSJed Brown 
2718d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2719d763cef2SBarry Smith 
2720d763cef2SBarry Smith .keywords: TS, get, time
2721d763cef2SBarry Smith @*/
27227087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2723d763cef2SBarry Smith {
2724d763cef2SBarry Smith   PetscFunctionBegin;
27250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2726f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2727d763cef2SBarry Smith   *t = ts->ptime;
2728d763cef2SBarry Smith   PetscFunctionReturn(0);
2729d763cef2SBarry Smith }
2730d763cef2SBarry Smith 
27314a2ae208SSatish Balay #undef __FUNCT__
27326a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
27336a4d4014SLisandro Dalcin /*@
27346a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
27356a4d4014SLisandro Dalcin 
27363f9fe445SBarry Smith    Logically Collective on TS
27376a4d4014SLisandro Dalcin 
27386a4d4014SLisandro Dalcin    Input Parameters:
27396a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
27406a4d4014SLisandro Dalcin -  time - the time
27416a4d4014SLisandro Dalcin 
27426a4d4014SLisandro Dalcin    Level: intermediate
27436a4d4014SLisandro Dalcin 
27446a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
27456a4d4014SLisandro Dalcin 
27466a4d4014SLisandro Dalcin .keywords: TS, set, time
27476a4d4014SLisandro Dalcin @*/
27487087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
27496a4d4014SLisandro Dalcin {
27506a4d4014SLisandro Dalcin   PetscFunctionBegin;
27510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2752c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
27536a4d4014SLisandro Dalcin   ts->ptime = t;
27546a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
27556a4d4014SLisandro Dalcin }
27566a4d4014SLisandro Dalcin 
27576a4d4014SLisandro Dalcin #undef __FUNCT__
27584a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2759d763cef2SBarry Smith /*@C
2760d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2761d763cef2SBarry Smith    TS options in the database.
2762d763cef2SBarry Smith 
27633f9fe445SBarry Smith    Logically Collective on TS
2764d763cef2SBarry Smith 
2765d763cef2SBarry Smith    Input Parameter:
2766d763cef2SBarry Smith +  ts     - The TS context
2767d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2768d763cef2SBarry Smith 
2769d763cef2SBarry Smith    Notes:
2770d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2771d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2772d763cef2SBarry Smith    hyphen.
2773d763cef2SBarry Smith 
2774d763cef2SBarry Smith    Level: advanced
2775d763cef2SBarry Smith 
2776d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2777d763cef2SBarry Smith 
2778d763cef2SBarry Smith .seealso: TSSetFromOptions()
2779d763cef2SBarry Smith 
2780d763cef2SBarry Smith @*/
27817087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2782d763cef2SBarry Smith {
2783dfbe8321SBarry Smith   PetscErrorCode ierr;
2784089b2837SJed Brown   SNES           snes;
2785d763cef2SBarry Smith 
2786d763cef2SBarry Smith   PetscFunctionBegin;
27870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2788d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2789089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2790089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2791d763cef2SBarry Smith   PetscFunctionReturn(0);
2792d763cef2SBarry Smith }
2793d763cef2SBarry Smith 
2794d763cef2SBarry Smith 
27954a2ae208SSatish Balay #undef __FUNCT__
27964a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2797d763cef2SBarry Smith /*@C
2798d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2799d763cef2SBarry Smith    TS options in the database.
2800d763cef2SBarry Smith 
28013f9fe445SBarry Smith    Logically Collective on TS
2802d763cef2SBarry Smith 
2803d763cef2SBarry Smith    Input Parameter:
2804d763cef2SBarry Smith +  ts     - The TS context
2805d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2806d763cef2SBarry Smith 
2807d763cef2SBarry Smith    Notes:
2808d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2809d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2810d763cef2SBarry Smith    hyphen.
2811d763cef2SBarry Smith 
2812d763cef2SBarry Smith    Level: advanced
2813d763cef2SBarry Smith 
2814d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2815d763cef2SBarry Smith 
2816d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2817d763cef2SBarry Smith 
2818d763cef2SBarry Smith @*/
28197087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2820d763cef2SBarry Smith {
2821dfbe8321SBarry Smith   PetscErrorCode ierr;
2822089b2837SJed Brown   SNES           snes;
2823d763cef2SBarry Smith 
2824d763cef2SBarry Smith   PetscFunctionBegin;
28250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2826d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2827089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2828089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2829d763cef2SBarry Smith   PetscFunctionReturn(0);
2830d763cef2SBarry Smith }
2831d763cef2SBarry Smith 
28324a2ae208SSatish Balay #undef __FUNCT__
28334a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2834d763cef2SBarry Smith /*@C
2835d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2836d763cef2SBarry Smith    TS options in the database.
2837d763cef2SBarry Smith 
2838d763cef2SBarry Smith    Not Collective
2839d763cef2SBarry Smith 
2840d763cef2SBarry Smith    Input Parameter:
2841d763cef2SBarry Smith .  ts - The TS context
2842d763cef2SBarry Smith 
2843d763cef2SBarry Smith    Output Parameter:
2844d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2845d763cef2SBarry Smith 
2846d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2847d763cef2SBarry Smith    sufficient length to hold the prefix.
2848d763cef2SBarry Smith 
2849d763cef2SBarry Smith    Level: intermediate
2850d763cef2SBarry Smith 
2851d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2852d763cef2SBarry Smith 
2853d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2854d763cef2SBarry Smith @*/
28557087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2856d763cef2SBarry Smith {
2857dfbe8321SBarry Smith   PetscErrorCode ierr;
2858d763cef2SBarry Smith 
2859d763cef2SBarry Smith   PetscFunctionBegin;
28600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28614482741eSBarry Smith   PetscValidPointer(prefix,2);
2862d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2863d763cef2SBarry Smith   PetscFunctionReturn(0);
2864d763cef2SBarry Smith }
2865d763cef2SBarry Smith 
28664a2ae208SSatish Balay #undef __FUNCT__
28674a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2868d763cef2SBarry Smith /*@C
2869d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2870d763cef2SBarry Smith 
2871d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2872d763cef2SBarry Smith 
2873d763cef2SBarry Smith    Input Parameter:
2874d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2875d763cef2SBarry Smith 
2876d763cef2SBarry Smith    Output Parameters:
2877b5abc632SBarry Smith +  J   - The Jacobian J of F, where U_t = G(U,t)
2878d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2879089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2880d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2881d763cef2SBarry Smith 
28820298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
2883d763cef2SBarry Smith 
2884d763cef2SBarry Smith    Level: intermediate
2885d763cef2SBarry Smith 
288626d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2887d763cef2SBarry Smith 
2888d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2889d763cef2SBarry Smith @*/
2890089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2891d763cef2SBarry Smith {
2892089b2837SJed Brown   PetscErrorCode ierr;
2893089b2837SJed Brown   SNES           snes;
289424989b8cSPeter Brune   DM             dm;
2895089b2837SJed Brown 
2896d763cef2SBarry Smith   PetscFunctionBegin;
2897089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
28980298fd71SBarry Smith   ierr = SNESGetJacobian(snes,J,M,NULL,NULL);CHKERRQ(ierr);
289924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
290024989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2901d763cef2SBarry Smith   PetscFunctionReturn(0);
2902d763cef2SBarry Smith }
2903d763cef2SBarry Smith 
29041713a123SBarry Smith #undef __FUNCT__
29052eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
29062eca1d9cSJed Brown /*@C
29072eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
29082eca1d9cSJed Brown 
29092eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
29102eca1d9cSJed Brown 
29112eca1d9cSJed Brown    Input Parameter:
29122eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
29132eca1d9cSJed Brown 
29142eca1d9cSJed Brown    Output Parameters:
29152eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
29162eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
29172eca1d9cSJed Brown .  f   - The function to compute the matrices
29182eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
29192eca1d9cSJed Brown 
29200298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
29212eca1d9cSJed Brown 
29222eca1d9cSJed Brown    Level: advanced
29232eca1d9cSJed Brown 
29242eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
29252eca1d9cSJed Brown 
29262eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
29272eca1d9cSJed Brown @*/
29287087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
29292eca1d9cSJed Brown {
2930089b2837SJed Brown   PetscErrorCode ierr;
2931089b2837SJed Brown   SNES           snes;
293224989b8cSPeter Brune   DM             dm;
29330910c330SBarry Smith 
29342eca1d9cSJed Brown   PetscFunctionBegin;
2935089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2936f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
29370298fd71SBarry Smith   ierr = SNESGetJacobian(snes,A,B,NULL,NULL);CHKERRQ(ierr);
293824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
293924989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
29402eca1d9cSJed Brown   PetscFunctionReturn(0);
29412eca1d9cSJed Brown }
29422eca1d9cSJed Brown 
294383a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
29446083293cSBarry Smith   PetscViewer viewer;
29456083293cSBarry Smith   Vec         initialsolution;
29466083293cSBarry Smith   PetscBool   showinitial;
294783a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2948473a3ab2SBarry Smith   PetscBool   showtimestepandtime;
294983a4ac43SBarry Smith };
29506083293cSBarry Smith 
29512eca1d9cSJed Brown #undef __FUNCT__
295283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
29531713a123SBarry Smith /*@C
295483a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
29551713a123SBarry Smith    VecView() for the solution at each timestep
29561713a123SBarry Smith 
29571713a123SBarry Smith    Collective on TS
29581713a123SBarry Smith 
29591713a123SBarry Smith    Input Parameters:
29601713a123SBarry Smith +  ts - the TS context
29611713a123SBarry Smith .  step - current time-step
2962142b95e3SSatish Balay .  ptime - current time
29630298fd71SBarry Smith -  dummy - either a viewer or NULL
29641713a123SBarry Smith 
296599fdda47SBarry Smith    Options Database:
296699fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
296799fdda47SBarry Smith 
296899fdda47SBarry 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
296999fdda47SBarry Smith        will look bad
297099fdda47SBarry Smith 
29711713a123SBarry Smith    Level: intermediate
29721713a123SBarry Smith 
29731713a123SBarry Smith .keywords: TS,  vector, monitor, view
29741713a123SBarry Smith 
2975a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
29761713a123SBarry Smith @*/
29770910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
29781713a123SBarry Smith {
2979dfbe8321SBarry Smith   PetscErrorCode   ierr;
298083a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
2981473a3ab2SBarry Smith   PetscDraw        draw;
29821713a123SBarry Smith 
29831713a123SBarry Smith   PetscFunctionBegin;
29846083293cSBarry Smith   if (!step && ictx->showinitial) {
29856083293cSBarry Smith     if (!ictx->initialsolution) {
29860910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
29871713a123SBarry Smith     }
29880910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
29896083293cSBarry Smith   }
29903fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
29910dcf80beSBarry Smith 
29926083293cSBarry Smith   if (ictx->showinitial) {
29936083293cSBarry Smith     PetscReal pause;
29946083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
29956083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
29966083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
29976083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
29986083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
29996083293cSBarry Smith   }
30000910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3001473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3002473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3003473a3ab2SBarry Smith     char      time[32];
3004473a3ab2SBarry Smith     size_t    len;
3005473a3ab2SBarry Smith 
3006473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3007473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3008473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3009473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
30100298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3011473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3012473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3013473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3014473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3015473a3ab2SBarry Smith   }
3016473a3ab2SBarry Smith 
30176083293cSBarry Smith   if (ictx->showinitial) {
30186083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
30196083293cSBarry Smith   }
30201713a123SBarry Smith   PetscFunctionReturn(0);
30211713a123SBarry Smith }
30221713a123SBarry Smith 
30231713a123SBarry Smith 
30246c699258SBarry Smith #undef __FUNCT__
302583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
30266083293cSBarry Smith /*@C
302783a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
30286083293cSBarry Smith 
30296083293cSBarry Smith    Collective on TS
30306083293cSBarry Smith 
30316083293cSBarry Smith    Input Parameters:
30326083293cSBarry Smith .    ctx - the monitor context
30336083293cSBarry Smith 
30346083293cSBarry Smith    Level: intermediate
30356083293cSBarry Smith 
30366083293cSBarry Smith .keywords: TS,  vector, monitor, view
30376083293cSBarry Smith 
303883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
30396083293cSBarry Smith @*/
304083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
30416083293cSBarry Smith {
30426083293cSBarry Smith   PetscErrorCode ierr;
30436083293cSBarry Smith 
30446083293cSBarry Smith   PetscFunctionBegin;
304583a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
304683a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
304783a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
30486083293cSBarry Smith   PetscFunctionReturn(0);
30496083293cSBarry Smith }
30506083293cSBarry Smith 
30516083293cSBarry Smith #undef __FUNCT__
305283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
30536083293cSBarry Smith /*@C
305483a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
30556083293cSBarry Smith 
30566083293cSBarry Smith    Collective on TS
30576083293cSBarry Smith 
30586083293cSBarry Smith    Input Parameter:
30596083293cSBarry Smith .    ts - time-step context
30606083293cSBarry Smith 
30616083293cSBarry Smith    Output Patameter:
30626083293cSBarry Smith .    ctx - the monitor context
30636083293cSBarry Smith 
306499fdda47SBarry Smith    Options Database:
306599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
306699fdda47SBarry Smith 
30676083293cSBarry Smith    Level: intermediate
30686083293cSBarry Smith 
30696083293cSBarry Smith .keywords: TS,  vector, monitor, view
30706083293cSBarry Smith 
307183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
30726083293cSBarry Smith @*/
307383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
30746083293cSBarry Smith {
30756083293cSBarry Smith   PetscErrorCode   ierr;
30766083293cSBarry Smith 
30776083293cSBarry Smith   PetscFunctionBegin;
307883a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
307983a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3080e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3081bbd56ea5SKarl Rupp 
308283a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3083473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
30840298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3085473a3ab2SBarry Smith 
3086473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
30870298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
30886083293cSBarry Smith   PetscFunctionReturn(0);
30896083293cSBarry Smith }
30906083293cSBarry Smith 
30916083293cSBarry Smith #undef __FUNCT__
309283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
30933a471f94SBarry Smith /*@C
309483a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
30953a471f94SBarry Smith    VecView() for the error at each timestep
30963a471f94SBarry Smith 
30973a471f94SBarry Smith    Collective on TS
30983a471f94SBarry Smith 
30993a471f94SBarry Smith    Input Parameters:
31003a471f94SBarry Smith +  ts - the TS context
31013a471f94SBarry Smith .  step - current time-step
31023a471f94SBarry Smith .  ptime - current time
31030298fd71SBarry Smith -  dummy - either a viewer or NULL
31043a471f94SBarry Smith 
31053a471f94SBarry Smith    Level: intermediate
31063a471f94SBarry Smith 
31073a471f94SBarry Smith .keywords: TS,  vector, monitor, view
31083a471f94SBarry Smith 
31093a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
31103a471f94SBarry Smith @*/
31110910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
31123a471f94SBarry Smith {
31133a471f94SBarry Smith   PetscErrorCode   ierr;
311483a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
311583a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
31163a471f94SBarry Smith   Vec              work;
31173a471f94SBarry Smith 
31183a471f94SBarry Smith   PetscFunctionBegin;
31193fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
31200910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
31213a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
31220910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
31233a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
31243a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
31253a471f94SBarry Smith   PetscFunctionReturn(0);
31263a471f94SBarry Smith }
31273a471f94SBarry Smith 
31282a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
31293a471f94SBarry Smith #undef __FUNCT__
31306c699258SBarry Smith #define __FUNCT__ "TSSetDM"
31316c699258SBarry Smith /*@
31326c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
31336c699258SBarry Smith 
31343f9fe445SBarry Smith    Logically Collective on TS and DM
31356c699258SBarry Smith 
31366c699258SBarry Smith    Input Parameters:
31376c699258SBarry Smith +  ts - the preconditioner context
31386c699258SBarry Smith -  dm - the dm
31396c699258SBarry Smith 
31406c699258SBarry Smith    Level: intermediate
31416c699258SBarry Smith 
31426c699258SBarry Smith 
31436c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
31446c699258SBarry Smith @*/
31457087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
31466c699258SBarry Smith {
31476c699258SBarry Smith   PetscErrorCode ierr;
3148089b2837SJed Brown   SNES           snes;
3149942e3340SBarry Smith   DMTS           tsdm;
31506c699258SBarry Smith 
31516c699258SBarry Smith   PetscFunctionBegin;
31520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
315370663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3154942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
31552a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3156942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3157942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
315824989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
315924989b8cSPeter Brune         tsdm->originaldm = dm;
316024989b8cSPeter Brune       }
316124989b8cSPeter Brune     }
31626bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
316324989b8cSPeter Brune   }
31646c699258SBarry Smith   ts->dm = dm;
3165bbd56ea5SKarl Rupp 
3166089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3167089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
31686c699258SBarry Smith   PetscFunctionReturn(0);
31696c699258SBarry Smith }
31706c699258SBarry Smith 
31716c699258SBarry Smith #undef __FUNCT__
31726c699258SBarry Smith #define __FUNCT__ "TSGetDM"
31736c699258SBarry Smith /*@
31746c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
31756c699258SBarry Smith 
31763f9fe445SBarry Smith    Not Collective
31776c699258SBarry Smith 
31786c699258SBarry Smith    Input Parameter:
31796c699258SBarry Smith . ts - the preconditioner context
31806c699258SBarry Smith 
31816c699258SBarry Smith    Output Parameter:
31826c699258SBarry Smith .  dm - the dm
31836c699258SBarry Smith 
31846c699258SBarry Smith    Level: intermediate
31856c699258SBarry Smith 
31866c699258SBarry Smith 
31876c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
31886c699258SBarry Smith @*/
31897087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
31906c699258SBarry Smith {
3191496e6a7aSJed Brown   PetscErrorCode ierr;
3192496e6a7aSJed Brown 
31936c699258SBarry Smith   PetscFunctionBegin;
31940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3195496e6a7aSJed Brown   if (!ts->dm) {
3196*ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
3197496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3198496e6a7aSJed Brown   }
31996c699258SBarry Smith   *dm = ts->dm;
32006c699258SBarry Smith   PetscFunctionReturn(0);
32016c699258SBarry Smith }
32021713a123SBarry Smith 
32030f5c6efeSJed Brown #undef __FUNCT__
32040f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
32050f5c6efeSJed Brown /*@
32060f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
32070f5c6efeSJed Brown 
32083f9fe445SBarry Smith    Logically Collective on SNES
32090f5c6efeSJed Brown 
32100f5c6efeSJed Brown    Input Parameter:
3211d42a1c89SJed Brown + snes - nonlinear solver
32120910c330SBarry Smith . U - the current state at which to evaluate the residual
3213d42a1c89SJed Brown - ctx - user context, must be a TS
32140f5c6efeSJed Brown 
32150f5c6efeSJed Brown    Output Parameter:
32160f5c6efeSJed Brown . F - the nonlinear residual
32170f5c6efeSJed Brown 
32180f5c6efeSJed Brown    Notes:
32190f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
32200f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
32210f5c6efeSJed Brown 
32220f5c6efeSJed Brown    Level: advanced
32230f5c6efeSJed Brown 
32240f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
32250f5c6efeSJed Brown @*/
32260910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
32270f5c6efeSJed Brown {
32280f5c6efeSJed Brown   TS             ts = (TS)ctx;
32290f5c6efeSJed Brown   PetscErrorCode ierr;
32300f5c6efeSJed Brown 
32310f5c6efeSJed Brown   PetscFunctionBegin;
32320f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
32330910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
32340f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
32350f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
32360910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
32370f5c6efeSJed Brown   PetscFunctionReturn(0);
32380f5c6efeSJed Brown }
32390f5c6efeSJed Brown 
32400f5c6efeSJed Brown #undef __FUNCT__
32410f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
32420f5c6efeSJed Brown /*@
32430f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
32440f5c6efeSJed Brown 
32450f5c6efeSJed Brown    Collective on SNES
32460f5c6efeSJed Brown 
32470f5c6efeSJed Brown    Input Parameter:
32480f5c6efeSJed Brown + snes - nonlinear solver
32490910c330SBarry Smith . U - the current state at which to evaluate the residual
32500f5c6efeSJed Brown - ctx - user context, must be a TS
32510f5c6efeSJed Brown 
32520f5c6efeSJed Brown    Output Parameter:
32530f5c6efeSJed Brown + A - the Jacobian
32540f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
32550f5c6efeSJed Brown - flag - indicates any structure change in the matrix
32560f5c6efeSJed Brown 
32570f5c6efeSJed Brown    Notes:
32580f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
32590f5c6efeSJed Brown 
32600f5c6efeSJed Brown    Level: developer
32610f5c6efeSJed Brown 
32620f5c6efeSJed Brown .seealso: SNESSetJacobian()
32630f5c6efeSJed Brown @*/
32640910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
32650f5c6efeSJed Brown {
32660f5c6efeSJed Brown   TS             ts = (TS)ctx;
32670f5c6efeSJed Brown   PetscErrorCode ierr;
32680f5c6efeSJed Brown 
32690f5c6efeSJed Brown   PetscFunctionBegin;
32700f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
32710910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
32720f5c6efeSJed Brown   PetscValidPointer(A,3);
32730f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
32740f5c6efeSJed Brown   PetscValidPointer(B,4);
32750f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
32760f5c6efeSJed Brown   PetscValidPointer(flag,5);
32770f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
32780910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
32790f5c6efeSJed Brown   PetscFunctionReturn(0);
32800f5c6efeSJed Brown }
3281325fc9f4SBarry Smith 
32820e4ef248SJed Brown #undef __FUNCT__
32830e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
32840e4ef248SJed Brown /*@C
32850e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
32860e4ef248SJed Brown 
32870e4ef248SJed Brown    Collective on TS
32880e4ef248SJed Brown 
32890e4ef248SJed Brown    Input Arguments:
32900e4ef248SJed Brown +  ts - time stepping context
32910e4ef248SJed Brown .  t - time at which to evaluate
32920910c330SBarry Smith .  U - state at which to evaluate
32930e4ef248SJed Brown -  ctx - context
32940e4ef248SJed Brown 
32950e4ef248SJed Brown    Output Arguments:
32960e4ef248SJed Brown .  F - right hand side
32970e4ef248SJed Brown 
32980e4ef248SJed Brown    Level: intermediate
32990e4ef248SJed Brown 
33000e4ef248SJed Brown    Notes:
33010e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
33020e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
33030e4ef248SJed Brown 
33040e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
33050e4ef248SJed Brown @*/
33060910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
33070e4ef248SJed Brown {
33080e4ef248SJed Brown   PetscErrorCode ierr;
33090e4ef248SJed Brown   Mat            Arhs,Brhs;
33100e4ef248SJed Brown   MatStructure   flg2;
33110e4ef248SJed Brown 
33120e4ef248SJed Brown   PetscFunctionBegin;
33130e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
33140910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
33150910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
33160e4ef248SJed Brown   PetscFunctionReturn(0);
33170e4ef248SJed Brown }
33180e4ef248SJed Brown 
33190e4ef248SJed Brown #undef __FUNCT__
33200e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
33210e4ef248SJed Brown /*@C
33220e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
33230e4ef248SJed Brown 
33240e4ef248SJed Brown    Collective on TS
33250e4ef248SJed Brown 
33260e4ef248SJed Brown    Input Arguments:
33270e4ef248SJed Brown +  ts - time stepping context
33280e4ef248SJed Brown .  t - time at which to evaluate
33290910c330SBarry Smith .  U - state at which to evaluate
33300e4ef248SJed Brown -  ctx - context
33310e4ef248SJed Brown 
33320e4ef248SJed Brown    Output Arguments:
33330e4ef248SJed Brown +  A - pointer to operator
33340e4ef248SJed Brown .  B - pointer to preconditioning matrix
33350e4ef248SJed Brown -  flg - matrix structure flag
33360e4ef248SJed Brown 
33370e4ef248SJed Brown    Level: intermediate
33380e4ef248SJed Brown 
33390e4ef248SJed Brown    Notes:
33400e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
33410e4ef248SJed Brown 
33420e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
33430e4ef248SJed Brown @*/
33440910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
33450e4ef248SJed Brown {
33460e4ef248SJed Brown   PetscFunctionBegin;
33470e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
33480e4ef248SJed Brown   PetscFunctionReturn(0);
33490e4ef248SJed Brown }
33500e4ef248SJed Brown 
33510026cea9SSean Farley #undef __FUNCT__
33520026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
33530026cea9SSean Farley /*@C
33540026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
33550026cea9SSean Farley 
33560026cea9SSean Farley    Collective on TS
33570026cea9SSean Farley 
33580026cea9SSean Farley    Input Arguments:
33590026cea9SSean Farley +  ts - time stepping context
33600026cea9SSean Farley .  t - time at which to evaluate
33610910c330SBarry Smith .  U - state at which to evaluate
33620910c330SBarry Smith .  Udot - time derivative of state vector
33630026cea9SSean Farley -  ctx - context
33640026cea9SSean Farley 
33650026cea9SSean Farley    Output Arguments:
33660026cea9SSean Farley .  F - left hand side
33670026cea9SSean Farley 
33680026cea9SSean Farley    Level: intermediate
33690026cea9SSean Farley 
33700026cea9SSean Farley    Notes:
33710910c330SBarry 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
33720026cea9SSean Farley    user is required to write their own TSComputeIFunction.
33730026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
33740026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
33750026cea9SSean Farley 
33760026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
33770026cea9SSean Farley @*/
33780910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
33790026cea9SSean Farley {
33800026cea9SSean Farley   PetscErrorCode ierr;
33810026cea9SSean Farley   Mat            A,B;
33820026cea9SSean Farley   MatStructure   flg2;
33830026cea9SSean Farley 
33840026cea9SSean Farley   PetscFunctionBegin;
33850298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
33860910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
33870910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
33880026cea9SSean Farley   PetscFunctionReturn(0);
33890026cea9SSean Farley }
33900026cea9SSean Farley 
33910026cea9SSean Farley #undef __FUNCT__
33920026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
33930026cea9SSean Farley /*@C
339424e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
33950026cea9SSean Farley 
33960026cea9SSean Farley    Collective on TS
33970026cea9SSean Farley 
33980026cea9SSean Farley    Input Arguments:
33990026cea9SSean Farley +  ts - time stepping context
34000026cea9SSean Farley .  t - time at which to evaluate
34010910c330SBarry Smith .  U - state at which to evaluate
34020910c330SBarry Smith .  Udot - time derivative of state vector
34030026cea9SSean Farley .  shift - shift to apply
34040026cea9SSean Farley -  ctx - context
34050026cea9SSean Farley 
34060026cea9SSean Farley    Output Arguments:
34070026cea9SSean Farley +  A - pointer to operator
34080026cea9SSean Farley .  B - pointer to preconditioning matrix
34090026cea9SSean Farley -  flg - matrix structure flag
34100026cea9SSean Farley 
34110026cea9SSean Farley    Level: intermediate
34120026cea9SSean Farley 
34130026cea9SSean Farley    Notes:
34140026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
34150026cea9SSean Farley 
34160026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
34170026cea9SSean Farley @*/
34180910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
34190026cea9SSean Farley {
34200026cea9SSean Farley   PetscFunctionBegin;
34210026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
34220026cea9SSean Farley   PetscFunctionReturn(0);
34230026cea9SSean Farley }
3424e817cc15SEmil Constantinescu #undef __FUNCT__
3425e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3426e817cc15SEmil Constantinescu /*@
3427e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3428e817cc15SEmil Constantinescu 
3429e817cc15SEmil Constantinescu    Not Collective
3430e817cc15SEmil Constantinescu 
3431e817cc15SEmil Constantinescu    Input Parameter:
3432e817cc15SEmil Constantinescu .  ts - the TS context
3433e817cc15SEmil Constantinescu 
3434e817cc15SEmil Constantinescu    Output Parameter:
3435e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3436e817cc15SEmil Constantinescu 
3437e817cc15SEmil Constantinescu    Level: beginner
3438e817cc15SEmil Constantinescu 
3439e817cc15SEmil Constantinescu .keywords: TS, equation type
3440e817cc15SEmil Constantinescu 
3441e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3442e817cc15SEmil Constantinescu @*/
3443e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3444e817cc15SEmil Constantinescu {
3445e817cc15SEmil Constantinescu   PetscFunctionBegin;
3446e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3447e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3448e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3449e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3450e817cc15SEmil Constantinescu }
3451e817cc15SEmil Constantinescu 
3452e817cc15SEmil Constantinescu #undef __FUNCT__
3453e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3454e817cc15SEmil Constantinescu /*@
3455e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3456e817cc15SEmil Constantinescu 
3457e817cc15SEmil Constantinescu    Not Collective
3458e817cc15SEmil Constantinescu 
3459e817cc15SEmil Constantinescu    Input Parameter:
3460e817cc15SEmil Constantinescu +  ts - the TS context
3461e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3462e817cc15SEmil Constantinescu 
3463e817cc15SEmil Constantinescu    Level: advanced
3464e817cc15SEmil Constantinescu 
3465e817cc15SEmil Constantinescu .keywords: TS, equation type
3466e817cc15SEmil Constantinescu 
3467e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3468e817cc15SEmil Constantinescu @*/
3469e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3470e817cc15SEmil Constantinescu {
3471e817cc15SEmil Constantinescu   PetscFunctionBegin;
3472e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3473e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3474e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3475e817cc15SEmil Constantinescu }
34760026cea9SSean Farley 
34774af1b03aSJed Brown #undef __FUNCT__
34784af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
34794af1b03aSJed Brown /*@
34804af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
34814af1b03aSJed Brown 
34824af1b03aSJed Brown    Not Collective
34834af1b03aSJed Brown 
34844af1b03aSJed Brown    Input Parameter:
34854af1b03aSJed Brown .  ts - the TS context
34864af1b03aSJed Brown 
34874af1b03aSJed Brown    Output Parameter:
34884af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
34894af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
34904af1b03aSJed Brown 
3491487e0bb9SJed Brown    Level: beginner
34924af1b03aSJed Brown 
3493cd652676SJed Brown    Notes:
3494cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
34954af1b03aSJed Brown 
34964af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
34974af1b03aSJed Brown 
34984af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
34994af1b03aSJed Brown @*/
35004af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
35014af1b03aSJed Brown {
35024af1b03aSJed Brown   PetscFunctionBegin;
35034af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35044af1b03aSJed Brown   PetscValidPointer(reason,2);
35054af1b03aSJed Brown   *reason = ts->reason;
35064af1b03aSJed Brown   PetscFunctionReturn(0);
35074af1b03aSJed Brown }
35084af1b03aSJed Brown 
3509fb1732b5SBarry Smith #undef __FUNCT__
3510d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3511d6ad946cSShri Abhyankar /*@
3512d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3513d6ad946cSShri Abhyankar 
3514d6ad946cSShri Abhyankar    Not Collective
3515d6ad946cSShri Abhyankar 
3516d6ad946cSShri Abhyankar    Input Parameter:
3517d6ad946cSShri Abhyankar +  ts - the TS context
3518d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3519d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3520d6ad946cSShri Abhyankar 
3521f5abba47SShri Abhyankar    Level: advanced
3522d6ad946cSShri Abhyankar 
3523d6ad946cSShri Abhyankar    Notes:
3524d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3525d6ad946cSShri Abhyankar 
3526d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3527d6ad946cSShri Abhyankar 
3528d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3529d6ad946cSShri Abhyankar @*/
3530d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3531d6ad946cSShri Abhyankar {
3532d6ad946cSShri Abhyankar   PetscFunctionBegin;
3533d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3534d6ad946cSShri Abhyankar   ts->reason = reason;
3535d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3536d6ad946cSShri Abhyankar }
3537d6ad946cSShri Abhyankar 
3538d6ad946cSShri Abhyankar #undef __FUNCT__
3539cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3540cc708dedSBarry Smith /*@
3541cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3542cc708dedSBarry Smith 
3543cc708dedSBarry Smith    Not Collective
3544cc708dedSBarry Smith 
3545cc708dedSBarry Smith    Input Parameter:
3546cc708dedSBarry Smith .  ts - the TS context
3547cc708dedSBarry Smith 
3548cc708dedSBarry Smith    Output Parameter:
3549cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3550cc708dedSBarry Smith 
3551487e0bb9SJed Brown    Level: beginner
3552cc708dedSBarry Smith 
3553cc708dedSBarry Smith    Notes:
3554cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3555cc708dedSBarry Smith 
3556cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3557cc708dedSBarry Smith 
3558cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3559cc708dedSBarry Smith @*/
3560cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3561cc708dedSBarry Smith {
3562cc708dedSBarry Smith   PetscFunctionBegin;
3563cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3564cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3565cc708dedSBarry Smith   *ftime = ts->solvetime;
3566cc708dedSBarry Smith   PetscFunctionReturn(0);
3567cc708dedSBarry Smith }
3568cc708dedSBarry Smith 
3569cc708dedSBarry Smith #undef __FUNCT__
35705ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
35719f67acb7SJed Brown /*@
35725ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
35739f67acb7SJed Brown    used by the time integrator.
35749f67acb7SJed Brown 
35759f67acb7SJed Brown    Not Collective
35769f67acb7SJed Brown 
35779f67acb7SJed Brown    Input Parameter:
35789f67acb7SJed Brown .  ts - TS context
35799f67acb7SJed Brown 
35809f67acb7SJed Brown    Output Parameter:
35819f67acb7SJed Brown .  nits - number of nonlinear iterations
35829f67acb7SJed Brown 
35839f67acb7SJed Brown    Notes:
35849f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
35859f67acb7SJed Brown 
35869f67acb7SJed Brown    Level: intermediate
35879f67acb7SJed Brown 
35889f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
35899f67acb7SJed Brown 
35905ef26d82SJed Brown .seealso:  TSGetKSPIterations()
35919f67acb7SJed Brown @*/
35925ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
35939f67acb7SJed Brown {
35949f67acb7SJed Brown   PetscFunctionBegin;
35959f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35969f67acb7SJed Brown   PetscValidIntPointer(nits,2);
35975ef26d82SJed Brown   *nits = ts->snes_its;
35989f67acb7SJed Brown   PetscFunctionReturn(0);
35999f67acb7SJed Brown }
36009f67acb7SJed Brown 
36019f67acb7SJed Brown #undef __FUNCT__
36025ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
36039f67acb7SJed Brown /*@
36045ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
36059f67acb7SJed Brown    used by the time integrator.
36069f67acb7SJed Brown 
36079f67acb7SJed Brown    Not Collective
36089f67acb7SJed Brown 
36099f67acb7SJed Brown    Input Parameter:
36109f67acb7SJed Brown .  ts - TS context
36119f67acb7SJed Brown 
36129f67acb7SJed Brown    Output Parameter:
36139f67acb7SJed Brown .  lits - number of linear iterations
36149f67acb7SJed Brown 
36159f67acb7SJed Brown    Notes:
36169f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
36179f67acb7SJed Brown 
36189f67acb7SJed Brown    Level: intermediate
36199f67acb7SJed Brown 
36209f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
36219f67acb7SJed Brown 
36225ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
36239f67acb7SJed Brown @*/
36245ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
36259f67acb7SJed Brown {
36269f67acb7SJed Brown   PetscFunctionBegin;
36279f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36289f67acb7SJed Brown   PetscValidIntPointer(lits,2);
36295ef26d82SJed Brown   *lits = ts->ksp_its;
36309f67acb7SJed Brown   PetscFunctionReturn(0);
36319f67acb7SJed Brown }
36329f67acb7SJed Brown 
36339f67acb7SJed Brown #undef __FUNCT__
3634cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3635cef5090cSJed Brown /*@
3636cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3637cef5090cSJed Brown 
3638cef5090cSJed Brown    Not Collective
3639cef5090cSJed Brown 
3640cef5090cSJed Brown    Input Parameter:
3641cef5090cSJed Brown .  ts - TS context
3642cef5090cSJed Brown 
3643cef5090cSJed Brown    Output Parameter:
3644cef5090cSJed Brown .  rejects - number of steps rejected
3645cef5090cSJed Brown 
3646cef5090cSJed Brown    Notes:
3647cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3648cef5090cSJed Brown 
3649cef5090cSJed Brown    Level: intermediate
3650cef5090cSJed Brown 
3651cef5090cSJed Brown .keywords: TS, get, number
3652cef5090cSJed Brown 
36535ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3654cef5090cSJed Brown @*/
3655cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3656cef5090cSJed Brown {
3657cef5090cSJed Brown   PetscFunctionBegin;
3658cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3659cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3660cef5090cSJed Brown   *rejects = ts->reject;
3661cef5090cSJed Brown   PetscFunctionReturn(0);
3662cef5090cSJed Brown }
3663cef5090cSJed Brown 
3664cef5090cSJed Brown #undef __FUNCT__
3665cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3666cef5090cSJed Brown /*@
3667cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3668cef5090cSJed Brown 
3669cef5090cSJed Brown    Not Collective
3670cef5090cSJed Brown 
3671cef5090cSJed Brown    Input Parameter:
3672cef5090cSJed Brown .  ts - TS context
3673cef5090cSJed Brown 
3674cef5090cSJed Brown    Output Parameter:
3675cef5090cSJed Brown .  fails - number of failed nonlinear solves
3676cef5090cSJed Brown 
3677cef5090cSJed Brown    Notes:
3678cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3679cef5090cSJed Brown 
3680cef5090cSJed Brown    Level: intermediate
3681cef5090cSJed Brown 
3682cef5090cSJed Brown .keywords: TS, get, number
3683cef5090cSJed Brown 
36845ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3685cef5090cSJed Brown @*/
3686cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3687cef5090cSJed Brown {
3688cef5090cSJed Brown   PetscFunctionBegin;
3689cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3690cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3691cef5090cSJed Brown   *fails = ts->num_snes_failures;
3692cef5090cSJed Brown   PetscFunctionReturn(0);
3693cef5090cSJed Brown }
3694cef5090cSJed Brown 
3695cef5090cSJed Brown #undef __FUNCT__
3696cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3697cef5090cSJed Brown /*@
3698cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3699cef5090cSJed Brown 
3700cef5090cSJed Brown    Not Collective
3701cef5090cSJed Brown 
3702cef5090cSJed Brown    Input Parameter:
3703cef5090cSJed Brown +  ts - TS context
3704cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3705cef5090cSJed Brown 
3706cef5090cSJed Brown    Notes:
3707cef5090cSJed Brown    The counter is reset to zero for each step
3708cef5090cSJed Brown 
3709cef5090cSJed Brown    Options Database Key:
3710cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3711cef5090cSJed Brown 
3712cef5090cSJed Brown    Level: intermediate
3713cef5090cSJed Brown 
3714cef5090cSJed Brown .keywords: TS, set, maximum, number
3715cef5090cSJed Brown 
37165ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3717cef5090cSJed Brown @*/
3718cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3719cef5090cSJed Brown {
3720cef5090cSJed Brown   PetscFunctionBegin;
3721cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3722cef5090cSJed Brown   ts->max_reject = rejects;
3723cef5090cSJed Brown   PetscFunctionReturn(0);
3724cef5090cSJed Brown }
3725cef5090cSJed Brown 
3726cef5090cSJed Brown #undef __FUNCT__
3727cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3728cef5090cSJed Brown /*@
3729cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3730cef5090cSJed Brown 
3731cef5090cSJed Brown    Not Collective
3732cef5090cSJed Brown 
3733cef5090cSJed Brown    Input Parameter:
3734cef5090cSJed Brown +  ts - TS context
3735cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3736cef5090cSJed Brown 
3737cef5090cSJed Brown    Notes:
3738cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3739cef5090cSJed Brown 
3740cef5090cSJed Brown    Options Database Key:
3741cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3742cef5090cSJed Brown 
3743cef5090cSJed Brown    Level: intermediate
3744cef5090cSJed Brown 
3745cef5090cSJed Brown .keywords: TS, set, maximum, number
3746cef5090cSJed Brown 
37475ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3748cef5090cSJed Brown @*/
3749cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3750cef5090cSJed Brown {
3751cef5090cSJed Brown   PetscFunctionBegin;
3752cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3753cef5090cSJed Brown   ts->max_snes_failures = fails;
3754cef5090cSJed Brown   PetscFunctionReturn(0);
3755cef5090cSJed Brown }
3756cef5090cSJed Brown 
3757cef5090cSJed Brown #undef __FUNCT__
3758cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3759cef5090cSJed Brown /*@
3760cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3761cef5090cSJed Brown 
3762cef5090cSJed Brown    Not Collective
3763cef5090cSJed Brown 
3764cef5090cSJed Brown    Input Parameter:
3765cef5090cSJed Brown +  ts - TS context
3766cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3767cef5090cSJed Brown 
3768cef5090cSJed Brown    Options Database Key:
3769cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3770cef5090cSJed Brown 
3771cef5090cSJed Brown    Level: intermediate
3772cef5090cSJed Brown 
3773cef5090cSJed Brown .keywords: TS, set, error
3774cef5090cSJed Brown 
37755ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3776cef5090cSJed Brown @*/
3777cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3778cef5090cSJed Brown {
3779cef5090cSJed Brown   PetscFunctionBegin;
3780cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3781cef5090cSJed Brown   ts->errorifstepfailed = err;
3782cef5090cSJed Brown   PetscFunctionReturn(0);
3783cef5090cSJed Brown }
3784cef5090cSJed Brown 
3785cef5090cSJed Brown #undef __FUNCT__
3786fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3787fb1732b5SBarry Smith /*@C
3788fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3789fb1732b5SBarry Smith 
3790fb1732b5SBarry Smith    Collective on TS
3791fb1732b5SBarry Smith 
3792fb1732b5SBarry Smith    Input Parameters:
3793fb1732b5SBarry Smith +  ts - the TS context
3794fb1732b5SBarry Smith .  step - current time-step
3795fb1732b5SBarry Smith .  ptime - current time
37960910c330SBarry Smith .  u - current state
3797fb1732b5SBarry Smith -  viewer - binary viewer
3798fb1732b5SBarry Smith 
3799fb1732b5SBarry Smith    Level: intermediate
3800fb1732b5SBarry Smith 
3801fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3802fb1732b5SBarry Smith 
3803fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3804fb1732b5SBarry Smith @*/
38050910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3806fb1732b5SBarry Smith {
3807fb1732b5SBarry Smith   PetscErrorCode ierr;
3808ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3809fb1732b5SBarry Smith 
3810fb1732b5SBarry Smith   PetscFunctionBegin;
38110910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3812ed81e22dSJed Brown   PetscFunctionReturn(0);
3813ed81e22dSJed Brown }
3814ed81e22dSJed Brown 
3815ed81e22dSJed Brown #undef __FUNCT__
3816ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3817ed81e22dSJed Brown /*@C
3818ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3819ed81e22dSJed Brown 
3820ed81e22dSJed Brown    Collective on TS
3821ed81e22dSJed Brown 
3822ed81e22dSJed Brown    Input Parameters:
3823ed81e22dSJed Brown +  ts - the TS context
3824ed81e22dSJed Brown .  step - current time-step
3825ed81e22dSJed Brown .  ptime - current time
38260910c330SBarry Smith .  u - current state
3827ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3828ed81e22dSJed Brown 
3829ed81e22dSJed Brown    Level: intermediate
3830ed81e22dSJed Brown 
3831ed81e22dSJed Brown    Notes:
3832ed81e22dSJed 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.
3833ed81e22dSJed Brown    These are named according to the file name template.
3834ed81e22dSJed Brown 
3835ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3836ed81e22dSJed Brown 
3837ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3838ed81e22dSJed Brown 
3839ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3840ed81e22dSJed Brown @*/
38410910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3842ed81e22dSJed Brown {
3843ed81e22dSJed Brown   PetscErrorCode ierr;
3844ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3845ed81e22dSJed Brown   PetscViewer    viewer;
3846ed81e22dSJed Brown 
3847ed81e22dSJed Brown   PetscFunctionBegin;
38488caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3849*ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
38500910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3851ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3852ed81e22dSJed Brown   PetscFunctionReturn(0);
3853ed81e22dSJed Brown }
3854ed81e22dSJed Brown 
3855ed81e22dSJed Brown #undef __FUNCT__
3856ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3857ed81e22dSJed Brown /*@C
3858ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3859ed81e22dSJed Brown 
3860ed81e22dSJed Brown    Collective on TS
3861ed81e22dSJed Brown 
3862ed81e22dSJed Brown    Input Parameters:
3863ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3864ed81e22dSJed Brown 
3865ed81e22dSJed Brown    Level: intermediate
3866ed81e22dSJed Brown 
3867ed81e22dSJed Brown    Note:
3868ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3869ed81e22dSJed Brown 
3870ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3871ed81e22dSJed Brown 
3872ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3873ed81e22dSJed Brown @*/
3874ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3875ed81e22dSJed Brown {
3876ed81e22dSJed Brown   PetscErrorCode ierr;
3877ed81e22dSJed Brown 
3878ed81e22dSJed Brown   PetscFunctionBegin;
3879ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3880fb1732b5SBarry Smith   PetscFunctionReturn(0);
3881fb1732b5SBarry Smith }
3882fb1732b5SBarry Smith 
388384df9cb4SJed Brown #undef __FUNCT__
3884ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt"
388584df9cb4SJed Brown /*@
3886ad6bc421SBarry Smith    TSGetTSAdapt - Get the adaptive controller context for the current method
388784df9cb4SJed Brown 
3888ed81e22dSJed Brown    Collective on TS if controller has not been created yet
388984df9cb4SJed Brown 
389084df9cb4SJed Brown    Input Arguments:
3891ed81e22dSJed Brown .  ts - time stepping context
389284df9cb4SJed Brown 
389384df9cb4SJed Brown    Output Arguments:
3894ed81e22dSJed Brown .  adapt - adaptive controller
389584df9cb4SJed Brown 
389684df9cb4SJed Brown    Level: intermediate
389784df9cb4SJed Brown 
3898ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
389984df9cb4SJed Brown @*/
3900ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt)
390184df9cb4SJed Brown {
390284df9cb4SJed Brown   PetscErrorCode ierr;
390384df9cb4SJed Brown 
390484df9cb4SJed Brown   PetscFunctionBegin;
390584df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
390684df9cb4SJed Brown   PetscValidPointer(adapt,2);
390784df9cb4SJed Brown   if (!ts->adapt) {
3908*ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
39091c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
39101c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
391184df9cb4SJed Brown   }
391284df9cb4SJed Brown   *adapt = ts->adapt;
391384df9cb4SJed Brown   PetscFunctionReturn(0);
391484df9cb4SJed Brown }
3915d6ebe24aSShri Abhyankar 
3916d6ebe24aSShri Abhyankar #undef __FUNCT__
39171c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
39181c3436cfSJed Brown /*@
39191c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
39201c3436cfSJed Brown 
39211c3436cfSJed Brown    Logically Collective
39221c3436cfSJed Brown 
39231c3436cfSJed Brown    Input Arguments:
39241c3436cfSJed Brown +  ts - time integration context
39251c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
39260298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
39271c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
39280298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
39291c3436cfSJed Brown 
39301c3436cfSJed Brown    Level: beginner
39311c3436cfSJed Brown 
3932c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
39331c3436cfSJed Brown @*/
39341c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
39351c3436cfSJed Brown {
39361c3436cfSJed Brown   PetscErrorCode ierr;
39371c3436cfSJed Brown 
39381c3436cfSJed Brown   PetscFunctionBegin;
3939c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
39401c3436cfSJed Brown   if (vatol) {
39411c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
39421c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
3943bbd56ea5SKarl Rupp 
39441c3436cfSJed Brown     ts->vatol = vatol;
39451c3436cfSJed Brown   }
3946c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
39471c3436cfSJed Brown   if (vrtol) {
39481c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
39491c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
3950bbd56ea5SKarl Rupp 
39511c3436cfSJed Brown     ts->vrtol = vrtol;
39521c3436cfSJed Brown   }
39531c3436cfSJed Brown   PetscFunctionReturn(0);
39541c3436cfSJed Brown }
39551c3436cfSJed Brown 
39561c3436cfSJed Brown #undef __FUNCT__
3957c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3958c5033834SJed Brown /*@
3959c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3960c5033834SJed Brown 
3961c5033834SJed Brown    Logically Collective
3962c5033834SJed Brown 
3963c5033834SJed Brown    Input Arguments:
3964c5033834SJed Brown .  ts - time integration context
3965c5033834SJed Brown 
3966c5033834SJed Brown    Output Arguments:
39670298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
39680298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
39690298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
39700298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
3971c5033834SJed Brown 
3972c5033834SJed Brown    Level: beginner
3973c5033834SJed Brown 
3974c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3975c5033834SJed Brown @*/
3976c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3977c5033834SJed Brown {
3978c5033834SJed Brown   PetscFunctionBegin;
3979c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3980c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3981c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3982c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3983c5033834SJed Brown   PetscFunctionReturn(0);
3984c5033834SJed Brown }
3985c5033834SJed Brown 
3986c5033834SJed Brown #undef __FUNCT__
39871c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
39881c3436cfSJed Brown /*@
39891c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
39901c3436cfSJed Brown 
39911c3436cfSJed Brown    Collective on TS
39921c3436cfSJed Brown 
39931c3436cfSJed Brown    Input Arguments:
39941c3436cfSJed Brown +  ts - time stepping context
39951c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
39961c3436cfSJed Brown 
39971c3436cfSJed Brown    Output Arguments:
39981c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
39991c3436cfSJed Brown 
40001c3436cfSJed Brown    Level: developer
40011c3436cfSJed Brown 
40021c3436cfSJed Brown .seealso: TSSetTolerances()
40031c3436cfSJed Brown @*/
40041c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
40051c3436cfSJed Brown {
40061c3436cfSJed Brown   PetscErrorCode    ierr;
40071c3436cfSJed Brown   PetscInt          i,n,N;
40080910c330SBarry Smith   const PetscScalar *u,*y;
40090910c330SBarry Smith   Vec               U;
40101c3436cfSJed Brown   PetscReal         sum,gsum;
40111c3436cfSJed Brown 
40121c3436cfSJed Brown   PetscFunctionBegin;
40131c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40141c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
40151c3436cfSJed Brown   PetscValidPointer(norm,3);
40160910c330SBarry Smith   U = ts->vec_sol;
40170910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4018*ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
40191c3436cfSJed Brown 
40200910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
40210910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
40220910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
40231c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
40241c3436cfSJed Brown   sum  = 0.;
4025ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4026ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4027ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4028ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4029ceefc113SJed Brown     for (i=0; i<n; i++) {
40300910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40310910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4032ceefc113SJed Brown     }
4033ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4034ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4035ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4036ceefc113SJed Brown     const PetscScalar *atol;
4037ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4038ceefc113SJed Brown     for (i=0; i<n; i++) {
40390910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40400910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4041ceefc113SJed Brown     }
4042ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4043ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4044ceefc113SJed Brown     const PetscScalar *rtol;
4045ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4046ceefc113SJed Brown     for (i=0; i<n; i++) {
40470910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40480910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4049ceefc113SJed Brown     }
4050ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4051ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
40521c3436cfSJed Brown     for (i=0; i<n; i++) {
40530910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
40540910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
40551c3436cfSJed Brown     }
4056ceefc113SJed Brown   }
40570910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
40581c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
40591c3436cfSJed Brown 
4060*ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
40611c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4062*ce94432eSBarry Smith   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
40631c3436cfSJed Brown   PetscFunctionReturn(0);
40641c3436cfSJed Brown }
40651c3436cfSJed Brown 
40661c3436cfSJed Brown #undef __FUNCT__
40678d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
40688d59e960SJed Brown /*@
40698d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
40708d59e960SJed Brown 
40718d59e960SJed Brown    Logically Collective on TS
40728d59e960SJed Brown 
40738d59e960SJed Brown    Input Arguments:
40748d59e960SJed Brown +  ts - time stepping context
40758d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
40768d59e960SJed Brown 
40778d59e960SJed Brown    Note:
40788d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
40798d59e960SJed Brown 
40808d59e960SJed Brown    Level: intermediate
40818d59e960SJed Brown 
40828d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
40838d59e960SJed Brown @*/
40848d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
40858d59e960SJed Brown {
40868d59e960SJed Brown   PetscFunctionBegin;
40878d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40888d59e960SJed Brown   ts->cfltime_local = cfltime;
40898d59e960SJed Brown   ts->cfltime       = -1.;
40908d59e960SJed Brown   PetscFunctionReturn(0);
40918d59e960SJed Brown }
40928d59e960SJed Brown 
40938d59e960SJed Brown #undef __FUNCT__
40948d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
40958d59e960SJed Brown /*@
40968d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
40978d59e960SJed Brown 
40988d59e960SJed Brown    Collective on TS
40998d59e960SJed Brown 
41008d59e960SJed Brown    Input Arguments:
41018d59e960SJed Brown .  ts - time stepping context
41028d59e960SJed Brown 
41038d59e960SJed Brown    Output Arguments:
41048d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
41058d59e960SJed Brown 
41068d59e960SJed Brown    Level: advanced
41078d59e960SJed Brown 
41088d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
41098d59e960SJed Brown @*/
41108d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
41118d59e960SJed Brown {
41128d59e960SJed Brown   PetscErrorCode ierr;
41138d59e960SJed Brown 
41148d59e960SJed Brown   PetscFunctionBegin;
41158d59e960SJed Brown   if (ts->cfltime < 0) {
4116*ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
41178d59e960SJed Brown   }
41188d59e960SJed Brown   *cfltime = ts->cfltime;
41198d59e960SJed Brown   PetscFunctionReturn(0);
41208d59e960SJed Brown }
41218d59e960SJed Brown 
41228d59e960SJed Brown #undef __FUNCT__
4123d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4124d6ebe24aSShri Abhyankar /*@
4125d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4126d6ebe24aSShri Abhyankar 
4127d6ebe24aSShri Abhyankar    Input Parameters:
4128d6ebe24aSShri Abhyankar .  ts   - the TS context.
4129d6ebe24aSShri Abhyankar .  xl   - lower bound.
4130d6ebe24aSShri Abhyankar .  xu   - upper bound.
4131d6ebe24aSShri Abhyankar 
4132d6ebe24aSShri Abhyankar    Notes:
4133d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
413433c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4135d6ebe24aSShri Abhyankar 
41362bd2b0e6SSatish Balay    Level: advanced
41372bd2b0e6SSatish Balay 
4138d6ebe24aSShri Abhyankar @*/
4139d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4140d6ebe24aSShri Abhyankar {
4141d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4142d6ebe24aSShri Abhyankar   SNES           snes;
4143d6ebe24aSShri Abhyankar 
4144d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4145d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4146d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4147d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4148d6ebe24aSShri Abhyankar }
4149d6ebe24aSShri Abhyankar 
4150325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4151c6db04a5SJed Brown #include <mex.h>
4152325fc9f4SBarry Smith 
4153325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4154325fc9f4SBarry Smith 
4155325fc9f4SBarry Smith #undef __FUNCT__
4156325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4157325fc9f4SBarry Smith /*
4158325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4159325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4160325fc9f4SBarry Smith 
4161325fc9f4SBarry Smith    Collective on TS
4162325fc9f4SBarry Smith 
4163325fc9f4SBarry Smith    Input Parameters:
4164325fc9f4SBarry Smith +  snes - the TS context
41650910c330SBarry Smith -  u - input vector
4166325fc9f4SBarry Smith 
4167325fc9f4SBarry Smith    Output Parameter:
4168325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4169325fc9f4SBarry Smith 
4170325fc9f4SBarry Smith    Notes:
4171325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4172325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4173325fc9f4SBarry Smith    themselves.
4174325fc9f4SBarry Smith 
4175325fc9f4SBarry Smith    Level: developer
4176325fc9f4SBarry Smith 
4177325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4178325fc9f4SBarry Smith 
4179325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4180325fc9f4SBarry Smith */
41810910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4182325fc9f4SBarry Smith {
4183325fc9f4SBarry Smith   PetscErrorCode  ierr;
4184325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4185325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4186325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4187325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4188325fc9f4SBarry Smith 
4189325fc9f4SBarry Smith   PetscFunctionBegin;
4190325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
41910910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
41920910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4193325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
41940910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4195325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4196325fc9f4SBarry Smith 
4197325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
41980910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
41990910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
42000910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4201bbd56ea5SKarl Rupp 
4202325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4203325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4204325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4205325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4206325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4207325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4208325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4209325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4210325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4211325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4212325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4213325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4214325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4215325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4216325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4217325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4218325fc9f4SBarry Smith   PetscFunctionReturn(0);
4219325fc9f4SBarry Smith }
4220325fc9f4SBarry Smith 
4221325fc9f4SBarry Smith 
4222325fc9f4SBarry Smith #undef __FUNCT__
4223325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4224325fc9f4SBarry Smith /*
4225325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4226325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4227e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4228325fc9f4SBarry Smith 
4229325fc9f4SBarry Smith    Logically Collective on TS
4230325fc9f4SBarry Smith 
4231325fc9f4SBarry Smith    Input Parameters:
4232325fc9f4SBarry Smith +  ts - the TS context
4233325fc9f4SBarry Smith -  func - function evaluation routine
4234325fc9f4SBarry Smith 
4235325fc9f4SBarry Smith    Calling sequence of func:
42360910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4237325fc9f4SBarry Smith 
4238325fc9f4SBarry Smith    Level: beginner
4239325fc9f4SBarry Smith 
4240325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4241325fc9f4SBarry Smith 
4242325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4243325fc9f4SBarry Smith */
4244cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4245325fc9f4SBarry Smith {
4246325fc9f4SBarry Smith   PetscErrorCode  ierr;
4247325fc9f4SBarry Smith   TSMatlabContext *sctx;
4248325fc9f4SBarry Smith 
4249325fc9f4SBarry Smith   PetscFunctionBegin;
4250325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4251325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4252325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4253325fc9f4SBarry Smith   /*
4254325fc9f4SBarry Smith      This should work, but it doesn't
4255325fc9f4SBarry Smith   sctx->ctx = ctx;
4256325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4257325fc9f4SBarry Smith   */
4258325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4259bbd56ea5SKarl Rupp 
42600298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4261325fc9f4SBarry Smith   PetscFunctionReturn(0);
4262325fc9f4SBarry Smith }
4263325fc9f4SBarry Smith 
4264325fc9f4SBarry Smith #undef __FUNCT__
4265325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4266325fc9f4SBarry Smith /*
4267325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4268325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4269325fc9f4SBarry Smith 
4270325fc9f4SBarry Smith    Collective on TS
4271325fc9f4SBarry Smith 
4272325fc9f4SBarry Smith    Input Parameters:
4273cdcf91faSSean Farley +  ts - the TS context
42740910c330SBarry Smith .  u - input vector
4275325fc9f4SBarry Smith .  A, B - the matrices
4276325fc9f4SBarry Smith -  ctx - user context
4277325fc9f4SBarry Smith 
4278325fc9f4SBarry Smith    Output Parameter:
4279325fc9f4SBarry Smith .  flag - structure of the matrix
4280325fc9f4SBarry Smith 
4281325fc9f4SBarry Smith    Level: developer
4282325fc9f4SBarry Smith 
4283325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4284325fc9f4SBarry Smith 
4285325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4286325fc9f4SBarry Smith @*/
42870910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4288325fc9f4SBarry Smith {
4289325fc9f4SBarry Smith   PetscErrorCode  ierr;
4290325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4291325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4292325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4293325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4294325fc9f4SBarry Smith 
4295325fc9f4SBarry Smith   PetscFunctionBegin;
4296cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42970910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4298325fc9f4SBarry Smith 
42990910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4300325fc9f4SBarry Smith 
4301cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
43020910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
43030910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
43040910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
43050910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4306bbd56ea5SKarl Rupp 
4307325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4308325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4309325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4310325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4311325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4312325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4313325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4314325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4315325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4316325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4317325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4318325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4319325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4320325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4321325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4322325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4323325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4324325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4325325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4326325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4327325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4328325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4329325fc9f4SBarry Smith   PetscFunctionReturn(0);
4330325fc9f4SBarry Smith }
4331325fc9f4SBarry Smith 
4332325fc9f4SBarry Smith 
4333325fc9f4SBarry Smith #undef __FUNCT__
4334325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4335325fc9f4SBarry Smith /*
4336325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4337e3c5b3baSBarry 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
4338325fc9f4SBarry Smith 
4339325fc9f4SBarry Smith    Logically Collective on TS
4340325fc9f4SBarry Smith 
4341325fc9f4SBarry Smith    Input Parameters:
4342cdcf91faSSean Farley +  ts - the TS context
4343325fc9f4SBarry Smith .  A,B - Jacobian matrices
4344325fc9f4SBarry Smith .  func - function evaluation routine
4345325fc9f4SBarry Smith -  ctx - user context
4346325fc9f4SBarry Smith 
4347325fc9f4SBarry Smith    Calling sequence of func:
43480910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4349325fc9f4SBarry Smith 
4350325fc9f4SBarry Smith 
4351325fc9f4SBarry Smith    Level: developer
4352325fc9f4SBarry Smith 
4353325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4354325fc9f4SBarry Smith 
4355325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4356325fc9f4SBarry Smith */
4357cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4358325fc9f4SBarry Smith {
4359325fc9f4SBarry Smith   PetscErrorCode  ierr;
4360325fc9f4SBarry Smith   TSMatlabContext *sctx;
4361325fc9f4SBarry Smith 
4362325fc9f4SBarry Smith   PetscFunctionBegin;
4363325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4364325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4365325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4366325fc9f4SBarry Smith   /*
4367325fc9f4SBarry Smith      This should work, but it doesn't
4368325fc9f4SBarry Smith   sctx->ctx = ctx;
4369325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4370325fc9f4SBarry Smith   */
4371325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4372bbd56ea5SKarl Rupp 
4373cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4374325fc9f4SBarry Smith   PetscFunctionReturn(0);
4375325fc9f4SBarry Smith }
4376325fc9f4SBarry Smith 
4377b5b1a830SBarry Smith #undef __FUNCT__
4378b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4379b5b1a830SBarry Smith /*
4380b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4381b5b1a830SBarry Smith 
4382b5b1a830SBarry Smith    Collective on TS
4383b5b1a830SBarry Smith 
4384b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4385b5b1a830SBarry Smith @*/
43860910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4387b5b1a830SBarry Smith {
4388b5b1a830SBarry Smith   PetscErrorCode  ierr;
4389b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4390a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4391b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4392b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4393b5b1a830SBarry Smith 
4394b5b1a830SBarry Smith   PetscFunctionBegin;
4395cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43960910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4397b5b1a830SBarry Smith 
4398cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
43990910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4400bbd56ea5SKarl Rupp 
4401b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4402b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4403b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4404b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4405b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4406b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4407b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4408b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4409b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4410b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4411b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4412b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4413b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4414b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4415b5b1a830SBarry Smith   PetscFunctionReturn(0);
4416b5b1a830SBarry Smith }
4417b5b1a830SBarry Smith 
4418b5b1a830SBarry Smith 
4419b5b1a830SBarry Smith #undef __FUNCT__
4420b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4421b5b1a830SBarry Smith /*
4422b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4423b5b1a830SBarry Smith 
4424b5b1a830SBarry Smith    Level: developer
4425b5b1a830SBarry Smith 
4426b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4427b5b1a830SBarry Smith 
4428b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4429b5b1a830SBarry Smith */
4430cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4431b5b1a830SBarry Smith {
4432b5b1a830SBarry Smith   PetscErrorCode  ierr;
4433b5b1a830SBarry Smith   TSMatlabContext *sctx;
4434b5b1a830SBarry Smith 
4435b5b1a830SBarry Smith   PetscFunctionBegin;
4436b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4437b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4438b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4439b5b1a830SBarry Smith   /*
4440b5b1a830SBarry Smith      This should work, but it doesn't
4441b5b1a830SBarry Smith   sctx->ctx = ctx;
4442b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4443b5b1a830SBarry Smith   */
4444b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4445bbd56ea5SKarl Rupp 
44460298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
4447b5b1a830SBarry Smith   PetscFunctionReturn(0);
4448b5b1a830SBarry Smith }
4449325fc9f4SBarry Smith #endif
4450b3603a34SBarry Smith 
44510b039ecaSBarry Smith 
44520b039ecaSBarry Smith 
4453b3603a34SBarry Smith #undef __FUNCT__
44544f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4455b3603a34SBarry Smith /*@C
44564f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4457b3603a34SBarry Smith        in a time based line graph
4458b3603a34SBarry Smith 
4459b3603a34SBarry Smith    Collective on TS
4460b3603a34SBarry Smith 
4461b3603a34SBarry Smith    Input Parameters:
4462b3603a34SBarry Smith +  ts - the TS context
4463b3603a34SBarry Smith .  step - current time-step
4464b3603a34SBarry Smith .  ptime - current time
4465b3603a34SBarry Smith -  lg - a line graph object
4466b3603a34SBarry Smith 
4467b3603a34SBarry Smith    Level: intermediate
4468b3603a34SBarry Smith 
44690b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4470b3603a34SBarry Smith 
4471b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4472b3603a34SBarry Smith 
4473b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4474b3603a34SBarry Smith @*/
44750910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4476b3603a34SBarry Smith {
4477b3603a34SBarry Smith   PetscErrorCode    ierr;
44780b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4479b3603a34SBarry Smith   const PetscScalar *yy;
448058ff32f7SBarry Smith   PetscInt          dim;
4481b3603a34SBarry Smith 
4482b3603a34SBarry Smith   PetscFunctionBegin;
448358ff32f7SBarry Smith   if (!step) {
4484a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4485a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4486a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
44870910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
44880b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
44890b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
449058ff32f7SBarry Smith   }
44910910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4492e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4493e3efe391SJed Brown   {
4494e3efe391SJed Brown     PetscReal *yreal;
4495e3efe391SJed Brown     PetscInt  i,n;
44960910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4497e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4498e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
44990b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4500e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4501e3efe391SJed Brown   }
4502e3efe391SJed Brown #else
45030b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4504e3efe391SJed Brown #endif
45050910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
45063fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
45070b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
45083923b477SBarry Smith   }
4509b3603a34SBarry Smith   PetscFunctionReturn(0);
4510b3603a34SBarry Smith }
4511b3603a34SBarry Smith 
4512b3603a34SBarry Smith #undef __FUNCT__
45134f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4514ef20d060SBarry Smith /*@C
45154f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4516ef20d060SBarry Smith        in a time based line graph
4517ef20d060SBarry Smith 
4518ef20d060SBarry Smith    Collective on TS
4519ef20d060SBarry Smith 
4520ef20d060SBarry Smith    Input Parameters:
4521ef20d060SBarry Smith +  ts - the TS context
4522ef20d060SBarry Smith .  step - current time-step
4523ef20d060SBarry Smith .  ptime - current time
4524ef20d060SBarry Smith -  lg - a line graph object
4525ef20d060SBarry Smith 
4526ef20d060SBarry Smith    Level: intermediate
4527ef20d060SBarry Smith 
4528abd5a294SJed Brown    Notes:
4529abd5a294SJed Brown    Only for sequential solves.
4530abd5a294SJed Brown 
4531abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4532abd5a294SJed Brown 
4533abd5a294SJed Brown    Options Database Keys:
45344f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4535ef20d060SBarry Smith 
4536ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4537ef20d060SBarry Smith 
4538abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4539ef20d060SBarry Smith @*/
45400910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4541ef20d060SBarry Smith {
4542ef20d060SBarry Smith   PetscErrorCode    ierr;
45430b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4544ef20d060SBarry Smith   const PetscScalar *yy;
4545ef20d060SBarry Smith   Vec               y;
4546a9f9c1f6SBarry Smith   PetscInt          dim;
4547ef20d060SBarry Smith 
4548ef20d060SBarry Smith   PetscFunctionBegin;
4549a9f9c1f6SBarry Smith   if (!step) {
4550a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4551a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
45521ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
45530910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4554a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4555a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4556a9f9c1f6SBarry Smith   }
45570910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4558ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
45590910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4560ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4561e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4562e3efe391SJed Brown   {
4563e3efe391SJed Brown     PetscReal *yreal;
4564e3efe391SJed Brown     PetscInt  i,n;
4565e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4566e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4567e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
45680b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4569e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4570e3efe391SJed Brown   }
4571e3efe391SJed Brown #else
45720b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4573e3efe391SJed Brown #endif
4574ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4575ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
45763fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
45770b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
45783923b477SBarry Smith   }
4579ef20d060SBarry Smith   PetscFunctionReturn(0);
4580ef20d060SBarry Smith }
4581ef20d060SBarry Smith 
4582201da799SBarry Smith #undef __FUNCT__
4583201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4584201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4585201da799SBarry Smith {
4586201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4587201da799SBarry Smith   PetscReal      x   = ptime,y;
4588201da799SBarry Smith   PetscErrorCode ierr;
4589201da799SBarry Smith   PetscInt       its;
4590201da799SBarry Smith 
4591201da799SBarry Smith   PetscFunctionBegin;
4592201da799SBarry Smith   if (!n) {
4593201da799SBarry Smith     PetscDrawAxis axis;
4594bbd56ea5SKarl Rupp 
4595201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4596201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4597201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4598bbd56ea5SKarl Rupp 
4599201da799SBarry Smith     ctx->snes_its = 0;
4600201da799SBarry Smith   }
4601201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4602201da799SBarry Smith   y    = its - ctx->snes_its;
4603201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
46043fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4605201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4606201da799SBarry Smith   }
4607201da799SBarry Smith   ctx->snes_its = its;
4608201da799SBarry Smith   PetscFunctionReturn(0);
4609201da799SBarry Smith }
4610201da799SBarry Smith 
4611201da799SBarry Smith #undef __FUNCT__
4612201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4613201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4614201da799SBarry Smith {
4615201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4616201da799SBarry Smith   PetscReal      x   = ptime,y;
4617201da799SBarry Smith   PetscErrorCode ierr;
4618201da799SBarry Smith   PetscInt       its;
4619201da799SBarry Smith 
4620201da799SBarry Smith   PetscFunctionBegin;
4621201da799SBarry Smith   if (!n) {
4622201da799SBarry Smith     PetscDrawAxis axis;
4623bbd56ea5SKarl Rupp 
4624201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4625201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4626201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4627bbd56ea5SKarl Rupp 
4628201da799SBarry Smith     ctx->ksp_its = 0;
4629201da799SBarry Smith   }
4630201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4631201da799SBarry Smith   y    = its - ctx->ksp_its;
4632201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
463399fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4634201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4635201da799SBarry Smith   }
4636201da799SBarry Smith   ctx->ksp_its = its;
4637201da799SBarry Smith   PetscFunctionReturn(0);
4638201da799SBarry Smith }
4639f9c1d6abSBarry Smith 
4640f9c1d6abSBarry Smith #undef __FUNCT__
4641f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4642f9c1d6abSBarry Smith /*@
4643f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4644f9c1d6abSBarry Smith 
4645f9c1d6abSBarry Smith    Collective on TS and Vec
4646f9c1d6abSBarry Smith 
4647f9c1d6abSBarry Smith    Input Parameters:
4648f9c1d6abSBarry Smith +  ts - the TS context
4649f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4650f9c1d6abSBarry Smith 
4651f9c1d6abSBarry Smith    Output Parameters:
4652f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4653f9c1d6abSBarry Smith 
4654f9c1d6abSBarry Smith    Level: developer
4655f9c1d6abSBarry Smith 
4656f9c1d6abSBarry Smith .keywords: TS, compute
4657f9c1d6abSBarry Smith 
4658f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4659f9c1d6abSBarry Smith @*/
4660f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4661f9c1d6abSBarry Smith {
4662f9c1d6abSBarry Smith   PetscErrorCode ierr;
4663f9c1d6abSBarry Smith 
4664f9c1d6abSBarry Smith   PetscFunctionBegin;
4665f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4666*ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4667f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4668f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4669f9c1d6abSBarry Smith }
4670