xref: /petsc/src/ts/interface/ts.c (revision 91b97e58921fdfa4201043b4928c82ff7e290038)
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 
37cce0b1b2SLisandro Dalcin   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_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
72*91b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
73*91b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
74*91b97e58SBarry Smith 
75*91b97e58SBarry 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 */
102bdad233fSMatthew Knepley   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
1033bca7d26SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
104d8cd7023SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_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);}
111cef5090cSJed Brown   ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,PETSC_NULL);CHKERRQ(ierr);
112cef5090cSJed Brown   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr);
113cef5090cSJed Brown   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
1141c3436cfSJed Brown   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr);
1151c3436cfSJed Brown   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_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) {
120649052a6SBarry Smith     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,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 
1314f09c107SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
1321d1e9da1SBarry Smith     ierr = TSMonitorLGCtxCreate(((PetscObject)ts)->comm,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 
1404f09c107SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,PETSC_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 
1494f09c107SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,PETSC_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 
158201da799SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,PETSC_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 
167201da799SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,PETSC_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 
1768189c53fSBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,PETSC_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 
18683a4ac43SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
18722d28d08SBarry Smith     ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,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 
19683a4ac43SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
19722d28d08SBarry Smith     ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,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;
201*91b97e58SBarry 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]) {
205fb1732b5SBarry Smith       ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
206fb1732b5SBarry Smith     } else {
207fb1732b5SBarry Smith       ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
208fb1732b5SBarry Smith     }
209fb1732b5SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
210fb1732b5SBarry Smith   }
211ed81e22dSJed Brown   opt  = PETSC_FALSE;
212*91b97e58SBarry 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);
213ed81e22dSJed Brown   if (flg) {
214ed81e22dSJed Brown     const char *ptr,*ptr2;
215ed81e22dSJed Brown     char       *filetemplate;
216*91b97e58SBarry Smith     if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
217ed81e22dSJed Brown     /* Do some cursory validation of the input. */
218ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
219*91b97e58SBarry Smith     if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
220ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
221ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
222*91b97e58SBarry Smith       if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
223ed81e22dSJed Brown       if (ptr2) break;
224ed81e22dSJed Brown     }
225ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
226ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
227ed81e22dSJed Brown   }
228bdad233fSMatthew Knepley 
229d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
230d1212d36SBarry Smith   if (flg) {
231d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
232d1212d36SBarry Smith     int                 ray = 0;
233d1212d36SBarry Smith     DMDADirection       ddir;
234d1212d36SBarry Smith     DM                  da;
235d1212d36SBarry Smith     PetscMPIInt         rank;
236d1212d36SBarry Smith 
237d1212d36SBarry Smith     if (dir[1] != '=') SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
238d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
239d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
240d1212d36SBarry Smith     else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
241d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
242d1212d36SBarry Smith 
243d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
244d1212d36SBarry Smith     ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr);
245d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
246d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
247d1212d36SBarry Smith     ierr = MPI_Comm_rank(((PetscObject)ts)->comm,&rank);CHKERRQ(ierr);
248d1212d36SBarry Smith     if (!rank) {
249d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
250d1212d36SBarry Smith     }
251d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
252d1212d36SBarry Smith   }
253d1212d36SBarry Smith 
254ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&adapt);CHKERRQ(ierr);
2551c3436cfSJed Brown   ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2561c3436cfSJed Brown 
257d52bd9f3SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
258d52bd9f3SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
259d52bd9f3SBarry Smith 
260bdad233fSMatthew Knepley   /* Handle specific TS options */
261abc0a331SBarry Smith   if (ts->ops->setfromoptions) {
262bdad233fSMatthew Knepley     ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
263bdad233fSMatthew Knepley   }
2645d973c19SBarry Smith 
2655d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
2665d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
267bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
268bdad233fSMatthew Knepley   PetscFunctionReturn(0);
269bdad233fSMatthew Knepley }
270bdad233fSMatthew Knepley 
271bdad233fSMatthew Knepley #undef __FUNCT__
272cdcf91faSSean Farley #undef __FUNCT__
2734a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
274a7a1495cSBarry Smith /*@
2758c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
276a7a1495cSBarry Smith       set with TSSetRHSJacobian().
277a7a1495cSBarry Smith 
278a7a1495cSBarry Smith    Collective on TS and Vec
279a7a1495cSBarry Smith 
280a7a1495cSBarry Smith    Input Parameters:
281316643e7SJed Brown +  ts - the TS context
282a7a1495cSBarry Smith .  t - current timestep
2830910c330SBarry Smith -  U - input vector
284a7a1495cSBarry Smith 
285a7a1495cSBarry Smith    Output Parameters:
286a7a1495cSBarry Smith +  A - Jacobian matrix
287a7a1495cSBarry Smith .  B - optional preconditioning matrix
288a7a1495cSBarry Smith -  flag - flag indicating matrix structure
289a7a1495cSBarry Smith 
290a7a1495cSBarry Smith    Notes:
291a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
292a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
293a7a1495cSBarry Smith 
29494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
295a7a1495cSBarry Smith    flag parameter.
296a7a1495cSBarry Smith 
297a7a1495cSBarry Smith    Level: developer
298a7a1495cSBarry Smith 
299a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
300a7a1495cSBarry Smith 
30194b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
302a7a1495cSBarry Smith @*/
3030910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
304a7a1495cSBarry Smith {
305dfbe8321SBarry Smith   PetscErrorCode ierr;
3060910c330SBarry Smith   PetscInt       Ustate;
30724989b8cSPeter Brune   DM             dm;
308942e3340SBarry Smith   DMTS           tsdm;
30924989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
31024989b8cSPeter Brune   void           *ctx;
31124989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
312a7a1495cSBarry Smith 
313a7a1495cSBarry Smith   PetscFunctionBegin;
3140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3150910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3160910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
31724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
318942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
31924989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
32024989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
3210910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
3220910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
3230e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
3240e4ef248SJed Brown     PetscFunctionReturn(0);
325f8ede8e7SJed Brown   }
326d90be118SSean Farley 
32724989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
328d90be118SSean Farley 
32924989b8cSPeter Brune   if (rhsjacobianfunc) {
3300910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
331a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
332a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3330910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
334a7a1495cSBarry Smith     PetscStackPop;
3350910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
336a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3370700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3380700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
339ef66eb69SBarry Smith   } else {
340214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
341214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
342214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
343ef66eb69SBarry Smith   }
3440e4ef248SJed Brown   ts->rhsjacobian.time       = t;
3450910c330SBarry Smith   ts->rhsjacobian.X          = U;
3460910c330SBarry Smith   ierr                       = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3470e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
348a7a1495cSBarry Smith   PetscFunctionReturn(0);
349a7a1495cSBarry Smith }
350a7a1495cSBarry Smith 
3514a2ae208SSatish Balay #undef __FUNCT__
3524a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
353316643e7SJed Brown /*@
354d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
355d763cef2SBarry Smith 
356316643e7SJed Brown    Collective on TS and Vec
357316643e7SJed Brown 
358316643e7SJed Brown    Input Parameters:
359316643e7SJed Brown +  ts - the TS context
360316643e7SJed Brown .  t - current time
3610910c330SBarry Smith -  U - state vector
362316643e7SJed Brown 
363316643e7SJed Brown    Output Parameter:
364316643e7SJed Brown .  y - right hand side
365316643e7SJed Brown 
366316643e7SJed Brown    Note:
367316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
368316643e7SJed Brown    is used internally within the nonlinear solvers.
369316643e7SJed Brown 
370316643e7SJed Brown    Level: developer
371316643e7SJed Brown 
372316643e7SJed Brown .keywords: TS, compute
373316643e7SJed Brown 
374316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
375316643e7SJed Brown @*/
3760910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
377d763cef2SBarry Smith {
378dfbe8321SBarry Smith   PetscErrorCode ierr;
37924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
38024989b8cSPeter Brune   TSIFunction    ifunction;
38124989b8cSPeter Brune   void           *ctx;
38224989b8cSPeter Brune   DM             dm;
38324989b8cSPeter Brune 
384d763cef2SBarry Smith   PetscFunctionBegin;
3850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3860910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3870700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
38824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
38924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
39024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
391d763cef2SBarry Smith 
39224989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
393d763cef2SBarry Smith 
3940910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
39524989b8cSPeter Brune   if (rhsfunction) {
396d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
3970910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
398d763cef2SBarry Smith     PetscStackPop;
399214bc6a2SJed Brown   } else {
400214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
401b2cd27e8SJed Brown   }
40244a41b28SSean Farley 
4030910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
404d763cef2SBarry Smith   PetscFunctionReturn(0);
405d763cef2SBarry Smith }
406d763cef2SBarry Smith 
4074a2ae208SSatish Balay #undef __FUNCT__
408ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
409ef20d060SBarry Smith /*@
410ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
411ef20d060SBarry Smith 
412ef20d060SBarry Smith    Collective on TS and Vec
413ef20d060SBarry Smith 
414ef20d060SBarry Smith    Input Parameters:
415ef20d060SBarry Smith +  ts - the TS context
416ef20d060SBarry Smith -  t - current time
417ef20d060SBarry Smith 
418ef20d060SBarry Smith    Output Parameter:
4190910c330SBarry Smith .  U - the solution
420ef20d060SBarry Smith 
421ef20d060SBarry Smith    Note:
422ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
423ef20d060SBarry Smith    is used internally within the nonlinear solvers.
424ef20d060SBarry Smith 
425ef20d060SBarry Smith    Level: developer
426ef20d060SBarry Smith 
427ef20d060SBarry Smith .keywords: TS, compute
428ef20d060SBarry Smith 
429abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
430ef20d060SBarry Smith @*/
4310910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
432ef20d060SBarry Smith {
433ef20d060SBarry Smith   PetscErrorCode     ierr;
434ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
435ef20d060SBarry Smith   void               *ctx;
436ef20d060SBarry Smith   DM                 dm;
437ef20d060SBarry Smith 
438ef20d060SBarry Smith   PetscFunctionBegin;
439ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4400910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
441ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
442ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
443ef20d060SBarry Smith 
444ef20d060SBarry Smith   if (solutionfunction) {
445ef20d060SBarry Smith     PetscStackPush("TS user right-hand-side function");
4460910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
447ef20d060SBarry Smith     PetscStackPop;
448ef20d060SBarry Smith   }
449ef20d060SBarry Smith   PetscFunctionReturn(0);
450ef20d060SBarry Smith }
451ef20d060SBarry Smith 
452ef20d060SBarry Smith #undef __FUNCT__
453214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
454214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
455214bc6a2SJed Brown {
4562dd45cf8SJed Brown   Vec            F;
457214bc6a2SJed Brown   PetscErrorCode ierr;
458214bc6a2SJed Brown 
459214bc6a2SJed Brown   PetscFunctionBegin;
4600da9a4f0SJed Brown   *Frhs = PETSC_NULL;
4612dd45cf8SJed Brown   ierr  = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
462214bc6a2SJed Brown   if (!ts->Frhs) {
4632dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
464214bc6a2SJed Brown   }
465214bc6a2SJed Brown   *Frhs = ts->Frhs;
466214bc6a2SJed Brown   PetscFunctionReturn(0);
467214bc6a2SJed Brown }
468214bc6a2SJed Brown 
469214bc6a2SJed Brown #undef __FUNCT__
470214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
471214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
472214bc6a2SJed Brown {
473214bc6a2SJed Brown   Mat            A,B;
4742dd45cf8SJed Brown   PetscErrorCode ierr;
475214bc6a2SJed Brown 
476214bc6a2SJed Brown   PetscFunctionBegin;
477214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
478214bc6a2SJed Brown   if (Arhs) {
479214bc6a2SJed Brown     if (!ts->Arhs) {
480214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
481214bc6a2SJed Brown     }
482214bc6a2SJed Brown     *Arhs = ts->Arhs;
483214bc6a2SJed Brown   }
484214bc6a2SJed Brown   if (Brhs) {
485214bc6a2SJed Brown     if (!ts->Brhs) {
486214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
487214bc6a2SJed Brown     }
488214bc6a2SJed Brown     *Brhs = ts->Brhs;
489214bc6a2SJed Brown   }
490214bc6a2SJed Brown   PetscFunctionReturn(0);
491214bc6a2SJed Brown }
492214bc6a2SJed Brown 
493214bc6a2SJed Brown #undef __FUNCT__
494316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
495316643e7SJed Brown /*@
4960910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
497316643e7SJed Brown 
498316643e7SJed Brown    Collective on TS and Vec
499316643e7SJed Brown 
500316643e7SJed Brown    Input Parameters:
501316643e7SJed Brown +  ts - the TS context
502316643e7SJed Brown .  t - current time
5030910c330SBarry Smith .  U - state vector
5040910c330SBarry Smith .  Udot - time derivative of state vector
505214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
506316643e7SJed Brown 
507316643e7SJed Brown    Output Parameter:
508316643e7SJed Brown .  Y - right hand side
509316643e7SJed Brown 
510316643e7SJed Brown    Note:
511316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
512316643e7SJed Brown    is used internally within the nonlinear solvers.
513316643e7SJed Brown 
514316643e7SJed Brown    If the user did did not write their equations in implicit form, this
515316643e7SJed Brown    function recasts them in implicit form.
516316643e7SJed Brown 
517316643e7SJed Brown    Level: developer
518316643e7SJed Brown 
519316643e7SJed Brown .keywords: TS, compute
520316643e7SJed Brown 
521316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
522316643e7SJed Brown @*/
5230910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
524316643e7SJed Brown {
525316643e7SJed Brown   PetscErrorCode ierr;
52624989b8cSPeter Brune   TSIFunction    ifunction;
52724989b8cSPeter Brune   TSRHSFunction  rhsfunction;
52824989b8cSPeter Brune   void           *ctx;
52924989b8cSPeter Brune   DM             dm;
530316643e7SJed Brown 
531316643e7SJed Brown   PetscFunctionBegin;
5320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5330910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5340910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
5350700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
536316643e7SJed Brown 
53724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
53824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
53924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
54024989b8cSPeter Brune 
54124989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
542d90be118SSean Farley 
5430910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
54424989b8cSPeter Brune   if (ifunction) {
545316643e7SJed Brown     PetscStackPush("TS user implicit function");
5460910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
547316643e7SJed Brown     PetscStackPop;
548214bc6a2SJed Brown   }
549214bc6a2SJed Brown   if (imex) {
55024989b8cSPeter Brune     if (!ifunction) {
5510910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
5522dd45cf8SJed Brown     }
55324989b8cSPeter Brune   } else if (rhsfunction) {
55424989b8cSPeter Brune     if (ifunction) {
555214bc6a2SJed Brown       Vec Frhs;
556214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
5570910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
558214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
5592dd45cf8SJed Brown     } else {
5600910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
5610910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
562316643e7SJed Brown     }
5634a6899ffSJed Brown   }
5640910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
565316643e7SJed Brown   PetscFunctionReturn(0);
566316643e7SJed Brown }
567316643e7SJed Brown 
568316643e7SJed Brown #undef __FUNCT__
569316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
570316643e7SJed Brown /*@
571316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
572316643e7SJed Brown 
573316643e7SJed Brown    Collective on TS and Vec
574316643e7SJed Brown 
575316643e7SJed Brown    Input
576316643e7SJed Brown       Input Parameters:
577316643e7SJed Brown +  ts - the TS context
578316643e7SJed Brown .  t - current timestep
5790910c330SBarry Smith .  U - state vector
5800910c330SBarry Smith .  Udot - time derivative of state vector
581214bc6a2SJed Brown .  shift - shift to apply, see note below
582214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
583316643e7SJed Brown 
584316643e7SJed Brown    Output Parameters:
585316643e7SJed Brown +  A - Jacobian matrix
586316643e7SJed Brown .  B - optional preconditioning matrix
587316643e7SJed Brown -  flag - flag indicating matrix structure
588316643e7SJed Brown 
589316643e7SJed Brown    Notes:
5900910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
591316643e7SJed Brown 
5920910c330SBarry Smith    dF/dU + shift*dF/dUdot
593316643e7SJed Brown 
594316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
595316643e7SJed Brown    is used internally within the nonlinear solvers.
596316643e7SJed Brown 
597316643e7SJed Brown    Level: developer
598316643e7SJed Brown 
599316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
600316643e7SJed Brown 
601316643e7SJed Brown .seealso:  TSSetIJacobian()
60263495f91SJed Brown @*/
6030910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
604316643e7SJed Brown {
6050910c330SBarry Smith   PetscInt       Ustate, Udotstate;
606316643e7SJed Brown   PetscErrorCode ierr;
60724989b8cSPeter Brune   TSIJacobian    ijacobian;
60824989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
60924989b8cSPeter Brune   DM             dm;
61024989b8cSPeter Brune   void           *ctx;
611316643e7SJed Brown 
612316643e7SJed Brown   PetscFunctionBegin;
6130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6140910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6150910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
616316643e7SJed Brown   PetscValidPointer(A,6);
6170700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
618316643e7SJed Brown   PetscValidPointer(B,7);
6190700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
620316643e7SJed Brown   PetscValidPointer(flg,8);
62124989b8cSPeter Brune 
62224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
62324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
62424989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
62524989b8cSPeter Brune 
6260910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
6270910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
6280910c330SBarry 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))) {
6290026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
6300026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
6310026cea9SSean Farley     PetscFunctionReturn(0);
6320026cea9SSean Farley   }
633316643e7SJed Brown 
63424989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
635316643e7SJed Brown 
6364e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
6370910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
63824989b8cSPeter Brune   if (ijacobian) {
6392dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
640316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
6410910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
642316643e7SJed Brown     PetscStackPop;
643214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
644214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
645214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
6464a6899ffSJed Brown   }
647214bc6a2SJed Brown   if (imex) {
648b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
649214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
6502dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
651214bc6a2SJed Brown       if (*A != *B) {
652214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
653214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
654214bc6a2SJed Brown       }
655214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
656214bc6a2SJed Brown     }
657214bc6a2SJed Brown   } else {
65824989b8cSPeter Brune     if (!ijacobian) {
6590910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr);
660214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
661214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
662316643e7SJed Brown       if (*A != *B) {
663316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
664316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
665316643e7SJed Brown       }
66624989b8cSPeter Brune     } else if (rhsjacobian) {
667214bc6a2SJed Brown       Mat          Arhs,Brhs;
668214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
669214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
6700910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
671214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
672214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
673214bc6a2SJed Brown       if (*A != *B) {
674214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
675214bc6a2SJed Brown       }
676214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
677214bc6a2SJed Brown     }
678316643e7SJed Brown   }
6790026cea9SSean Farley 
6800026cea9SSean Farley   ts->ijacobian.time = t;
6810910c330SBarry Smith   ts->ijacobian.X    = U;
6820910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
683bbd56ea5SKarl Rupp 
6840910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
6850910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
686bbd56ea5SKarl Rupp 
6870026cea9SSean Farley   ts->ijacobian.shift      = shift;
6880026cea9SSean Farley   ts->ijacobian.imex       = imex;
6890026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
690bbd56ea5SKarl Rupp 
6910910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
692316643e7SJed Brown   PetscFunctionReturn(0);
693316643e7SJed Brown }
694316643e7SJed Brown 
695316643e7SJed Brown #undef __FUNCT__
6964a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
697d763cef2SBarry Smith /*@C
698d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
699b5abc632SBarry Smith     where U_t = G(t,u).
700d763cef2SBarry Smith 
7013f9fe445SBarry Smith     Logically Collective on TS
702d763cef2SBarry Smith 
703d763cef2SBarry Smith     Input Parameters:
704d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
705ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
706d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
707d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
708d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
709d763cef2SBarry Smith 
710d763cef2SBarry Smith     Calling sequence of func:
71187828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
712d763cef2SBarry Smith 
713d763cef2SBarry Smith +   t - current timestep
714d763cef2SBarry Smith .   u - input vector
715d763cef2SBarry Smith .   F - function vector
716d763cef2SBarry Smith -   ctx - [optional] user-defined function context
717d763cef2SBarry Smith 
718d763cef2SBarry Smith     Level: beginner
719d763cef2SBarry Smith 
720d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
721d763cef2SBarry Smith 
722d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
723d763cef2SBarry Smith @*/
724089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
725d763cef2SBarry Smith {
726089b2837SJed Brown   PetscErrorCode ierr;
727089b2837SJed Brown   SNES           snes;
728e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
72924989b8cSPeter Brune   DM             dm;
730d763cef2SBarry Smith 
731089b2837SJed Brown   PetscFunctionBegin;
7320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
733ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
73424989b8cSPeter Brune 
73524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
73624989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
737089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
738e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
739e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
740e856ceecSJed Brown     r    = ralloc;
741e856ceecSJed Brown   }
742089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
743e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
744d763cef2SBarry Smith   PetscFunctionReturn(0);
745d763cef2SBarry Smith }
746d763cef2SBarry Smith 
7474a2ae208SSatish Balay #undef __FUNCT__
748ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
749ef20d060SBarry Smith /*@C
750abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
751ef20d060SBarry Smith 
752ef20d060SBarry Smith     Logically Collective on TS
753ef20d060SBarry Smith 
754ef20d060SBarry Smith     Input Parameters:
755ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
756ef20d060SBarry Smith .   f - routine for evaluating the solution
757ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
758ef20d060SBarry Smith           function evaluation routine (may be PETSC_NULL)
759ef20d060SBarry Smith 
760ef20d060SBarry Smith     Calling sequence of func:
761ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
762ef20d060SBarry Smith 
763ef20d060SBarry Smith +   t - current timestep
764ef20d060SBarry Smith .   u - output vector
765ef20d060SBarry Smith -   ctx - [optional] user-defined function context
766ef20d060SBarry Smith 
767abd5a294SJed Brown     Notes:
768abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
769abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
770abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
771abd5a294SJed Brown 
7724f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
773abd5a294SJed Brown 
774ef20d060SBarry Smith     Level: beginner
775ef20d060SBarry Smith 
776ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
777ef20d060SBarry Smith 
778abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction()
779ef20d060SBarry Smith @*/
780ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
781ef20d060SBarry Smith {
782ef20d060SBarry Smith   PetscErrorCode ierr;
783ef20d060SBarry Smith   DM             dm;
784ef20d060SBarry Smith 
785ef20d060SBarry Smith   PetscFunctionBegin;
786ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
787ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
788ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
789ef20d060SBarry Smith   PetscFunctionReturn(0);
790ef20d060SBarry Smith }
791ef20d060SBarry Smith 
792ef20d060SBarry Smith #undef __FUNCT__
7934a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
794d763cef2SBarry Smith /*@C
795d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
796b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
797d763cef2SBarry Smith 
7983f9fe445SBarry Smith    Logically Collective on TS
799d763cef2SBarry Smith 
800d763cef2SBarry Smith    Input Parameters:
801d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
802d763cef2SBarry Smith .  A   - Jacobian matrix
803d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
804d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
805d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
806d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
807d763cef2SBarry Smith 
808d763cef2SBarry Smith    Calling sequence of func:
80987828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
810d763cef2SBarry Smith 
811d763cef2SBarry Smith +  t - current timestep
812d763cef2SBarry Smith .  u - input vector
813d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
814d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
815d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
81694b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
817d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
818d763cef2SBarry Smith 
819d763cef2SBarry Smith    Notes:
82094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
821d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
822d763cef2SBarry Smith 
823d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
824d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
82556335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
826d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
827d763cef2SBarry Smith    throughout the global iterations.
828d763cef2SBarry Smith 
829d763cef2SBarry Smith    Level: beginner
830d763cef2SBarry Smith 
831d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
832d763cef2SBarry Smith 
833ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
834d763cef2SBarry Smith 
835d763cef2SBarry Smith @*/
836089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
837d763cef2SBarry Smith {
838277b19d0SLisandro Dalcin   PetscErrorCode ierr;
839089b2837SJed Brown   SNES           snes;
84024989b8cSPeter Brune   DM             dm;
84124989b8cSPeter Brune   TSIJacobian    ijacobian;
842277b19d0SLisandro Dalcin 
843d763cef2SBarry Smith   PetscFunctionBegin;
8440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
845277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
846277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
847277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
848277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
849d763cef2SBarry Smith 
85024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
85124989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
85224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
85324989b8cSPeter Brune 
854089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
8555f659677SPeter Brune   if (!ijacobian) {
856089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
8570e4ef248SJed Brown   }
8580e4ef248SJed Brown   if (A) {
8590e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8600e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
861bbd56ea5SKarl Rupp 
8620e4ef248SJed Brown     ts->Arhs = A;
8630e4ef248SJed Brown   }
8640e4ef248SJed Brown   if (B) {
8650e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8660e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
867bbd56ea5SKarl Rupp 
8680e4ef248SJed Brown     ts->Brhs = B;
8690e4ef248SJed Brown   }
870d763cef2SBarry Smith   PetscFunctionReturn(0);
871d763cef2SBarry Smith }
872d763cef2SBarry Smith 
873316643e7SJed Brown 
874316643e7SJed Brown #undef __FUNCT__
875316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
876316643e7SJed Brown /*@C
877b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
878316643e7SJed Brown 
8793f9fe445SBarry Smith    Logically Collective on TS
880316643e7SJed Brown 
881316643e7SJed Brown    Input Parameters:
882316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
883ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
884316643e7SJed Brown .  f   - the function evaluation routine
885316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
886316643e7SJed Brown 
887316643e7SJed Brown    Calling sequence of f:
888316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
889316643e7SJed Brown 
890316643e7SJed Brown +  t   - time at step/stage being solved
891316643e7SJed Brown .  u   - state vector
892316643e7SJed Brown .  u_t - time derivative of state vector
893316643e7SJed Brown .  F   - function vector
894316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
895316643e7SJed Brown 
896316643e7SJed Brown    Important:
897d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
898316643e7SJed Brown 
899316643e7SJed Brown    Level: beginner
900316643e7SJed Brown 
901316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
902316643e7SJed Brown 
903d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
904316643e7SJed Brown @*/
905089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
906316643e7SJed Brown {
907089b2837SJed Brown   PetscErrorCode ierr;
908089b2837SJed Brown   SNES           snes;
909e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
91024989b8cSPeter Brune   DM             dm;
911316643e7SJed Brown 
912316643e7SJed Brown   PetscFunctionBegin;
9130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
914ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
91524989b8cSPeter Brune 
91624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
91724989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
91824989b8cSPeter Brune 
919089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
920e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
921e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
922e856ceecSJed Brown     res  = resalloc;
923e856ceecSJed Brown   }
924089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
925e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
926089b2837SJed Brown   PetscFunctionReturn(0);
927089b2837SJed Brown }
928089b2837SJed Brown 
929089b2837SJed Brown #undef __FUNCT__
930089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
931089b2837SJed Brown /*@C
932089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
933089b2837SJed Brown 
934089b2837SJed Brown    Not Collective
935089b2837SJed Brown 
936089b2837SJed Brown    Input Parameter:
937089b2837SJed Brown .  ts - the TS context
938089b2837SJed Brown 
939089b2837SJed Brown    Output Parameter:
940089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
941089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
942089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
943089b2837SJed Brown 
944089b2837SJed Brown    Level: advanced
945089b2837SJed Brown 
946089b2837SJed Brown .keywords: TS, nonlinear, get, function
947089b2837SJed Brown 
948089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
949089b2837SJed Brown @*/
950089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
951089b2837SJed Brown {
952089b2837SJed Brown   PetscErrorCode ierr;
953089b2837SJed Brown   SNES           snes;
95424989b8cSPeter Brune   DM             dm;
955089b2837SJed Brown 
956089b2837SJed Brown   PetscFunctionBegin;
957089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
958089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
959089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
96024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
962089b2837SJed Brown   PetscFunctionReturn(0);
963089b2837SJed Brown }
964089b2837SJed Brown 
965089b2837SJed Brown #undef __FUNCT__
966089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
967089b2837SJed Brown /*@C
968089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
969089b2837SJed Brown 
970089b2837SJed Brown    Not Collective
971089b2837SJed Brown 
972089b2837SJed Brown    Input Parameter:
973089b2837SJed Brown .  ts - the TS context
974089b2837SJed Brown 
975089b2837SJed Brown    Output Parameter:
976089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
977089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
978089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
979089b2837SJed Brown 
980089b2837SJed Brown    Level: advanced
981089b2837SJed Brown 
982089b2837SJed Brown .keywords: TS, nonlinear, get, function
983089b2837SJed Brown 
984089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
985089b2837SJed Brown @*/
986089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
987089b2837SJed Brown {
988089b2837SJed Brown   PetscErrorCode ierr;
989089b2837SJed Brown   SNES           snes;
99024989b8cSPeter Brune   DM             dm;
991089b2837SJed Brown 
992089b2837SJed Brown   PetscFunctionBegin;
993089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
994089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
995089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
99624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
99724989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
998316643e7SJed Brown   PetscFunctionReturn(0);
999316643e7SJed Brown }
1000316643e7SJed Brown 
1001316643e7SJed Brown #undef __FUNCT__
1002316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1003316643e7SJed Brown /*@C
1004a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1005a4f0a591SBarry Smith         you provided with TSSetIFunction().
1006316643e7SJed Brown 
10073f9fe445SBarry Smith    Logically Collective on TS
1008316643e7SJed Brown 
1009316643e7SJed Brown    Input Parameters:
1010316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1011316643e7SJed Brown .  A   - Jacobian matrix
1012316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
1013316643e7SJed Brown .  f   - the Jacobian evaluation routine
1014316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
1015316643e7SJed Brown 
1016316643e7SJed Brown    Calling sequence of f:
10171b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
1018316643e7SJed Brown 
1019316643e7SJed Brown +  t    - time at step/stage being solved
10201b4a444bSJed Brown .  U    - state vector
10211b4a444bSJed Brown .  U_t  - time derivative of state vector
1022316643e7SJed Brown .  a    - shift
1023b5abc632SBarry Smith .  A    - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1024316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
1025316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1026316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1027316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1028316643e7SJed Brown 
1029316643e7SJed Brown    Notes:
1030316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
1031316643e7SJed Brown 
1032a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1033b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1034a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1035a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1036a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1037a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1038a4f0a591SBarry Smith 
1039316643e7SJed Brown    Level: beginner
1040316643e7SJed Brown 
1041316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1042316643e7SJed Brown 
1043ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
1044316643e7SJed Brown 
1045316643e7SJed Brown @*/
10467087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1047316643e7SJed Brown {
1048316643e7SJed Brown   PetscErrorCode ierr;
1049089b2837SJed Brown   SNES           snes;
105024989b8cSPeter Brune   DM             dm;
1051316643e7SJed Brown 
1052316643e7SJed Brown   PetscFunctionBegin;
10530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10540700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
10550700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1056316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1057316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
105824989b8cSPeter Brune 
105924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
106024989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
106124989b8cSPeter Brune 
1062089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1063089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1064316643e7SJed Brown   PetscFunctionReturn(0);
1065316643e7SJed Brown }
1066316643e7SJed Brown 
10674a2ae208SSatish Balay #undef __FUNCT__
106855849f57SBarry Smith #define __FUNCT__ "TSLoad"
106955849f57SBarry Smith /*@C
107055849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
107155849f57SBarry Smith 
107255849f57SBarry Smith   Collective on PetscViewer
107355849f57SBarry Smith 
107455849f57SBarry Smith   Input Parameters:
107555849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
107655849f57SBarry Smith            some related function before a call to TSLoad().
107755849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
107855849f57SBarry Smith 
107955849f57SBarry Smith    Level: intermediate
108055849f57SBarry Smith 
108155849f57SBarry Smith   Notes:
108255849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
108355849f57SBarry Smith 
108455849f57SBarry Smith   Notes for advanced users:
108555849f57SBarry Smith   Most users should not need to know the details of the binary storage
108655849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
108755849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
108855849f57SBarry Smith   format is
108955849f57SBarry Smith .vb
109055849f57SBarry Smith      has not yet been determined
109155849f57SBarry Smith .ve
109255849f57SBarry Smith 
109355849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
109455849f57SBarry Smith @*/
1095f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
109655849f57SBarry Smith {
109755849f57SBarry Smith   PetscErrorCode ierr;
109855849f57SBarry Smith   PetscBool      isbinary;
109955849f57SBarry Smith   PetscInt       classid;
110055849f57SBarry Smith   char           type[256];
11012d53ad75SBarry Smith   DMTS           sdm;
1102ad6bc421SBarry Smith   DM             dm;
110355849f57SBarry Smith 
110455849f57SBarry Smith   PetscFunctionBegin;
1105f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
110655849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
110755849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
110855849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
110955849f57SBarry Smith 
111055849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1111f2c2a1b9SBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Not TS next in file");
111255849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1113f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1114f2c2a1b9SBarry Smith   if (ts->ops->load) {
1115f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1116f2c2a1b9SBarry Smith   }
1117ad6bc421SBarry Smith   ierr = DMCreate(((PetscObject)ts)->comm,&dm);CHKERRQ(ierr);
1118ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1119ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1120f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1121f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
11222d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
11232d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
112455849f57SBarry Smith   PetscFunctionReturn(0);
112555849f57SBarry Smith }
112655849f57SBarry Smith 
112755849f57SBarry Smith #undef __FUNCT__
11284a2ae208SSatish Balay #define __FUNCT__ "TSView"
11297e2c5f70SBarry Smith /*@C
1130d763cef2SBarry Smith     TSView - Prints the TS data structure.
1131d763cef2SBarry Smith 
11324c49b128SBarry Smith     Collective on TS
1133d763cef2SBarry Smith 
1134d763cef2SBarry Smith     Input Parameters:
1135d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1136d763cef2SBarry Smith -   viewer - visualization context
1137d763cef2SBarry Smith 
1138d763cef2SBarry Smith     Options Database Key:
1139d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1140d763cef2SBarry Smith 
1141d763cef2SBarry Smith     Notes:
1142d763cef2SBarry Smith     The available visualization contexts include
1143b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1144b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1145d763cef2SBarry Smith          output where only the first processor opens
1146d763cef2SBarry Smith          the file.  All other processors send their
1147d763cef2SBarry Smith          data to the first processor to print.
1148d763cef2SBarry Smith 
1149d763cef2SBarry Smith     The user can open an alternative visualization context with
1150b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1151d763cef2SBarry Smith 
1152d763cef2SBarry Smith     Level: beginner
1153d763cef2SBarry Smith 
1154d763cef2SBarry Smith .keywords: TS, timestep, view
1155d763cef2SBarry Smith 
1156b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1157d763cef2SBarry Smith @*/
11587087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1159d763cef2SBarry Smith {
1160dfbe8321SBarry Smith   PetscErrorCode ierr;
116119fd82e9SBarry Smith   TSType         type;
11622b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
11632d53ad75SBarry Smith   DMTS           sdm;
1164d763cef2SBarry Smith 
1165d763cef2SBarry Smith   PetscFunctionBegin;
11660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11673050cee2SBarry Smith   if (!viewer) {
11687adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
11693050cee2SBarry Smith   }
11700700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1171c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1172fd16b177SBarry Smith 
1173251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1174251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
117555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
11762b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
117732077d6dSBarry Smith   if (iascii) {
1178317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
117977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1180a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1181d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
11825ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1183c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1184d763cef2SBarry Smith     }
11855ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1186193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
11872d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
11882d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1189d52bd9f3SBarry Smith     if (ts->ops->view) {
1190d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1191d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1192d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1193d52bd9f3SBarry Smith     }
11940f5bd95cSBarry Smith   } else if (isstring) {
1195a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1196b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
119755849f57SBarry Smith   } else if (isbinary) {
119855849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
119955849f57SBarry Smith     MPI_Comm    comm;
120055849f57SBarry Smith     PetscMPIInt rank;
120155849f57SBarry Smith     char        type[256];
120255849f57SBarry Smith 
120355849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
120455849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
120555849f57SBarry Smith     if (!rank) {
120655849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
120755849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
120855849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
120955849f57SBarry Smith     }
121055849f57SBarry Smith     if (ts->ops->view) {
121155849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
121255849f57SBarry Smith     }
1213f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1214f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
12152d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12162d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
12172b0a91c0SBarry Smith   } else if (isdraw) {
12182b0a91c0SBarry Smith     PetscDraw draw;
12192b0a91c0SBarry Smith     char      str[36];
122089fd9fafSBarry Smith     PetscReal x,y,bottom,h;
12212b0a91c0SBarry Smith 
12222b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
12232b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
12242b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
12252b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
122689fd9fafSBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,PETSC_NULL,&h);CHKERRQ(ierr);
122789fd9fafSBarry Smith     bottom = y - h;
12282b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
12292b0a91c0SBarry Smith     if (ts->ops->view) {
12302b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
12312b0a91c0SBarry Smith     }
12322b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1233d763cef2SBarry Smith   }
1234b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1235251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1236b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1237d763cef2SBarry Smith   PetscFunctionReturn(0);
1238d763cef2SBarry Smith }
1239d763cef2SBarry Smith 
1240d763cef2SBarry Smith 
12414a2ae208SSatish Balay #undef __FUNCT__
12424a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1243b07ff414SBarry Smith /*@
1244d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1245d763cef2SBarry Smith    the timesteppers.
1246d763cef2SBarry Smith 
12473f9fe445SBarry Smith    Logically Collective on TS
1248d763cef2SBarry Smith 
1249d763cef2SBarry Smith    Input Parameters:
1250d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1251d763cef2SBarry Smith -  usrP - optional user context
1252d763cef2SBarry Smith 
1253d763cef2SBarry Smith    Level: intermediate
1254d763cef2SBarry Smith 
1255d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1256d763cef2SBarry Smith 
1257d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1258d763cef2SBarry Smith @*/
12597087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1260d763cef2SBarry Smith {
1261d763cef2SBarry Smith   PetscFunctionBegin;
12620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1263d763cef2SBarry Smith   ts->user = usrP;
1264d763cef2SBarry Smith   PetscFunctionReturn(0);
1265d763cef2SBarry Smith }
1266d763cef2SBarry Smith 
12674a2ae208SSatish Balay #undef __FUNCT__
12684a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1269b07ff414SBarry Smith /*@
1270d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1271d763cef2SBarry Smith     timestepper.
1272d763cef2SBarry Smith 
1273d763cef2SBarry Smith     Not Collective
1274d763cef2SBarry Smith 
1275d763cef2SBarry Smith     Input Parameter:
1276d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1277d763cef2SBarry Smith 
1278d763cef2SBarry Smith     Output Parameter:
1279d763cef2SBarry Smith .   usrP - user context
1280d763cef2SBarry Smith 
1281d763cef2SBarry Smith     Level: intermediate
1282d763cef2SBarry Smith 
1283d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1284d763cef2SBarry Smith 
1285d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1286d763cef2SBarry Smith @*/
1287e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1288d763cef2SBarry Smith {
1289d763cef2SBarry Smith   PetscFunctionBegin;
12900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1291e71120c6SJed Brown   *(void**)usrP = ts->user;
1292d763cef2SBarry Smith   PetscFunctionReturn(0);
1293d763cef2SBarry Smith }
1294d763cef2SBarry Smith 
12954a2ae208SSatish Balay #undef __FUNCT__
12964a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1297d763cef2SBarry Smith /*@
1298b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1299d763cef2SBarry Smith 
1300d763cef2SBarry Smith    Not Collective
1301d763cef2SBarry Smith 
1302d763cef2SBarry Smith    Input Parameter:
1303d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1304d763cef2SBarry Smith 
1305d763cef2SBarry Smith    Output Parameter:
1306b8123daeSJed Brown .  iter - number of steps completed so far
1307d763cef2SBarry Smith 
1308d763cef2SBarry Smith    Level: intermediate
1309d763cef2SBarry Smith 
1310d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1311b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1312d763cef2SBarry Smith @*/
13137087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1314d763cef2SBarry Smith {
1315d763cef2SBarry Smith   PetscFunctionBegin;
13160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13174482741eSBarry Smith   PetscValidIntPointer(iter,2);
1318d763cef2SBarry Smith   *iter = ts->steps;
1319d763cef2SBarry Smith   PetscFunctionReturn(0);
1320d763cef2SBarry Smith }
1321d763cef2SBarry Smith 
13224a2ae208SSatish Balay #undef __FUNCT__
13234a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1324d763cef2SBarry Smith /*@
1325d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1326d763cef2SBarry Smith    as well as the initial time.
1327d763cef2SBarry Smith 
13283f9fe445SBarry Smith    Logically Collective on TS
1329d763cef2SBarry Smith 
1330d763cef2SBarry Smith    Input Parameters:
1331d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1332d763cef2SBarry Smith .  initial_time - the initial time
1333d763cef2SBarry Smith -  time_step - the size of the timestep
1334d763cef2SBarry Smith 
1335d763cef2SBarry Smith    Level: intermediate
1336d763cef2SBarry Smith 
1337d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1338d763cef2SBarry Smith 
1339d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1340d763cef2SBarry Smith @*/
13417087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1342d763cef2SBarry Smith {
1343e144a568SJed Brown   PetscErrorCode ierr;
1344e144a568SJed Brown 
1345d763cef2SBarry Smith   PetscFunctionBegin;
13460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1347e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1348d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1349d763cef2SBarry Smith   PetscFunctionReturn(0);
1350d763cef2SBarry Smith }
1351d763cef2SBarry Smith 
13524a2ae208SSatish Balay #undef __FUNCT__
13534a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1354d763cef2SBarry Smith /*@
1355d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1356d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1357d763cef2SBarry Smith 
13583f9fe445SBarry Smith    Logically Collective on TS
1359d763cef2SBarry Smith 
1360d763cef2SBarry Smith    Input Parameters:
1361d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1362d763cef2SBarry Smith -  time_step - the size of the timestep
1363d763cef2SBarry Smith 
1364d763cef2SBarry Smith    Level: intermediate
1365d763cef2SBarry Smith 
1366d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1367d763cef2SBarry Smith 
1368d763cef2SBarry Smith .keywords: TS, set, timestep
1369d763cef2SBarry Smith @*/
13707087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1371d763cef2SBarry Smith {
1372d763cef2SBarry Smith   PetscFunctionBegin;
13730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1374c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1375d763cef2SBarry Smith   ts->time_step      = time_step;
137631748224SBarry Smith   ts->time_step_orig = time_step;
1377d763cef2SBarry Smith   PetscFunctionReturn(0);
1378d763cef2SBarry Smith }
1379d763cef2SBarry Smith 
13804a2ae208SSatish Balay #undef __FUNCT__
1381a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1382a43b19c4SJed Brown /*@
138349354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
138449354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
138549354f04SShri Abhyankar      or just return at the final time TS computed.
1386a43b19c4SJed Brown 
1387a43b19c4SJed Brown   Logically Collective on TS
1388a43b19c4SJed Brown 
1389a43b19c4SJed Brown    Input Parameter:
1390a43b19c4SJed Brown +   ts - the time-step context
139149354f04SShri Abhyankar -   eftopt - exact final time option
1392a43b19c4SJed Brown 
1393a43b19c4SJed Brown    Level: beginner
1394a43b19c4SJed Brown 
1395a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1396a43b19c4SJed Brown @*/
139749354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1398a43b19c4SJed Brown {
1399a43b19c4SJed Brown   PetscFunctionBegin;
1400a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
140149354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
140249354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1403a43b19c4SJed Brown   PetscFunctionReturn(0);
1404a43b19c4SJed Brown }
1405a43b19c4SJed Brown 
1406a43b19c4SJed Brown #undef __FUNCT__
14074a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1408d763cef2SBarry Smith /*@
1409d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1410d763cef2SBarry Smith 
1411d763cef2SBarry Smith    Not Collective
1412d763cef2SBarry Smith 
1413d763cef2SBarry Smith    Input Parameter:
1414d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1415d763cef2SBarry Smith 
1416d763cef2SBarry Smith    Output Parameter:
1417d763cef2SBarry Smith .  dt - the current timestep size
1418d763cef2SBarry Smith 
1419d763cef2SBarry Smith    Level: intermediate
1420d763cef2SBarry Smith 
1421d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1422d763cef2SBarry Smith 
1423d763cef2SBarry Smith .keywords: TS, get, timestep
1424d763cef2SBarry Smith @*/
14257087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1426d763cef2SBarry Smith {
1427d763cef2SBarry Smith   PetscFunctionBegin;
14280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1429f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1430d763cef2SBarry Smith   *dt = ts->time_step;
1431d763cef2SBarry Smith   PetscFunctionReturn(0);
1432d763cef2SBarry Smith }
1433d763cef2SBarry Smith 
14344a2ae208SSatish Balay #undef __FUNCT__
14354a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1436d8e5e3e6SSatish Balay /*@
1437d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1438d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1439d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1440d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1441d763cef2SBarry Smith 
1442d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1443d763cef2SBarry Smith 
1444d763cef2SBarry Smith    Input Parameter:
1445d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1446d763cef2SBarry Smith 
1447d763cef2SBarry Smith    Output Parameter:
1448d763cef2SBarry Smith .  v - the vector containing the solution
1449d763cef2SBarry Smith 
1450d763cef2SBarry Smith    Level: intermediate
1451d763cef2SBarry Smith 
1452d763cef2SBarry Smith .seealso: TSGetTimeStep()
1453d763cef2SBarry Smith 
1454d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1455d763cef2SBarry Smith @*/
14567087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1457d763cef2SBarry Smith {
1458d763cef2SBarry Smith   PetscFunctionBegin;
14590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14604482741eSBarry Smith   PetscValidPointer(v,2);
14618737fe31SLisandro Dalcin   *v = ts->vec_sol;
1462d763cef2SBarry Smith   PetscFunctionReturn(0);
1463d763cef2SBarry Smith }
1464d763cef2SBarry Smith 
1465bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
14664a2ae208SSatish Balay #undef __FUNCT__
1467bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1468d8e5e3e6SSatish Balay /*@
1469bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1470d763cef2SBarry Smith 
1471bdad233fSMatthew Knepley   Not collective
1472d763cef2SBarry Smith 
1473bdad233fSMatthew Knepley   Input Parameters:
1474bdad233fSMatthew Knepley + ts   - The TS
1475bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1476d763cef2SBarry Smith .vb
14770910c330SBarry Smith          U_t - A U = 0      (linear)
14780910c330SBarry Smith          U_t - A(t) U = 0   (linear)
14790910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1480d763cef2SBarry Smith .ve
1481d763cef2SBarry Smith 
1482d763cef2SBarry Smith    Level: beginner
1483d763cef2SBarry Smith 
1484bdad233fSMatthew Knepley .keywords: TS, problem type
1485bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1486d763cef2SBarry Smith @*/
14877087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1488a7cc72afSBarry Smith {
14899e2a6581SJed Brown   PetscErrorCode ierr;
14909e2a6581SJed Brown 
1491d763cef2SBarry Smith   PetscFunctionBegin;
14920700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1493bdad233fSMatthew Knepley   ts->problem_type = type;
14949e2a6581SJed Brown   if (type == TS_LINEAR) {
14959e2a6581SJed Brown     SNES snes;
14969e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
14979e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
14989e2a6581SJed Brown   }
1499d763cef2SBarry Smith   PetscFunctionReturn(0);
1500d763cef2SBarry Smith }
1501d763cef2SBarry Smith 
1502bdad233fSMatthew Knepley #undef __FUNCT__
1503bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1504bdad233fSMatthew Knepley /*@C
1505bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1506bdad233fSMatthew Knepley 
1507bdad233fSMatthew Knepley   Not collective
1508bdad233fSMatthew Knepley 
1509bdad233fSMatthew Knepley   Input Parameter:
1510bdad233fSMatthew Knepley . ts   - The TS
1511bdad233fSMatthew Knepley 
1512bdad233fSMatthew Knepley   Output Parameter:
1513bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1514bdad233fSMatthew Knepley .vb
1515089b2837SJed Brown          M U_t = A U
1516089b2837SJed Brown          M(t) U_t = A(t) U
1517b5abc632SBarry Smith          F(t,U,U_t)
1518bdad233fSMatthew Knepley .ve
1519bdad233fSMatthew Knepley 
1520bdad233fSMatthew Knepley    Level: beginner
1521bdad233fSMatthew Knepley 
1522bdad233fSMatthew Knepley .keywords: TS, problem type
1523bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1524bdad233fSMatthew Knepley @*/
15257087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1526a7cc72afSBarry Smith {
1527bdad233fSMatthew Knepley   PetscFunctionBegin;
15280700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
15294482741eSBarry Smith   PetscValidIntPointer(type,2);
1530bdad233fSMatthew Knepley   *type = ts->problem_type;
1531bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1532bdad233fSMatthew Knepley }
1533d763cef2SBarry Smith 
15344a2ae208SSatish Balay #undef __FUNCT__
15354a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1536d763cef2SBarry Smith /*@
1537d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1538d763cef2SBarry Smith    of a timestepper.
1539d763cef2SBarry Smith 
1540d763cef2SBarry Smith    Collective on TS
1541d763cef2SBarry Smith 
1542d763cef2SBarry Smith    Input Parameter:
1543d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1544d763cef2SBarry Smith 
1545d763cef2SBarry Smith    Notes:
1546d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1547d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1548d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1549d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1550d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1551d763cef2SBarry Smith 
1552d763cef2SBarry Smith    Level: advanced
1553d763cef2SBarry Smith 
1554d763cef2SBarry Smith .keywords: TS, timestep, setup
1555d763cef2SBarry Smith 
1556d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1557d763cef2SBarry Smith @*/
15587087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1559d763cef2SBarry Smith {
1560dfbe8321SBarry Smith   PetscErrorCode ierr;
15616c6b9e74SPeter Brune   DM             dm;
15626c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
15636c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
15646c6b9e74SPeter Brune   TSIJacobian    ijac;
15656c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1566d763cef2SBarry Smith 
1567d763cef2SBarry Smith   PetscFunctionBegin;
15680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1569277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1570277b19d0SLisandro Dalcin 
15717adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
15729596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1573d763cef2SBarry Smith   }
1574277b19d0SLisandro Dalcin 
1575277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1576277b19d0SLisandro Dalcin 
1577ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr);
15781c3436cfSJed Brown 
1579277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1580000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1581277b19d0SLisandro Dalcin   }
1582277b19d0SLisandro Dalcin 
15836c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
15846c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
15856c6b9e74SPeter Brune    */
15866c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
15876c6b9e74SPeter Brune   ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr);
15886c6b9e74SPeter Brune   if (!func) {
15896c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
15906c6b9e74SPeter Brune   }
15916c6b9e74SPeter 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.
15926c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
15936c6b9e74SPeter Brune    */
15946c6b9e74SPeter Brune   ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr);
15956c6b9e74SPeter Brune   ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr);
15966c6b9e74SPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr);
15976c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
15986c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
15996c6b9e74SPeter Brune   }
1600277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1601277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1602277b19d0SLisandro Dalcin }
1603277b19d0SLisandro Dalcin 
1604277b19d0SLisandro Dalcin #undef __FUNCT__
1605277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1606277b19d0SLisandro Dalcin /*@
1607277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1608277b19d0SLisandro Dalcin 
1609277b19d0SLisandro Dalcin    Collective on TS
1610277b19d0SLisandro Dalcin 
1611277b19d0SLisandro Dalcin    Input Parameter:
1612277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1613277b19d0SLisandro Dalcin 
1614277b19d0SLisandro Dalcin    Level: beginner
1615277b19d0SLisandro Dalcin 
1616277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1617277b19d0SLisandro Dalcin 
1618277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1619277b19d0SLisandro Dalcin @*/
1620277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1621277b19d0SLisandro Dalcin {
1622277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1623277b19d0SLisandro Dalcin 
1624277b19d0SLisandro Dalcin   PetscFunctionBegin;
1625277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1626277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1627277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1628277b19d0SLisandro Dalcin   }
1629277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1630bbd56ea5SKarl Rupp 
16314e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
16324e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1633214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
16346bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1635e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1636e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
163738637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1638bbd56ea5SKarl Rupp 
1639277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1640d763cef2SBarry Smith   PetscFunctionReturn(0);
1641d763cef2SBarry Smith }
1642d763cef2SBarry Smith 
16434a2ae208SSatish Balay #undef __FUNCT__
16444a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1645d8e5e3e6SSatish Balay /*@
1646d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1647d763cef2SBarry Smith    with TSCreate().
1648d763cef2SBarry Smith 
1649d763cef2SBarry Smith    Collective on TS
1650d763cef2SBarry Smith 
1651d763cef2SBarry Smith    Input Parameter:
1652d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1653d763cef2SBarry Smith 
1654d763cef2SBarry Smith    Level: beginner
1655d763cef2SBarry Smith 
1656d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1657d763cef2SBarry Smith 
1658d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1659d763cef2SBarry Smith @*/
16606bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1661d763cef2SBarry Smith {
16626849ba73SBarry Smith   PetscErrorCode ierr;
1663d763cef2SBarry Smith 
1664d763cef2SBarry Smith   PetscFunctionBegin;
16656bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
16666bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
16676bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1668d763cef2SBarry Smith 
16696bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1670277b19d0SLisandro Dalcin 
1671be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
16726bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
16736bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
16746d4c513bSLisandro Dalcin 
167584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
16766bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
16776bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
16786bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
16796d4c513bSLisandro Dalcin 
1680a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1681d763cef2SBarry Smith   PetscFunctionReturn(0);
1682d763cef2SBarry Smith }
1683d763cef2SBarry Smith 
16844a2ae208SSatish Balay #undef __FUNCT__
16854a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1686d8e5e3e6SSatish Balay /*@
1687d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1688d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1689d763cef2SBarry Smith 
1690d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1691d763cef2SBarry Smith 
1692d763cef2SBarry Smith    Input Parameter:
1693d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1694d763cef2SBarry Smith 
1695d763cef2SBarry Smith    Output Parameter:
1696d763cef2SBarry Smith .  snes - the nonlinear solver context
1697d763cef2SBarry Smith 
1698d763cef2SBarry Smith    Notes:
1699d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1700d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
170194b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1702d763cef2SBarry Smith 
1703d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1704d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1705d763cef2SBarry Smith 
1706d763cef2SBarry Smith    Level: beginner
1707d763cef2SBarry Smith 
1708d763cef2SBarry Smith .keywords: timestep, get, SNES
1709d763cef2SBarry Smith @*/
17107087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1711d763cef2SBarry Smith {
1712d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1713d372ba47SLisandro Dalcin 
1714d763cef2SBarry Smith   PetscFunctionBegin;
17150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17164482741eSBarry Smith   PetscValidPointer(snes,2);
1717d372ba47SLisandro Dalcin   if (!ts->snes) {
1718d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
17196c6b9e74SPeter Brune     ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1720d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1721d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1722496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
17239e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
17249e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
17259e2a6581SJed Brown     }
1726d372ba47SLisandro Dalcin   }
1727d763cef2SBarry Smith   *snes = ts->snes;
1728d763cef2SBarry Smith   PetscFunctionReturn(0);
1729d763cef2SBarry Smith }
1730d763cef2SBarry Smith 
17314a2ae208SSatish Balay #undef __FUNCT__
1732deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1733deb2cd25SJed Brown /*@
1734deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1735deb2cd25SJed Brown 
1736deb2cd25SJed Brown    Collective
1737deb2cd25SJed Brown 
1738deb2cd25SJed Brown    Input Parameter:
1739deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1740deb2cd25SJed Brown -  snes - the nonlinear solver context
1741deb2cd25SJed Brown 
1742deb2cd25SJed Brown    Notes:
1743deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1744deb2cd25SJed Brown 
1745deb2cd25SJed Brown    Level: developer
1746deb2cd25SJed Brown 
1747deb2cd25SJed Brown .keywords: timestep, set, SNES
1748deb2cd25SJed Brown @*/
1749deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1750deb2cd25SJed Brown {
1751deb2cd25SJed Brown   PetscErrorCode ierr;
1752740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1753deb2cd25SJed Brown 
1754deb2cd25SJed Brown   PetscFunctionBegin;
1755deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1756deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1757deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1758deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1759bbd56ea5SKarl Rupp 
1760deb2cd25SJed Brown   ts->snes = snes;
1761bbd56ea5SKarl Rupp 
1762740132f1SEmil Constantinescu   ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1763740132f1SEmil Constantinescu   ierr = SNESGetJacobian(ts->snes,PETSC_NULL,PETSC_NULL,&func,PETSC_NULL);CHKERRQ(ierr);
1764740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
1765740132f1SEmil Constantinescu     ierr = SNESSetJacobian(ts->snes,PETSC_NULL,PETSC_NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1766740132f1SEmil Constantinescu   }
1767deb2cd25SJed Brown   PetscFunctionReturn(0);
1768deb2cd25SJed Brown }
1769deb2cd25SJed Brown 
1770deb2cd25SJed Brown #undef __FUNCT__
177194b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1772d8e5e3e6SSatish Balay /*@
177394b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1774d763cef2SBarry Smith    a TS (timestepper) context.
1775d763cef2SBarry Smith 
177694b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1777d763cef2SBarry Smith 
1778d763cef2SBarry Smith    Input Parameter:
1779d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1780d763cef2SBarry Smith 
1781d763cef2SBarry Smith    Output Parameter:
178294b7f48cSBarry Smith .  ksp - the nonlinear solver context
1783d763cef2SBarry Smith 
1784d763cef2SBarry Smith    Notes:
178594b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1786d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1787d763cef2SBarry Smith    KSP and PC contexts as well.
1788d763cef2SBarry Smith 
178994b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
179094b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1791d763cef2SBarry Smith 
1792d763cef2SBarry Smith    Level: beginner
1793d763cef2SBarry Smith 
179494b7f48cSBarry Smith .keywords: timestep, get, KSP
1795d763cef2SBarry Smith @*/
17967087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1797d763cef2SBarry Smith {
1798d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1799089b2837SJed Brown   SNES           snes;
1800d372ba47SLisandro Dalcin 
1801d763cef2SBarry Smith   PetscFunctionBegin;
18020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18034482741eSBarry Smith   PetscValidPointer(ksp,2);
180417186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1805e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1806089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1807089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1808d763cef2SBarry Smith   PetscFunctionReturn(0);
1809d763cef2SBarry Smith }
1810d763cef2SBarry Smith 
1811d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1812d763cef2SBarry Smith 
18134a2ae208SSatish Balay #undef __FUNCT__
1814adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1815adb62b0dSMatthew Knepley /*@
1816adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1817adb62b0dSMatthew Knepley    maximum time for iteration.
1818adb62b0dSMatthew Knepley 
18193f9fe445SBarry Smith    Not Collective
1820adb62b0dSMatthew Knepley 
1821adb62b0dSMatthew Knepley    Input Parameters:
1822adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1823adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1824adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1825adb62b0dSMatthew Knepley 
1826adb62b0dSMatthew Knepley    Level: intermediate
1827adb62b0dSMatthew Knepley 
1828adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1829adb62b0dSMatthew Knepley @*/
18307087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1831adb62b0dSMatthew Knepley {
1832adb62b0dSMatthew Knepley   PetscFunctionBegin;
18330700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1834abc0a331SBarry Smith   if (maxsteps) {
18354482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1836adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1837adb62b0dSMatthew Knepley   }
1838abc0a331SBarry Smith   if (maxtime) {
18394482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1840adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
1841adb62b0dSMatthew Knepley   }
1842adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1843adb62b0dSMatthew Knepley }
1844adb62b0dSMatthew Knepley 
1845adb62b0dSMatthew Knepley #undef __FUNCT__
18464a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1847d763cef2SBarry Smith /*@
1848d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1849d763cef2SBarry Smith    maximum time for iteration.
1850d763cef2SBarry Smith 
18513f9fe445SBarry Smith    Logically Collective on TS
1852d763cef2SBarry Smith 
1853d763cef2SBarry Smith    Input Parameters:
1854d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1855d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1856d763cef2SBarry Smith -  maxtime - final time to iterate to
1857d763cef2SBarry Smith 
1858d763cef2SBarry Smith    Options Database Keys:
1859d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
18603bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1861d763cef2SBarry Smith 
1862d763cef2SBarry Smith    Notes:
1863d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1864d763cef2SBarry Smith 
1865d763cef2SBarry Smith    Level: intermediate
1866d763cef2SBarry Smith 
1867d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1868a43b19c4SJed Brown 
1869a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1870d763cef2SBarry Smith @*/
18717087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1872d763cef2SBarry Smith {
1873d763cef2SBarry Smith   PetscFunctionBegin;
18740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1875c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1876c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
187739b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
187839b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
1879d763cef2SBarry Smith   PetscFunctionReturn(0);
1880d763cef2SBarry Smith }
1881d763cef2SBarry Smith 
18824a2ae208SSatish Balay #undef __FUNCT__
18834a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1884d763cef2SBarry Smith /*@
1885d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1886d763cef2SBarry Smith    for use by the TS routines.
1887d763cef2SBarry Smith 
18883f9fe445SBarry Smith    Logically Collective on TS and Vec
1889d763cef2SBarry Smith 
1890d763cef2SBarry Smith    Input Parameters:
1891d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
18920910c330SBarry Smith -  u - the solution vector
1893d763cef2SBarry Smith 
1894d763cef2SBarry Smith    Level: beginner
1895d763cef2SBarry Smith 
1896d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1897d763cef2SBarry Smith @*/
18980910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
1899d763cef2SBarry Smith {
19008737fe31SLisandro Dalcin   PetscErrorCode ierr;
1901496e6a7aSJed Brown   DM             dm;
19028737fe31SLisandro Dalcin 
1903d763cef2SBarry Smith   PetscFunctionBegin;
19040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19050910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
19060910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
19076bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1908bbd56ea5SKarl Rupp 
19090910c330SBarry Smith   ts->vec_sol = u;
1910bbd56ea5SKarl Rupp 
1911496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
19120910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
1913d763cef2SBarry Smith   PetscFunctionReturn(0);
1914d763cef2SBarry Smith }
1915d763cef2SBarry Smith 
1916e74ef692SMatthew Knepley #undef __FUNCT__
1917e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1918ac226902SBarry Smith /*@C
1919000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
19203f2090d5SJed Brown   called once at the beginning of each time step.
1921000e7ae3SMatthew Knepley 
19223f9fe445SBarry Smith   Logically Collective on TS
1923000e7ae3SMatthew Knepley 
1924000e7ae3SMatthew Knepley   Input Parameters:
1925000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1926000e7ae3SMatthew Knepley - func - The function
1927000e7ae3SMatthew Knepley 
1928000e7ae3SMatthew Knepley   Calling sequence of func:
1929000e7ae3SMatthew Knepley . func (TS ts);
1930000e7ae3SMatthew Knepley 
1931000e7ae3SMatthew Knepley   Level: intermediate
1932000e7ae3SMatthew Knepley 
1933b8123daeSJed Brown   Note:
1934b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1935b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1936b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1937b8123daeSJed Brown 
1938000e7ae3SMatthew Knepley .keywords: TS, timestep
1939b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1940000e7ae3SMatthew Knepley @*/
19417087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1942000e7ae3SMatthew Knepley {
1943000e7ae3SMatthew Knepley   PetscFunctionBegin;
19440700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1945000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1946000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1947000e7ae3SMatthew Knepley }
1948000e7ae3SMatthew Knepley 
1949e74ef692SMatthew Knepley #undef __FUNCT__
19503f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
195109ee8438SJed Brown /*@
19523f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
19533f2090d5SJed Brown 
19543f2090d5SJed Brown   Collective on TS
19553f2090d5SJed Brown 
19563f2090d5SJed Brown   Input Parameters:
19573f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
19583f2090d5SJed Brown 
19593f2090d5SJed Brown   Notes:
19603f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
19613f2090d5SJed Brown   so most users would not generally call this routine themselves.
19623f2090d5SJed Brown 
19633f2090d5SJed Brown   Level: developer
19643f2090d5SJed Brown 
19653f2090d5SJed Brown .keywords: TS, timestep
1966b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
19673f2090d5SJed Brown @*/
19687087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
19693f2090d5SJed Brown {
19703f2090d5SJed Brown   PetscErrorCode ierr;
19713f2090d5SJed Brown 
19723f2090d5SJed Brown   PetscFunctionBegin;
19730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
197472ac3e02SJed Brown   if (ts->ops->prestep) {
19753f2090d5SJed Brown     PetscStackPush("TS PreStep function");
19763f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
19773f2090d5SJed Brown     PetscStackPop;
1978312ce896SJed Brown   }
19793f2090d5SJed Brown   PetscFunctionReturn(0);
19803f2090d5SJed Brown }
19813f2090d5SJed Brown 
19823f2090d5SJed Brown #undef __FUNCT__
1983b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1984b8123daeSJed Brown /*@C
1985b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1986b8123daeSJed Brown   called once at the beginning of each stage.
1987b8123daeSJed Brown 
1988b8123daeSJed Brown   Logically Collective on TS
1989b8123daeSJed Brown 
1990b8123daeSJed Brown   Input Parameters:
1991b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1992b8123daeSJed Brown - func - The function
1993b8123daeSJed Brown 
1994b8123daeSJed Brown   Calling sequence of func:
1995b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1996b8123daeSJed Brown 
1997b8123daeSJed Brown   Level: intermediate
1998b8123daeSJed Brown 
1999b8123daeSJed Brown   Note:
2000b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2001b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2002b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2003b8123daeSJed Brown 
2004b8123daeSJed Brown .keywords: TS, timestep
2005b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2006b8123daeSJed Brown @*/
2007b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2008b8123daeSJed Brown {
2009b8123daeSJed Brown   PetscFunctionBegin;
2010b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2011b8123daeSJed Brown   ts->ops->prestage = func;
2012b8123daeSJed Brown   PetscFunctionReturn(0);
2013b8123daeSJed Brown }
2014b8123daeSJed Brown 
2015b8123daeSJed Brown #undef __FUNCT__
2016b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2017b8123daeSJed Brown /*@
2018b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2019b8123daeSJed Brown 
2020b8123daeSJed Brown   Collective on TS
2021b8123daeSJed Brown 
2022b8123daeSJed Brown   Input Parameters:
2023b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2024b8123daeSJed Brown 
2025b8123daeSJed Brown   Notes:
2026b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2027b8123daeSJed Brown   most users would not generally call this routine themselves.
2028b8123daeSJed Brown 
2029b8123daeSJed Brown   Level: developer
2030b8123daeSJed Brown 
2031b8123daeSJed Brown .keywords: TS, timestep
2032b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2033b8123daeSJed Brown @*/
2034b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2035b8123daeSJed Brown {
2036b8123daeSJed Brown   PetscErrorCode ierr;
2037b8123daeSJed Brown 
2038b8123daeSJed Brown   PetscFunctionBegin;
2039b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2040b8123daeSJed Brown   if (ts->ops->prestage) {
2041b8123daeSJed Brown     PetscStackPush("TS PreStage function");
2042b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
2043b8123daeSJed Brown     PetscStackPop;
2044b8123daeSJed Brown   }
2045b8123daeSJed Brown   PetscFunctionReturn(0);
2046b8123daeSJed Brown }
2047b8123daeSJed Brown 
2048b8123daeSJed Brown #undef __FUNCT__
2049e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2050ac226902SBarry Smith /*@C
2051000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
20523f2090d5SJed Brown   called once at the end of each time step.
2053000e7ae3SMatthew Knepley 
20543f9fe445SBarry Smith   Logically Collective on TS
2055000e7ae3SMatthew Knepley 
2056000e7ae3SMatthew Knepley   Input Parameters:
2057000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2058000e7ae3SMatthew Knepley - func - The function
2059000e7ae3SMatthew Knepley 
2060000e7ae3SMatthew Knepley   Calling sequence of func:
2061b8123daeSJed Brown $ func (TS ts);
2062000e7ae3SMatthew Knepley 
2063000e7ae3SMatthew Knepley   Level: intermediate
2064000e7ae3SMatthew Knepley 
2065000e7ae3SMatthew Knepley .keywords: TS, timestep
2066b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2067000e7ae3SMatthew Knepley @*/
20687087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2069000e7ae3SMatthew Knepley {
2070000e7ae3SMatthew Knepley   PetscFunctionBegin;
20710700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2072000e7ae3SMatthew Knepley   ts->ops->poststep = func;
2073000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2074000e7ae3SMatthew Knepley }
2075000e7ae3SMatthew Knepley 
2076e74ef692SMatthew Knepley #undef __FUNCT__
20773f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
207809ee8438SJed Brown /*@
20793f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
20803f2090d5SJed Brown 
20813f2090d5SJed Brown   Collective on TS
20823f2090d5SJed Brown 
20833f2090d5SJed Brown   Input Parameters:
20843f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
20853f2090d5SJed Brown 
20863f2090d5SJed Brown   Notes:
20873f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
20883f2090d5SJed Brown   so most users would not generally call this routine themselves.
20893f2090d5SJed Brown 
20903f2090d5SJed Brown   Level: developer
20913f2090d5SJed Brown 
20923f2090d5SJed Brown .keywords: TS, timestep
20933f2090d5SJed Brown @*/
20947087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
20953f2090d5SJed Brown {
20963f2090d5SJed Brown   PetscErrorCode ierr;
20973f2090d5SJed Brown 
20983f2090d5SJed Brown   PetscFunctionBegin;
20990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
210072ac3e02SJed Brown   if (ts->ops->poststep) {
21013f2090d5SJed Brown     PetscStackPush("TS PostStep function");
21023f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
21033f2090d5SJed Brown     PetscStackPop;
210472ac3e02SJed Brown   }
21053f2090d5SJed Brown   PetscFunctionReturn(0);
21063f2090d5SJed Brown }
21073f2090d5SJed Brown 
2108d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2109d763cef2SBarry Smith 
21104a2ae208SSatish Balay #undef __FUNCT__
2111a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2112d763cef2SBarry Smith /*@C
2113a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2114d763cef2SBarry Smith    timestep to display the iteration's  progress.
2115d763cef2SBarry Smith 
21163f9fe445SBarry Smith    Logically Collective on TS
2117d763cef2SBarry Smith 
2118d763cef2SBarry Smith    Input Parameters:
2119d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2120e213d8f1SJed Brown .  monitor - monitoring routine
2121329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
2122b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
2123b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2124b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
2125d763cef2SBarry Smith 
2126e213d8f1SJed Brown    Calling sequence of monitor:
21270910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2128d763cef2SBarry Smith 
2129d763cef2SBarry Smith +    ts - the TS context
213088c05cc5SBarry 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
213188c05cc5SBarry Smith                                been interpolated to)
21321f06c33eSBarry Smith .    time - current time
21330910c330SBarry Smith .    u - current iterate
2134d763cef2SBarry Smith -    mctx - [optional] monitoring context
2135d763cef2SBarry Smith 
2136d763cef2SBarry Smith    Notes:
2137d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2138d763cef2SBarry Smith    already has been loaded.
2139d763cef2SBarry Smith 
2140025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2141025f1a04SBarry Smith 
2142d763cef2SBarry Smith    Level: intermediate
2143d763cef2SBarry Smith 
2144d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2145d763cef2SBarry Smith 
2146a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2147d763cef2SBarry Smith @*/
2148c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2149d763cef2SBarry Smith {
2150d763cef2SBarry Smith   PetscFunctionBegin;
21510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
215217186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2153d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
21548704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2155d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2156d763cef2SBarry Smith   PetscFunctionReturn(0);
2157d763cef2SBarry Smith }
2158d763cef2SBarry Smith 
21594a2ae208SSatish Balay #undef __FUNCT__
2160a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2161d763cef2SBarry Smith /*@C
2162a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2163d763cef2SBarry Smith 
21643f9fe445SBarry Smith    Logically Collective on TS
2165d763cef2SBarry Smith 
2166d763cef2SBarry Smith    Input Parameters:
2167d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2168d763cef2SBarry Smith 
2169d763cef2SBarry Smith    Notes:
2170d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2171d763cef2SBarry Smith 
2172d763cef2SBarry Smith    Level: intermediate
2173d763cef2SBarry Smith 
2174d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2175d763cef2SBarry Smith 
2176a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2177d763cef2SBarry Smith @*/
21787087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2179d763cef2SBarry Smith {
2180d952e501SBarry Smith   PetscErrorCode ierr;
2181d952e501SBarry Smith   PetscInt       i;
2182d952e501SBarry Smith 
2183d763cef2SBarry Smith   PetscFunctionBegin;
21840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2185d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
21868704b422SBarry Smith     if (ts->monitordestroy[i]) {
21878704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2188d952e501SBarry Smith     }
2189d952e501SBarry Smith   }
2190d763cef2SBarry Smith   ts->numbermonitors = 0;
2191d763cef2SBarry Smith   PetscFunctionReturn(0);
2192d763cef2SBarry Smith }
2193d763cef2SBarry Smith 
21944a2ae208SSatish Balay #undef __FUNCT__
2195a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2196d8e5e3e6SSatish Balay /*@
2197a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
21985516499fSSatish Balay 
21995516499fSSatish Balay    Level: intermediate
220041251cbbSSatish Balay 
22015516499fSSatish Balay .keywords: TS, set, monitor
22025516499fSSatish Balay 
220341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
220441251cbbSSatish Balay @*/
2205649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2206d763cef2SBarry Smith {
2207dfbe8321SBarry Smith   PetscErrorCode ierr;
2208649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
2209d132466eSBarry Smith 
2210d763cef2SBarry Smith   PetscFunctionBegin;
2211649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2212971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2213649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2214d763cef2SBarry Smith   PetscFunctionReturn(0);
2215d763cef2SBarry Smith }
2216d763cef2SBarry Smith 
22174a2ae208SSatish Balay #undef __FUNCT__
2218cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2219cd652676SJed Brown /*@
2220cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2221cd652676SJed Brown 
2222cd652676SJed Brown    Logically Collective on TS
2223cd652676SJed Brown 
2224cd652676SJed Brown    Input Argument:
2225cd652676SJed Brown .  ts - time stepping context
2226cd652676SJed Brown 
2227cd652676SJed Brown    Output Argument:
2228cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2229cd652676SJed Brown 
2230cd652676SJed Brown    Level: intermediate
2231cd652676SJed Brown 
2232cd652676SJed Brown .keywords: TS, set
2233cd652676SJed Brown 
2234cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2235cd652676SJed Brown @*/
2236cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2237cd652676SJed Brown {
2238cd652676SJed Brown   PetscFunctionBegin;
2239cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2240cd652676SJed Brown   ts->retain_stages = flg;
2241cd652676SJed Brown   PetscFunctionReturn(0);
2242cd652676SJed Brown }
2243cd652676SJed Brown 
2244cd652676SJed Brown #undef __FUNCT__
2245cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2246cd652676SJed Brown /*@
2247cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2248cd652676SJed Brown 
2249cd652676SJed Brown    Collective on TS
2250cd652676SJed Brown 
2251cd652676SJed Brown    Input Argument:
2252cd652676SJed Brown +  ts - time stepping context
2253cd652676SJed Brown -  t - time to interpolate to
2254cd652676SJed Brown 
2255cd652676SJed Brown    Output Argument:
22560910c330SBarry Smith .  U - state at given time
2257cd652676SJed Brown 
2258cd652676SJed Brown    Notes:
2259cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2260cd652676SJed Brown 
2261cd652676SJed Brown    Level: intermediate
2262cd652676SJed Brown 
2263cd652676SJed Brown    Developer Notes:
2264cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2265cd652676SJed Brown 
2266cd652676SJed Brown .keywords: TS, set
2267cd652676SJed Brown 
2268cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2269cd652676SJed Brown @*/
22700910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2271cd652676SJed Brown {
2272cd652676SJed Brown   PetscErrorCode ierr;
2273cd652676SJed Brown 
2274cd652676SJed Brown   PetscFunctionBegin;
2275cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2276d8cd7023SBarry Smith   if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime);
2277cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
22780910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2279cd652676SJed Brown   PetscFunctionReturn(0);
2280cd652676SJed Brown }
2281cd652676SJed Brown 
2282cd652676SJed Brown #undef __FUNCT__
22834a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2284d763cef2SBarry Smith /*@
22856d9e5789SSean Farley    TSStep - Steps one time step
2286d763cef2SBarry Smith 
2287d763cef2SBarry Smith    Collective on TS
2288d763cef2SBarry Smith 
2289d763cef2SBarry Smith    Input Parameter:
2290d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2291d763cef2SBarry Smith 
22926d9e5789SSean Farley    Level: intermediate
2293d763cef2SBarry Smith 
2294b8123daeSJed Brown    Notes:
2295b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2296b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2297b8123daeSJed Brown 
229825cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
229925cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
230025cb2221SBarry Smith 
2301d763cef2SBarry Smith .keywords: TS, timestep, solve
2302d763cef2SBarry Smith 
230325cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2304d763cef2SBarry Smith @*/
2305193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2306d763cef2SBarry Smith {
2307362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2308dfbe8321SBarry Smith   PetscErrorCode ierr;
2309d763cef2SBarry Smith 
2310d763cef2SBarry Smith   PetscFunctionBegin;
23110700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2312d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2313d405a339SMatthew Knepley 
2314362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2315362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2316bbd56ea5SKarl Rupp 
2317d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2318193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2319d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2320bbd56ea5SKarl Rupp 
2321362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2322362cd11cSLisandro Dalcin 
2323362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2324cef5090cSJed Brown     if (ts->errorifstepfailed) {
2325cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2326cef5090cSJed Brown         SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
2327cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2328cef5090cSJed Brown     }
2329362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2330db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2331db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2332362cd11cSLisandro Dalcin   }
2333d763cef2SBarry Smith   PetscFunctionReturn(0);
2334d763cef2SBarry Smith }
2335d763cef2SBarry Smith 
23364a2ae208SSatish Balay #undef __FUNCT__
233705175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
233805175c85SJed Brown /*@
233905175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
234005175c85SJed Brown 
23411c3436cfSJed Brown    Collective on TS
234205175c85SJed Brown 
234305175c85SJed Brown    Input Arguments:
23441c3436cfSJed Brown +  ts - time stepping context
23451c3436cfSJed Brown .  order - desired order of accuracy
23461c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
234705175c85SJed Brown 
234805175c85SJed Brown    Output Arguments:
23490910c330SBarry Smith .  U - state at the end of the current step
235005175c85SJed Brown 
235105175c85SJed Brown    Level: advanced
235205175c85SJed Brown 
2353108c343cSJed Brown    Notes:
2354108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2355108c343cSJed 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.
2356108c343cSJed Brown 
23571c3436cfSJed Brown .seealso: TSStep(), TSAdapt
235805175c85SJed Brown @*/
23590910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
236005175c85SJed Brown {
236105175c85SJed Brown   PetscErrorCode ierr;
236205175c85SJed Brown 
236305175c85SJed Brown   PetscFunctionBegin;
236405175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
236505175c85SJed Brown   PetscValidType(ts,1);
23660910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
23671c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
23680910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
236905175c85SJed Brown   PetscFunctionReturn(0);
237005175c85SJed Brown }
237105175c85SJed Brown 
237205175c85SJed Brown #undef __FUNCT__
23736a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
23746a4d4014SLisandro Dalcin /*@
23756a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
23766a4d4014SLisandro Dalcin 
23776a4d4014SLisandro Dalcin    Collective on TS
23786a4d4014SLisandro Dalcin 
23796a4d4014SLisandro Dalcin    Input Parameter:
23806a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2381cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
23825a3a76d0SJed Brown 
23836a4d4014SLisandro Dalcin    Level: beginner
23846a4d4014SLisandro Dalcin 
23855a3a76d0SJed Brown    Notes:
23865a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
23875a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
23885a3a76d0SJed Brown    stepped over the final time.
23895a3a76d0SJed Brown 
23906a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
23916a4d4014SLisandro Dalcin 
23926a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
23936a4d4014SLisandro Dalcin @*/
2394cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
23956a4d4014SLisandro Dalcin {
23964d7d938eSLisandro Dalcin   PetscBool         flg;
23974d7d938eSLisandro Dalcin   PetscViewer       viewer;
23986a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2399cffb1e40SBarry Smith   PetscViewerFormat format;
2400f22f69f0SBarry Smith 
24016a4d4014SLisandro Dalcin   PetscFunctionBegin;
24020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2403f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
240449354f04SShri 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 */
24050910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
24065a3a76d0SJed Brown       Vec y;
24070910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
24085a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
24095a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
24105a3a76d0SJed Brown     }
2411f2c2a1b9SBarry Smith     if (u) {
24120910c330SBarry Smith       ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2413f2c2a1b9SBarry Smith     }
2414bbd56ea5SKarl Rupp   } else if (u) {
24150910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
24165a3a76d0SJed Brown   }
2417b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
24186a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2419193ac0bcSJed Brown   ts->steps             = 0;
24205ef26d82SJed Brown   ts->ksp_its           = 0;
24215ef26d82SJed Brown   ts->snes_its          = 0;
2422c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2423c610991cSLisandro Dalcin   ts->reject            = 0;
2424193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2425193ac0bcSJed Brown 
2426193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2427193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
24280910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2429bbd56ea5SKarl Rupp 
2430cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2431193ac0bcSJed Brown   } else {
24326a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2433362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2434db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2435db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2436e1a7a14fSJed Brown     while (!ts->reason) {
2437193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2438193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2439193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2440193ac0bcSJed Brown     }
244149354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
24420910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2443bbd56ea5SKarl Rupp 
2444cc708dedSBarry Smith       ts->solvetime = ts->max_time;
24450574a7fbSJed Brown     } else {
2446ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2447cc708dedSBarry Smith       ts->solvetime = ts->ptime;
24480574a7fbSJed Brown     }
2449193ac0bcSJed Brown   }
24503923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2451cffb1e40SBarry Smith   ierr = PetscOptionsGetViewer(((PetscObject)ts)->comm,((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
24524d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2453cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
24544d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2455cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2456cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
24572b0a91c0SBarry Smith   }
24586a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
24596a4d4014SLisandro Dalcin }
24606a4d4014SLisandro Dalcin 
24616a4d4014SLisandro Dalcin #undef __FUNCT__
24624a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2463228d79bcSJed Brown /*@
2464228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2465228d79bcSJed Brown 
2466228d79bcSJed Brown    Collective on TS
2467228d79bcSJed Brown 
2468228d79bcSJed Brown    Input Parameters:
2469228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2470228d79bcSJed Brown .  step - step number that has just completed
2471228d79bcSJed Brown .  ptime - model time of the state
24720910c330SBarry Smith -  u - state at the current model time
2473228d79bcSJed Brown 
2474228d79bcSJed Brown    Notes:
2475228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2476228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2477228d79bcSJed Brown 
2478228d79bcSJed Brown    Level: advanced
2479228d79bcSJed Brown 
2480228d79bcSJed Brown .keywords: TS, timestep
2481228d79bcSJed Brown @*/
24820910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2483d763cef2SBarry Smith {
24846849ba73SBarry Smith   PetscErrorCode ierr;
2485a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2486d763cef2SBarry Smith 
2487d763cef2SBarry Smith   PetscFunctionBegin;
2488d763cef2SBarry Smith   for (i=0; i<n; i++) {
24890910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2490d763cef2SBarry Smith   }
2491d763cef2SBarry Smith   PetscFunctionReturn(0);
2492d763cef2SBarry Smith }
2493d763cef2SBarry Smith 
2494d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
24950b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
24960b039ecaSBarry Smith   PetscDrawLG lg;
24970b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2498201da799SBarry Smith   PetscInt    ksp_its,snes_its;
24990b039ecaSBarry Smith };
25000b039ecaSBarry Smith 
2501d763cef2SBarry Smith 
25024a2ae208SSatish Balay #undef __FUNCT__
2503a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2504d763cef2SBarry Smith /*@C
2505a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2506a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2507d763cef2SBarry Smith 
2508d763cef2SBarry Smith    Collective on TS
2509d763cef2SBarry Smith 
2510d763cef2SBarry Smith    Input Parameters:
2511d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2512d763cef2SBarry Smith .  label - the title to put in the title bar
25137c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2514a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2515a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2516d763cef2SBarry Smith 
2517d763cef2SBarry Smith    Output Parameter:
25180b039ecaSBarry Smith .  ctx - the context
2519d763cef2SBarry Smith 
2520d763cef2SBarry Smith    Options Database Key:
25214f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
25224f09c107SBarry Smith .  -ts_monitor_lg_solution -
252399fdda47SBarry Smith .  -ts_monitor_lg_error -
252499fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
252599fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
252699fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2527d763cef2SBarry Smith 
2528d763cef2SBarry Smith    Notes:
2529a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2530d763cef2SBarry Smith 
2531d763cef2SBarry Smith    Level: intermediate
2532d763cef2SBarry Smith 
25337c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2534d763cef2SBarry Smith 
25354f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
25367c922b88SBarry Smith 
2537d763cef2SBarry Smith @*/
2538a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2539d763cef2SBarry Smith {
2540b0a32e0cSBarry Smith   PetscDraw      win;
2541dfbe8321SBarry Smith   PetscErrorCode ierr;
254299fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2543d763cef2SBarry Smith 
2544d763cef2SBarry Smith   PetscFunctionBegin;
2545201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2546a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
25473fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
25480b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
254999fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr);
255099fdda47SBarry Smith   if (flg) {
25510b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
255299fdda47SBarry Smith   }
25530b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2554a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2555d763cef2SBarry Smith   PetscFunctionReturn(0);
2556d763cef2SBarry Smith }
2557d763cef2SBarry Smith 
25584a2ae208SSatish Balay #undef __FUNCT__
25594f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
25604f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2561d763cef2SBarry Smith {
25620b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2563c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2564dfbe8321SBarry Smith   PetscErrorCode ierr;
2565d763cef2SBarry Smith 
2566d763cef2SBarry Smith   PetscFunctionBegin;
2567a9f9c1f6SBarry Smith   if (!n) {
2568a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2569a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2570a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2571a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2572a9f9c1f6SBarry Smith   }
2573c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
25740b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
257599fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))) {
25760b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
25773923b477SBarry Smith   }
2578d763cef2SBarry Smith   PetscFunctionReturn(0);
2579d763cef2SBarry Smith }
2580d763cef2SBarry Smith 
25814a2ae208SSatish Balay #undef __FUNCT__
2582a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2583d763cef2SBarry Smith /*@C
2584a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2585a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2586d763cef2SBarry Smith 
25870b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2588d763cef2SBarry Smith 
2589d763cef2SBarry Smith    Input Parameter:
25900b039ecaSBarry Smith .  ctx - the monitor context
2591d763cef2SBarry Smith 
2592d763cef2SBarry Smith    Level: intermediate
2593d763cef2SBarry Smith 
2594d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2595d763cef2SBarry Smith 
25964f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2597d763cef2SBarry Smith @*/
2598a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2599d763cef2SBarry Smith {
2600b0a32e0cSBarry Smith   PetscDraw      draw;
2601dfbe8321SBarry Smith   PetscErrorCode ierr;
2602d763cef2SBarry Smith 
2603d763cef2SBarry Smith   PetscFunctionBegin;
26040b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
26056bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
26060b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
26070b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2608d763cef2SBarry Smith   PetscFunctionReturn(0);
2609d763cef2SBarry Smith }
2610d763cef2SBarry Smith 
26114a2ae208SSatish Balay #undef __FUNCT__
26124a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2613d763cef2SBarry Smith /*@
2614b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2615d763cef2SBarry Smith 
2616d763cef2SBarry Smith    Not Collective
2617d763cef2SBarry Smith 
2618d763cef2SBarry Smith    Input Parameter:
2619d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2620d763cef2SBarry Smith 
2621d763cef2SBarry Smith    Output Parameter:
2622d763cef2SBarry Smith .  t  - the current time
2623d763cef2SBarry Smith 
2624d763cef2SBarry Smith    Level: beginner
2625d763cef2SBarry Smith 
2626b8123daeSJed Brown    Note:
2627b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2628b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2629b8123daeSJed Brown 
2630d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2631d763cef2SBarry Smith 
2632d763cef2SBarry Smith .keywords: TS, get, time
2633d763cef2SBarry Smith @*/
26347087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2635d763cef2SBarry Smith {
2636d763cef2SBarry Smith   PetscFunctionBegin;
26370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2638f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2639d763cef2SBarry Smith   *t = ts->ptime;
2640d763cef2SBarry Smith   PetscFunctionReturn(0);
2641d763cef2SBarry Smith }
2642d763cef2SBarry Smith 
26434a2ae208SSatish Balay #undef __FUNCT__
26446a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
26456a4d4014SLisandro Dalcin /*@
26466a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
26476a4d4014SLisandro Dalcin 
26483f9fe445SBarry Smith    Logically Collective on TS
26496a4d4014SLisandro Dalcin 
26506a4d4014SLisandro Dalcin    Input Parameters:
26516a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
26526a4d4014SLisandro Dalcin -  time - the time
26536a4d4014SLisandro Dalcin 
26546a4d4014SLisandro Dalcin    Level: intermediate
26556a4d4014SLisandro Dalcin 
26566a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
26576a4d4014SLisandro Dalcin 
26586a4d4014SLisandro Dalcin .keywords: TS, set, time
26596a4d4014SLisandro Dalcin @*/
26607087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
26616a4d4014SLisandro Dalcin {
26626a4d4014SLisandro Dalcin   PetscFunctionBegin;
26630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2664c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
26656a4d4014SLisandro Dalcin   ts->ptime = t;
26666a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
26676a4d4014SLisandro Dalcin }
26686a4d4014SLisandro Dalcin 
26696a4d4014SLisandro Dalcin #undef __FUNCT__
26704a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2671d763cef2SBarry Smith /*@C
2672d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2673d763cef2SBarry Smith    TS options in the database.
2674d763cef2SBarry Smith 
26753f9fe445SBarry Smith    Logically Collective on TS
2676d763cef2SBarry Smith 
2677d763cef2SBarry Smith    Input Parameter:
2678d763cef2SBarry Smith +  ts     - The TS context
2679d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2680d763cef2SBarry Smith 
2681d763cef2SBarry Smith    Notes:
2682d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2683d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2684d763cef2SBarry Smith    hyphen.
2685d763cef2SBarry Smith 
2686d763cef2SBarry Smith    Level: advanced
2687d763cef2SBarry Smith 
2688d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2689d763cef2SBarry Smith 
2690d763cef2SBarry Smith .seealso: TSSetFromOptions()
2691d763cef2SBarry Smith 
2692d763cef2SBarry Smith @*/
26937087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2694d763cef2SBarry Smith {
2695dfbe8321SBarry Smith   PetscErrorCode ierr;
2696089b2837SJed Brown   SNES           snes;
2697d763cef2SBarry Smith 
2698d763cef2SBarry Smith   PetscFunctionBegin;
26990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2700d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2701089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2702089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2703d763cef2SBarry Smith   PetscFunctionReturn(0);
2704d763cef2SBarry Smith }
2705d763cef2SBarry Smith 
2706d763cef2SBarry Smith 
27074a2ae208SSatish Balay #undef __FUNCT__
27084a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2709d763cef2SBarry Smith /*@C
2710d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2711d763cef2SBarry Smith    TS options in the database.
2712d763cef2SBarry Smith 
27133f9fe445SBarry Smith    Logically Collective on TS
2714d763cef2SBarry Smith 
2715d763cef2SBarry Smith    Input Parameter:
2716d763cef2SBarry Smith +  ts     - The TS context
2717d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2718d763cef2SBarry Smith 
2719d763cef2SBarry Smith    Notes:
2720d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2721d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2722d763cef2SBarry Smith    hyphen.
2723d763cef2SBarry Smith 
2724d763cef2SBarry Smith    Level: advanced
2725d763cef2SBarry Smith 
2726d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2727d763cef2SBarry Smith 
2728d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2729d763cef2SBarry Smith 
2730d763cef2SBarry Smith @*/
27317087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2732d763cef2SBarry Smith {
2733dfbe8321SBarry Smith   PetscErrorCode ierr;
2734089b2837SJed Brown   SNES           snes;
2735d763cef2SBarry Smith 
2736d763cef2SBarry Smith   PetscFunctionBegin;
27370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2738d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2739089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2740089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2741d763cef2SBarry Smith   PetscFunctionReturn(0);
2742d763cef2SBarry Smith }
2743d763cef2SBarry Smith 
27444a2ae208SSatish Balay #undef __FUNCT__
27454a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2746d763cef2SBarry Smith /*@C
2747d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2748d763cef2SBarry Smith    TS options in the database.
2749d763cef2SBarry Smith 
2750d763cef2SBarry Smith    Not Collective
2751d763cef2SBarry Smith 
2752d763cef2SBarry Smith    Input Parameter:
2753d763cef2SBarry Smith .  ts - The TS context
2754d763cef2SBarry Smith 
2755d763cef2SBarry Smith    Output Parameter:
2756d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2757d763cef2SBarry Smith 
2758d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2759d763cef2SBarry Smith    sufficient length to hold the prefix.
2760d763cef2SBarry Smith 
2761d763cef2SBarry Smith    Level: intermediate
2762d763cef2SBarry Smith 
2763d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2764d763cef2SBarry Smith 
2765d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2766d763cef2SBarry Smith @*/
27677087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2768d763cef2SBarry Smith {
2769dfbe8321SBarry Smith   PetscErrorCode ierr;
2770d763cef2SBarry Smith 
2771d763cef2SBarry Smith   PetscFunctionBegin;
27720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27734482741eSBarry Smith   PetscValidPointer(prefix,2);
2774d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2775d763cef2SBarry Smith   PetscFunctionReturn(0);
2776d763cef2SBarry Smith }
2777d763cef2SBarry Smith 
27784a2ae208SSatish Balay #undef __FUNCT__
27794a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2780d763cef2SBarry Smith /*@C
2781d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2782d763cef2SBarry Smith 
2783d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2784d763cef2SBarry Smith 
2785d763cef2SBarry Smith    Input Parameter:
2786d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2787d763cef2SBarry Smith 
2788d763cef2SBarry Smith    Output Parameters:
2789b5abc632SBarry Smith +  J   - The Jacobian J of F, where U_t = G(U,t)
2790d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2791089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2792d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2793d763cef2SBarry Smith 
2794d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2795d763cef2SBarry Smith 
2796d763cef2SBarry Smith    Level: intermediate
2797d763cef2SBarry Smith 
279826d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2799d763cef2SBarry Smith 
2800d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2801d763cef2SBarry Smith @*/
2802089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2803d763cef2SBarry Smith {
2804089b2837SJed Brown   PetscErrorCode ierr;
2805089b2837SJed Brown   SNES           snes;
280624989b8cSPeter Brune   DM             dm;
2807089b2837SJed Brown 
2808d763cef2SBarry Smith   PetscFunctionBegin;
2809089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2810089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
281124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
281224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2813d763cef2SBarry Smith   PetscFunctionReturn(0);
2814d763cef2SBarry Smith }
2815d763cef2SBarry Smith 
28161713a123SBarry Smith #undef __FUNCT__
28172eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
28182eca1d9cSJed Brown /*@C
28192eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
28202eca1d9cSJed Brown 
28212eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
28222eca1d9cSJed Brown 
28232eca1d9cSJed Brown    Input Parameter:
28242eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
28252eca1d9cSJed Brown 
28262eca1d9cSJed Brown    Output Parameters:
28272eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
28282eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
28292eca1d9cSJed Brown .  f   - The function to compute the matrices
28302eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
28312eca1d9cSJed Brown 
28322eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
28332eca1d9cSJed Brown 
28342eca1d9cSJed Brown    Level: advanced
28352eca1d9cSJed Brown 
28362eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
28372eca1d9cSJed Brown 
28382eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
28392eca1d9cSJed Brown @*/
28407087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
28412eca1d9cSJed Brown {
2842089b2837SJed Brown   PetscErrorCode ierr;
2843089b2837SJed Brown   SNES           snes;
284424989b8cSPeter Brune   DM             dm;
28450910c330SBarry Smith 
28462eca1d9cSJed Brown   PetscFunctionBegin;
2847089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2848f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
2849089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
285024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
285124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
28522eca1d9cSJed Brown   PetscFunctionReturn(0);
28532eca1d9cSJed Brown }
28542eca1d9cSJed Brown 
285583a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
28566083293cSBarry Smith   PetscViewer viewer;
28576083293cSBarry Smith   Vec         initialsolution;
28586083293cSBarry Smith   PetscBool   showinitial;
285983a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2860473a3ab2SBarry Smith   PetscBool   showtimestepandtime;
286183a4ac43SBarry Smith };
28626083293cSBarry Smith 
28632eca1d9cSJed Brown #undef __FUNCT__
286483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
28651713a123SBarry Smith /*@C
286683a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
28671713a123SBarry Smith    VecView() for the solution at each timestep
28681713a123SBarry Smith 
28691713a123SBarry Smith    Collective on TS
28701713a123SBarry Smith 
28711713a123SBarry Smith    Input Parameters:
28721713a123SBarry Smith +  ts - the TS context
28731713a123SBarry Smith .  step - current time-step
2874142b95e3SSatish Balay .  ptime - current time
28751713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
28761713a123SBarry Smith 
287799fdda47SBarry Smith    Options Database:
287899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
287999fdda47SBarry Smith 
288099fdda47SBarry 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
288199fdda47SBarry Smith        will look bad
288299fdda47SBarry Smith 
28831713a123SBarry Smith    Level: intermediate
28841713a123SBarry Smith 
28851713a123SBarry Smith .keywords: TS,  vector, monitor, view
28861713a123SBarry Smith 
2887a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
28881713a123SBarry Smith @*/
28890910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
28901713a123SBarry Smith {
2891dfbe8321SBarry Smith   PetscErrorCode   ierr;
289283a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
2893473a3ab2SBarry Smith   PetscDraw        draw;
28941713a123SBarry Smith 
28951713a123SBarry Smith   PetscFunctionBegin;
28966083293cSBarry Smith   if (!step && ictx->showinitial) {
28976083293cSBarry Smith     if (!ictx->initialsolution) {
28980910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
28991713a123SBarry Smith     }
29000910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
29016083293cSBarry Smith   }
29023fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
29030dcf80beSBarry Smith 
29046083293cSBarry Smith   if (ictx->showinitial) {
29056083293cSBarry Smith     PetscReal pause;
29066083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
29076083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
29086083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
29096083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
29106083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
29116083293cSBarry Smith   }
29120910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
2913473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
2914473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
2915473a3ab2SBarry Smith     char      time[32];
2916473a3ab2SBarry Smith     size_t    len;
2917473a3ab2SBarry Smith 
2918473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
2919473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
2920473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
2921473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
2922473a3ab2SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,PETSC_NULL);CHKERRQ(ierr);
2923473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
2924473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
2925473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
2926473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
2927473a3ab2SBarry Smith   }
2928473a3ab2SBarry Smith 
29296083293cSBarry Smith   if (ictx->showinitial) {
29306083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
29316083293cSBarry Smith   }
29321713a123SBarry Smith   PetscFunctionReturn(0);
29331713a123SBarry Smith }
29341713a123SBarry Smith 
29351713a123SBarry Smith 
29366c699258SBarry Smith #undef __FUNCT__
293783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
29386083293cSBarry Smith /*@C
293983a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
29406083293cSBarry Smith 
29416083293cSBarry Smith    Collective on TS
29426083293cSBarry Smith 
29436083293cSBarry Smith    Input Parameters:
29446083293cSBarry Smith .    ctx - the monitor context
29456083293cSBarry Smith 
29466083293cSBarry Smith    Level: intermediate
29476083293cSBarry Smith 
29486083293cSBarry Smith .keywords: TS,  vector, monitor, view
29496083293cSBarry Smith 
295083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
29516083293cSBarry Smith @*/
295283a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
29536083293cSBarry Smith {
29546083293cSBarry Smith   PetscErrorCode ierr;
29556083293cSBarry Smith 
29566083293cSBarry Smith   PetscFunctionBegin;
295783a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
295883a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
295983a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
29606083293cSBarry Smith   PetscFunctionReturn(0);
29616083293cSBarry Smith }
29626083293cSBarry Smith 
29636083293cSBarry Smith #undef __FUNCT__
296483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
29656083293cSBarry Smith /*@C
296683a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
29676083293cSBarry Smith 
29686083293cSBarry Smith    Collective on TS
29696083293cSBarry Smith 
29706083293cSBarry Smith    Input Parameter:
29716083293cSBarry Smith .    ts - time-step context
29726083293cSBarry Smith 
29736083293cSBarry Smith    Output Patameter:
29746083293cSBarry Smith .    ctx - the monitor context
29756083293cSBarry Smith 
297699fdda47SBarry Smith    Options Database:
297799fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
297899fdda47SBarry Smith 
29796083293cSBarry Smith    Level: intermediate
29806083293cSBarry Smith 
29816083293cSBarry Smith .keywords: TS,  vector, monitor, view
29826083293cSBarry Smith 
298383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
29846083293cSBarry Smith @*/
298583a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
29866083293cSBarry Smith {
29876083293cSBarry Smith   PetscErrorCode   ierr;
29886083293cSBarry Smith 
29896083293cSBarry Smith   PetscFunctionBegin;
299083a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
299183a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
2992e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
2993bbd56ea5SKarl Rupp 
299483a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
2995473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
299699fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr);
2997473a3ab2SBarry Smith 
2998473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
2999473a3ab2SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,PETSC_NULL);CHKERRQ(ierr);
30006083293cSBarry Smith   PetscFunctionReturn(0);
30016083293cSBarry Smith }
30026083293cSBarry Smith 
30036083293cSBarry Smith #undef __FUNCT__
300483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
30053a471f94SBarry Smith /*@C
300683a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
30073a471f94SBarry Smith    VecView() for the error at each timestep
30083a471f94SBarry Smith 
30093a471f94SBarry Smith    Collective on TS
30103a471f94SBarry Smith 
30113a471f94SBarry Smith    Input Parameters:
30123a471f94SBarry Smith +  ts - the TS context
30133a471f94SBarry Smith .  step - current time-step
30143a471f94SBarry Smith .  ptime - current time
30153a471f94SBarry Smith -  dummy - either a viewer or PETSC_NULL
30163a471f94SBarry Smith 
30173a471f94SBarry Smith    Level: intermediate
30183a471f94SBarry Smith 
30193a471f94SBarry Smith .keywords: TS,  vector, monitor, view
30203a471f94SBarry Smith 
30213a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
30223a471f94SBarry Smith @*/
30230910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
30243a471f94SBarry Smith {
30253a471f94SBarry Smith   PetscErrorCode   ierr;
302683a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
302783a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
30283a471f94SBarry Smith   Vec              work;
30293a471f94SBarry Smith 
30303a471f94SBarry Smith   PetscFunctionBegin;
30313fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
30320910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
30333a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
30340910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
30353a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
30363a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
30373a471f94SBarry Smith   PetscFunctionReturn(0);
30383a471f94SBarry Smith }
30393a471f94SBarry Smith 
30402a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
30413a471f94SBarry Smith #undef __FUNCT__
30426c699258SBarry Smith #define __FUNCT__ "TSSetDM"
30436c699258SBarry Smith /*@
30446c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
30456c699258SBarry Smith 
30463f9fe445SBarry Smith    Logically Collective on TS and DM
30476c699258SBarry Smith 
30486c699258SBarry Smith    Input Parameters:
30496c699258SBarry Smith +  ts - the preconditioner context
30506c699258SBarry Smith -  dm - the dm
30516c699258SBarry Smith 
30526c699258SBarry Smith    Level: intermediate
30536c699258SBarry Smith 
30546c699258SBarry Smith 
30556c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
30566c699258SBarry Smith @*/
30577087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
30586c699258SBarry Smith {
30596c699258SBarry Smith   PetscErrorCode ierr;
3060089b2837SJed Brown   SNES           snes;
3061942e3340SBarry Smith   DMTS           tsdm;
30626c699258SBarry Smith 
30636c699258SBarry Smith   PetscFunctionBegin;
30640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
306570663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3066942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
30672a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3068942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3069942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
307024989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
307124989b8cSPeter Brune         tsdm->originaldm = dm;
307224989b8cSPeter Brune       }
307324989b8cSPeter Brune     }
30746bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
307524989b8cSPeter Brune   }
30766c699258SBarry Smith   ts->dm = dm;
3077bbd56ea5SKarl Rupp 
3078089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3079089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
30806c699258SBarry Smith   PetscFunctionReturn(0);
30816c699258SBarry Smith }
30826c699258SBarry Smith 
30836c699258SBarry Smith #undef __FUNCT__
30846c699258SBarry Smith #define __FUNCT__ "TSGetDM"
30856c699258SBarry Smith /*@
30866c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
30876c699258SBarry Smith 
30883f9fe445SBarry Smith    Not Collective
30896c699258SBarry Smith 
30906c699258SBarry Smith    Input Parameter:
30916c699258SBarry Smith . ts - the preconditioner context
30926c699258SBarry Smith 
30936c699258SBarry Smith    Output Parameter:
30946c699258SBarry Smith .  dm - the dm
30956c699258SBarry Smith 
30966c699258SBarry Smith    Level: intermediate
30976c699258SBarry Smith 
30986c699258SBarry Smith 
30996c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
31006c699258SBarry Smith @*/
31017087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
31026c699258SBarry Smith {
3103496e6a7aSJed Brown   PetscErrorCode ierr;
3104496e6a7aSJed Brown 
31056c699258SBarry Smith   PetscFunctionBegin;
31060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3107496e6a7aSJed Brown   if (!ts->dm) {
3108496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
3109496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3110496e6a7aSJed Brown   }
31116c699258SBarry Smith   *dm = ts->dm;
31126c699258SBarry Smith   PetscFunctionReturn(0);
31136c699258SBarry Smith }
31141713a123SBarry Smith 
31150f5c6efeSJed Brown #undef __FUNCT__
31160f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
31170f5c6efeSJed Brown /*@
31180f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
31190f5c6efeSJed Brown 
31203f9fe445SBarry Smith    Logically Collective on SNES
31210f5c6efeSJed Brown 
31220f5c6efeSJed Brown    Input Parameter:
3123d42a1c89SJed Brown + snes - nonlinear solver
31240910c330SBarry Smith . U - the current state at which to evaluate the residual
3125d42a1c89SJed Brown - ctx - user context, must be a TS
31260f5c6efeSJed Brown 
31270f5c6efeSJed Brown    Output Parameter:
31280f5c6efeSJed Brown . F - the nonlinear residual
31290f5c6efeSJed Brown 
31300f5c6efeSJed Brown    Notes:
31310f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
31320f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
31330f5c6efeSJed Brown 
31340f5c6efeSJed Brown    Level: advanced
31350f5c6efeSJed Brown 
31360f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
31370f5c6efeSJed Brown @*/
31380910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
31390f5c6efeSJed Brown {
31400f5c6efeSJed Brown   TS             ts = (TS)ctx;
31410f5c6efeSJed Brown   PetscErrorCode ierr;
31420f5c6efeSJed Brown 
31430f5c6efeSJed Brown   PetscFunctionBegin;
31440f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31450910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
31460f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
31470f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
31480910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
31490f5c6efeSJed Brown   PetscFunctionReturn(0);
31500f5c6efeSJed Brown }
31510f5c6efeSJed Brown 
31520f5c6efeSJed Brown #undef __FUNCT__
31530f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
31540f5c6efeSJed Brown /*@
31550f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
31560f5c6efeSJed Brown 
31570f5c6efeSJed Brown    Collective on SNES
31580f5c6efeSJed Brown 
31590f5c6efeSJed Brown    Input Parameter:
31600f5c6efeSJed Brown + snes - nonlinear solver
31610910c330SBarry Smith . U - the current state at which to evaluate the residual
31620f5c6efeSJed Brown - ctx - user context, must be a TS
31630f5c6efeSJed Brown 
31640f5c6efeSJed Brown    Output Parameter:
31650f5c6efeSJed Brown + A - the Jacobian
31660f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
31670f5c6efeSJed Brown - flag - indicates any structure change in the matrix
31680f5c6efeSJed Brown 
31690f5c6efeSJed Brown    Notes:
31700f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
31710f5c6efeSJed Brown 
31720f5c6efeSJed Brown    Level: developer
31730f5c6efeSJed Brown 
31740f5c6efeSJed Brown .seealso: SNESSetJacobian()
31750f5c6efeSJed Brown @*/
31760910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
31770f5c6efeSJed Brown {
31780f5c6efeSJed Brown   TS             ts = (TS)ctx;
31790f5c6efeSJed Brown   PetscErrorCode ierr;
31800f5c6efeSJed Brown 
31810f5c6efeSJed Brown   PetscFunctionBegin;
31820f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31830910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
31840f5c6efeSJed Brown   PetscValidPointer(A,3);
31850f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
31860f5c6efeSJed Brown   PetscValidPointer(B,4);
31870f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
31880f5c6efeSJed Brown   PetscValidPointer(flag,5);
31890f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
31900910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
31910f5c6efeSJed Brown   PetscFunctionReturn(0);
31920f5c6efeSJed Brown }
3193325fc9f4SBarry Smith 
31940e4ef248SJed Brown #undef __FUNCT__
31950e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
31960e4ef248SJed Brown /*@C
31970e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
31980e4ef248SJed Brown 
31990e4ef248SJed Brown    Collective on TS
32000e4ef248SJed Brown 
32010e4ef248SJed Brown    Input Arguments:
32020e4ef248SJed Brown +  ts - time stepping context
32030e4ef248SJed Brown .  t - time at which to evaluate
32040910c330SBarry Smith .  U - state at which to evaluate
32050e4ef248SJed Brown -  ctx - context
32060e4ef248SJed Brown 
32070e4ef248SJed Brown    Output Arguments:
32080e4ef248SJed Brown .  F - right hand side
32090e4ef248SJed Brown 
32100e4ef248SJed Brown    Level: intermediate
32110e4ef248SJed Brown 
32120e4ef248SJed Brown    Notes:
32130e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
32140e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
32150e4ef248SJed Brown 
32160e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
32170e4ef248SJed Brown @*/
32180910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
32190e4ef248SJed Brown {
32200e4ef248SJed Brown   PetscErrorCode ierr;
32210e4ef248SJed Brown   Mat            Arhs,Brhs;
32220e4ef248SJed Brown   MatStructure   flg2;
32230e4ef248SJed Brown 
32240e4ef248SJed Brown   PetscFunctionBegin;
32250e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
32260910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
32270910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
32280e4ef248SJed Brown   PetscFunctionReturn(0);
32290e4ef248SJed Brown }
32300e4ef248SJed Brown 
32310e4ef248SJed Brown #undef __FUNCT__
32320e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
32330e4ef248SJed Brown /*@C
32340e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
32350e4ef248SJed Brown 
32360e4ef248SJed Brown    Collective on TS
32370e4ef248SJed Brown 
32380e4ef248SJed Brown    Input Arguments:
32390e4ef248SJed Brown +  ts - time stepping context
32400e4ef248SJed Brown .  t - time at which to evaluate
32410910c330SBarry Smith .  U - state at which to evaluate
32420e4ef248SJed Brown -  ctx - context
32430e4ef248SJed Brown 
32440e4ef248SJed Brown    Output Arguments:
32450e4ef248SJed Brown +  A - pointer to operator
32460e4ef248SJed Brown .  B - pointer to preconditioning matrix
32470e4ef248SJed Brown -  flg - matrix structure flag
32480e4ef248SJed Brown 
32490e4ef248SJed Brown    Level: intermediate
32500e4ef248SJed Brown 
32510e4ef248SJed Brown    Notes:
32520e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
32530e4ef248SJed Brown 
32540e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
32550e4ef248SJed Brown @*/
32560910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
32570e4ef248SJed Brown {
32580e4ef248SJed Brown   PetscFunctionBegin;
32590e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
32600e4ef248SJed Brown   PetscFunctionReturn(0);
32610e4ef248SJed Brown }
32620e4ef248SJed Brown 
32630026cea9SSean Farley #undef __FUNCT__
32640026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
32650026cea9SSean Farley /*@C
32660026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
32670026cea9SSean Farley 
32680026cea9SSean Farley    Collective on TS
32690026cea9SSean Farley 
32700026cea9SSean Farley    Input Arguments:
32710026cea9SSean Farley +  ts - time stepping context
32720026cea9SSean Farley .  t - time at which to evaluate
32730910c330SBarry Smith .  U - state at which to evaluate
32740910c330SBarry Smith .  Udot - time derivative of state vector
32750026cea9SSean Farley -  ctx - context
32760026cea9SSean Farley 
32770026cea9SSean Farley    Output Arguments:
32780026cea9SSean Farley .  F - left hand side
32790026cea9SSean Farley 
32800026cea9SSean Farley    Level: intermediate
32810026cea9SSean Farley 
32820026cea9SSean Farley    Notes:
32830910c330SBarry 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
32840026cea9SSean Farley    user is required to write their own TSComputeIFunction.
32850026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
32860026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
32870026cea9SSean Farley 
32880026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
32890026cea9SSean Farley @*/
32900910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
32910026cea9SSean Farley {
32920026cea9SSean Farley   PetscErrorCode ierr;
32930026cea9SSean Farley   Mat            A,B;
32940026cea9SSean Farley   MatStructure   flg2;
32950026cea9SSean Farley 
32960026cea9SSean Farley   PetscFunctionBegin;
32970026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
32980910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
32990910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
33000026cea9SSean Farley   PetscFunctionReturn(0);
33010026cea9SSean Farley }
33020026cea9SSean Farley 
33030026cea9SSean Farley #undef __FUNCT__
33040026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
33050026cea9SSean Farley /*@C
330624e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
33070026cea9SSean Farley 
33080026cea9SSean Farley    Collective on TS
33090026cea9SSean Farley 
33100026cea9SSean Farley    Input Arguments:
33110026cea9SSean Farley +  ts - time stepping context
33120026cea9SSean Farley .  t - time at which to evaluate
33130910c330SBarry Smith .  U - state at which to evaluate
33140910c330SBarry Smith .  Udot - time derivative of state vector
33150026cea9SSean Farley .  shift - shift to apply
33160026cea9SSean Farley -  ctx - context
33170026cea9SSean Farley 
33180026cea9SSean Farley    Output Arguments:
33190026cea9SSean Farley +  A - pointer to operator
33200026cea9SSean Farley .  B - pointer to preconditioning matrix
33210026cea9SSean Farley -  flg - matrix structure flag
33220026cea9SSean Farley 
33230026cea9SSean Farley    Level: intermediate
33240026cea9SSean Farley 
33250026cea9SSean Farley    Notes:
33260026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
33270026cea9SSean Farley 
33280026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
33290026cea9SSean Farley @*/
33300910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
33310026cea9SSean Farley {
33320026cea9SSean Farley   PetscFunctionBegin;
33330026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
33340026cea9SSean Farley   PetscFunctionReturn(0);
33350026cea9SSean Farley }
3336e817cc15SEmil Constantinescu #undef __FUNCT__
3337e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3338e817cc15SEmil Constantinescu /*@
3339e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3340e817cc15SEmil Constantinescu 
3341e817cc15SEmil Constantinescu    Not Collective
3342e817cc15SEmil Constantinescu 
3343e817cc15SEmil Constantinescu    Input Parameter:
3344e817cc15SEmil Constantinescu .  ts - the TS context
3345e817cc15SEmil Constantinescu 
3346e817cc15SEmil Constantinescu    Output Parameter:
3347e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3348e817cc15SEmil Constantinescu 
3349e817cc15SEmil Constantinescu    Level: beginner
3350e817cc15SEmil Constantinescu 
3351e817cc15SEmil Constantinescu .keywords: TS, equation type
3352e817cc15SEmil Constantinescu 
3353e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3354e817cc15SEmil Constantinescu @*/
3355e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3356e817cc15SEmil Constantinescu {
3357e817cc15SEmil Constantinescu   PetscFunctionBegin;
3358e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3359e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3360e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3361e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3362e817cc15SEmil Constantinescu }
3363e817cc15SEmil Constantinescu 
3364e817cc15SEmil Constantinescu #undef __FUNCT__
3365e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3366e817cc15SEmil Constantinescu /*@
3367e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3368e817cc15SEmil Constantinescu 
3369e817cc15SEmil Constantinescu    Not Collective
3370e817cc15SEmil Constantinescu 
3371e817cc15SEmil Constantinescu    Input Parameter:
3372e817cc15SEmil Constantinescu +  ts - the TS context
3373e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3374e817cc15SEmil Constantinescu 
3375e817cc15SEmil Constantinescu    Level: advanced
3376e817cc15SEmil Constantinescu 
3377e817cc15SEmil Constantinescu .keywords: TS, equation type
3378e817cc15SEmil Constantinescu 
3379e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3380e817cc15SEmil Constantinescu @*/
3381e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3382e817cc15SEmil Constantinescu {
3383e817cc15SEmil Constantinescu   PetscFunctionBegin;
3384e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3385e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3386e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3387e817cc15SEmil Constantinescu }
33880026cea9SSean Farley 
33894af1b03aSJed Brown #undef __FUNCT__
33904af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
33914af1b03aSJed Brown /*@
33924af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
33934af1b03aSJed Brown 
33944af1b03aSJed Brown    Not Collective
33954af1b03aSJed Brown 
33964af1b03aSJed Brown    Input Parameter:
33974af1b03aSJed Brown .  ts - the TS context
33984af1b03aSJed Brown 
33994af1b03aSJed Brown    Output Parameter:
34004af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
34014af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
34024af1b03aSJed Brown 
3403487e0bb9SJed Brown    Level: beginner
34044af1b03aSJed Brown 
3405cd652676SJed Brown    Notes:
3406cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
34074af1b03aSJed Brown 
34084af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
34094af1b03aSJed Brown 
34104af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
34114af1b03aSJed Brown @*/
34124af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
34134af1b03aSJed Brown {
34144af1b03aSJed Brown   PetscFunctionBegin;
34154af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34164af1b03aSJed Brown   PetscValidPointer(reason,2);
34174af1b03aSJed Brown   *reason = ts->reason;
34184af1b03aSJed Brown   PetscFunctionReturn(0);
34194af1b03aSJed Brown }
34204af1b03aSJed Brown 
3421fb1732b5SBarry Smith #undef __FUNCT__
3422d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3423d6ad946cSShri Abhyankar /*@
3424d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3425d6ad946cSShri Abhyankar 
3426d6ad946cSShri Abhyankar    Not Collective
3427d6ad946cSShri Abhyankar 
3428d6ad946cSShri Abhyankar    Input Parameter:
3429d6ad946cSShri Abhyankar +  ts - the TS context
3430d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3431d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3432d6ad946cSShri Abhyankar 
3433f5abba47SShri Abhyankar    Level: advanced
3434d6ad946cSShri Abhyankar 
3435d6ad946cSShri Abhyankar    Notes:
3436d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3437d6ad946cSShri Abhyankar 
3438d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3439d6ad946cSShri Abhyankar 
3440d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3441d6ad946cSShri Abhyankar @*/
3442d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3443d6ad946cSShri Abhyankar {
3444d6ad946cSShri Abhyankar   PetscFunctionBegin;
3445d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3446d6ad946cSShri Abhyankar   ts->reason = reason;
3447d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3448d6ad946cSShri Abhyankar }
3449d6ad946cSShri Abhyankar 
3450d6ad946cSShri Abhyankar #undef __FUNCT__
3451cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3452cc708dedSBarry Smith /*@
3453cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3454cc708dedSBarry Smith 
3455cc708dedSBarry Smith    Not Collective
3456cc708dedSBarry Smith 
3457cc708dedSBarry Smith    Input Parameter:
3458cc708dedSBarry Smith .  ts - the TS context
3459cc708dedSBarry Smith 
3460cc708dedSBarry Smith    Output Parameter:
3461cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3462cc708dedSBarry Smith 
3463487e0bb9SJed Brown    Level: beginner
3464cc708dedSBarry Smith 
3465cc708dedSBarry Smith    Notes:
3466cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3467cc708dedSBarry Smith 
3468cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3469cc708dedSBarry Smith 
3470cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3471cc708dedSBarry Smith @*/
3472cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3473cc708dedSBarry Smith {
3474cc708dedSBarry Smith   PetscFunctionBegin;
3475cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3476cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3477cc708dedSBarry Smith   *ftime = ts->solvetime;
3478cc708dedSBarry Smith   PetscFunctionReturn(0);
3479cc708dedSBarry Smith }
3480cc708dedSBarry Smith 
3481cc708dedSBarry Smith #undef __FUNCT__
34825ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
34839f67acb7SJed Brown /*@
34845ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
34859f67acb7SJed Brown    used by the time integrator.
34869f67acb7SJed Brown 
34879f67acb7SJed Brown    Not Collective
34889f67acb7SJed Brown 
34899f67acb7SJed Brown    Input Parameter:
34909f67acb7SJed Brown .  ts - TS context
34919f67acb7SJed Brown 
34929f67acb7SJed Brown    Output Parameter:
34939f67acb7SJed Brown .  nits - number of nonlinear iterations
34949f67acb7SJed Brown 
34959f67acb7SJed Brown    Notes:
34969f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
34979f67acb7SJed Brown 
34989f67acb7SJed Brown    Level: intermediate
34999f67acb7SJed Brown 
35009f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
35019f67acb7SJed Brown 
35025ef26d82SJed Brown .seealso:  TSGetKSPIterations()
35039f67acb7SJed Brown @*/
35045ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
35059f67acb7SJed Brown {
35069f67acb7SJed Brown   PetscFunctionBegin;
35079f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35089f67acb7SJed Brown   PetscValidIntPointer(nits,2);
35095ef26d82SJed Brown   *nits = ts->snes_its;
35109f67acb7SJed Brown   PetscFunctionReturn(0);
35119f67acb7SJed Brown }
35129f67acb7SJed Brown 
35139f67acb7SJed Brown #undef __FUNCT__
35145ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
35159f67acb7SJed Brown /*@
35165ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
35179f67acb7SJed Brown    used by the time integrator.
35189f67acb7SJed Brown 
35199f67acb7SJed Brown    Not Collective
35209f67acb7SJed Brown 
35219f67acb7SJed Brown    Input Parameter:
35229f67acb7SJed Brown .  ts - TS context
35239f67acb7SJed Brown 
35249f67acb7SJed Brown    Output Parameter:
35259f67acb7SJed Brown .  lits - number of linear iterations
35269f67acb7SJed Brown 
35279f67acb7SJed Brown    Notes:
35289f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
35299f67acb7SJed Brown 
35309f67acb7SJed Brown    Level: intermediate
35319f67acb7SJed Brown 
35329f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
35339f67acb7SJed Brown 
35345ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
35359f67acb7SJed Brown @*/
35365ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
35379f67acb7SJed Brown {
35389f67acb7SJed Brown   PetscFunctionBegin;
35399f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35409f67acb7SJed Brown   PetscValidIntPointer(lits,2);
35415ef26d82SJed Brown   *lits = ts->ksp_its;
35429f67acb7SJed Brown   PetscFunctionReturn(0);
35439f67acb7SJed Brown }
35449f67acb7SJed Brown 
35459f67acb7SJed Brown #undef __FUNCT__
3546cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3547cef5090cSJed Brown /*@
3548cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3549cef5090cSJed Brown 
3550cef5090cSJed Brown    Not Collective
3551cef5090cSJed Brown 
3552cef5090cSJed Brown    Input Parameter:
3553cef5090cSJed Brown .  ts - TS context
3554cef5090cSJed Brown 
3555cef5090cSJed Brown    Output Parameter:
3556cef5090cSJed Brown .  rejects - number of steps rejected
3557cef5090cSJed Brown 
3558cef5090cSJed Brown    Notes:
3559cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3560cef5090cSJed Brown 
3561cef5090cSJed Brown    Level: intermediate
3562cef5090cSJed Brown 
3563cef5090cSJed Brown .keywords: TS, get, number
3564cef5090cSJed Brown 
35655ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3566cef5090cSJed Brown @*/
3567cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3568cef5090cSJed Brown {
3569cef5090cSJed Brown   PetscFunctionBegin;
3570cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3571cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3572cef5090cSJed Brown   *rejects = ts->reject;
3573cef5090cSJed Brown   PetscFunctionReturn(0);
3574cef5090cSJed Brown }
3575cef5090cSJed Brown 
3576cef5090cSJed Brown #undef __FUNCT__
3577cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3578cef5090cSJed Brown /*@
3579cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3580cef5090cSJed Brown 
3581cef5090cSJed Brown    Not Collective
3582cef5090cSJed Brown 
3583cef5090cSJed Brown    Input Parameter:
3584cef5090cSJed Brown .  ts - TS context
3585cef5090cSJed Brown 
3586cef5090cSJed Brown    Output Parameter:
3587cef5090cSJed Brown .  fails - number of failed nonlinear solves
3588cef5090cSJed Brown 
3589cef5090cSJed Brown    Notes:
3590cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3591cef5090cSJed Brown 
3592cef5090cSJed Brown    Level: intermediate
3593cef5090cSJed Brown 
3594cef5090cSJed Brown .keywords: TS, get, number
3595cef5090cSJed Brown 
35965ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3597cef5090cSJed Brown @*/
3598cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3599cef5090cSJed Brown {
3600cef5090cSJed Brown   PetscFunctionBegin;
3601cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3602cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3603cef5090cSJed Brown   *fails = ts->num_snes_failures;
3604cef5090cSJed Brown   PetscFunctionReturn(0);
3605cef5090cSJed Brown }
3606cef5090cSJed Brown 
3607cef5090cSJed Brown #undef __FUNCT__
3608cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3609cef5090cSJed Brown /*@
3610cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3611cef5090cSJed Brown 
3612cef5090cSJed Brown    Not Collective
3613cef5090cSJed Brown 
3614cef5090cSJed Brown    Input Parameter:
3615cef5090cSJed Brown +  ts - TS context
3616cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3617cef5090cSJed Brown 
3618cef5090cSJed Brown    Notes:
3619cef5090cSJed Brown    The counter is reset to zero for each step
3620cef5090cSJed Brown 
3621cef5090cSJed Brown    Options Database Key:
3622cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3623cef5090cSJed Brown 
3624cef5090cSJed Brown    Level: intermediate
3625cef5090cSJed Brown 
3626cef5090cSJed Brown .keywords: TS, set, maximum, number
3627cef5090cSJed Brown 
36285ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3629cef5090cSJed Brown @*/
3630cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3631cef5090cSJed Brown {
3632cef5090cSJed Brown   PetscFunctionBegin;
3633cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3634cef5090cSJed Brown   ts->max_reject = rejects;
3635cef5090cSJed Brown   PetscFunctionReturn(0);
3636cef5090cSJed Brown }
3637cef5090cSJed Brown 
3638cef5090cSJed Brown #undef __FUNCT__
3639cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3640cef5090cSJed Brown /*@
3641cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3642cef5090cSJed Brown 
3643cef5090cSJed Brown    Not Collective
3644cef5090cSJed Brown 
3645cef5090cSJed Brown    Input Parameter:
3646cef5090cSJed Brown +  ts - TS context
3647cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3648cef5090cSJed Brown 
3649cef5090cSJed Brown    Notes:
3650cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3651cef5090cSJed Brown 
3652cef5090cSJed Brown    Options Database Key:
3653cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3654cef5090cSJed Brown 
3655cef5090cSJed Brown    Level: intermediate
3656cef5090cSJed Brown 
3657cef5090cSJed Brown .keywords: TS, set, maximum, number
3658cef5090cSJed Brown 
36595ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3660cef5090cSJed Brown @*/
3661cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3662cef5090cSJed Brown {
3663cef5090cSJed Brown   PetscFunctionBegin;
3664cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3665cef5090cSJed Brown   ts->max_snes_failures = fails;
3666cef5090cSJed Brown   PetscFunctionReturn(0);
3667cef5090cSJed Brown }
3668cef5090cSJed Brown 
3669cef5090cSJed Brown #undef __FUNCT__
3670cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3671cef5090cSJed Brown /*@
3672cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3673cef5090cSJed Brown 
3674cef5090cSJed Brown    Not Collective
3675cef5090cSJed Brown 
3676cef5090cSJed Brown    Input Parameter:
3677cef5090cSJed Brown +  ts - TS context
3678cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3679cef5090cSJed Brown 
3680cef5090cSJed Brown    Options Database Key:
3681cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3682cef5090cSJed Brown 
3683cef5090cSJed Brown    Level: intermediate
3684cef5090cSJed Brown 
3685cef5090cSJed Brown .keywords: TS, set, error
3686cef5090cSJed Brown 
36875ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3688cef5090cSJed Brown @*/
3689cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3690cef5090cSJed Brown {
3691cef5090cSJed Brown   PetscFunctionBegin;
3692cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3693cef5090cSJed Brown   ts->errorifstepfailed = err;
3694cef5090cSJed Brown   PetscFunctionReturn(0);
3695cef5090cSJed Brown }
3696cef5090cSJed Brown 
3697cef5090cSJed Brown #undef __FUNCT__
3698fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3699fb1732b5SBarry Smith /*@C
3700fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3701fb1732b5SBarry Smith 
3702fb1732b5SBarry Smith    Collective on TS
3703fb1732b5SBarry Smith 
3704fb1732b5SBarry Smith    Input Parameters:
3705fb1732b5SBarry Smith +  ts - the TS context
3706fb1732b5SBarry Smith .  step - current time-step
3707fb1732b5SBarry Smith .  ptime - current time
37080910c330SBarry Smith .  u - current state
3709fb1732b5SBarry Smith -  viewer - binary viewer
3710fb1732b5SBarry Smith 
3711fb1732b5SBarry Smith    Level: intermediate
3712fb1732b5SBarry Smith 
3713fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3714fb1732b5SBarry Smith 
3715fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3716fb1732b5SBarry Smith @*/
37170910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3718fb1732b5SBarry Smith {
3719fb1732b5SBarry Smith   PetscErrorCode ierr;
3720ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3721fb1732b5SBarry Smith 
3722fb1732b5SBarry Smith   PetscFunctionBegin;
37230910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3724ed81e22dSJed Brown   PetscFunctionReturn(0);
3725ed81e22dSJed Brown }
3726ed81e22dSJed Brown 
3727ed81e22dSJed Brown #undef __FUNCT__
3728ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3729ed81e22dSJed Brown /*@C
3730ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3731ed81e22dSJed Brown 
3732ed81e22dSJed Brown    Collective on TS
3733ed81e22dSJed Brown 
3734ed81e22dSJed Brown    Input Parameters:
3735ed81e22dSJed Brown +  ts - the TS context
3736ed81e22dSJed Brown .  step - current time-step
3737ed81e22dSJed Brown .  ptime - current time
37380910c330SBarry Smith .  u - current state
3739ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3740ed81e22dSJed Brown 
3741ed81e22dSJed Brown    Level: intermediate
3742ed81e22dSJed Brown 
3743ed81e22dSJed Brown    Notes:
3744ed81e22dSJed 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.
3745ed81e22dSJed Brown    These are named according to the file name template.
3746ed81e22dSJed Brown 
3747ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3748ed81e22dSJed Brown 
3749ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3750ed81e22dSJed Brown 
3751ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3752ed81e22dSJed Brown @*/
37530910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3754ed81e22dSJed Brown {
3755ed81e22dSJed Brown   PetscErrorCode ierr;
3756ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3757ed81e22dSJed Brown   PetscViewer    viewer;
3758ed81e22dSJed Brown 
3759ed81e22dSJed Brown   PetscFunctionBegin;
37608caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3761ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
37620910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3763ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3764ed81e22dSJed Brown   PetscFunctionReturn(0);
3765ed81e22dSJed Brown }
3766ed81e22dSJed Brown 
3767ed81e22dSJed Brown #undef __FUNCT__
3768ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3769ed81e22dSJed Brown /*@C
3770ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3771ed81e22dSJed Brown 
3772ed81e22dSJed Brown    Collective on TS
3773ed81e22dSJed Brown 
3774ed81e22dSJed Brown    Input Parameters:
3775ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3776ed81e22dSJed Brown 
3777ed81e22dSJed Brown    Level: intermediate
3778ed81e22dSJed Brown 
3779ed81e22dSJed Brown    Note:
3780ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3781ed81e22dSJed Brown 
3782ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3783ed81e22dSJed Brown 
3784ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3785ed81e22dSJed Brown @*/
3786ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3787ed81e22dSJed Brown {
3788ed81e22dSJed Brown   PetscErrorCode ierr;
3789ed81e22dSJed Brown 
3790ed81e22dSJed Brown   PetscFunctionBegin;
3791ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3792fb1732b5SBarry Smith   PetscFunctionReturn(0);
3793fb1732b5SBarry Smith }
3794fb1732b5SBarry Smith 
379584df9cb4SJed Brown #undef __FUNCT__
3796ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt"
379784df9cb4SJed Brown /*@
3798ad6bc421SBarry Smith    TSGetTSAdapt - Get the adaptive controller context for the current method
379984df9cb4SJed Brown 
3800ed81e22dSJed Brown    Collective on TS if controller has not been created yet
380184df9cb4SJed Brown 
380284df9cb4SJed Brown    Input Arguments:
3803ed81e22dSJed Brown .  ts - time stepping context
380484df9cb4SJed Brown 
380584df9cb4SJed Brown    Output Arguments:
3806ed81e22dSJed Brown .  adapt - adaptive controller
380784df9cb4SJed Brown 
380884df9cb4SJed Brown    Level: intermediate
380984df9cb4SJed Brown 
3810ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
381184df9cb4SJed Brown @*/
3812ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt)
381384df9cb4SJed Brown {
381484df9cb4SJed Brown   PetscErrorCode ierr;
381584df9cb4SJed Brown 
381684df9cb4SJed Brown   PetscFunctionBegin;
381784df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
381884df9cb4SJed Brown   PetscValidPointer(adapt,2);
381984df9cb4SJed Brown   if (!ts->adapt) {
382084df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
38211c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
38221c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
382384df9cb4SJed Brown   }
382484df9cb4SJed Brown   *adapt = ts->adapt;
382584df9cb4SJed Brown   PetscFunctionReturn(0);
382684df9cb4SJed Brown }
3827d6ebe24aSShri Abhyankar 
3828d6ebe24aSShri Abhyankar #undef __FUNCT__
38291c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
38301c3436cfSJed Brown /*@
38311c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
38321c3436cfSJed Brown 
38331c3436cfSJed Brown    Logically Collective
38341c3436cfSJed Brown 
38351c3436cfSJed Brown    Input Arguments:
38361c3436cfSJed Brown +  ts - time integration context
38371c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
38381c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
38391c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
38401c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
38411c3436cfSJed Brown 
38421c3436cfSJed Brown    Level: beginner
38431c3436cfSJed Brown 
3844c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
38451c3436cfSJed Brown @*/
38461c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
38471c3436cfSJed Brown {
38481c3436cfSJed Brown   PetscErrorCode ierr;
38491c3436cfSJed Brown 
38501c3436cfSJed Brown   PetscFunctionBegin;
3851c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
38521c3436cfSJed Brown   if (vatol) {
38531c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
38541c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
3855bbd56ea5SKarl Rupp 
38561c3436cfSJed Brown     ts->vatol = vatol;
38571c3436cfSJed Brown   }
3858c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
38591c3436cfSJed Brown   if (vrtol) {
38601c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
38611c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
3862bbd56ea5SKarl Rupp 
38631c3436cfSJed Brown     ts->vrtol = vrtol;
38641c3436cfSJed Brown   }
38651c3436cfSJed Brown   PetscFunctionReturn(0);
38661c3436cfSJed Brown }
38671c3436cfSJed Brown 
38681c3436cfSJed Brown #undef __FUNCT__
3869c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3870c5033834SJed Brown /*@
3871c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3872c5033834SJed Brown 
3873c5033834SJed Brown    Logically Collective
3874c5033834SJed Brown 
3875c5033834SJed Brown    Input Arguments:
3876c5033834SJed Brown .  ts - time integration context
3877c5033834SJed Brown 
3878c5033834SJed Brown    Output Arguments:
3879c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3880c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3881c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3882c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3883c5033834SJed Brown 
3884c5033834SJed Brown    Level: beginner
3885c5033834SJed Brown 
3886c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3887c5033834SJed Brown @*/
3888c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3889c5033834SJed Brown {
3890c5033834SJed Brown   PetscFunctionBegin;
3891c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3892c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3893c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3894c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3895c5033834SJed Brown   PetscFunctionReturn(0);
3896c5033834SJed Brown }
3897c5033834SJed Brown 
3898c5033834SJed Brown #undef __FUNCT__
38991c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
39001c3436cfSJed Brown /*@
39011c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
39021c3436cfSJed Brown 
39031c3436cfSJed Brown    Collective on TS
39041c3436cfSJed Brown 
39051c3436cfSJed Brown    Input Arguments:
39061c3436cfSJed Brown +  ts - time stepping context
39071c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
39081c3436cfSJed Brown 
39091c3436cfSJed Brown    Output Arguments:
39101c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
39111c3436cfSJed Brown 
39121c3436cfSJed Brown    Level: developer
39131c3436cfSJed Brown 
39141c3436cfSJed Brown .seealso: TSSetTolerances()
39151c3436cfSJed Brown @*/
39161c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
39171c3436cfSJed Brown {
39181c3436cfSJed Brown   PetscErrorCode    ierr;
39191c3436cfSJed Brown   PetscInt          i,n,N;
39200910c330SBarry Smith   const PetscScalar *u,*y;
39210910c330SBarry Smith   Vec               U;
39221c3436cfSJed Brown   PetscReal         sum,gsum;
39231c3436cfSJed Brown 
39241c3436cfSJed Brown   PetscFunctionBegin;
39251c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39261c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
39271c3436cfSJed Brown   PetscValidPointer(norm,3);
39280910c330SBarry Smith   U = ts->vec_sol;
39290910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
39300910c330SBarry Smith   if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
39311c3436cfSJed Brown 
39320910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
39330910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
39340910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
39351c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
39361c3436cfSJed Brown   sum  = 0.;
3937ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3938ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3939ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3940ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3941ceefc113SJed Brown     for (i=0; i<n; i++) {
39420910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39430910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3944ceefc113SJed Brown     }
3945ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3946ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3947ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3948ceefc113SJed Brown     const PetscScalar *atol;
3949ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3950ceefc113SJed Brown     for (i=0; i<n; i++) {
39510910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39520910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3953ceefc113SJed Brown     }
3954ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3955ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3956ceefc113SJed Brown     const PetscScalar *rtol;
3957ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3958ceefc113SJed Brown     for (i=0; i<n; i++) {
39590910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39600910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3961ceefc113SJed Brown     }
3962ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3963ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
39641c3436cfSJed Brown     for (i=0; i<n; i++) {
39650910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39660910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
39671c3436cfSJed Brown     }
3968ceefc113SJed Brown   }
39690910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
39701c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
39711c3436cfSJed Brown 
39721c3436cfSJed Brown   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
39731c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
39741c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
39751c3436cfSJed Brown   PetscFunctionReturn(0);
39761c3436cfSJed Brown }
39771c3436cfSJed Brown 
39781c3436cfSJed Brown #undef __FUNCT__
39798d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
39808d59e960SJed Brown /*@
39818d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
39828d59e960SJed Brown 
39838d59e960SJed Brown    Logically Collective on TS
39848d59e960SJed Brown 
39858d59e960SJed Brown    Input Arguments:
39868d59e960SJed Brown +  ts - time stepping context
39878d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
39888d59e960SJed Brown 
39898d59e960SJed Brown    Note:
39908d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
39918d59e960SJed Brown 
39928d59e960SJed Brown    Level: intermediate
39938d59e960SJed Brown 
39948d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
39958d59e960SJed Brown @*/
39968d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
39978d59e960SJed Brown {
39988d59e960SJed Brown   PetscFunctionBegin;
39998d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40008d59e960SJed Brown   ts->cfltime_local = cfltime;
40018d59e960SJed Brown   ts->cfltime       = -1.;
40028d59e960SJed Brown   PetscFunctionReturn(0);
40038d59e960SJed Brown }
40048d59e960SJed Brown 
40058d59e960SJed Brown #undef __FUNCT__
40068d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
40078d59e960SJed Brown /*@
40088d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
40098d59e960SJed Brown 
40108d59e960SJed Brown    Collective on TS
40118d59e960SJed Brown 
40128d59e960SJed Brown    Input Arguments:
40138d59e960SJed Brown .  ts - time stepping context
40148d59e960SJed Brown 
40158d59e960SJed Brown    Output Arguments:
40168d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
40178d59e960SJed Brown 
40188d59e960SJed Brown    Level: advanced
40198d59e960SJed Brown 
40208d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
40218d59e960SJed Brown @*/
40228d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
40238d59e960SJed Brown {
40248d59e960SJed Brown   PetscErrorCode ierr;
40258d59e960SJed Brown 
40268d59e960SJed Brown   PetscFunctionBegin;
40278d59e960SJed Brown   if (ts->cfltime < 0) {
40288d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
40298d59e960SJed Brown   }
40308d59e960SJed Brown   *cfltime = ts->cfltime;
40318d59e960SJed Brown   PetscFunctionReturn(0);
40328d59e960SJed Brown }
40338d59e960SJed Brown 
40348d59e960SJed Brown #undef __FUNCT__
4035d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4036d6ebe24aSShri Abhyankar /*@
4037d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4038d6ebe24aSShri Abhyankar 
4039d6ebe24aSShri Abhyankar    Input Parameters:
4040d6ebe24aSShri Abhyankar .  ts   - the TS context.
4041d6ebe24aSShri Abhyankar .  xl   - lower bound.
4042d6ebe24aSShri Abhyankar .  xu   - upper bound.
4043d6ebe24aSShri Abhyankar 
4044d6ebe24aSShri Abhyankar    Notes:
4045d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
404633c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4047d6ebe24aSShri Abhyankar 
40482bd2b0e6SSatish Balay    Level: advanced
40492bd2b0e6SSatish Balay 
4050d6ebe24aSShri Abhyankar @*/
4051d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4052d6ebe24aSShri Abhyankar {
4053d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4054d6ebe24aSShri Abhyankar   SNES           snes;
4055d6ebe24aSShri Abhyankar 
4056d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4057d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4058d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4059d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4060d6ebe24aSShri Abhyankar }
4061d6ebe24aSShri Abhyankar 
4062325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4063c6db04a5SJed Brown #include <mex.h>
4064325fc9f4SBarry Smith 
4065325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4066325fc9f4SBarry Smith 
4067325fc9f4SBarry Smith #undef __FUNCT__
4068325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4069325fc9f4SBarry Smith /*
4070325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4071325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4072325fc9f4SBarry Smith 
4073325fc9f4SBarry Smith    Collective on TS
4074325fc9f4SBarry Smith 
4075325fc9f4SBarry Smith    Input Parameters:
4076325fc9f4SBarry Smith +  snes - the TS context
40770910c330SBarry Smith -  u - input vector
4078325fc9f4SBarry Smith 
4079325fc9f4SBarry Smith    Output Parameter:
4080325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4081325fc9f4SBarry Smith 
4082325fc9f4SBarry Smith    Notes:
4083325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4084325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4085325fc9f4SBarry Smith    themselves.
4086325fc9f4SBarry Smith 
4087325fc9f4SBarry Smith    Level: developer
4088325fc9f4SBarry Smith 
4089325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4090325fc9f4SBarry Smith 
4091325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4092325fc9f4SBarry Smith */
40930910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4094325fc9f4SBarry Smith {
4095325fc9f4SBarry Smith   PetscErrorCode  ierr;
4096325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4097325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4098325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4099325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4100325fc9f4SBarry Smith 
4101325fc9f4SBarry Smith   PetscFunctionBegin;
4102325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
41030910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
41040910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4105325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
41060910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4107325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4108325fc9f4SBarry Smith 
4109325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
41100910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
41110910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
41120910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4113bbd56ea5SKarl Rupp 
4114325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4115325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4116325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4117325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4118325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4119325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4120325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4121325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4122325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4123325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4124325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4125325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4126325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4127325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4128325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4129325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4130325fc9f4SBarry Smith   PetscFunctionReturn(0);
4131325fc9f4SBarry Smith }
4132325fc9f4SBarry Smith 
4133325fc9f4SBarry Smith 
4134325fc9f4SBarry Smith #undef __FUNCT__
4135325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4136325fc9f4SBarry Smith /*
4137325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4138325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4139e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4140325fc9f4SBarry Smith 
4141325fc9f4SBarry Smith    Logically Collective on TS
4142325fc9f4SBarry Smith 
4143325fc9f4SBarry Smith    Input Parameters:
4144325fc9f4SBarry Smith +  ts - the TS context
4145325fc9f4SBarry Smith -  func - function evaluation routine
4146325fc9f4SBarry Smith 
4147325fc9f4SBarry Smith    Calling sequence of func:
41480910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4149325fc9f4SBarry Smith 
4150325fc9f4SBarry Smith    Level: beginner
4151325fc9f4SBarry Smith 
4152325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4153325fc9f4SBarry Smith 
4154325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4155325fc9f4SBarry Smith */
4156cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4157325fc9f4SBarry Smith {
4158325fc9f4SBarry Smith   PetscErrorCode  ierr;
4159325fc9f4SBarry Smith   TSMatlabContext *sctx;
4160325fc9f4SBarry Smith 
4161325fc9f4SBarry Smith   PetscFunctionBegin;
4162325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4163325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4164325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4165325fc9f4SBarry Smith   /*
4166325fc9f4SBarry Smith      This should work, but it doesn't
4167325fc9f4SBarry Smith   sctx->ctx = ctx;
4168325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4169325fc9f4SBarry Smith   */
4170325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4171bbd56ea5SKarl Rupp 
4172cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4173325fc9f4SBarry Smith   PetscFunctionReturn(0);
4174325fc9f4SBarry Smith }
4175325fc9f4SBarry Smith 
4176325fc9f4SBarry Smith #undef __FUNCT__
4177325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4178325fc9f4SBarry Smith /*
4179325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4180325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4181325fc9f4SBarry Smith 
4182325fc9f4SBarry Smith    Collective on TS
4183325fc9f4SBarry Smith 
4184325fc9f4SBarry Smith    Input Parameters:
4185cdcf91faSSean Farley +  ts - the TS context
41860910c330SBarry Smith .  u - input vector
4187325fc9f4SBarry Smith .  A, B - the matrices
4188325fc9f4SBarry Smith -  ctx - user context
4189325fc9f4SBarry Smith 
4190325fc9f4SBarry Smith    Output Parameter:
4191325fc9f4SBarry Smith .  flag - structure of the matrix
4192325fc9f4SBarry Smith 
4193325fc9f4SBarry Smith    Level: developer
4194325fc9f4SBarry Smith 
4195325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4196325fc9f4SBarry Smith 
4197325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4198325fc9f4SBarry Smith @*/
41990910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4200325fc9f4SBarry Smith {
4201325fc9f4SBarry Smith   PetscErrorCode  ierr;
4202325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4203325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4204325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4205325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4206325fc9f4SBarry Smith 
4207325fc9f4SBarry Smith   PetscFunctionBegin;
4208cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42090910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4210325fc9f4SBarry Smith 
42110910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4212325fc9f4SBarry Smith 
4213cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
42140910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
42150910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
42160910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
42170910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4218bbd56ea5SKarl Rupp 
4219325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4220325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4221325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4222325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4223325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4224325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4225325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4226325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4227325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4228325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4229325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4230325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4231325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4232325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4233325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4234325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4235325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4236325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4237325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4238325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4239325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4240325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4241325fc9f4SBarry Smith   PetscFunctionReturn(0);
4242325fc9f4SBarry Smith }
4243325fc9f4SBarry Smith 
4244325fc9f4SBarry Smith 
4245325fc9f4SBarry Smith #undef __FUNCT__
4246325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4247325fc9f4SBarry Smith /*
4248325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4249e3c5b3baSBarry 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
4250325fc9f4SBarry Smith 
4251325fc9f4SBarry Smith    Logically Collective on TS
4252325fc9f4SBarry Smith 
4253325fc9f4SBarry Smith    Input Parameters:
4254cdcf91faSSean Farley +  ts - the TS context
4255325fc9f4SBarry Smith .  A,B - Jacobian matrices
4256325fc9f4SBarry Smith .  func - function evaluation routine
4257325fc9f4SBarry Smith -  ctx - user context
4258325fc9f4SBarry Smith 
4259325fc9f4SBarry Smith    Calling sequence of func:
42600910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4261325fc9f4SBarry Smith 
4262325fc9f4SBarry Smith 
4263325fc9f4SBarry Smith    Level: developer
4264325fc9f4SBarry Smith 
4265325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4266325fc9f4SBarry Smith 
4267325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4268325fc9f4SBarry Smith */
4269cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4270325fc9f4SBarry Smith {
4271325fc9f4SBarry Smith   PetscErrorCode  ierr;
4272325fc9f4SBarry Smith   TSMatlabContext *sctx;
4273325fc9f4SBarry Smith 
4274325fc9f4SBarry Smith   PetscFunctionBegin;
4275325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4276325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4277325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4278325fc9f4SBarry Smith   /*
4279325fc9f4SBarry Smith      This should work, but it doesn't
4280325fc9f4SBarry Smith   sctx->ctx = ctx;
4281325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4282325fc9f4SBarry Smith   */
4283325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4284bbd56ea5SKarl Rupp 
4285cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4286325fc9f4SBarry Smith   PetscFunctionReturn(0);
4287325fc9f4SBarry Smith }
4288325fc9f4SBarry Smith 
4289b5b1a830SBarry Smith #undef __FUNCT__
4290b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4291b5b1a830SBarry Smith /*
4292b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4293b5b1a830SBarry Smith 
4294b5b1a830SBarry Smith    Collective on TS
4295b5b1a830SBarry Smith 
4296b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4297b5b1a830SBarry Smith @*/
42980910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4299b5b1a830SBarry Smith {
4300b5b1a830SBarry Smith   PetscErrorCode  ierr;
4301b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4302a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4303b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4304b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4305b5b1a830SBarry Smith 
4306b5b1a830SBarry Smith   PetscFunctionBegin;
4307cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43080910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4309b5b1a830SBarry Smith 
4310cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
43110910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4312bbd56ea5SKarl Rupp 
4313b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4314b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4315b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4316b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4317b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4318b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4319b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4320b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4321b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4322b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4323b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4324b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4325b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4326b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4327b5b1a830SBarry Smith   PetscFunctionReturn(0);
4328b5b1a830SBarry Smith }
4329b5b1a830SBarry Smith 
4330b5b1a830SBarry Smith 
4331b5b1a830SBarry Smith #undef __FUNCT__
4332b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4333b5b1a830SBarry Smith /*
4334b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4335b5b1a830SBarry Smith 
4336b5b1a830SBarry Smith    Level: developer
4337b5b1a830SBarry Smith 
4338b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4339b5b1a830SBarry Smith 
4340b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4341b5b1a830SBarry Smith */
4342cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4343b5b1a830SBarry Smith {
4344b5b1a830SBarry Smith   PetscErrorCode  ierr;
4345b5b1a830SBarry Smith   TSMatlabContext *sctx;
4346b5b1a830SBarry Smith 
4347b5b1a830SBarry Smith   PetscFunctionBegin;
4348b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4349b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4350b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4351b5b1a830SBarry Smith   /*
4352b5b1a830SBarry Smith      This should work, but it doesn't
4353b5b1a830SBarry Smith   sctx->ctx = ctx;
4354b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4355b5b1a830SBarry Smith   */
4356b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4357bbd56ea5SKarl Rupp 
4358cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
4359b5b1a830SBarry Smith   PetscFunctionReturn(0);
4360b5b1a830SBarry Smith }
4361325fc9f4SBarry Smith #endif
4362b3603a34SBarry Smith 
43630b039ecaSBarry Smith 
43640b039ecaSBarry Smith 
4365b3603a34SBarry Smith #undef __FUNCT__
43664f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4367b3603a34SBarry Smith /*@C
43684f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4369b3603a34SBarry Smith        in a time based line graph
4370b3603a34SBarry Smith 
4371b3603a34SBarry Smith    Collective on TS
4372b3603a34SBarry Smith 
4373b3603a34SBarry Smith    Input Parameters:
4374b3603a34SBarry Smith +  ts - the TS context
4375b3603a34SBarry Smith .  step - current time-step
4376b3603a34SBarry Smith .  ptime - current time
4377b3603a34SBarry Smith -  lg - a line graph object
4378b3603a34SBarry Smith 
4379b3603a34SBarry Smith    Level: intermediate
4380b3603a34SBarry Smith 
43810b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4382b3603a34SBarry Smith 
4383b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4384b3603a34SBarry Smith 
4385b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4386b3603a34SBarry Smith @*/
43870910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4388b3603a34SBarry Smith {
4389b3603a34SBarry Smith   PetscErrorCode    ierr;
43900b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4391b3603a34SBarry Smith   const PetscScalar *yy;
439258ff32f7SBarry Smith   PetscInt          dim;
4393b3603a34SBarry Smith 
4394b3603a34SBarry Smith   PetscFunctionBegin;
439558ff32f7SBarry Smith   if (!step) {
4396a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4397a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4398a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
43990910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
44000b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
44010b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
440258ff32f7SBarry Smith   }
44030910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4404e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4405e3efe391SJed Brown   {
4406e3efe391SJed Brown     PetscReal *yreal;
4407e3efe391SJed Brown     PetscInt  i,n;
44080910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4409e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4410e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
44110b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4412e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4413e3efe391SJed Brown   }
4414e3efe391SJed Brown #else
44150b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4416e3efe391SJed Brown #endif
44170910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
44183fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
44190b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
44203923b477SBarry Smith   }
4421b3603a34SBarry Smith   PetscFunctionReturn(0);
4422b3603a34SBarry Smith }
4423b3603a34SBarry Smith 
4424b3603a34SBarry Smith #undef __FUNCT__
44254f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4426ef20d060SBarry Smith /*@C
44274f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4428ef20d060SBarry Smith        in a time based line graph
4429ef20d060SBarry Smith 
4430ef20d060SBarry Smith    Collective on TS
4431ef20d060SBarry Smith 
4432ef20d060SBarry Smith    Input Parameters:
4433ef20d060SBarry Smith +  ts - the TS context
4434ef20d060SBarry Smith .  step - current time-step
4435ef20d060SBarry Smith .  ptime - current time
4436ef20d060SBarry Smith -  lg - a line graph object
4437ef20d060SBarry Smith 
4438ef20d060SBarry Smith    Level: intermediate
4439ef20d060SBarry Smith 
4440abd5a294SJed Brown    Notes:
4441abd5a294SJed Brown    Only for sequential solves.
4442abd5a294SJed Brown 
4443abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4444abd5a294SJed Brown 
4445abd5a294SJed Brown    Options Database Keys:
44464f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4447ef20d060SBarry Smith 
4448ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4449ef20d060SBarry Smith 
4450abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4451ef20d060SBarry Smith @*/
44520910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4453ef20d060SBarry Smith {
4454ef20d060SBarry Smith   PetscErrorCode    ierr;
44550b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4456ef20d060SBarry Smith   const PetscScalar *yy;
4457ef20d060SBarry Smith   Vec               y;
4458a9f9c1f6SBarry Smith   PetscInt          dim;
4459ef20d060SBarry Smith 
4460ef20d060SBarry Smith   PetscFunctionBegin;
4461a9f9c1f6SBarry Smith   if (!step) {
4462a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4463a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
44641ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
44650910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4466a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4467a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4468a9f9c1f6SBarry Smith   }
44690910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4470ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
44710910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4472ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4473e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4474e3efe391SJed Brown   {
4475e3efe391SJed Brown     PetscReal *yreal;
4476e3efe391SJed Brown     PetscInt  i,n;
4477e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4478e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4479e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
44800b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4481e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4482e3efe391SJed Brown   }
4483e3efe391SJed Brown #else
44840b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4485e3efe391SJed Brown #endif
4486ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4487ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
44883fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
44890b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
44903923b477SBarry Smith   }
4491ef20d060SBarry Smith   PetscFunctionReturn(0);
4492ef20d060SBarry Smith }
4493ef20d060SBarry Smith 
4494201da799SBarry Smith #undef __FUNCT__
4495201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4496201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4497201da799SBarry Smith {
4498201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4499201da799SBarry Smith   PetscReal      x   = ptime,y;
4500201da799SBarry Smith   PetscErrorCode ierr;
4501201da799SBarry Smith   PetscInt       its;
4502201da799SBarry Smith 
4503201da799SBarry Smith   PetscFunctionBegin;
4504201da799SBarry Smith   if (!n) {
4505201da799SBarry Smith     PetscDrawAxis axis;
4506bbd56ea5SKarl Rupp 
4507201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4508201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4509201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4510bbd56ea5SKarl Rupp 
4511201da799SBarry Smith     ctx->snes_its = 0;
4512201da799SBarry Smith   }
4513201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4514201da799SBarry Smith   y    = its - ctx->snes_its;
4515201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
45163fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4517201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4518201da799SBarry Smith   }
4519201da799SBarry Smith   ctx->snes_its = its;
4520201da799SBarry Smith   PetscFunctionReturn(0);
4521201da799SBarry Smith }
4522201da799SBarry Smith 
4523201da799SBarry Smith #undef __FUNCT__
4524201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4525201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4526201da799SBarry Smith {
4527201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4528201da799SBarry Smith   PetscReal      x   = ptime,y;
4529201da799SBarry Smith   PetscErrorCode ierr;
4530201da799SBarry Smith   PetscInt       its;
4531201da799SBarry Smith 
4532201da799SBarry Smith   PetscFunctionBegin;
4533201da799SBarry Smith   if (!n) {
4534201da799SBarry Smith     PetscDrawAxis axis;
4535bbd56ea5SKarl Rupp 
4536201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4537201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4538201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4539bbd56ea5SKarl Rupp 
4540201da799SBarry Smith     ctx->ksp_its = 0;
4541201da799SBarry Smith   }
4542201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4543201da799SBarry Smith   y    = its - ctx->ksp_its;
4544201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
454599fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4546201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4547201da799SBarry Smith   }
4548201da799SBarry Smith   ctx->ksp_its = its;
4549201da799SBarry Smith   PetscFunctionReturn(0);
4550201da799SBarry Smith }
4551f9c1d6abSBarry Smith 
4552f9c1d6abSBarry Smith #undef __FUNCT__
4553f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4554f9c1d6abSBarry Smith /*@
4555f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4556f9c1d6abSBarry Smith 
4557f9c1d6abSBarry Smith    Collective on TS and Vec
4558f9c1d6abSBarry Smith 
4559f9c1d6abSBarry Smith    Input Parameters:
4560f9c1d6abSBarry Smith +  ts - the TS context
4561f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4562f9c1d6abSBarry Smith 
4563f9c1d6abSBarry Smith    Output Parameters:
4564f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4565f9c1d6abSBarry Smith 
4566f9c1d6abSBarry Smith    Level: developer
4567f9c1d6abSBarry Smith 
4568f9c1d6abSBarry Smith .keywords: TS, compute
4569f9c1d6abSBarry Smith 
4570f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4571f9c1d6abSBarry Smith @*/
4572f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4573f9c1d6abSBarry Smith {
4574f9c1d6abSBarry Smith   PetscErrorCode ierr;
4575f9c1d6abSBarry Smith 
4576f9c1d6abSBarry Smith   PetscFunctionBegin;
4577f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4578f9c1d6abSBarry Smith   if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4579f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4580f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4581f9c1d6abSBarry Smith }
4582