xref: /petsc/src/ts/interface/ts.c (revision c2fbc07f1af24e04288618fb73f899034795b9a1)
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
7291b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
7391b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
7491b97e58SBarry Smith 
7591b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
76bdad233fSMatthew Knepley 
77bdad233fSMatthew Knepley    Level: beginner
78bdad233fSMatthew Knepley 
79bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
80bdad233fSMatthew Knepley 
81a313700dSBarry Smith .seealso: TSGetType()
82bdad233fSMatthew Knepley @*/
837087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
84bdad233fSMatthew Knepley {
85ace3abfcSBarry Smith   PetscBool              opt,flg;
86dfbe8321SBarry Smith   PetscErrorCode         ierr;
87649052a6SBarry Smith   PetscViewer            monviewer;
88eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
89089b2837SJed Brown   SNES                   snes;
901c3436cfSJed Brown   TSAdapt                adapt;
9131748224SBarry Smith   PetscReal              time_step;
9249354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
93d1212d36SBarry Smith   char                   dir[16];
94bdad233fSMatthew Knepley 
95bdad233fSMatthew Knepley   PetscFunctionBegin;
960700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
973194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
98a43b19c4SJed Brown   /* Handle TS type options */
99a43b19c4SJed Brown   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
100bdad233fSMatthew Knepley 
101bdad233fSMatthew Knepley   /* Handle generic TS options */
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;
20191b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
202fb1732b5SBarry Smith   if (flg) {
203fb1732b5SBarry Smith     PetscViewer ctx;
204fb1732b5SBarry Smith     if (monfilename[0]) {
205fb1732b5SBarry Smith       ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
206*c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
207fb1732b5SBarry Smith     } else {
208fb1732b5SBarry Smith       ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
209*c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PETSC_NULL);CHKERRQ(ierr);
210fb1732b5SBarry Smith     }
211fb1732b5SBarry Smith   }
212ed81e22dSJed Brown   opt  = PETSC_FALSE;
21391b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
214ed81e22dSJed Brown   if (flg) {
215ed81e22dSJed Brown     const char *ptr,*ptr2;
216ed81e22dSJed Brown     char       *filetemplate;
21791b97e58SBarry Smith     if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
218ed81e22dSJed Brown     /* Do some cursory validation of the input. */
219ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
22091b97e58SBarry Smith     if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
221ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
222ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
22391b97e58SBarry 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");
224ed81e22dSJed Brown       if (ptr2) break;
225ed81e22dSJed Brown     }
226ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
227ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
228ed81e22dSJed Brown   }
229bdad233fSMatthew Knepley 
230d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
231d1212d36SBarry Smith   if (flg) {
232d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
233d1212d36SBarry Smith     int                 ray = 0;
234d1212d36SBarry Smith     DMDADirection       ddir;
235d1212d36SBarry Smith     DM                  da;
236d1212d36SBarry Smith     PetscMPIInt         rank;
237d1212d36SBarry Smith 
238d1212d36SBarry Smith     if (dir[1] != '=') SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
239d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
240d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
241d1212d36SBarry Smith     else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
242d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
243d1212d36SBarry Smith 
244d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
245d1212d36SBarry Smith     ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr);
246d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
247d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
248d1212d36SBarry Smith     ierr = MPI_Comm_rank(((PetscObject)ts)->comm,&rank);CHKERRQ(ierr);
249d1212d36SBarry Smith     if (!rank) {
250d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
251d1212d36SBarry Smith     }
252d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
253d1212d36SBarry Smith   }
254d1212d36SBarry Smith 
255ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&adapt);CHKERRQ(ierr);
2561c3436cfSJed Brown   ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2571c3436cfSJed Brown 
258d52bd9f3SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
259d52bd9f3SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
260d52bd9f3SBarry Smith 
261bdad233fSMatthew Knepley   /* Handle specific TS options */
262abc0a331SBarry Smith   if (ts->ops->setfromoptions) {
263bdad233fSMatthew Knepley     ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
264bdad233fSMatthew Knepley   }
2655d973c19SBarry Smith 
2665d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
2675d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
268bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
269bdad233fSMatthew Knepley   PetscFunctionReturn(0);
270bdad233fSMatthew Knepley }
271bdad233fSMatthew Knepley 
272bdad233fSMatthew Knepley #undef __FUNCT__
273cdcf91faSSean Farley #undef __FUNCT__
2744a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
275a7a1495cSBarry Smith /*@
2768c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
277a7a1495cSBarry Smith       set with TSSetRHSJacobian().
278a7a1495cSBarry Smith 
279a7a1495cSBarry Smith    Collective on TS and Vec
280a7a1495cSBarry Smith 
281a7a1495cSBarry Smith    Input Parameters:
282316643e7SJed Brown +  ts - the TS context
283a7a1495cSBarry Smith .  t - current timestep
2840910c330SBarry Smith -  U - input vector
285a7a1495cSBarry Smith 
286a7a1495cSBarry Smith    Output Parameters:
287a7a1495cSBarry Smith +  A - Jacobian matrix
288a7a1495cSBarry Smith .  B - optional preconditioning matrix
289a7a1495cSBarry Smith -  flag - flag indicating matrix structure
290a7a1495cSBarry Smith 
291a7a1495cSBarry Smith    Notes:
292a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
293a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
294a7a1495cSBarry Smith 
29594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
296a7a1495cSBarry Smith    flag parameter.
297a7a1495cSBarry Smith 
298a7a1495cSBarry Smith    Level: developer
299a7a1495cSBarry Smith 
300a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
301a7a1495cSBarry Smith 
30294b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
303a7a1495cSBarry Smith @*/
3040910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
305a7a1495cSBarry Smith {
306dfbe8321SBarry Smith   PetscErrorCode ierr;
3070910c330SBarry Smith   PetscInt       Ustate;
30824989b8cSPeter Brune   DM             dm;
309942e3340SBarry Smith   DMTS           tsdm;
31024989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
31124989b8cSPeter Brune   void           *ctx;
31224989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
313a7a1495cSBarry Smith 
314a7a1495cSBarry Smith   PetscFunctionBegin;
3150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3160910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3170910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
31824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
319942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
32024989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
32124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
3220910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
3230910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
3240e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
3250e4ef248SJed Brown     PetscFunctionReturn(0);
326f8ede8e7SJed Brown   }
327d90be118SSean Farley 
32824989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
329d90be118SSean Farley 
33024989b8cSPeter Brune   if (rhsjacobianfunc) {
3310910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
332a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
333a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3340910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
335a7a1495cSBarry Smith     PetscStackPop;
3360910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
337a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3380700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3390700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
340ef66eb69SBarry Smith   } else {
341214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
342214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
343214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
344ef66eb69SBarry Smith   }
3450e4ef248SJed Brown   ts->rhsjacobian.time       = t;
3460910c330SBarry Smith   ts->rhsjacobian.X          = U;
3470910c330SBarry Smith   ierr                       = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3480e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
349a7a1495cSBarry Smith   PetscFunctionReturn(0);
350a7a1495cSBarry Smith }
351a7a1495cSBarry Smith 
3524a2ae208SSatish Balay #undef __FUNCT__
3534a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
354316643e7SJed Brown /*@
355d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
356d763cef2SBarry Smith 
357316643e7SJed Brown    Collective on TS and Vec
358316643e7SJed Brown 
359316643e7SJed Brown    Input Parameters:
360316643e7SJed Brown +  ts - the TS context
361316643e7SJed Brown .  t - current time
3620910c330SBarry Smith -  U - state vector
363316643e7SJed Brown 
364316643e7SJed Brown    Output Parameter:
365316643e7SJed Brown .  y - right hand side
366316643e7SJed Brown 
367316643e7SJed Brown    Note:
368316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
369316643e7SJed Brown    is used internally within the nonlinear solvers.
370316643e7SJed Brown 
371316643e7SJed Brown    Level: developer
372316643e7SJed Brown 
373316643e7SJed Brown .keywords: TS, compute
374316643e7SJed Brown 
375316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
376316643e7SJed Brown @*/
3770910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
378d763cef2SBarry Smith {
379dfbe8321SBarry Smith   PetscErrorCode ierr;
38024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
38124989b8cSPeter Brune   TSIFunction    ifunction;
38224989b8cSPeter Brune   void           *ctx;
38324989b8cSPeter Brune   DM             dm;
38424989b8cSPeter Brune 
385d763cef2SBarry Smith   PetscFunctionBegin;
3860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3870910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3880700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
38924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
39024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
39124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
392d763cef2SBarry Smith 
39324989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
394d763cef2SBarry Smith 
3950910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
39624989b8cSPeter Brune   if (rhsfunction) {
397d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
3980910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
399d763cef2SBarry Smith     PetscStackPop;
400214bc6a2SJed Brown   } else {
401214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
402b2cd27e8SJed Brown   }
40344a41b28SSean Farley 
4040910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
405d763cef2SBarry Smith   PetscFunctionReturn(0);
406d763cef2SBarry Smith }
407d763cef2SBarry Smith 
4084a2ae208SSatish Balay #undef __FUNCT__
409ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
410ef20d060SBarry Smith /*@
411ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
412ef20d060SBarry Smith 
413ef20d060SBarry Smith    Collective on TS and Vec
414ef20d060SBarry Smith 
415ef20d060SBarry Smith    Input Parameters:
416ef20d060SBarry Smith +  ts - the TS context
417ef20d060SBarry Smith -  t - current time
418ef20d060SBarry Smith 
419ef20d060SBarry Smith    Output Parameter:
4200910c330SBarry Smith .  U - the solution
421ef20d060SBarry Smith 
422ef20d060SBarry Smith    Note:
423ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
424ef20d060SBarry Smith    is used internally within the nonlinear solvers.
425ef20d060SBarry Smith 
426ef20d060SBarry Smith    Level: developer
427ef20d060SBarry Smith 
428ef20d060SBarry Smith .keywords: TS, compute
429ef20d060SBarry Smith 
430abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
431ef20d060SBarry Smith @*/
4320910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
433ef20d060SBarry Smith {
434ef20d060SBarry Smith   PetscErrorCode     ierr;
435ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
436ef20d060SBarry Smith   void               *ctx;
437ef20d060SBarry Smith   DM                 dm;
438ef20d060SBarry Smith 
439ef20d060SBarry Smith   PetscFunctionBegin;
440ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4410910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
442ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
443ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
444ef20d060SBarry Smith 
445ef20d060SBarry Smith   if (solutionfunction) {
446ef20d060SBarry Smith     PetscStackPush("TS user right-hand-side function");
4470910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
448ef20d060SBarry Smith     PetscStackPop;
449ef20d060SBarry Smith   }
450ef20d060SBarry Smith   PetscFunctionReturn(0);
451ef20d060SBarry Smith }
452ef20d060SBarry Smith 
453ef20d060SBarry Smith #undef __FUNCT__
454214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
455214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
456214bc6a2SJed Brown {
4572dd45cf8SJed Brown   Vec            F;
458214bc6a2SJed Brown   PetscErrorCode ierr;
459214bc6a2SJed Brown 
460214bc6a2SJed Brown   PetscFunctionBegin;
4610da9a4f0SJed Brown   *Frhs = PETSC_NULL;
4622dd45cf8SJed Brown   ierr  = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
463214bc6a2SJed Brown   if (!ts->Frhs) {
4642dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
465214bc6a2SJed Brown   }
466214bc6a2SJed Brown   *Frhs = ts->Frhs;
467214bc6a2SJed Brown   PetscFunctionReturn(0);
468214bc6a2SJed Brown }
469214bc6a2SJed Brown 
470214bc6a2SJed Brown #undef __FUNCT__
471214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
472214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
473214bc6a2SJed Brown {
474214bc6a2SJed Brown   Mat            A,B;
4752dd45cf8SJed Brown   PetscErrorCode ierr;
476214bc6a2SJed Brown 
477214bc6a2SJed Brown   PetscFunctionBegin;
478214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
479214bc6a2SJed Brown   if (Arhs) {
480214bc6a2SJed Brown     if (!ts->Arhs) {
481214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
482214bc6a2SJed Brown     }
483214bc6a2SJed Brown     *Arhs = ts->Arhs;
484214bc6a2SJed Brown   }
485214bc6a2SJed Brown   if (Brhs) {
486214bc6a2SJed Brown     if (!ts->Brhs) {
487214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
488214bc6a2SJed Brown     }
489214bc6a2SJed Brown     *Brhs = ts->Brhs;
490214bc6a2SJed Brown   }
491214bc6a2SJed Brown   PetscFunctionReturn(0);
492214bc6a2SJed Brown }
493214bc6a2SJed Brown 
494214bc6a2SJed Brown #undef __FUNCT__
495316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
496316643e7SJed Brown /*@
4970910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
498316643e7SJed Brown 
499316643e7SJed Brown    Collective on TS and Vec
500316643e7SJed Brown 
501316643e7SJed Brown    Input Parameters:
502316643e7SJed Brown +  ts - the TS context
503316643e7SJed Brown .  t - current time
5040910c330SBarry Smith .  U - state vector
5050910c330SBarry Smith .  Udot - time derivative of state vector
506214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
507316643e7SJed Brown 
508316643e7SJed Brown    Output Parameter:
509316643e7SJed Brown .  Y - right hand side
510316643e7SJed Brown 
511316643e7SJed Brown    Note:
512316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
513316643e7SJed Brown    is used internally within the nonlinear solvers.
514316643e7SJed Brown 
515316643e7SJed Brown    If the user did did not write their equations in implicit form, this
516316643e7SJed Brown    function recasts them in implicit form.
517316643e7SJed Brown 
518316643e7SJed Brown    Level: developer
519316643e7SJed Brown 
520316643e7SJed Brown .keywords: TS, compute
521316643e7SJed Brown 
522316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
523316643e7SJed Brown @*/
5240910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
525316643e7SJed Brown {
526316643e7SJed Brown   PetscErrorCode ierr;
52724989b8cSPeter Brune   TSIFunction    ifunction;
52824989b8cSPeter Brune   TSRHSFunction  rhsfunction;
52924989b8cSPeter Brune   void           *ctx;
53024989b8cSPeter Brune   DM             dm;
531316643e7SJed Brown 
532316643e7SJed Brown   PetscFunctionBegin;
5330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5340910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5350910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
5360700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
537316643e7SJed Brown 
53824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
53924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
54024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
54124989b8cSPeter Brune 
54224989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
543d90be118SSean Farley 
5440910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
54524989b8cSPeter Brune   if (ifunction) {
546316643e7SJed Brown     PetscStackPush("TS user implicit function");
5470910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
548316643e7SJed Brown     PetscStackPop;
549214bc6a2SJed Brown   }
550214bc6a2SJed Brown   if (imex) {
55124989b8cSPeter Brune     if (!ifunction) {
5520910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
5532dd45cf8SJed Brown     }
55424989b8cSPeter Brune   } else if (rhsfunction) {
55524989b8cSPeter Brune     if (ifunction) {
556214bc6a2SJed Brown       Vec Frhs;
557214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
5580910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
559214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
5602dd45cf8SJed Brown     } else {
5610910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
5620910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
563316643e7SJed Brown     }
5644a6899ffSJed Brown   }
5650910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
566316643e7SJed Brown   PetscFunctionReturn(0);
567316643e7SJed Brown }
568316643e7SJed Brown 
569316643e7SJed Brown #undef __FUNCT__
570316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
571316643e7SJed Brown /*@
572316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
573316643e7SJed Brown 
574316643e7SJed Brown    Collective on TS and Vec
575316643e7SJed Brown 
576316643e7SJed Brown    Input
577316643e7SJed Brown       Input Parameters:
578316643e7SJed Brown +  ts - the TS context
579316643e7SJed Brown .  t - current timestep
5800910c330SBarry Smith .  U - state vector
5810910c330SBarry Smith .  Udot - time derivative of state vector
582214bc6a2SJed Brown .  shift - shift to apply, see note below
583214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
584316643e7SJed Brown 
585316643e7SJed Brown    Output Parameters:
586316643e7SJed Brown +  A - Jacobian matrix
587316643e7SJed Brown .  B - optional preconditioning matrix
588316643e7SJed Brown -  flag - flag indicating matrix structure
589316643e7SJed Brown 
590316643e7SJed Brown    Notes:
5910910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
592316643e7SJed Brown 
5930910c330SBarry Smith    dF/dU + shift*dF/dUdot
594316643e7SJed Brown 
595316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
596316643e7SJed Brown    is used internally within the nonlinear solvers.
597316643e7SJed Brown 
598316643e7SJed Brown    Level: developer
599316643e7SJed Brown 
600316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
601316643e7SJed Brown 
602316643e7SJed Brown .seealso:  TSSetIJacobian()
60363495f91SJed Brown @*/
6040910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
605316643e7SJed Brown {
6060910c330SBarry Smith   PetscInt       Ustate, Udotstate;
607316643e7SJed Brown   PetscErrorCode ierr;
60824989b8cSPeter Brune   TSIJacobian    ijacobian;
60924989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
61024989b8cSPeter Brune   DM             dm;
61124989b8cSPeter Brune   void           *ctx;
612316643e7SJed Brown 
613316643e7SJed Brown   PetscFunctionBegin;
6140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6150910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6160910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
617316643e7SJed Brown   PetscValidPointer(A,6);
6180700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
619316643e7SJed Brown   PetscValidPointer(B,7);
6200700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
621316643e7SJed Brown   PetscValidPointer(flg,8);
62224989b8cSPeter Brune 
62324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
62424989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
62524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
62624989b8cSPeter Brune 
6270910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
6280910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
6290910c330SBarry 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))) {
6300026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
6310026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
6320026cea9SSean Farley     PetscFunctionReturn(0);
6330026cea9SSean Farley   }
634316643e7SJed Brown 
63524989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
636316643e7SJed Brown 
6374e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
6380910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
63924989b8cSPeter Brune   if (ijacobian) {
6402dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
641316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
6420910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
643316643e7SJed Brown     PetscStackPop;
644214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
645214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
646214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
6474a6899ffSJed Brown   }
648214bc6a2SJed Brown   if (imex) {
649b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
650214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
6512dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
652214bc6a2SJed Brown       if (*A != *B) {
653214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
654214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
655214bc6a2SJed Brown       }
656214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
657214bc6a2SJed Brown     }
658214bc6a2SJed Brown   } else {
65924989b8cSPeter Brune     if (!ijacobian) {
6600910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr);
661214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
662214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
663316643e7SJed Brown       if (*A != *B) {
664316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
665316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
666316643e7SJed Brown       }
66724989b8cSPeter Brune     } else if (rhsjacobian) {
668214bc6a2SJed Brown       Mat          Arhs,Brhs;
669214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
670214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
6710910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
672214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
673214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
674214bc6a2SJed Brown       if (*A != *B) {
675214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
676214bc6a2SJed Brown       }
677214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
678214bc6a2SJed Brown     }
679316643e7SJed Brown   }
6800026cea9SSean Farley 
6810026cea9SSean Farley   ts->ijacobian.time = t;
6820910c330SBarry Smith   ts->ijacobian.X    = U;
6830910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
684bbd56ea5SKarl Rupp 
6850910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
6860910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
687bbd56ea5SKarl Rupp 
6880026cea9SSean Farley   ts->ijacobian.shift      = shift;
6890026cea9SSean Farley   ts->ijacobian.imex       = imex;
6900026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
691bbd56ea5SKarl Rupp 
6920910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
693316643e7SJed Brown   PetscFunctionReturn(0);
694316643e7SJed Brown }
695316643e7SJed Brown 
696316643e7SJed Brown #undef __FUNCT__
6974a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
698d763cef2SBarry Smith /*@C
699d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
700b5abc632SBarry Smith     where U_t = G(t,u).
701d763cef2SBarry Smith 
7023f9fe445SBarry Smith     Logically Collective on TS
703d763cef2SBarry Smith 
704d763cef2SBarry Smith     Input Parameters:
705d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
706ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
707d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
708d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
709d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
710d763cef2SBarry Smith 
711d763cef2SBarry Smith     Calling sequence of func:
71287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
713d763cef2SBarry Smith 
714d763cef2SBarry Smith +   t - current timestep
715d763cef2SBarry Smith .   u - input vector
716d763cef2SBarry Smith .   F - function vector
717d763cef2SBarry Smith -   ctx - [optional] user-defined function context
718d763cef2SBarry Smith 
719d763cef2SBarry Smith     Level: beginner
720d763cef2SBarry Smith 
721d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
722d763cef2SBarry Smith 
723d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
724d763cef2SBarry Smith @*/
725089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
726d763cef2SBarry Smith {
727089b2837SJed Brown   PetscErrorCode ierr;
728089b2837SJed Brown   SNES           snes;
729e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
73024989b8cSPeter Brune   DM             dm;
731d763cef2SBarry Smith 
732089b2837SJed Brown   PetscFunctionBegin;
7330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
734ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
73524989b8cSPeter Brune 
73624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
73724989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
738089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
739e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
740e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
741e856ceecSJed Brown     r    = ralloc;
742e856ceecSJed Brown   }
743089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
744e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
745d763cef2SBarry Smith   PetscFunctionReturn(0);
746d763cef2SBarry Smith }
747d763cef2SBarry Smith 
7484a2ae208SSatish Balay #undef __FUNCT__
749ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
750ef20d060SBarry Smith /*@C
751abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
752ef20d060SBarry Smith 
753ef20d060SBarry Smith     Logically Collective on TS
754ef20d060SBarry Smith 
755ef20d060SBarry Smith     Input Parameters:
756ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
757ef20d060SBarry Smith .   f - routine for evaluating the solution
758ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
759ef20d060SBarry Smith           function evaluation routine (may be PETSC_NULL)
760ef20d060SBarry Smith 
761ef20d060SBarry Smith     Calling sequence of func:
762ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
763ef20d060SBarry Smith 
764ef20d060SBarry Smith +   t - current timestep
765ef20d060SBarry Smith .   u - output vector
766ef20d060SBarry Smith -   ctx - [optional] user-defined function context
767ef20d060SBarry Smith 
768abd5a294SJed Brown     Notes:
769abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
770abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
771abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
772abd5a294SJed Brown 
7734f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
774abd5a294SJed Brown 
775ef20d060SBarry Smith     Level: beginner
776ef20d060SBarry Smith 
777ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
778ef20d060SBarry Smith 
779abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction()
780ef20d060SBarry Smith @*/
781ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
782ef20d060SBarry Smith {
783ef20d060SBarry Smith   PetscErrorCode ierr;
784ef20d060SBarry Smith   DM             dm;
785ef20d060SBarry Smith 
786ef20d060SBarry Smith   PetscFunctionBegin;
787ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
788ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
789ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
790ef20d060SBarry Smith   PetscFunctionReturn(0);
791ef20d060SBarry Smith }
792ef20d060SBarry Smith 
793ef20d060SBarry Smith #undef __FUNCT__
7944a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
795d763cef2SBarry Smith /*@C
796d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
797b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
798d763cef2SBarry Smith 
7993f9fe445SBarry Smith    Logically Collective on TS
800d763cef2SBarry Smith 
801d763cef2SBarry Smith    Input Parameters:
802d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
803d763cef2SBarry Smith .  A   - Jacobian matrix
804d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
805d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
806d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
807d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
808d763cef2SBarry Smith 
809d763cef2SBarry Smith    Calling sequence of func:
81087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
811d763cef2SBarry Smith 
812d763cef2SBarry Smith +  t - current timestep
813d763cef2SBarry Smith .  u - input vector
814d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
815d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
816d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
81794b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
818d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
819d763cef2SBarry Smith 
820d763cef2SBarry Smith    Notes:
82194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
822d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
823d763cef2SBarry Smith 
824d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
825d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
82656335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
827d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
828d763cef2SBarry Smith    throughout the global iterations.
829d763cef2SBarry Smith 
830d763cef2SBarry Smith    Level: beginner
831d763cef2SBarry Smith 
832d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
833d763cef2SBarry Smith 
834ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
835d763cef2SBarry Smith 
836d763cef2SBarry Smith @*/
837089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
838d763cef2SBarry Smith {
839277b19d0SLisandro Dalcin   PetscErrorCode ierr;
840089b2837SJed Brown   SNES           snes;
84124989b8cSPeter Brune   DM             dm;
84224989b8cSPeter Brune   TSIJacobian    ijacobian;
843277b19d0SLisandro Dalcin 
844d763cef2SBarry Smith   PetscFunctionBegin;
8450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
846277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
847277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
848277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
849277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
850d763cef2SBarry Smith 
85124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
85224989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
85324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
85424989b8cSPeter Brune 
855089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
8565f659677SPeter Brune   if (!ijacobian) {
857089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
8580e4ef248SJed Brown   }
8590e4ef248SJed Brown   if (A) {
8600e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8610e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
862bbd56ea5SKarl Rupp 
8630e4ef248SJed Brown     ts->Arhs = A;
8640e4ef248SJed Brown   }
8650e4ef248SJed Brown   if (B) {
8660e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8670e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
868bbd56ea5SKarl Rupp 
8690e4ef248SJed Brown     ts->Brhs = B;
8700e4ef248SJed Brown   }
871d763cef2SBarry Smith   PetscFunctionReturn(0);
872d763cef2SBarry Smith }
873d763cef2SBarry Smith 
874316643e7SJed Brown 
875316643e7SJed Brown #undef __FUNCT__
876316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
877316643e7SJed Brown /*@C
878b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
879316643e7SJed Brown 
8803f9fe445SBarry Smith    Logically Collective on TS
881316643e7SJed Brown 
882316643e7SJed Brown    Input Parameters:
883316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
884ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
885316643e7SJed Brown .  f   - the function evaluation routine
886316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
887316643e7SJed Brown 
888316643e7SJed Brown    Calling sequence of f:
889316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
890316643e7SJed Brown 
891316643e7SJed Brown +  t   - time at step/stage being solved
892316643e7SJed Brown .  u   - state vector
893316643e7SJed Brown .  u_t - time derivative of state vector
894316643e7SJed Brown .  F   - function vector
895316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
896316643e7SJed Brown 
897316643e7SJed Brown    Important:
898d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
899316643e7SJed Brown 
900316643e7SJed Brown    Level: beginner
901316643e7SJed Brown 
902316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
903316643e7SJed Brown 
904d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
905316643e7SJed Brown @*/
906089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
907316643e7SJed Brown {
908089b2837SJed Brown   PetscErrorCode ierr;
909089b2837SJed Brown   SNES           snes;
910e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
91124989b8cSPeter Brune   DM             dm;
912316643e7SJed Brown 
913316643e7SJed Brown   PetscFunctionBegin;
9140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
915ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
91624989b8cSPeter Brune 
91724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
91824989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
91924989b8cSPeter Brune 
920089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
921e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
922e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
923e856ceecSJed Brown     res  = resalloc;
924e856ceecSJed Brown   }
925089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
926e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
927089b2837SJed Brown   PetscFunctionReturn(0);
928089b2837SJed Brown }
929089b2837SJed Brown 
930089b2837SJed Brown #undef __FUNCT__
931089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
932089b2837SJed Brown /*@C
933089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
934089b2837SJed Brown 
935089b2837SJed Brown    Not Collective
936089b2837SJed Brown 
937089b2837SJed Brown    Input Parameter:
938089b2837SJed Brown .  ts - the TS context
939089b2837SJed Brown 
940089b2837SJed Brown    Output Parameter:
941089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
942089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
943089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
944089b2837SJed Brown 
945089b2837SJed Brown    Level: advanced
946089b2837SJed Brown 
947089b2837SJed Brown .keywords: TS, nonlinear, get, function
948089b2837SJed Brown 
949089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
950089b2837SJed Brown @*/
951089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
952089b2837SJed Brown {
953089b2837SJed Brown   PetscErrorCode ierr;
954089b2837SJed Brown   SNES           snes;
95524989b8cSPeter Brune   DM             dm;
956089b2837SJed Brown 
957089b2837SJed Brown   PetscFunctionBegin;
958089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
959089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
960089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
96124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96224989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
963089b2837SJed Brown   PetscFunctionReturn(0);
964089b2837SJed Brown }
965089b2837SJed Brown 
966089b2837SJed Brown #undef __FUNCT__
967089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
968089b2837SJed Brown /*@C
969089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
970089b2837SJed Brown 
971089b2837SJed Brown    Not Collective
972089b2837SJed Brown 
973089b2837SJed Brown    Input Parameter:
974089b2837SJed Brown .  ts - the TS context
975089b2837SJed Brown 
976089b2837SJed Brown    Output Parameter:
977089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
978089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
979089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
980089b2837SJed Brown 
981089b2837SJed Brown    Level: advanced
982089b2837SJed Brown 
983089b2837SJed Brown .keywords: TS, nonlinear, get, function
984089b2837SJed Brown 
985089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
986089b2837SJed Brown @*/
987089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
988089b2837SJed Brown {
989089b2837SJed Brown   PetscErrorCode ierr;
990089b2837SJed Brown   SNES           snes;
99124989b8cSPeter Brune   DM             dm;
992089b2837SJed Brown 
993089b2837SJed Brown   PetscFunctionBegin;
994089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
995089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
996089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
99724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
99824989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
999316643e7SJed Brown   PetscFunctionReturn(0);
1000316643e7SJed Brown }
1001316643e7SJed Brown 
1002316643e7SJed Brown #undef __FUNCT__
1003316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1004316643e7SJed Brown /*@C
1005a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1006a4f0a591SBarry Smith         you provided with TSSetIFunction().
1007316643e7SJed Brown 
10083f9fe445SBarry Smith    Logically Collective on TS
1009316643e7SJed Brown 
1010316643e7SJed Brown    Input Parameters:
1011316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1012316643e7SJed Brown .  A   - Jacobian matrix
1013316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
1014316643e7SJed Brown .  f   - the Jacobian evaluation routine
1015316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
1016316643e7SJed Brown 
1017316643e7SJed Brown    Calling sequence of f:
10181b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
1019316643e7SJed Brown 
1020316643e7SJed Brown +  t    - time at step/stage being solved
10211b4a444bSJed Brown .  U    - state vector
10221b4a444bSJed Brown .  U_t  - time derivative of state vector
1023316643e7SJed Brown .  a    - shift
1024b5abc632SBarry Smith .  A    - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1025316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
1026316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1027316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1028316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1029316643e7SJed Brown 
1030316643e7SJed Brown    Notes:
1031316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
1032316643e7SJed Brown 
1033a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1034b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1035a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1036a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1037a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1038a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1039a4f0a591SBarry Smith 
1040316643e7SJed Brown    Level: beginner
1041316643e7SJed Brown 
1042316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1043316643e7SJed Brown 
1044ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
1045316643e7SJed Brown 
1046316643e7SJed Brown @*/
10477087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1048316643e7SJed Brown {
1049316643e7SJed Brown   PetscErrorCode ierr;
1050089b2837SJed Brown   SNES           snes;
105124989b8cSPeter Brune   DM             dm;
1052316643e7SJed Brown 
1053316643e7SJed Brown   PetscFunctionBegin;
10540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10550700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
10560700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1057316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1058316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
105924989b8cSPeter Brune 
106024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
106124989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
106224989b8cSPeter Brune 
1063089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1064089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1065316643e7SJed Brown   PetscFunctionReturn(0);
1066316643e7SJed Brown }
1067316643e7SJed Brown 
10684a2ae208SSatish Balay #undef __FUNCT__
106955849f57SBarry Smith #define __FUNCT__ "TSLoad"
107055849f57SBarry Smith /*@C
107155849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
107255849f57SBarry Smith 
107355849f57SBarry Smith   Collective on PetscViewer
107455849f57SBarry Smith 
107555849f57SBarry Smith   Input Parameters:
107655849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
107755849f57SBarry Smith            some related function before a call to TSLoad().
107855849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
107955849f57SBarry Smith 
108055849f57SBarry Smith    Level: intermediate
108155849f57SBarry Smith 
108255849f57SBarry Smith   Notes:
108355849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
108455849f57SBarry Smith 
108555849f57SBarry Smith   Notes for advanced users:
108655849f57SBarry Smith   Most users should not need to know the details of the binary storage
108755849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
108855849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
108955849f57SBarry Smith   format is
109055849f57SBarry Smith .vb
109155849f57SBarry Smith      has not yet been determined
109255849f57SBarry Smith .ve
109355849f57SBarry Smith 
109455849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
109555849f57SBarry Smith @*/
1096f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
109755849f57SBarry Smith {
109855849f57SBarry Smith   PetscErrorCode ierr;
109955849f57SBarry Smith   PetscBool      isbinary;
110055849f57SBarry Smith   PetscInt       classid;
110155849f57SBarry Smith   char           type[256];
11022d53ad75SBarry Smith   DMTS           sdm;
1103ad6bc421SBarry Smith   DM             dm;
110455849f57SBarry Smith 
110555849f57SBarry Smith   PetscFunctionBegin;
1106f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
110755849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
110855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
110955849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
111055849f57SBarry Smith 
111155849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1112f2c2a1b9SBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Not TS next in file");
111355849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1114f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1115f2c2a1b9SBarry Smith   if (ts->ops->load) {
1116f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1117f2c2a1b9SBarry Smith   }
1118ad6bc421SBarry Smith   ierr = DMCreate(((PetscObject)ts)->comm,&dm);CHKERRQ(ierr);
1119ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1120ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1121f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1122f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
11232d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
11242d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
112555849f57SBarry Smith   PetscFunctionReturn(0);
112655849f57SBarry Smith }
112755849f57SBarry Smith 
112855849f57SBarry Smith #undef __FUNCT__
11294a2ae208SSatish Balay #define __FUNCT__ "TSView"
11307e2c5f70SBarry Smith /*@C
1131d763cef2SBarry Smith     TSView - Prints the TS data structure.
1132d763cef2SBarry Smith 
11334c49b128SBarry Smith     Collective on TS
1134d763cef2SBarry Smith 
1135d763cef2SBarry Smith     Input Parameters:
1136d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1137d763cef2SBarry Smith -   viewer - visualization context
1138d763cef2SBarry Smith 
1139d763cef2SBarry Smith     Options Database Key:
1140d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1141d763cef2SBarry Smith 
1142d763cef2SBarry Smith     Notes:
1143d763cef2SBarry Smith     The available visualization contexts include
1144b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1145b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1146d763cef2SBarry Smith          output where only the first processor opens
1147d763cef2SBarry Smith          the file.  All other processors send their
1148d763cef2SBarry Smith          data to the first processor to print.
1149d763cef2SBarry Smith 
1150d763cef2SBarry Smith     The user can open an alternative visualization context with
1151b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1152d763cef2SBarry Smith 
1153d763cef2SBarry Smith     Level: beginner
1154d763cef2SBarry Smith 
1155d763cef2SBarry Smith .keywords: TS, timestep, view
1156d763cef2SBarry Smith 
1157b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1158d763cef2SBarry Smith @*/
11597087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1160d763cef2SBarry Smith {
1161dfbe8321SBarry Smith   PetscErrorCode ierr;
116219fd82e9SBarry Smith   TSType         type;
11632b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
11642d53ad75SBarry Smith   DMTS           sdm;
1165d763cef2SBarry Smith 
1166d763cef2SBarry Smith   PetscFunctionBegin;
11670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11683050cee2SBarry Smith   if (!viewer) {
11697adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
11703050cee2SBarry Smith   }
11710700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1172c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1173fd16b177SBarry Smith 
1174251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1175251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
117655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
11772b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
117832077d6dSBarry Smith   if (iascii) {
1179317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
118077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1181a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1182d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
11835ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1184c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1185d763cef2SBarry Smith     }
11865ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1187193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
11882d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
11892d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1190d52bd9f3SBarry Smith     if (ts->ops->view) {
1191d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1192d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1193d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1194d52bd9f3SBarry Smith     }
11950f5bd95cSBarry Smith   } else if (isstring) {
1196a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1197b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
119855849f57SBarry Smith   } else if (isbinary) {
119955849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
120055849f57SBarry Smith     MPI_Comm    comm;
120155849f57SBarry Smith     PetscMPIInt rank;
120255849f57SBarry Smith     char        type[256];
120355849f57SBarry Smith 
120455849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
120555849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
120655849f57SBarry Smith     if (!rank) {
120755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
120855849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
120955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
121055849f57SBarry Smith     }
121155849f57SBarry Smith     if (ts->ops->view) {
121255849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
121355849f57SBarry Smith     }
1214f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1215f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
12162d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12172d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
12182b0a91c0SBarry Smith   } else if (isdraw) {
12192b0a91c0SBarry Smith     PetscDraw draw;
12202b0a91c0SBarry Smith     char      str[36];
122189fd9fafSBarry Smith     PetscReal x,y,bottom,h;
12222b0a91c0SBarry Smith 
12232b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
12242b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
12252b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
12262b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
122789fd9fafSBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,PETSC_NULL,&h);CHKERRQ(ierr);
122889fd9fafSBarry Smith     bottom = y - h;
12292b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
12302b0a91c0SBarry Smith     if (ts->ops->view) {
12312b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
12322b0a91c0SBarry Smith     }
12332b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1234d763cef2SBarry Smith   }
1235b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1236251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1237b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1238d763cef2SBarry Smith   PetscFunctionReturn(0);
1239d763cef2SBarry Smith }
1240d763cef2SBarry Smith 
1241d763cef2SBarry Smith 
12424a2ae208SSatish Balay #undef __FUNCT__
12434a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1244b07ff414SBarry Smith /*@
1245d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1246d763cef2SBarry Smith    the timesteppers.
1247d763cef2SBarry Smith 
12483f9fe445SBarry Smith    Logically Collective on TS
1249d763cef2SBarry Smith 
1250d763cef2SBarry Smith    Input Parameters:
1251d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1252d763cef2SBarry Smith -  usrP - optional user context
1253d763cef2SBarry Smith 
1254d763cef2SBarry Smith    Level: intermediate
1255d763cef2SBarry Smith 
1256d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1257d763cef2SBarry Smith 
1258d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1259d763cef2SBarry Smith @*/
12607087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1261d763cef2SBarry Smith {
1262d763cef2SBarry Smith   PetscFunctionBegin;
12630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1264d763cef2SBarry Smith   ts->user = usrP;
1265d763cef2SBarry Smith   PetscFunctionReturn(0);
1266d763cef2SBarry Smith }
1267d763cef2SBarry Smith 
12684a2ae208SSatish Balay #undef __FUNCT__
12694a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1270b07ff414SBarry Smith /*@
1271d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1272d763cef2SBarry Smith     timestepper.
1273d763cef2SBarry Smith 
1274d763cef2SBarry Smith     Not Collective
1275d763cef2SBarry Smith 
1276d763cef2SBarry Smith     Input Parameter:
1277d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1278d763cef2SBarry Smith 
1279d763cef2SBarry Smith     Output Parameter:
1280d763cef2SBarry Smith .   usrP - user context
1281d763cef2SBarry Smith 
1282d763cef2SBarry Smith     Level: intermediate
1283d763cef2SBarry Smith 
1284d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1285d763cef2SBarry Smith 
1286d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1287d763cef2SBarry Smith @*/
1288e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1289d763cef2SBarry Smith {
1290d763cef2SBarry Smith   PetscFunctionBegin;
12910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1292e71120c6SJed Brown   *(void**)usrP = ts->user;
1293d763cef2SBarry Smith   PetscFunctionReturn(0);
1294d763cef2SBarry Smith }
1295d763cef2SBarry Smith 
12964a2ae208SSatish Balay #undef __FUNCT__
12974a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1298d763cef2SBarry Smith /*@
1299b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1300d763cef2SBarry Smith 
1301d763cef2SBarry Smith    Not Collective
1302d763cef2SBarry Smith 
1303d763cef2SBarry Smith    Input Parameter:
1304d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1305d763cef2SBarry Smith 
1306d763cef2SBarry Smith    Output Parameter:
1307b8123daeSJed Brown .  iter - number of steps completed so far
1308d763cef2SBarry Smith 
1309d763cef2SBarry Smith    Level: intermediate
1310d763cef2SBarry Smith 
1311d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1312b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1313d763cef2SBarry Smith @*/
13147087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1315d763cef2SBarry Smith {
1316d763cef2SBarry Smith   PetscFunctionBegin;
13170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13184482741eSBarry Smith   PetscValidIntPointer(iter,2);
1319d763cef2SBarry Smith   *iter = ts->steps;
1320d763cef2SBarry Smith   PetscFunctionReturn(0);
1321d763cef2SBarry Smith }
1322d763cef2SBarry Smith 
13234a2ae208SSatish Balay #undef __FUNCT__
13244a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1325d763cef2SBarry Smith /*@
1326d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1327d763cef2SBarry Smith    as well as the initial time.
1328d763cef2SBarry Smith 
13293f9fe445SBarry Smith    Logically Collective on TS
1330d763cef2SBarry Smith 
1331d763cef2SBarry Smith    Input Parameters:
1332d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1333d763cef2SBarry Smith .  initial_time - the initial time
1334d763cef2SBarry Smith -  time_step - the size of the timestep
1335d763cef2SBarry Smith 
1336d763cef2SBarry Smith    Level: intermediate
1337d763cef2SBarry Smith 
1338d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1339d763cef2SBarry Smith 
1340d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1341d763cef2SBarry Smith @*/
13427087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1343d763cef2SBarry Smith {
1344e144a568SJed Brown   PetscErrorCode ierr;
1345e144a568SJed Brown 
1346d763cef2SBarry Smith   PetscFunctionBegin;
13470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1348e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1349d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1350d763cef2SBarry Smith   PetscFunctionReturn(0);
1351d763cef2SBarry Smith }
1352d763cef2SBarry Smith 
13534a2ae208SSatish Balay #undef __FUNCT__
13544a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1355d763cef2SBarry Smith /*@
1356d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1357d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1358d763cef2SBarry Smith 
13593f9fe445SBarry Smith    Logically Collective on TS
1360d763cef2SBarry Smith 
1361d763cef2SBarry Smith    Input Parameters:
1362d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1363d763cef2SBarry Smith -  time_step - the size of the timestep
1364d763cef2SBarry Smith 
1365d763cef2SBarry Smith    Level: intermediate
1366d763cef2SBarry Smith 
1367d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1368d763cef2SBarry Smith 
1369d763cef2SBarry Smith .keywords: TS, set, timestep
1370d763cef2SBarry Smith @*/
13717087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1372d763cef2SBarry Smith {
1373d763cef2SBarry Smith   PetscFunctionBegin;
13740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1375c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1376d763cef2SBarry Smith   ts->time_step      = time_step;
137731748224SBarry Smith   ts->time_step_orig = time_step;
1378d763cef2SBarry Smith   PetscFunctionReturn(0);
1379d763cef2SBarry Smith }
1380d763cef2SBarry Smith 
13814a2ae208SSatish Balay #undef __FUNCT__
1382a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1383a43b19c4SJed Brown /*@
138449354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
138549354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
138649354f04SShri Abhyankar      or just return at the final time TS computed.
1387a43b19c4SJed Brown 
1388a43b19c4SJed Brown   Logically Collective on TS
1389a43b19c4SJed Brown 
1390a43b19c4SJed Brown    Input Parameter:
1391a43b19c4SJed Brown +   ts - the time-step context
139249354f04SShri Abhyankar -   eftopt - exact final time option
1393a43b19c4SJed Brown 
1394a43b19c4SJed Brown    Level: beginner
1395a43b19c4SJed Brown 
1396a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1397a43b19c4SJed Brown @*/
139849354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1399a43b19c4SJed Brown {
1400a43b19c4SJed Brown   PetscFunctionBegin;
1401a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
140249354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
140349354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1404a43b19c4SJed Brown   PetscFunctionReturn(0);
1405a43b19c4SJed Brown }
1406a43b19c4SJed Brown 
1407a43b19c4SJed Brown #undef __FUNCT__
14084a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1409d763cef2SBarry Smith /*@
1410d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1411d763cef2SBarry Smith 
1412d763cef2SBarry Smith    Not Collective
1413d763cef2SBarry Smith 
1414d763cef2SBarry Smith    Input Parameter:
1415d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1416d763cef2SBarry Smith 
1417d763cef2SBarry Smith    Output Parameter:
1418d763cef2SBarry Smith .  dt - the current timestep size
1419d763cef2SBarry Smith 
1420d763cef2SBarry Smith    Level: intermediate
1421d763cef2SBarry Smith 
1422d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1423d763cef2SBarry Smith 
1424d763cef2SBarry Smith .keywords: TS, get, timestep
1425d763cef2SBarry Smith @*/
14267087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1427d763cef2SBarry Smith {
1428d763cef2SBarry Smith   PetscFunctionBegin;
14290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1430f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1431d763cef2SBarry Smith   *dt = ts->time_step;
1432d763cef2SBarry Smith   PetscFunctionReturn(0);
1433d763cef2SBarry Smith }
1434d763cef2SBarry Smith 
14354a2ae208SSatish Balay #undef __FUNCT__
14364a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1437d8e5e3e6SSatish Balay /*@
1438d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1439d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1440d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1441d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1442d763cef2SBarry Smith 
1443d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1444d763cef2SBarry Smith 
1445d763cef2SBarry Smith    Input Parameter:
1446d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1447d763cef2SBarry Smith 
1448d763cef2SBarry Smith    Output Parameter:
1449d763cef2SBarry Smith .  v - the vector containing the solution
1450d763cef2SBarry Smith 
1451d763cef2SBarry Smith    Level: intermediate
1452d763cef2SBarry Smith 
1453d763cef2SBarry Smith .seealso: TSGetTimeStep()
1454d763cef2SBarry Smith 
1455d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1456d763cef2SBarry Smith @*/
14577087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1458d763cef2SBarry Smith {
1459d763cef2SBarry Smith   PetscFunctionBegin;
14600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14614482741eSBarry Smith   PetscValidPointer(v,2);
14628737fe31SLisandro Dalcin   *v = ts->vec_sol;
1463d763cef2SBarry Smith   PetscFunctionReturn(0);
1464d763cef2SBarry Smith }
1465d763cef2SBarry Smith 
1466bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
14674a2ae208SSatish Balay #undef __FUNCT__
1468bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1469d8e5e3e6SSatish Balay /*@
1470bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1471d763cef2SBarry Smith 
1472bdad233fSMatthew Knepley   Not collective
1473d763cef2SBarry Smith 
1474bdad233fSMatthew Knepley   Input Parameters:
1475bdad233fSMatthew Knepley + ts   - The TS
1476bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1477d763cef2SBarry Smith .vb
14780910c330SBarry Smith          U_t - A U = 0      (linear)
14790910c330SBarry Smith          U_t - A(t) U = 0   (linear)
14800910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1481d763cef2SBarry Smith .ve
1482d763cef2SBarry Smith 
1483d763cef2SBarry Smith    Level: beginner
1484d763cef2SBarry Smith 
1485bdad233fSMatthew Knepley .keywords: TS, problem type
1486bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1487d763cef2SBarry Smith @*/
14887087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1489a7cc72afSBarry Smith {
14909e2a6581SJed Brown   PetscErrorCode ierr;
14919e2a6581SJed Brown 
1492d763cef2SBarry Smith   PetscFunctionBegin;
14930700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1494bdad233fSMatthew Knepley   ts->problem_type = type;
14959e2a6581SJed Brown   if (type == TS_LINEAR) {
14969e2a6581SJed Brown     SNES snes;
14979e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
14989e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
14999e2a6581SJed Brown   }
1500d763cef2SBarry Smith   PetscFunctionReturn(0);
1501d763cef2SBarry Smith }
1502d763cef2SBarry Smith 
1503bdad233fSMatthew Knepley #undef __FUNCT__
1504bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1505bdad233fSMatthew Knepley /*@C
1506bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1507bdad233fSMatthew Knepley 
1508bdad233fSMatthew Knepley   Not collective
1509bdad233fSMatthew Knepley 
1510bdad233fSMatthew Knepley   Input Parameter:
1511bdad233fSMatthew Knepley . ts   - The TS
1512bdad233fSMatthew Knepley 
1513bdad233fSMatthew Knepley   Output Parameter:
1514bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1515bdad233fSMatthew Knepley .vb
1516089b2837SJed Brown          M U_t = A U
1517089b2837SJed Brown          M(t) U_t = A(t) U
1518b5abc632SBarry Smith          F(t,U,U_t)
1519bdad233fSMatthew Knepley .ve
1520bdad233fSMatthew Knepley 
1521bdad233fSMatthew Knepley    Level: beginner
1522bdad233fSMatthew Knepley 
1523bdad233fSMatthew Knepley .keywords: TS, problem type
1524bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1525bdad233fSMatthew Knepley @*/
15267087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1527a7cc72afSBarry Smith {
1528bdad233fSMatthew Knepley   PetscFunctionBegin;
15290700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
15304482741eSBarry Smith   PetscValidIntPointer(type,2);
1531bdad233fSMatthew Knepley   *type = ts->problem_type;
1532bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1533bdad233fSMatthew Knepley }
1534d763cef2SBarry Smith 
15354a2ae208SSatish Balay #undef __FUNCT__
15364a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1537d763cef2SBarry Smith /*@
1538d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1539d763cef2SBarry Smith    of a timestepper.
1540d763cef2SBarry Smith 
1541d763cef2SBarry Smith    Collective on TS
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith    Input Parameter:
1544d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1545d763cef2SBarry Smith 
1546d763cef2SBarry Smith    Notes:
1547d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1548d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1549d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1550d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1551d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1552d763cef2SBarry Smith 
1553d763cef2SBarry Smith    Level: advanced
1554d763cef2SBarry Smith 
1555d763cef2SBarry Smith .keywords: TS, timestep, setup
1556d763cef2SBarry Smith 
1557d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1558d763cef2SBarry Smith @*/
15597087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1560d763cef2SBarry Smith {
1561dfbe8321SBarry Smith   PetscErrorCode ierr;
15626c6b9e74SPeter Brune   DM             dm;
15636c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
15646c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
15656c6b9e74SPeter Brune   TSIJacobian    ijac;
15666c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1567d763cef2SBarry Smith 
1568d763cef2SBarry Smith   PetscFunctionBegin;
15690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1570277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1571277b19d0SLisandro Dalcin 
15727adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
15739596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1574d763cef2SBarry Smith   }
1575277b19d0SLisandro Dalcin 
1576277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1577277b19d0SLisandro Dalcin 
1578ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr);
15791c3436cfSJed Brown 
1580277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1581000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1582277b19d0SLisandro Dalcin   }
1583277b19d0SLisandro Dalcin 
15846c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
15856c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
15866c6b9e74SPeter Brune    */
15876c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
15886c6b9e74SPeter Brune   ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr);
15896c6b9e74SPeter Brune   if (!func) {
15906c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
15916c6b9e74SPeter Brune   }
15926c6b9e74SPeter 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.
15936c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
15946c6b9e74SPeter Brune    */
15956c6b9e74SPeter Brune   ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr);
15966c6b9e74SPeter Brune   ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr);
15976c6b9e74SPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr);
15986c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
15996c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
16006c6b9e74SPeter Brune   }
1601277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1602277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1603277b19d0SLisandro Dalcin }
1604277b19d0SLisandro Dalcin 
1605277b19d0SLisandro Dalcin #undef __FUNCT__
1606277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1607277b19d0SLisandro Dalcin /*@
1608277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1609277b19d0SLisandro Dalcin 
1610277b19d0SLisandro Dalcin    Collective on TS
1611277b19d0SLisandro Dalcin 
1612277b19d0SLisandro Dalcin    Input Parameter:
1613277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1614277b19d0SLisandro Dalcin 
1615277b19d0SLisandro Dalcin    Level: beginner
1616277b19d0SLisandro Dalcin 
1617277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1618277b19d0SLisandro Dalcin 
1619277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1620277b19d0SLisandro Dalcin @*/
1621277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1622277b19d0SLisandro Dalcin {
1623277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1624277b19d0SLisandro Dalcin 
1625277b19d0SLisandro Dalcin   PetscFunctionBegin;
1626277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1627277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1628277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1629277b19d0SLisandro Dalcin   }
1630277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1631bbd56ea5SKarl Rupp 
16324e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
16334e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1634214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
16356bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1636e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1637e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
163838637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1639bbd56ea5SKarl Rupp 
1640277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1641d763cef2SBarry Smith   PetscFunctionReturn(0);
1642d763cef2SBarry Smith }
1643d763cef2SBarry Smith 
16444a2ae208SSatish Balay #undef __FUNCT__
16454a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1646d8e5e3e6SSatish Balay /*@
1647d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1648d763cef2SBarry Smith    with TSCreate().
1649d763cef2SBarry Smith 
1650d763cef2SBarry Smith    Collective on TS
1651d763cef2SBarry Smith 
1652d763cef2SBarry Smith    Input Parameter:
1653d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1654d763cef2SBarry Smith 
1655d763cef2SBarry Smith    Level: beginner
1656d763cef2SBarry Smith 
1657d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1658d763cef2SBarry Smith 
1659d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1660d763cef2SBarry Smith @*/
16616bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1662d763cef2SBarry Smith {
16636849ba73SBarry Smith   PetscErrorCode ierr;
1664d763cef2SBarry Smith 
1665d763cef2SBarry Smith   PetscFunctionBegin;
16666bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
16676bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
16686bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1669d763cef2SBarry Smith 
16706bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1671277b19d0SLisandro Dalcin 
1672be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
16736bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
16746bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
16756d4c513bSLisandro Dalcin 
167684df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
16776bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
16786bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
16796bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
16806d4c513bSLisandro Dalcin 
1681a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1682d763cef2SBarry Smith   PetscFunctionReturn(0);
1683d763cef2SBarry Smith }
1684d763cef2SBarry Smith 
16854a2ae208SSatish Balay #undef __FUNCT__
16864a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1687d8e5e3e6SSatish Balay /*@
1688d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1689d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1690d763cef2SBarry Smith 
1691d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1692d763cef2SBarry Smith 
1693d763cef2SBarry Smith    Input Parameter:
1694d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1695d763cef2SBarry Smith 
1696d763cef2SBarry Smith    Output Parameter:
1697d763cef2SBarry Smith .  snes - the nonlinear solver context
1698d763cef2SBarry Smith 
1699d763cef2SBarry Smith    Notes:
1700d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1701d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
170294b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1703d763cef2SBarry Smith 
1704d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1705d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1706d763cef2SBarry Smith 
1707d763cef2SBarry Smith    Level: beginner
1708d763cef2SBarry Smith 
1709d763cef2SBarry Smith .keywords: timestep, get, SNES
1710d763cef2SBarry Smith @*/
17117087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1712d763cef2SBarry Smith {
1713d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1714d372ba47SLisandro Dalcin 
1715d763cef2SBarry Smith   PetscFunctionBegin;
17160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17174482741eSBarry Smith   PetscValidPointer(snes,2);
1718d372ba47SLisandro Dalcin   if (!ts->snes) {
1719d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
17206c6b9e74SPeter Brune     ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1721d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1722d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1723496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
17249e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
17259e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
17269e2a6581SJed Brown     }
1727d372ba47SLisandro Dalcin   }
1728d763cef2SBarry Smith   *snes = ts->snes;
1729d763cef2SBarry Smith   PetscFunctionReturn(0);
1730d763cef2SBarry Smith }
1731d763cef2SBarry Smith 
17324a2ae208SSatish Balay #undef __FUNCT__
1733deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1734deb2cd25SJed Brown /*@
1735deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1736deb2cd25SJed Brown 
1737deb2cd25SJed Brown    Collective
1738deb2cd25SJed Brown 
1739deb2cd25SJed Brown    Input Parameter:
1740deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1741deb2cd25SJed Brown -  snes - the nonlinear solver context
1742deb2cd25SJed Brown 
1743deb2cd25SJed Brown    Notes:
1744deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1745deb2cd25SJed Brown 
1746deb2cd25SJed Brown    Level: developer
1747deb2cd25SJed Brown 
1748deb2cd25SJed Brown .keywords: timestep, set, SNES
1749deb2cd25SJed Brown @*/
1750deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1751deb2cd25SJed Brown {
1752deb2cd25SJed Brown   PetscErrorCode ierr;
1753740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1754deb2cd25SJed Brown 
1755deb2cd25SJed Brown   PetscFunctionBegin;
1756deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1757deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1758deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1759deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1760bbd56ea5SKarl Rupp 
1761deb2cd25SJed Brown   ts->snes = snes;
1762bbd56ea5SKarl Rupp 
1763740132f1SEmil Constantinescu   ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1764740132f1SEmil Constantinescu   ierr = SNESGetJacobian(ts->snes,PETSC_NULL,PETSC_NULL,&func,PETSC_NULL);CHKERRQ(ierr);
1765740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
1766740132f1SEmil Constantinescu     ierr = SNESSetJacobian(ts->snes,PETSC_NULL,PETSC_NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1767740132f1SEmil Constantinescu   }
1768deb2cd25SJed Brown   PetscFunctionReturn(0);
1769deb2cd25SJed Brown }
1770deb2cd25SJed Brown 
1771deb2cd25SJed Brown #undef __FUNCT__
177294b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1773d8e5e3e6SSatish Balay /*@
177494b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1775d763cef2SBarry Smith    a TS (timestepper) context.
1776d763cef2SBarry Smith 
177794b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1778d763cef2SBarry Smith 
1779d763cef2SBarry Smith    Input Parameter:
1780d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1781d763cef2SBarry Smith 
1782d763cef2SBarry Smith    Output Parameter:
178394b7f48cSBarry Smith .  ksp - the nonlinear solver context
1784d763cef2SBarry Smith 
1785d763cef2SBarry Smith    Notes:
178694b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1787d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1788d763cef2SBarry Smith    KSP and PC contexts as well.
1789d763cef2SBarry Smith 
179094b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
179194b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1792d763cef2SBarry Smith 
1793d763cef2SBarry Smith    Level: beginner
1794d763cef2SBarry Smith 
179594b7f48cSBarry Smith .keywords: timestep, get, KSP
1796d763cef2SBarry Smith @*/
17977087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1798d763cef2SBarry Smith {
1799d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1800089b2837SJed Brown   SNES           snes;
1801d372ba47SLisandro Dalcin 
1802d763cef2SBarry Smith   PetscFunctionBegin;
18030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18044482741eSBarry Smith   PetscValidPointer(ksp,2);
180517186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1806e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1807089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1808089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1809d763cef2SBarry Smith   PetscFunctionReturn(0);
1810d763cef2SBarry Smith }
1811d763cef2SBarry Smith 
1812d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1813d763cef2SBarry Smith 
18144a2ae208SSatish Balay #undef __FUNCT__
1815adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1816adb62b0dSMatthew Knepley /*@
1817adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1818adb62b0dSMatthew Knepley    maximum time for iteration.
1819adb62b0dSMatthew Knepley 
18203f9fe445SBarry Smith    Not Collective
1821adb62b0dSMatthew Knepley 
1822adb62b0dSMatthew Knepley    Input Parameters:
1823adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1824adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1825adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1826adb62b0dSMatthew Knepley 
1827adb62b0dSMatthew Knepley    Level: intermediate
1828adb62b0dSMatthew Knepley 
1829adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1830adb62b0dSMatthew Knepley @*/
18317087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1832adb62b0dSMatthew Knepley {
1833adb62b0dSMatthew Knepley   PetscFunctionBegin;
18340700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1835abc0a331SBarry Smith   if (maxsteps) {
18364482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1837adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1838adb62b0dSMatthew Knepley   }
1839abc0a331SBarry Smith   if (maxtime) {
18404482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1841adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
1842adb62b0dSMatthew Knepley   }
1843adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1844adb62b0dSMatthew Knepley }
1845adb62b0dSMatthew Knepley 
1846adb62b0dSMatthew Knepley #undef __FUNCT__
18474a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1848d763cef2SBarry Smith /*@
1849d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1850d763cef2SBarry Smith    maximum time for iteration.
1851d763cef2SBarry Smith 
18523f9fe445SBarry Smith    Logically Collective on TS
1853d763cef2SBarry Smith 
1854d763cef2SBarry Smith    Input Parameters:
1855d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1856d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1857d763cef2SBarry Smith -  maxtime - final time to iterate to
1858d763cef2SBarry Smith 
1859d763cef2SBarry Smith    Options Database Keys:
1860d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
18613bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1862d763cef2SBarry Smith 
1863d763cef2SBarry Smith    Notes:
1864d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1865d763cef2SBarry Smith 
1866d763cef2SBarry Smith    Level: intermediate
1867d763cef2SBarry Smith 
1868d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1869a43b19c4SJed Brown 
1870a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1871d763cef2SBarry Smith @*/
18727087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1873d763cef2SBarry Smith {
1874d763cef2SBarry Smith   PetscFunctionBegin;
18750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1876c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1877c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
187839b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
187939b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
1880d763cef2SBarry Smith   PetscFunctionReturn(0);
1881d763cef2SBarry Smith }
1882d763cef2SBarry Smith 
18834a2ae208SSatish Balay #undef __FUNCT__
18844a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1885d763cef2SBarry Smith /*@
1886d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1887d763cef2SBarry Smith    for use by the TS routines.
1888d763cef2SBarry Smith 
18893f9fe445SBarry Smith    Logically Collective on TS and Vec
1890d763cef2SBarry Smith 
1891d763cef2SBarry Smith    Input Parameters:
1892d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
18930910c330SBarry Smith -  u - the solution vector
1894d763cef2SBarry Smith 
1895d763cef2SBarry Smith    Level: beginner
1896d763cef2SBarry Smith 
1897d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1898d763cef2SBarry Smith @*/
18990910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
1900d763cef2SBarry Smith {
19018737fe31SLisandro Dalcin   PetscErrorCode ierr;
1902496e6a7aSJed Brown   DM             dm;
19038737fe31SLisandro Dalcin 
1904d763cef2SBarry Smith   PetscFunctionBegin;
19050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19060910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
19070910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
19086bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1909bbd56ea5SKarl Rupp 
19100910c330SBarry Smith   ts->vec_sol = u;
1911bbd56ea5SKarl Rupp 
1912496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
19130910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
1914d763cef2SBarry Smith   PetscFunctionReturn(0);
1915d763cef2SBarry Smith }
1916d763cef2SBarry Smith 
1917e74ef692SMatthew Knepley #undef __FUNCT__
1918e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1919ac226902SBarry Smith /*@C
1920000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
19213f2090d5SJed Brown   called once at the beginning of each time step.
1922000e7ae3SMatthew Knepley 
19233f9fe445SBarry Smith   Logically Collective on TS
1924000e7ae3SMatthew Knepley 
1925000e7ae3SMatthew Knepley   Input Parameters:
1926000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1927000e7ae3SMatthew Knepley - func - The function
1928000e7ae3SMatthew Knepley 
1929000e7ae3SMatthew Knepley   Calling sequence of func:
1930000e7ae3SMatthew Knepley . func (TS ts);
1931000e7ae3SMatthew Knepley 
1932000e7ae3SMatthew Knepley   Level: intermediate
1933000e7ae3SMatthew Knepley 
1934b8123daeSJed Brown   Note:
1935b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1936b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1937b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1938b8123daeSJed Brown 
1939000e7ae3SMatthew Knepley .keywords: TS, timestep
1940b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1941000e7ae3SMatthew Knepley @*/
19427087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1943000e7ae3SMatthew Knepley {
1944000e7ae3SMatthew Knepley   PetscFunctionBegin;
19450700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1946000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1947000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1948000e7ae3SMatthew Knepley }
1949000e7ae3SMatthew Knepley 
1950e74ef692SMatthew Knepley #undef __FUNCT__
19513f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
195209ee8438SJed Brown /*@
19533f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
19543f2090d5SJed Brown 
19553f2090d5SJed Brown   Collective on TS
19563f2090d5SJed Brown 
19573f2090d5SJed Brown   Input Parameters:
19583f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
19593f2090d5SJed Brown 
19603f2090d5SJed Brown   Notes:
19613f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
19623f2090d5SJed Brown   so most users would not generally call this routine themselves.
19633f2090d5SJed Brown 
19643f2090d5SJed Brown   Level: developer
19653f2090d5SJed Brown 
19663f2090d5SJed Brown .keywords: TS, timestep
1967b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
19683f2090d5SJed Brown @*/
19697087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
19703f2090d5SJed Brown {
19713f2090d5SJed Brown   PetscErrorCode ierr;
19723f2090d5SJed Brown 
19733f2090d5SJed Brown   PetscFunctionBegin;
19740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
197572ac3e02SJed Brown   if (ts->ops->prestep) {
19763f2090d5SJed Brown     PetscStackPush("TS PreStep function");
19773f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
19783f2090d5SJed Brown     PetscStackPop;
1979312ce896SJed Brown   }
19803f2090d5SJed Brown   PetscFunctionReturn(0);
19813f2090d5SJed Brown }
19823f2090d5SJed Brown 
19833f2090d5SJed Brown #undef __FUNCT__
1984b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1985b8123daeSJed Brown /*@C
1986b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1987b8123daeSJed Brown   called once at the beginning of each stage.
1988b8123daeSJed Brown 
1989b8123daeSJed Brown   Logically Collective on TS
1990b8123daeSJed Brown 
1991b8123daeSJed Brown   Input Parameters:
1992b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1993b8123daeSJed Brown - func - The function
1994b8123daeSJed Brown 
1995b8123daeSJed Brown   Calling sequence of func:
1996b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1997b8123daeSJed Brown 
1998b8123daeSJed Brown   Level: intermediate
1999b8123daeSJed Brown 
2000b8123daeSJed Brown   Note:
2001b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2002b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2003b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2004b8123daeSJed Brown 
2005b8123daeSJed Brown .keywords: TS, timestep
2006b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2007b8123daeSJed Brown @*/
2008b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2009b8123daeSJed Brown {
2010b8123daeSJed Brown   PetscFunctionBegin;
2011b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2012b8123daeSJed Brown   ts->ops->prestage = func;
2013b8123daeSJed Brown   PetscFunctionReturn(0);
2014b8123daeSJed Brown }
2015b8123daeSJed Brown 
2016b8123daeSJed Brown #undef __FUNCT__
2017b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2018b8123daeSJed Brown /*@
2019b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2020b8123daeSJed Brown 
2021b8123daeSJed Brown   Collective on TS
2022b8123daeSJed Brown 
2023b8123daeSJed Brown   Input Parameters:
2024b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2025b8123daeSJed Brown 
2026b8123daeSJed Brown   Notes:
2027b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2028b8123daeSJed Brown   most users would not generally call this routine themselves.
2029b8123daeSJed Brown 
2030b8123daeSJed Brown   Level: developer
2031b8123daeSJed Brown 
2032b8123daeSJed Brown .keywords: TS, timestep
2033b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2034b8123daeSJed Brown @*/
2035b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2036b8123daeSJed Brown {
2037b8123daeSJed Brown   PetscErrorCode ierr;
2038b8123daeSJed Brown 
2039b8123daeSJed Brown   PetscFunctionBegin;
2040b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2041b8123daeSJed Brown   if (ts->ops->prestage) {
2042b8123daeSJed Brown     PetscStackPush("TS PreStage function");
2043b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
2044b8123daeSJed Brown     PetscStackPop;
2045b8123daeSJed Brown   }
2046b8123daeSJed Brown   PetscFunctionReturn(0);
2047b8123daeSJed Brown }
2048b8123daeSJed Brown 
2049b8123daeSJed Brown #undef __FUNCT__
2050e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2051ac226902SBarry Smith /*@C
2052000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
20533f2090d5SJed Brown   called once at the end of each time step.
2054000e7ae3SMatthew Knepley 
20553f9fe445SBarry Smith   Logically Collective on TS
2056000e7ae3SMatthew Knepley 
2057000e7ae3SMatthew Knepley   Input Parameters:
2058000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2059000e7ae3SMatthew Knepley - func - The function
2060000e7ae3SMatthew Knepley 
2061000e7ae3SMatthew Knepley   Calling sequence of func:
2062b8123daeSJed Brown $ func (TS ts);
2063000e7ae3SMatthew Knepley 
2064000e7ae3SMatthew Knepley   Level: intermediate
2065000e7ae3SMatthew Knepley 
2066000e7ae3SMatthew Knepley .keywords: TS, timestep
2067b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2068000e7ae3SMatthew Knepley @*/
20697087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2070000e7ae3SMatthew Knepley {
2071000e7ae3SMatthew Knepley   PetscFunctionBegin;
20720700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2073000e7ae3SMatthew Knepley   ts->ops->poststep = func;
2074000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2075000e7ae3SMatthew Knepley }
2076000e7ae3SMatthew Knepley 
2077e74ef692SMatthew Knepley #undef __FUNCT__
20783f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
207909ee8438SJed Brown /*@
20803f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
20813f2090d5SJed Brown 
20823f2090d5SJed Brown   Collective on TS
20833f2090d5SJed Brown 
20843f2090d5SJed Brown   Input Parameters:
20853f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
20863f2090d5SJed Brown 
20873f2090d5SJed Brown   Notes:
20883f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
20893f2090d5SJed Brown   so most users would not generally call this routine themselves.
20903f2090d5SJed Brown 
20913f2090d5SJed Brown   Level: developer
20923f2090d5SJed Brown 
20933f2090d5SJed Brown .keywords: TS, timestep
20943f2090d5SJed Brown @*/
20957087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
20963f2090d5SJed Brown {
20973f2090d5SJed Brown   PetscErrorCode ierr;
20983f2090d5SJed Brown 
20993f2090d5SJed Brown   PetscFunctionBegin;
21000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
210172ac3e02SJed Brown   if (ts->ops->poststep) {
21023f2090d5SJed Brown     PetscStackPush("TS PostStep function");
21033f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
21043f2090d5SJed Brown     PetscStackPop;
210572ac3e02SJed Brown   }
21063f2090d5SJed Brown   PetscFunctionReturn(0);
21073f2090d5SJed Brown }
21083f2090d5SJed Brown 
2109d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2110d763cef2SBarry Smith 
21114a2ae208SSatish Balay #undef __FUNCT__
2112a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2113d763cef2SBarry Smith /*@C
2114a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2115d763cef2SBarry Smith    timestep to display the iteration's  progress.
2116d763cef2SBarry Smith 
21173f9fe445SBarry Smith    Logically Collective on TS
2118d763cef2SBarry Smith 
2119d763cef2SBarry Smith    Input Parameters:
2120d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2121e213d8f1SJed Brown .  monitor - monitoring routine
2122329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
2123b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
2124b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2125b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
2126d763cef2SBarry Smith 
2127e213d8f1SJed Brown    Calling sequence of monitor:
21280910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2129d763cef2SBarry Smith 
2130d763cef2SBarry Smith +    ts - the TS context
213188c05cc5SBarry 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
213288c05cc5SBarry Smith                                been interpolated to)
21331f06c33eSBarry Smith .    time - current time
21340910c330SBarry Smith .    u - current iterate
2135d763cef2SBarry Smith -    mctx - [optional] monitoring context
2136d763cef2SBarry Smith 
2137d763cef2SBarry Smith    Notes:
2138d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2139d763cef2SBarry Smith    already has been loaded.
2140d763cef2SBarry Smith 
2141025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2142025f1a04SBarry Smith 
2143d763cef2SBarry Smith    Level: intermediate
2144d763cef2SBarry Smith 
2145d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2146d763cef2SBarry Smith 
2147a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2148d763cef2SBarry Smith @*/
2149c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2150d763cef2SBarry Smith {
2151d763cef2SBarry Smith   PetscFunctionBegin;
21520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
215317186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2154d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
21558704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2156d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2157d763cef2SBarry Smith   PetscFunctionReturn(0);
2158d763cef2SBarry Smith }
2159d763cef2SBarry Smith 
21604a2ae208SSatish Balay #undef __FUNCT__
2161a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2162d763cef2SBarry Smith /*@C
2163a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2164d763cef2SBarry Smith 
21653f9fe445SBarry Smith    Logically Collective on TS
2166d763cef2SBarry Smith 
2167d763cef2SBarry Smith    Input Parameters:
2168d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2169d763cef2SBarry Smith 
2170d763cef2SBarry Smith    Notes:
2171d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2172d763cef2SBarry Smith 
2173d763cef2SBarry Smith    Level: intermediate
2174d763cef2SBarry Smith 
2175d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2176d763cef2SBarry Smith 
2177a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2178d763cef2SBarry Smith @*/
21797087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2180d763cef2SBarry Smith {
2181d952e501SBarry Smith   PetscErrorCode ierr;
2182d952e501SBarry Smith   PetscInt       i;
2183d952e501SBarry Smith 
2184d763cef2SBarry Smith   PetscFunctionBegin;
21850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2186d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
21878704b422SBarry Smith     if (ts->monitordestroy[i]) {
21888704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2189d952e501SBarry Smith     }
2190d952e501SBarry Smith   }
2191d763cef2SBarry Smith   ts->numbermonitors = 0;
2192d763cef2SBarry Smith   PetscFunctionReturn(0);
2193d763cef2SBarry Smith }
2194d763cef2SBarry Smith 
21954a2ae208SSatish Balay #undef __FUNCT__
2196a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2197d8e5e3e6SSatish Balay /*@
2198a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
21995516499fSSatish Balay 
22005516499fSSatish Balay    Level: intermediate
220141251cbbSSatish Balay 
22025516499fSSatish Balay .keywords: TS, set, monitor
22035516499fSSatish Balay 
220441251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
220541251cbbSSatish Balay @*/
2206649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2207d763cef2SBarry Smith {
2208dfbe8321SBarry Smith   PetscErrorCode ierr;
2209649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
2210d132466eSBarry Smith 
2211d763cef2SBarry Smith   PetscFunctionBegin;
2212649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2213971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2214649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2215d763cef2SBarry Smith   PetscFunctionReturn(0);
2216d763cef2SBarry Smith }
2217d763cef2SBarry Smith 
22184a2ae208SSatish Balay #undef __FUNCT__
2219cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2220cd652676SJed Brown /*@
2221cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2222cd652676SJed Brown 
2223cd652676SJed Brown    Logically Collective on TS
2224cd652676SJed Brown 
2225cd652676SJed Brown    Input Argument:
2226cd652676SJed Brown .  ts - time stepping context
2227cd652676SJed Brown 
2228cd652676SJed Brown    Output Argument:
2229cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2230cd652676SJed Brown 
2231cd652676SJed Brown    Level: intermediate
2232cd652676SJed Brown 
2233cd652676SJed Brown .keywords: TS, set
2234cd652676SJed Brown 
2235cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2236cd652676SJed Brown @*/
2237cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2238cd652676SJed Brown {
2239cd652676SJed Brown   PetscFunctionBegin;
2240cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2241cd652676SJed Brown   ts->retain_stages = flg;
2242cd652676SJed Brown   PetscFunctionReturn(0);
2243cd652676SJed Brown }
2244cd652676SJed Brown 
2245cd652676SJed Brown #undef __FUNCT__
2246cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2247cd652676SJed Brown /*@
2248cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2249cd652676SJed Brown 
2250cd652676SJed Brown    Collective on TS
2251cd652676SJed Brown 
2252cd652676SJed Brown    Input Argument:
2253cd652676SJed Brown +  ts - time stepping context
2254cd652676SJed Brown -  t - time to interpolate to
2255cd652676SJed Brown 
2256cd652676SJed Brown    Output Argument:
22570910c330SBarry Smith .  U - state at given time
2258cd652676SJed Brown 
2259cd652676SJed Brown    Notes:
2260cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2261cd652676SJed Brown 
2262cd652676SJed Brown    Level: intermediate
2263cd652676SJed Brown 
2264cd652676SJed Brown    Developer Notes:
2265cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2266cd652676SJed Brown 
2267cd652676SJed Brown .keywords: TS, set
2268cd652676SJed Brown 
2269cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2270cd652676SJed Brown @*/
22710910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2272cd652676SJed Brown {
2273cd652676SJed Brown   PetscErrorCode ierr;
2274cd652676SJed Brown 
2275cd652676SJed Brown   PetscFunctionBegin;
2276cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2277d8cd7023SBarry 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);
2278cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
22790910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2280cd652676SJed Brown   PetscFunctionReturn(0);
2281cd652676SJed Brown }
2282cd652676SJed Brown 
2283cd652676SJed Brown #undef __FUNCT__
22844a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2285d763cef2SBarry Smith /*@
22866d9e5789SSean Farley    TSStep - Steps one time step
2287d763cef2SBarry Smith 
2288d763cef2SBarry Smith    Collective on TS
2289d763cef2SBarry Smith 
2290d763cef2SBarry Smith    Input Parameter:
2291d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2292d763cef2SBarry Smith 
22936d9e5789SSean Farley    Level: intermediate
2294d763cef2SBarry Smith 
2295b8123daeSJed Brown    Notes:
2296b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2297b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2298b8123daeSJed Brown 
229925cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
230025cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
230125cb2221SBarry Smith 
2302d763cef2SBarry Smith .keywords: TS, timestep, solve
2303d763cef2SBarry Smith 
230425cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2305d763cef2SBarry Smith @*/
2306193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2307d763cef2SBarry Smith {
2308362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2309dfbe8321SBarry Smith   PetscErrorCode ierr;
2310d763cef2SBarry Smith 
2311d763cef2SBarry Smith   PetscFunctionBegin;
23120700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2313d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2314d405a339SMatthew Knepley 
2315362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2316362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2317bbd56ea5SKarl Rupp 
2318d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2319193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2320d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2321bbd56ea5SKarl Rupp 
2322362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2323362cd11cSLisandro Dalcin 
2324362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2325cef5090cSJed Brown     if (ts->errorifstepfailed) {
2326cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2327cef5090cSJed 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]);
2328cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2329cef5090cSJed Brown     }
2330362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2331db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2332db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2333362cd11cSLisandro Dalcin   }
2334d763cef2SBarry Smith   PetscFunctionReturn(0);
2335d763cef2SBarry Smith }
2336d763cef2SBarry Smith 
23374a2ae208SSatish Balay #undef __FUNCT__
233805175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
233905175c85SJed Brown /*@
234005175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
234105175c85SJed Brown 
23421c3436cfSJed Brown    Collective on TS
234305175c85SJed Brown 
234405175c85SJed Brown    Input Arguments:
23451c3436cfSJed Brown +  ts - time stepping context
23461c3436cfSJed Brown .  order - desired order of accuracy
23471c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
234805175c85SJed Brown 
234905175c85SJed Brown    Output Arguments:
23500910c330SBarry Smith .  U - state at the end of the current step
235105175c85SJed Brown 
235205175c85SJed Brown    Level: advanced
235305175c85SJed Brown 
2354108c343cSJed Brown    Notes:
2355108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2356108c343cSJed 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.
2357108c343cSJed Brown 
23581c3436cfSJed Brown .seealso: TSStep(), TSAdapt
235905175c85SJed Brown @*/
23600910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
236105175c85SJed Brown {
236205175c85SJed Brown   PetscErrorCode ierr;
236305175c85SJed Brown 
236405175c85SJed Brown   PetscFunctionBegin;
236505175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
236605175c85SJed Brown   PetscValidType(ts,1);
23670910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
23681c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
23690910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
237005175c85SJed Brown   PetscFunctionReturn(0);
237105175c85SJed Brown }
237205175c85SJed Brown 
237305175c85SJed Brown #undef __FUNCT__
23746a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
23756a4d4014SLisandro Dalcin /*@
23766a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
23776a4d4014SLisandro Dalcin 
23786a4d4014SLisandro Dalcin    Collective on TS
23796a4d4014SLisandro Dalcin 
23806a4d4014SLisandro Dalcin    Input Parameter:
23816a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2382cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
23835a3a76d0SJed Brown 
23846a4d4014SLisandro Dalcin    Level: beginner
23856a4d4014SLisandro Dalcin 
23865a3a76d0SJed Brown    Notes:
23875a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
23885a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
23895a3a76d0SJed Brown    stepped over the final time.
23905a3a76d0SJed Brown 
23916a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
23926a4d4014SLisandro Dalcin 
23936a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
23946a4d4014SLisandro Dalcin @*/
2395cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
23966a4d4014SLisandro Dalcin {
23974d7d938eSLisandro Dalcin   PetscBool         flg;
23984d7d938eSLisandro Dalcin   PetscViewer       viewer;
23996a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2400cffb1e40SBarry Smith   PetscViewerFormat format;
2401f22f69f0SBarry Smith 
24026a4d4014SLisandro Dalcin   PetscFunctionBegin;
24030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2404f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
240549354f04SShri 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 */
24060910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
24075a3a76d0SJed Brown       Vec y;
24080910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
24095a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
24105a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
24115a3a76d0SJed Brown     }
2412f2c2a1b9SBarry Smith     if (u) {
24130910c330SBarry Smith       ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2414f2c2a1b9SBarry Smith     }
2415bbd56ea5SKarl Rupp   } else if (u) {
24160910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
24175a3a76d0SJed Brown   }
2418b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
24196a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2420193ac0bcSJed Brown   ts->steps             = 0;
24215ef26d82SJed Brown   ts->ksp_its           = 0;
24225ef26d82SJed Brown   ts->snes_its          = 0;
2423c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2424c610991cSLisandro Dalcin   ts->reject            = 0;
2425193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2426193ac0bcSJed Brown 
2427193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2428193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
24290910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2430bbd56ea5SKarl Rupp 
2431cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2432193ac0bcSJed Brown   } else {
24336a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2434362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2435db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2436db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2437e1a7a14fSJed Brown     while (!ts->reason) {
2438193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2439193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2440193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2441193ac0bcSJed Brown     }
244249354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
24430910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2444bbd56ea5SKarl Rupp 
2445cc708dedSBarry Smith       ts->solvetime = ts->max_time;
24460574a7fbSJed Brown     } else {
2447ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2448cc708dedSBarry Smith       ts->solvetime = ts->ptime;
24490574a7fbSJed Brown     }
2450193ac0bcSJed Brown   }
24513923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2452cffb1e40SBarry Smith   ierr = PetscOptionsGetViewer(((PetscObject)ts)->comm,((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
24534d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2454cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
24554d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2456cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2457cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
24582b0a91c0SBarry Smith   }
24596a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
24606a4d4014SLisandro Dalcin }
24616a4d4014SLisandro Dalcin 
24626a4d4014SLisandro Dalcin #undef __FUNCT__
24634a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2464228d79bcSJed Brown /*@
2465228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2466228d79bcSJed Brown 
2467228d79bcSJed Brown    Collective on TS
2468228d79bcSJed Brown 
2469228d79bcSJed Brown    Input Parameters:
2470228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2471228d79bcSJed Brown .  step - step number that has just completed
2472228d79bcSJed Brown .  ptime - model time of the state
24730910c330SBarry Smith -  u - state at the current model time
2474228d79bcSJed Brown 
2475228d79bcSJed Brown    Notes:
2476228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2477228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2478228d79bcSJed Brown 
2479228d79bcSJed Brown    Level: advanced
2480228d79bcSJed Brown 
2481228d79bcSJed Brown .keywords: TS, timestep
2482228d79bcSJed Brown @*/
24830910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2484d763cef2SBarry Smith {
24856849ba73SBarry Smith   PetscErrorCode ierr;
2486a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2487d763cef2SBarry Smith 
2488d763cef2SBarry Smith   PetscFunctionBegin;
2489d763cef2SBarry Smith   for (i=0; i<n; i++) {
24900910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2491d763cef2SBarry Smith   }
2492d763cef2SBarry Smith   PetscFunctionReturn(0);
2493d763cef2SBarry Smith }
2494d763cef2SBarry Smith 
2495d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
24960b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
24970b039ecaSBarry Smith   PetscDrawLG lg;
24980b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2499201da799SBarry Smith   PetscInt    ksp_its,snes_its;
25000b039ecaSBarry Smith };
25010b039ecaSBarry Smith 
2502d763cef2SBarry Smith 
25034a2ae208SSatish Balay #undef __FUNCT__
2504a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2505d763cef2SBarry Smith /*@C
2506a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2507a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2508d763cef2SBarry Smith 
2509d763cef2SBarry Smith    Collective on TS
2510d763cef2SBarry Smith 
2511d763cef2SBarry Smith    Input Parameters:
2512d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2513d763cef2SBarry Smith .  label - the title to put in the title bar
25147c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2515a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2516a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2517d763cef2SBarry Smith 
2518d763cef2SBarry Smith    Output Parameter:
25190b039ecaSBarry Smith .  ctx - the context
2520d763cef2SBarry Smith 
2521d763cef2SBarry Smith    Options Database Key:
25224f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
25234f09c107SBarry Smith .  -ts_monitor_lg_solution -
252499fdda47SBarry Smith .  -ts_monitor_lg_error -
252599fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
252699fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
252799fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2528d763cef2SBarry Smith 
2529d763cef2SBarry Smith    Notes:
2530a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2531d763cef2SBarry Smith 
2532d763cef2SBarry Smith    Level: intermediate
2533d763cef2SBarry Smith 
25347c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2535d763cef2SBarry Smith 
25364f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
25377c922b88SBarry Smith 
2538d763cef2SBarry Smith @*/
2539a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2540d763cef2SBarry Smith {
2541b0a32e0cSBarry Smith   PetscDraw      win;
2542dfbe8321SBarry Smith   PetscErrorCode ierr;
254399fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2544d763cef2SBarry Smith 
2545d763cef2SBarry Smith   PetscFunctionBegin;
2546201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2547a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
25483fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
25490b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
255099fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr);
255199fdda47SBarry Smith   if (flg) {
25520b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
255399fdda47SBarry Smith   }
25540b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2555a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2556d763cef2SBarry Smith   PetscFunctionReturn(0);
2557d763cef2SBarry Smith }
2558d763cef2SBarry Smith 
25594a2ae208SSatish Balay #undef __FUNCT__
25604f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
25614f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2562d763cef2SBarry Smith {
25630b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2564c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2565dfbe8321SBarry Smith   PetscErrorCode ierr;
2566d763cef2SBarry Smith 
2567d763cef2SBarry Smith   PetscFunctionBegin;
2568a9f9c1f6SBarry Smith   if (!n) {
2569a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2570a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2571a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2572a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2573a9f9c1f6SBarry Smith   }
2574c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
25750b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
257699fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))) {
25770b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
25783923b477SBarry Smith   }
2579d763cef2SBarry Smith   PetscFunctionReturn(0);
2580d763cef2SBarry Smith }
2581d763cef2SBarry Smith 
25824a2ae208SSatish Balay #undef __FUNCT__
2583a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2584d763cef2SBarry Smith /*@C
2585a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2586a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2587d763cef2SBarry Smith 
25880b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2589d763cef2SBarry Smith 
2590d763cef2SBarry Smith    Input Parameter:
25910b039ecaSBarry Smith .  ctx - the monitor context
2592d763cef2SBarry Smith 
2593d763cef2SBarry Smith    Level: intermediate
2594d763cef2SBarry Smith 
2595d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2596d763cef2SBarry Smith 
25974f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2598d763cef2SBarry Smith @*/
2599a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2600d763cef2SBarry Smith {
2601b0a32e0cSBarry Smith   PetscDraw      draw;
2602dfbe8321SBarry Smith   PetscErrorCode ierr;
2603d763cef2SBarry Smith 
2604d763cef2SBarry Smith   PetscFunctionBegin;
26050b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
26066bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
26070b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
26080b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2609d763cef2SBarry Smith   PetscFunctionReturn(0);
2610d763cef2SBarry Smith }
2611d763cef2SBarry Smith 
26124a2ae208SSatish Balay #undef __FUNCT__
26134a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2614d763cef2SBarry Smith /*@
2615b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2616d763cef2SBarry Smith 
2617d763cef2SBarry Smith    Not Collective
2618d763cef2SBarry Smith 
2619d763cef2SBarry Smith    Input Parameter:
2620d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2621d763cef2SBarry Smith 
2622d763cef2SBarry Smith    Output Parameter:
2623d763cef2SBarry Smith .  t  - the current time
2624d763cef2SBarry Smith 
2625d763cef2SBarry Smith    Level: beginner
2626d763cef2SBarry Smith 
2627b8123daeSJed Brown    Note:
2628b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2629b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2630b8123daeSJed Brown 
2631d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2632d763cef2SBarry Smith 
2633d763cef2SBarry Smith .keywords: TS, get, time
2634d763cef2SBarry Smith @*/
26357087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2636d763cef2SBarry Smith {
2637d763cef2SBarry Smith   PetscFunctionBegin;
26380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2639f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2640d763cef2SBarry Smith   *t = ts->ptime;
2641d763cef2SBarry Smith   PetscFunctionReturn(0);
2642d763cef2SBarry Smith }
2643d763cef2SBarry Smith 
26444a2ae208SSatish Balay #undef __FUNCT__
26456a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
26466a4d4014SLisandro Dalcin /*@
26476a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
26486a4d4014SLisandro Dalcin 
26493f9fe445SBarry Smith    Logically Collective on TS
26506a4d4014SLisandro Dalcin 
26516a4d4014SLisandro Dalcin    Input Parameters:
26526a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
26536a4d4014SLisandro Dalcin -  time - the time
26546a4d4014SLisandro Dalcin 
26556a4d4014SLisandro Dalcin    Level: intermediate
26566a4d4014SLisandro Dalcin 
26576a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
26586a4d4014SLisandro Dalcin 
26596a4d4014SLisandro Dalcin .keywords: TS, set, time
26606a4d4014SLisandro Dalcin @*/
26617087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
26626a4d4014SLisandro Dalcin {
26636a4d4014SLisandro Dalcin   PetscFunctionBegin;
26640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2665c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
26666a4d4014SLisandro Dalcin   ts->ptime = t;
26676a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
26686a4d4014SLisandro Dalcin }
26696a4d4014SLisandro Dalcin 
26706a4d4014SLisandro Dalcin #undef __FUNCT__
26714a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2672d763cef2SBarry Smith /*@C
2673d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2674d763cef2SBarry Smith    TS options in the database.
2675d763cef2SBarry Smith 
26763f9fe445SBarry Smith    Logically Collective on TS
2677d763cef2SBarry Smith 
2678d763cef2SBarry Smith    Input Parameter:
2679d763cef2SBarry Smith +  ts     - The TS context
2680d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2681d763cef2SBarry Smith 
2682d763cef2SBarry Smith    Notes:
2683d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2684d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2685d763cef2SBarry Smith    hyphen.
2686d763cef2SBarry Smith 
2687d763cef2SBarry Smith    Level: advanced
2688d763cef2SBarry Smith 
2689d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2690d763cef2SBarry Smith 
2691d763cef2SBarry Smith .seealso: TSSetFromOptions()
2692d763cef2SBarry Smith 
2693d763cef2SBarry Smith @*/
26947087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2695d763cef2SBarry Smith {
2696dfbe8321SBarry Smith   PetscErrorCode ierr;
2697089b2837SJed Brown   SNES           snes;
2698d763cef2SBarry Smith 
2699d763cef2SBarry Smith   PetscFunctionBegin;
27000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2701d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2702089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2703089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2704d763cef2SBarry Smith   PetscFunctionReturn(0);
2705d763cef2SBarry Smith }
2706d763cef2SBarry Smith 
2707d763cef2SBarry Smith 
27084a2ae208SSatish Balay #undef __FUNCT__
27094a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2710d763cef2SBarry Smith /*@C
2711d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2712d763cef2SBarry Smith    TS options in the database.
2713d763cef2SBarry Smith 
27143f9fe445SBarry Smith    Logically Collective on TS
2715d763cef2SBarry Smith 
2716d763cef2SBarry Smith    Input Parameter:
2717d763cef2SBarry Smith +  ts     - The TS context
2718d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2719d763cef2SBarry Smith 
2720d763cef2SBarry Smith    Notes:
2721d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2722d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2723d763cef2SBarry Smith    hyphen.
2724d763cef2SBarry Smith 
2725d763cef2SBarry Smith    Level: advanced
2726d763cef2SBarry Smith 
2727d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2728d763cef2SBarry Smith 
2729d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2730d763cef2SBarry Smith 
2731d763cef2SBarry Smith @*/
27327087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2733d763cef2SBarry Smith {
2734dfbe8321SBarry Smith   PetscErrorCode ierr;
2735089b2837SJed Brown   SNES           snes;
2736d763cef2SBarry Smith 
2737d763cef2SBarry Smith   PetscFunctionBegin;
27380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2739d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2740089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2741089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2742d763cef2SBarry Smith   PetscFunctionReturn(0);
2743d763cef2SBarry Smith }
2744d763cef2SBarry Smith 
27454a2ae208SSatish Balay #undef __FUNCT__
27464a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2747d763cef2SBarry Smith /*@C
2748d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2749d763cef2SBarry Smith    TS options in the database.
2750d763cef2SBarry Smith 
2751d763cef2SBarry Smith    Not Collective
2752d763cef2SBarry Smith 
2753d763cef2SBarry Smith    Input Parameter:
2754d763cef2SBarry Smith .  ts - The TS context
2755d763cef2SBarry Smith 
2756d763cef2SBarry Smith    Output Parameter:
2757d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2758d763cef2SBarry Smith 
2759d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2760d763cef2SBarry Smith    sufficient length to hold the prefix.
2761d763cef2SBarry Smith 
2762d763cef2SBarry Smith    Level: intermediate
2763d763cef2SBarry Smith 
2764d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2765d763cef2SBarry Smith 
2766d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2767d763cef2SBarry Smith @*/
27687087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2769d763cef2SBarry Smith {
2770dfbe8321SBarry Smith   PetscErrorCode ierr;
2771d763cef2SBarry Smith 
2772d763cef2SBarry Smith   PetscFunctionBegin;
27730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27744482741eSBarry Smith   PetscValidPointer(prefix,2);
2775d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2776d763cef2SBarry Smith   PetscFunctionReturn(0);
2777d763cef2SBarry Smith }
2778d763cef2SBarry Smith 
27794a2ae208SSatish Balay #undef __FUNCT__
27804a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2781d763cef2SBarry Smith /*@C
2782d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2783d763cef2SBarry Smith 
2784d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2785d763cef2SBarry Smith 
2786d763cef2SBarry Smith    Input Parameter:
2787d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2788d763cef2SBarry Smith 
2789d763cef2SBarry Smith    Output Parameters:
2790b5abc632SBarry Smith +  J   - The Jacobian J of F, where U_t = G(U,t)
2791d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2792089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2793d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2794d763cef2SBarry Smith 
2795d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2796d763cef2SBarry Smith 
2797d763cef2SBarry Smith    Level: intermediate
2798d763cef2SBarry Smith 
279926d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2800d763cef2SBarry Smith 
2801d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2802d763cef2SBarry Smith @*/
2803089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2804d763cef2SBarry Smith {
2805089b2837SJed Brown   PetscErrorCode ierr;
2806089b2837SJed Brown   SNES           snes;
280724989b8cSPeter Brune   DM             dm;
2808089b2837SJed Brown 
2809d763cef2SBarry Smith   PetscFunctionBegin;
2810089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2811089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
281224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
281324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2814d763cef2SBarry Smith   PetscFunctionReturn(0);
2815d763cef2SBarry Smith }
2816d763cef2SBarry Smith 
28171713a123SBarry Smith #undef __FUNCT__
28182eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
28192eca1d9cSJed Brown /*@C
28202eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
28212eca1d9cSJed Brown 
28222eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
28232eca1d9cSJed Brown 
28242eca1d9cSJed Brown    Input Parameter:
28252eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
28262eca1d9cSJed Brown 
28272eca1d9cSJed Brown    Output Parameters:
28282eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
28292eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
28302eca1d9cSJed Brown .  f   - The function to compute the matrices
28312eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
28322eca1d9cSJed Brown 
28332eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
28342eca1d9cSJed Brown 
28352eca1d9cSJed Brown    Level: advanced
28362eca1d9cSJed Brown 
28372eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
28382eca1d9cSJed Brown 
28392eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
28402eca1d9cSJed Brown @*/
28417087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
28422eca1d9cSJed Brown {
2843089b2837SJed Brown   PetscErrorCode ierr;
2844089b2837SJed Brown   SNES           snes;
284524989b8cSPeter Brune   DM             dm;
28460910c330SBarry Smith 
28472eca1d9cSJed Brown   PetscFunctionBegin;
2848089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2849f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
2850089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
285124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
285224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
28532eca1d9cSJed Brown   PetscFunctionReturn(0);
28542eca1d9cSJed Brown }
28552eca1d9cSJed Brown 
285683a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
28576083293cSBarry Smith   PetscViewer viewer;
28586083293cSBarry Smith   Vec         initialsolution;
28596083293cSBarry Smith   PetscBool   showinitial;
286083a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2861473a3ab2SBarry Smith   PetscBool   showtimestepandtime;
286283a4ac43SBarry Smith };
28636083293cSBarry Smith 
28642eca1d9cSJed Brown #undef __FUNCT__
286583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
28661713a123SBarry Smith /*@C
286783a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
28681713a123SBarry Smith    VecView() for the solution at each timestep
28691713a123SBarry Smith 
28701713a123SBarry Smith    Collective on TS
28711713a123SBarry Smith 
28721713a123SBarry Smith    Input Parameters:
28731713a123SBarry Smith +  ts - the TS context
28741713a123SBarry Smith .  step - current time-step
2875142b95e3SSatish Balay .  ptime - current time
28761713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
28771713a123SBarry Smith 
287899fdda47SBarry Smith    Options Database:
287999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
288099fdda47SBarry Smith 
288199fdda47SBarry 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
288299fdda47SBarry Smith        will look bad
288399fdda47SBarry Smith 
28841713a123SBarry Smith    Level: intermediate
28851713a123SBarry Smith 
28861713a123SBarry Smith .keywords: TS,  vector, monitor, view
28871713a123SBarry Smith 
2888a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
28891713a123SBarry Smith @*/
28900910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
28911713a123SBarry Smith {
2892dfbe8321SBarry Smith   PetscErrorCode   ierr;
289383a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
2894473a3ab2SBarry Smith   PetscDraw        draw;
28951713a123SBarry Smith 
28961713a123SBarry Smith   PetscFunctionBegin;
28976083293cSBarry Smith   if (!step && ictx->showinitial) {
28986083293cSBarry Smith     if (!ictx->initialsolution) {
28990910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
29001713a123SBarry Smith     }
29010910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
29026083293cSBarry Smith   }
29033fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
29040dcf80beSBarry Smith 
29056083293cSBarry Smith   if (ictx->showinitial) {
29066083293cSBarry Smith     PetscReal pause;
29076083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
29086083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
29096083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
29106083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
29116083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
29126083293cSBarry Smith   }
29130910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
2914473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
2915473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
2916473a3ab2SBarry Smith     char      time[32];
2917473a3ab2SBarry Smith     size_t    len;
2918473a3ab2SBarry Smith 
2919473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
2920473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
2921473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
2922473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
2923473a3ab2SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,PETSC_NULL);CHKERRQ(ierr);
2924473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
2925473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
2926473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
2927473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
2928473a3ab2SBarry Smith   }
2929473a3ab2SBarry Smith 
29306083293cSBarry Smith   if (ictx->showinitial) {
29316083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
29326083293cSBarry Smith   }
29331713a123SBarry Smith   PetscFunctionReturn(0);
29341713a123SBarry Smith }
29351713a123SBarry Smith 
29361713a123SBarry Smith 
29376c699258SBarry Smith #undef __FUNCT__
293883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
29396083293cSBarry Smith /*@C
294083a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
29416083293cSBarry Smith 
29426083293cSBarry Smith    Collective on TS
29436083293cSBarry Smith 
29446083293cSBarry Smith    Input Parameters:
29456083293cSBarry Smith .    ctx - the monitor context
29466083293cSBarry Smith 
29476083293cSBarry Smith    Level: intermediate
29486083293cSBarry Smith 
29496083293cSBarry Smith .keywords: TS,  vector, monitor, view
29506083293cSBarry Smith 
295183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
29526083293cSBarry Smith @*/
295383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
29546083293cSBarry Smith {
29556083293cSBarry Smith   PetscErrorCode ierr;
29566083293cSBarry Smith 
29576083293cSBarry Smith   PetscFunctionBegin;
295883a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
295983a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
296083a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
29616083293cSBarry Smith   PetscFunctionReturn(0);
29626083293cSBarry Smith }
29636083293cSBarry Smith 
29646083293cSBarry Smith #undef __FUNCT__
296583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
29666083293cSBarry Smith /*@C
296783a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
29686083293cSBarry Smith 
29696083293cSBarry Smith    Collective on TS
29706083293cSBarry Smith 
29716083293cSBarry Smith    Input Parameter:
29726083293cSBarry Smith .    ts - time-step context
29736083293cSBarry Smith 
29746083293cSBarry Smith    Output Patameter:
29756083293cSBarry Smith .    ctx - the monitor context
29766083293cSBarry Smith 
297799fdda47SBarry Smith    Options Database:
297899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
297999fdda47SBarry Smith 
29806083293cSBarry Smith    Level: intermediate
29816083293cSBarry Smith 
29826083293cSBarry Smith .keywords: TS,  vector, monitor, view
29836083293cSBarry Smith 
298483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
29856083293cSBarry Smith @*/
298683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
29876083293cSBarry Smith {
29886083293cSBarry Smith   PetscErrorCode   ierr;
29896083293cSBarry Smith 
29906083293cSBarry Smith   PetscFunctionBegin;
299183a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
299283a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
2993e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
2994bbd56ea5SKarl Rupp 
299583a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
2996473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
299799fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr);
2998473a3ab2SBarry Smith 
2999473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
3000473a3ab2SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,PETSC_NULL);CHKERRQ(ierr);
30016083293cSBarry Smith   PetscFunctionReturn(0);
30026083293cSBarry Smith }
30036083293cSBarry Smith 
30046083293cSBarry Smith #undef __FUNCT__
300583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
30063a471f94SBarry Smith /*@C
300783a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
30083a471f94SBarry Smith    VecView() for the error at each timestep
30093a471f94SBarry Smith 
30103a471f94SBarry Smith    Collective on TS
30113a471f94SBarry Smith 
30123a471f94SBarry Smith    Input Parameters:
30133a471f94SBarry Smith +  ts - the TS context
30143a471f94SBarry Smith .  step - current time-step
30153a471f94SBarry Smith .  ptime - current time
30163a471f94SBarry Smith -  dummy - either a viewer or PETSC_NULL
30173a471f94SBarry Smith 
30183a471f94SBarry Smith    Level: intermediate
30193a471f94SBarry Smith 
30203a471f94SBarry Smith .keywords: TS,  vector, monitor, view
30213a471f94SBarry Smith 
30223a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
30233a471f94SBarry Smith @*/
30240910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
30253a471f94SBarry Smith {
30263a471f94SBarry Smith   PetscErrorCode   ierr;
302783a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
302883a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
30293a471f94SBarry Smith   Vec              work;
30303a471f94SBarry Smith 
30313a471f94SBarry Smith   PetscFunctionBegin;
30323fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
30330910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
30343a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
30350910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
30363a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
30373a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
30383a471f94SBarry Smith   PetscFunctionReturn(0);
30393a471f94SBarry Smith }
30403a471f94SBarry Smith 
30412a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
30423a471f94SBarry Smith #undef __FUNCT__
30436c699258SBarry Smith #define __FUNCT__ "TSSetDM"
30446c699258SBarry Smith /*@
30456c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
30466c699258SBarry Smith 
30473f9fe445SBarry Smith    Logically Collective on TS and DM
30486c699258SBarry Smith 
30496c699258SBarry Smith    Input Parameters:
30506c699258SBarry Smith +  ts - the preconditioner context
30516c699258SBarry Smith -  dm - the dm
30526c699258SBarry Smith 
30536c699258SBarry Smith    Level: intermediate
30546c699258SBarry Smith 
30556c699258SBarry Smith 
30566c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
30576c699258SBarry Smith @*/
30587087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
30596c699258SBarry Smith {
30606c699258SBarry Smith   PetscErrorCode ierr;
3061089b2837SJed Brown   SNES           snes;
3062942e3340SBarry Smith   DMTS           tsdm;
30636c699258SBarry Smith 
30646c699258SBarry Smith   PetscFunctionBegin;
30650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
306670663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3067942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
30682a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3069942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3070942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
307124989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
307224989b8cSPeter Brune         tsdm->originaldm = dm;
307324989b8cSPeter Brune       }
307424989b8cSPeter Brune     }
30756bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
307624989b8cSPeter Brune   }
30776c699258SBarry Smith   ts->dm = dm;
3078bbd56ea5SKarl Rupp 
3079089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3080089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
30816c699258SBarry Smith   PetscFunctionReturn(0);
30826c699258SBarry Smith }
30836c699258SBarry Smith 
30846c699258SBarry Smith #undef __FUNCT__
30856c699258SBarry Smith #define __FUNCT__ "TSGetDM"
30866c699258SBarry Smith /*@
30876c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
30886c699258SBarry Smith 
30893f9fe445SBarry Smith    Not Collective
30906c699258SBarry Smith 
30916c699258SBarry Smith    Input Parameter:
30926c699258SBarry Smith . ts - the preconditioner context
30936c699258SBarry Smith 
30946c699258SBarry Smith    Output Parameter:
30956c699258SBarry Smith .  dm - the dm
30966c699258SBarry Smith 
30976c699258SBarry Smith    Level: intermediate
30986c699258SBarry Smith 
30996c699258SBarry Smith 
31006c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
31016c699258SBarry Smith @*/
31027087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
31036c699258SBarry Smith {
3104496e6a7aSJed Brown   PetscErrorCode ierr;
3105496e6a7aSJed Brown 
31066c699258SBarry Smith   PetscFunctionBegin;
31070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3108496e6a7aSJed Brown   if (!ts->dm) {
3109496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
3110496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3111496e6a7aSJed Brown   }
31126c699258SBarry Smith   *dm = ts->dm;
31136c699258SBarry Smith   PetscFunctionReturn(0);
31146c699258SBarry Smith }
31151713a123SBarry Smith 
31160f5c6efeSJed Brown #undef __FUNCT__
31170f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
31180f5c6efeSJed Brown /*@
31190f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
31200f5c6efeSJed Brown 
31213f9fe445SBarry Smith    Logically Collective on SNES
31220f5c6efeSJed Brown 
31230f5c6efeSJed Brown    Input Parameter:
3124d42a1c89SJed Brown + snes - nonlinear solver
31250910c330SBarry Smith . U - the current state at which to evaluate the residual
3126d42a1c89SJed Brown - ctx - user context, must be a TS
31270f5c6efeSJed Brown 
31280f5c6efeSJed Brown    Output Parameter:
31290f5c6efeSJed Brown . F - the nonlinear residual
31300f5c6efeSJed Brown 
31310f5c6efeSJed Brown    Notes:
31320f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
31330f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
31340f5c6efeSJed Brown 
31350f5c6efeSJed Brown    Level: advanced
31360f5c6efeSJed Brown 
31370f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
31380f5c6efeSJed Brown @*/
31390910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
31400f5c6efeSJed Brown {
31410f5c6efeSJed Brown   TS             ts = (TS)ctx;
31420f5c6efeSJed Brown   PetscErrorCode ierr;
31430f5c6efeSJed Brown 
31440f5c6efeSJed Brown   PetscFunctionBegin;
31450f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31460910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
31470f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
31480f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
31490910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
31500f5c6efeSJed Brown   PetscFunctionReturn(0);
31510f5c6efeSJed Brown }
31520f5c6efeSJed Brown 
31530f5c6efeSJed Brown #undef __FUNCT__
31540f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
31550f5c6efeSJed Brown /*@
31560f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
31570f5c6efeSJed Brown 
31580f5c6efeSJed Brown    Collective on SNES
31590f5c6efeSJed Brown 
31600f5c6efeSJed Brown    Input Parameter:
31610f5c6efeSJed Brown + snes - nonlinear solver
31620910c330SBarry Smith . U - the current state at which to evaluate the residual
31630f5c6efeSJed Brown - ctx - user context, must be a TS
31640f5c6efeSJed Brown 
31650f5c6efeSJed Brown    Output Parameter:
31660f5c6efeSJed Brown + A - the Jacobian
31670f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
31680f5c6efeSJed Brown - flag - indicates any structure change in the matrix
31690f5c6efeSJed Brown 
31700f5c6efeSJed Brown    Notes:
31710f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
31720f5c6efeSJed Brown 
31730f5c6efeSJed Brown    Level: developer
31740f5c6efeSJed Brown 
31750f5c6efeSJed Brown .seealso: SNESSetJacobian()
31760f5c6efeSJed Brown @*/
31770910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
31780f5c6efeSJed Brown {
31790f5c6efeSJed Brown   TS             ts = (TS)ctx;
31800f5c6efeSJed Brown   PetscErrorCode ierr;
31810f5c6efeSJed Brown 
31820f5c6efeSJed Brown   PetscFunctionBegin;
31830f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31840910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
31850f5c6efeSJed Brown   PetscValidPointer(A,3);
31860f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
31870f5c6efeSJed Brown   PetscValidPointer(B,4);
31880f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
31890f5c6efeSJed Brown   PetscValidPointer(flag,5);
31900f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
31910910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
31920f5c6efeSJed Brown   PetscFunctionReturn(0);
31930f5c6efeSJed Brown }
3194325fc9f4SBarry Smith 
31950e4ef248SJed Brown #undef __FUNCT__
31960e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
31970e4ef248SJed Brown /*@C
31980e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
31990e4ef248SJed Brown 
32000e4ef248SJed Brown    Collective on TS
32010e4ef248SJed Brown 
32020e4ef248SJed Brown    Input Arguments:
32030e4ef248SJed Brown +  ts - time stepping context
32040e4ef248SJed Brown .  t - time at which to evaluate
32050910c330SBarry Smith .  U - state at which to evaluate
32060e4ef248SJed Brown -  ctx - context
32070e4ef248SJed Brown 
32080e4ef248SJed Brown    Output Arguments:
32090e4ef248SJed Brown .  F - right hand side
32100e4ef248SJed Brown 
32110e4ef248SJed Brown    Level: intermediate
32120e4ef248SJed Brown 
32130e4ef248SJed Brown    Notes:
32140e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
32150e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
32160e4ef248SJed Brown 
32170e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
32180e4ef248SJed Brown @*/
32190910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
32200e4ef248SJed Brown {
32210e4ef248SJed Brown   PetscErrorCode ierr;
32220e4ef248SJed Brown   Mat            Arhs,Brhs;
32230e4ef248SJed Brown   MatStructure   flg2;
32240e4ef248SJed Brown 
32250e4ef248SJed Brown   PetscFunctionBegin;
32260e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
32270910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
32280910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
32290e4ef248SJed Brown   PetscFunctionReturn(0);
32300e4ef248SJed Brown }
32310e4ef248SJed Brown 
32320e4ef248SJed Brown #undef __FUNCT__
32330e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
32340e4ef248SJed Brown /*@C
32350e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
32360e4ef248SJed Brown 
32370e4ef248SJed Brown    Collective on TS
32380e4ef248SJed Brown 
32390e4ef248SJed Brown    Input Arguments:
32400e4ef248SJed Brown +  ts - time stepping context
32410e4ef248SJed Brown .  t - time at which to evaluate
32420910c330SBarry Smith .  U - state at which to evaluate
32430e4ef248SJed Brown -  ctx - context
32440e4ef248SJed Brown 
32450e4ef248SJed Brown    Output Arguments:
32460e4ef248SJed Brown +  A - pointer to operator
32470e4ef248SJed Brown .  B - pointer to preconditioning matrix
32480e4ef248SJed Brown -  flg - matrix structure flag
32490e4ef248SJed Brown 
32500e4ef248SJed Brown    Level: intermediate
32510e4ef248SJed Brown 
32520e4ef248SJed Brown    Notes:
32530e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
32540e4ef248SJed Brown 
32550e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
32560e4ef248SJed Brown @*/
32570910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
32580e4ef248SJed Brown {
32590e4ef248SJed Brown   PetscFunctionBegin;
32600e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
32610e4ef248SJed Brown   PetscFunctionReturn(0);
32620e4ef248SJed Brown }
32630e4ef248SJed Brown 
32640026cea9SSean Farley #undef __FUNCT__
32650026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
32660026cea9SSean Farley /*@C
32670026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
32680026cea9SSean Farley 
32690026cea9SSean Farley    Collective on TS
32700026cea9SSean Farley 
32710026cea9SSean Farley    Input Arguments:
32720026cea9SSean Farley +  ts - time stepping context
32730026cea9SSean Farley .  t - time at which to evaluate
32740910c330SBarry Smith .  U - state at which to evaluate
32750910c330SBarry Smith .  Udot - time derivative of state vector
32760026cea9SSean Farley -  ctx - context
32770026cea9SSean Farley 
32780026cea9SSean Farley    Output Arguments:
32790026cea9SSean Farley .  F - left hand side
32800026cea9SSean Farley 
32810026cea9SSean Farley    Level: intermediate
32820026cea9SSean Farley 
32830026cea9SSean Farley    Notes:
32840910c330SBarry 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
32850026cea9SSean Farley    user is required to write their own TSComputeIFunction.
32860026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
32870026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
32880026cea9SSean Farley 
32890026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
32900026cea9SSean Farley @*/
32910910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
32920026cea9SSean Farley {
32930026cea9SSean Farley   PetscErrorCode ierr;
32940026cea9SSean Farley   Mat            A,B;
32950026cea9SSean Farley   MatStructure   flg2;
32960026cea9SSean Farley 
32970026cea9SSean Farley   PetscFunctionBegin;
32980026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
32990910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
33000910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
33010026cea9SSean Farley   PetscFunctionReturn(0);
33020026cea9SSean Farley }
33030026cea9SSean Farley 
33040026cea9SSean Farley #undef __FUNCT__
33050026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
33060026cea9SSean Farley /*@C
330724e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
33080026cea9SSean Farley 
33090026cea9SSean Farley    Collective on TS
33100026cea9SSean Farley 
33110026cea9SSean Farley    Input Arguments:
33120026cea9SSean Farley +  ts - time stepping context
33130026cea9SSean Farley .  t - time at which to evaluate
33140910c330SBarry Smith .  U - state at which to evaluate
33150910c330SBarry Smith .  Udot - time derivative of state vector
33160026cea9SSean Farley .  shift - shift to apply
33170026cea9SSean Farley -  ctx - context
33180026cea9SSean Farley 
33190026cea9SSean Farley    Output Arguments:
33200026cea9SSean Farley +  A - pointer to operator
33210026cea9SSean Farley .  B - pointer to preconditioning matrix
33220026cea9SSean Farley -  flg - matrix structure flag
33230026cea9SSean Farley 
33240026cea9SSean Farley    Level: intermediate
33250026cea9SSean Farley 
33260026cea9SSean Farley    Notes:
33270026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
33280026cea9SSean Farley 
33290026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
33300026cea9SSean Farley @*/
33310910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
33320026cea9SSean Farley {
33330026cea9SSean Farley   PetscFunctionBegin;
33340026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
33350026cea9SSean Farley   PetscFunctionReturn(0);
33360026cea9SSean Farley }
3337e817cc15SEmil Constantinescu #undef __FUNCT__
3338e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3339e817cc15SEmil Constantinescu /*@
3340e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3341e817cc15SEmil Constantinescu 
3342e817cc15SEmil Constantinescu    Not Collective
3343e817cc15SEmil Constantinescu 
3344e817cc15SEmil Constantinescu    Input Parameter:
3345e817cc15SEmil Constantinescu .  ts - the TS context
3346e817cc15SEmil Constantinescu 
3347e817cc15SEmil Constantinescu    Output Parameter:
3348e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3349e817cc15SEmil Constantinescu 
3350e817cc15SEmil Constantinescu    Level: beginner
3351e817cc15SEmil Constantinescu 
3352e817cc15SEmil Constantinescu .keywords: TS, equation type
3353e817cc15SEmil Constantinescu 
3354e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3355e817cc15SEmil Constantinescu @*/
3356e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3357e817cc15SEmil Constantinescu {
3358e817cc15SEmil Constantinescu   PetscFunctionBegin;
3359e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3360e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3361e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3362e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3363e817cc15SEmil Constantinescu }
3364e817cc15SEmil Constantinescu 
3365e817cc15SEmil Constantinescu #undef __FUNCT__
3366e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3367e817cc15SEmil Constantinescu /*@
3368e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3369e817cc15SEmil Constantinescu 
3370e817cc15SEmil Constantinescu    Not Collective
3371e817cc15SEmil Constantinescu 
3372e817cc15SEmil Constantinescu    Input Parameter:
3373e817cc15SEmil Constantinescu +  ts - the TS context
3374e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3375e817cc15SEmil Constantinescu 
3376e817cc15SEmil Constantinescu    Level: advanced
3377e817cc15SEmil Constantinescu 
3378e817cc15SEmil Constantinescu .keywords: TS, equation type
3379e817cc15SEmil Constantinescu 
3380e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3381e817cc15SEmil Constantinescu @*/
3382e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3383e817cc15SEmil Constantinescu {
3384e817cc15SEmil Constantinescu   PetscFunctionBegin;
3385e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3386e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3387e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3388e817cc15SEmil Constantinescu }
33890026cea9SSean Farley 
33904af1b03aSJed Brown #undef __FUNCT__
33914af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
33924af1b03aSJed Brown /*@
33934af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
33944af1b03aSJed Brown 
33954af1b03aSJed Brown    Not Collective
33964af1b03aSJed Brown 
33974af1b03aSJed Brown    Input Parameter:
33984af1b03aSJed Brown .  ts - the TS context
33994af1b03aSJed Brown 
34004af1b03aSJed Brown    Output Parameter:
34014af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
34024af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
34034af1b03aSJed Brown 
3404487e0bb9SJed Brown    Level: beginner
34054af1b03aSJed Brown 
3406cd652676SJed Brown    Notes:
3407cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
34084af1b03aSJed Brown 
34094af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
34104af1b03aSJed Brown 
34114af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
34124af1b03aSJed Brown @*/
34134af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
34144af1b03aSJed Brown {
34154af1b03aSJed Brown   PetscFunctionBegin;
34164af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34174af1b03aSJed Brown   PetscValidPointer(reason,2);
34184af1b03aSJed Brown   *reason = ts->reason;
34194af1b03aSJed Brown   PetscFunctionReturn(0);
34204af1b03aSJed Brown }
34214af1b03aSJed Brown 
3422fb1732b5SBarry Smith #undef __FUNCT__
3423d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3424d6ad946cSShri Abhyankar /*@
3425d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3426d6ad946cSShri Abhyankar 
3427d6ad946cSShri Abhyankar    Not Collective
3428d6ad946cSShri Abhyankar 
3429d6ad946cSShri Abhyankar    Input Parameter:
3430d6ad946cSShri Abhyankar +  ts - the TS context
3431d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3432d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3433d6ad946cSShri Abhyankar 
3434f5abba47SShri Abhyankar    Level: advanced
3435d6ad946cSShri Abhyankar 
3436d6ad946cSShri Abhyankar    Notes:
3437d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3438d6ad946cSShri Abhyankar 
3439d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3440d6ad946cSShri Abhyankar 
3441d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3442d6ad946cSShri Abhyankar @*/
3443d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3444d6ad946cSShri Abhyankar {
3445d6ad946cSShri Abhyankar   PetscFunctionBegin;
3446d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3447d6ad946cSShri Abhyankar   ts->reason = reason;
3448d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3449d6ad946cSShri Abhyankar }
3450d6ad946cSShri Abhyankar 
3451d6ad946cSShri Abhyankar #undef __FUNCT__
3452cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3453cc708dedSBarry Smith /*@
3454cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3455cc708dedSBarry Smith 
3456cc708dedSBarry Smith    Not Collective
3457cc708dedSBarry Smith 
3458cc708dedSBarry Smith    Input Parameter:
3459cc708dedSBarry Smith .  ts - the TS context
3460cc708dedSBarry Smith 
3461cc708dedSBarry Smith    Output Parameter:
3462cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3463cc708dedSBarry Smith 
3464487e0bb9SJed Brown    Level: beginner
3465cc708dedSBarry Smith 
3466cc708dedSBarry Smith    Notes:
3467cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3468cc708dedSBarry Smith 
3469cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3470cc708dedSBarry Smith 
3471cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3472cc708dedSBarry Smith @*/
3473cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3474cc708dedSBarry Smith {
3475cc708dedSBarry Smith   PetscFunctionBegin;
3476cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3477cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3478cc708dedSBarry Smith   *ftime = ts->solvetime;
3479cc708dedSBarry Smith   PetscFunctionReturn(0);
3480cc708dedSBarry Smith }
3481cc708dedSBarry Smith 
3482cc708dedSBarry Smith #undef __FUNCT__
34835ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
34849f67acb7SJed Brown /*@
34855ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
34869f67acb7SJed Brown    used by the time integrator.
34879f67acb7SJed Brown 
34889f67acb7SJed Brown    Not Collective
34899f67acb7SJed Brown 
34909f67acb7SJed Brown    Input Parameter:
34919f67acb7SJed Brown .  ts - TS context
34929f67acb7SJed Brown 
34939f67acb7SJed Brown    Output Parameter:
34949f67acb7SJed Brown .  nits - number of nonlinear iterations
34959f67acb7SJed Brown 
34969f67acb7SJed Brown    Notes:
34979f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
34989f67acb7SJed Brown 
34999f67acb7SJed Brown    Level: intermediate
35009f67acb7SJed Brown 
35019f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
35029f67acb7SJed Brown 
35035ef26d82SJed Brown .seealso:  TSGetKSPIterations()
35049f67acb7SJed Brown @*/
35055ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
35069f67acb7SJed Brown {
35079f67acb7SJed Brown   PetscFunctionBegin;
35089f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35099f67acb7SJed Brown   PetscValidIntPointer(nits,2);
35105ef26d82SJed Brown   *nits = ts->snes_its;
35119f67acb7SJed Brown   PetscFunctionReturn(0);
35129f67acb7SJed Brown }
35139f67acb7SJed Brown 
35149f67acb7SJed Brown #undef __FUNCT__
35155ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
35169f67acb7SJed Brown /*@
35175ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
35189f67acb7SJed Brown    used by the time integrator.
35199f67acb7SJed Brown 
35209f67acb7SJed Brown    Not Collective
35219f67acb7SJed Brown 
35229f67acb7SJed Brown    Input Parameter:
35239f67acb7SJed Brown .  ts - TS context
35249f67acb7SJed Brown 
35259f67acb7SJed Brown    Output Parameter:
35269f67acb7SJed Brown .  lits - number of linear iterations
35279f67acb7SJed Brown 
35289f67acb7SJed Brown    Notes:
35299f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
35309f67acb7SJed Brown 
35319f67acb7SJed Brown    Level: intermediate
35329f67acb7SJed Brown 
35339f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
35349f67acb7SJed Brown 
35355ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
35369f67acb7SJed Brown @*/
35375ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
35389f67acb7SJed Brown {
35399f67acb7SJed Brown   PetscFunctionBegin;
35409f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35419f67acb7SJed Brown   PetscValidIntPointer(lits,2);
35425ef26d82SJed Brown   *lits = ts->ksp_its;
35439f67acb7SJed Brown   PetscFunctionReturn(0);
35449f67acb7SJed Brown }
35459f67acb7SJed Brown 
35469f67acb7SJed Brown #undef __FUNCT__
3547cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3548cef5090cSJed Brown /*@
3549cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3550cef5090cSJed Brown 
3551cef5090cSJed Brown    Not Collective
3552cef5090cSJed Brown 
3553cef5090cSJed Brown    Input Parameter:
3554cef5090cSJed Brown .  ts - TS context
3555cef5090cSJed Brown 
3556cef5090cSJed Brown    Output Parameter:
3557cef5090cSJed Brown .  rejects - number of steps rejected
3558cef5090cSJed Brown 
3559cef5090cSJed Brown    Notes:
3560cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3561cef5090cSJed Brown 
3562cef5090cSJed Brown    Level: intermediate
3563cef5090cSJed Brown 
3564cef5090cSJed Brown .keywords: TS, get, number
3565cef5090cSJed Brown 
35665ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3567cef5090cSJed Brown @*/
3568cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3569cef5090cSJed Brown {
3570cef5090cSJed Brown   PetscFunctionBegin;
3571cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3572cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3573cef5090cSJed Brown   *rejects = ts->reject;
3574cef5090cSJed Brown   PetscFunctionReturn(0);
3575cef5090cSJed Brown }
3576cef5090cSJed Brown 
3577cef5090cSJed Brown #undef __FUNCT__
3578cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3579cef5090cSJed Brown /*@
3580cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3581cef5090cSJed Brown 
3582cef5090cSJed Brown    Not Collective
3583cef5090cSJed Brown 
3584cef5090cSJed Brown    Input Parameter:
3585cef5090cSJed Brown .  ts - TS context
3586cef5090cSJed Brown 
3587cef5090cSJed Brown    Output Parameter:
3588cef5090cSJed Brown .  fails - number of failed nonlinear solves
3589cef5090cSJed Brown 
3590cef5090cSJed Brown    Notes:
3591cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3592cef5090cSJed Brown 
3593cef5090cSJed Brown    Level: intermediate
3594cef5090cSJed Brown 
3595cef5090cSJed Brown .keywords: TS, get, number
3596cef5090cSJed Brown 
35975ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3598cef5090cSJed Brown @*/
3599cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3600cef5090cSJed Brown {
3601cef5090cSJed Brown   PetscFunctionBegin;
3602cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3603cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3604cef5090cSJed Brown   *fails = ts->num_snes_failures;
3605cef5090cSJed Brown   PetscFunctionReturn(0);
3606cef5090cSJed Brown }
3607cef5090cSJed Brown 
3608cef5090cSJed Brown #undef __FUNCT__
3609cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3610cef5090cSJed Brown /*@
3611cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3612cef5090cSJed Brown 
3613cef5090cSJed Brown    Not Collective
3614cef5090cSJed Brown 
3615cef5090cSJed Brown    Input Parameter:
3616cef5090cSJed Brown +  ts - TS context
3617cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3618cef5090cSJed Brown 
3619cef5090cSJed Brown    Notes:
3620cef5090cSJed Brown    The counter is reset to zero for each step
3621cef5090cSJed Brown 
3622cef5090cSJed Brown    Options Database Key:
3623cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3624cef5090cSJed Brown 
3625cef5090cSJed Brown    Level: intermediate
3626cef5090cSJed Brown 
3627cef5090cSJed Brown .keywords: TS, set, maximum, number
3628cef5090cSJed Brown 
36295ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3630cef5090cSJed Brown @*/
3631cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3632cef5090cSJed Brown {
3633cef5090cSJed Brown   PetscFunctionBegin;
3634cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3635cef5090cSJed Brown   ts->max_reject = rejects;
3636cef5090cSJed Brown   PetscFunctionReturn(0);
3637cef5090cSJed Brown }
3638cef5090cSJed Brown 
3639cef5090cSJed Brown #undef __FUNCT__
3640cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3641cef5090cSJed Brown /*@
3642cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3643cef5090cSJed Brown 
3644cef5090cSJed Brown    Not Collective
3645cef5090cSJed Brown 
3646cef5090cSJed Brown    Input Parameter:
3647cef5090cSJed Brown +  ts - TS context
3648cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3649cef5090cSJed Brown 
3650cef5090cSJed Brown    Notes:
3651cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3652cef5090cSJed Brown 
3653cef5090cSJed Brown    Options Database Key:
3654cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3655cef5090cSJed Brown 
3656cef5090cSJed Brown    Level: intermediate
3657cef5090cSJed Brown 
3658cef5090cSJed Brown .keywords: TS, set, maximum, number
3659cef5090cSJed Brown 
36605ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3661cef5090cSJed Brown @*/
3662cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3663cef5090cSJed Brown {
3664cef5090cSJed Brown   PetscFunctionBegin;
3665cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3666cef5090cSJed Brown   ts->max_snes_failures = fails;
3667cef5090cSJed Brown   PetscFunctionReturn(0);
3668cef5090cSJed Brown }
3669cef5090cSJed Brown 
3670cef5090cSJed Brown #undef __FUNCT__
3671cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3672cef5090cSJed Brown /*@
3673cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3674cef5090cSJed Brown 
3675cef5090cSJed Brown    Not Collective
3676cef5090cSJed Brown 
3677cef5090cSJed Brown    Input Parameter:
3678cef5090cSJed Brown +  ts - TS context
3679cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3680cef5090cSJed Brown 
3681cef5090cSJed Brown    Options Database Key:
3682cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3683cef5090cSJed Brown 
3684cef5090cSJed Brown    Level: intermediate
3685cef5090cSJed Brown 
3686cef5090cSJed Brown .keywords: TS, set, error
3687cef5090cSJed Brown 
36885ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3689cef5090cSJed Brown @*/
3690cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3691cef5090cSJed Brown {
3692cef5090cSJed Brown   PetscFunctionBegin;
3693cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3694cef5090cSJed Brown   ts->errorifstepfailed = err;
3695cef5090cSJed Brown   PetscFunctionReturn(0);
3696cef5090cSJed Brown }
3697cef5090cSJed Brown 
3698cef5090cSJed Brown #undef __FUNCT__
3699fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3700fb1732b5SBarry Smith /*@C
3701fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3702fb1732b5SBarry Smith 
3703fb1732b5SBarry Smith    Collective on TS
3704fb1732b5SBarry Smith 
3705fb1732b5SBarry Smith    Input Parameters:
3706fb1732b5SBarry Smith +  ts - the TS context
3707fb1732b5SBarry Smith .  step - current time-step
3708fb1732b5SBarry Smith .  ptime - current time
37090910c330SBarry Smith .  u - current state
3710fb1732b5SBarry Smith -  viewer - binary viewer
3711fb1732b5SBarry Smith 
3712fb1732b5SBarry Smith    Level: intermediate
3713fb1732b5SBarry Smith 
3714fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3715fb1732b5SBarry Smith 
3716fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3717fb1732b5SBarry Smith @*/
37180910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3719fb1732b5SBarry Smith {
3720fb1732b5SBarry Smith   PetscErrorCode ierr;
3721ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3722fb1732b5SBarry Smith 
3723fb1732b5SBarry Smith   PetscFunctionBegin;
37240910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3725ed81e22dSJed Brown   PetscFunctionReturn(0);
3726ed81e22dSJed Brown }
3727ed81e22dSJed Brown 
3728ed81e22dSJed Brown #undef __FUNCT__
3729ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3730ed81e22dSJed Brown /*@C
3731ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3732ed81e22dSJed Brown 
3733ed81e22dSJed Brown    Collective on TS
3734ed81e22dSJed Brown 
3735ed81e22dSJed Brown    Input Parameters:
3736ed81e22dSJed Brown +  ts - the TS context
3737ed81e22dSJed Brown .  step - current time-step
3738ed81e22dSJed Brown .  ptime - current time
37390910c330SBarry Smith .  u - current state
3740ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3741ed81e22dSJed Brown 
3742ed81e22dSJed Brown    Level: intermediate
3743ed81e22dSJed Brown 
3744ed81e22dSJed Brown    Notes:
3745ed81e22dSJed 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.
3746ed81e22dSJed Brown    These are named according to the file name template.
3747ed81e22dSJed Brown 
3748ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3749ed81e22dSJed Brown 
3750ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3751ed81e22dSJed Brown 
3752ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3753ed81e22dSJed Brown @*/
37540910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3755ed81e22dSJed Brown {
3756ed81e22dSJed Brown   PetscErrorCode ierr;
3757ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3758ed81e22dSJed Brown   PetscViewer    viewer;
3759ed81e22dSJed Brown 
3760ed81e22dSJed Brown   PetscFunctionBegin;
37618caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3762ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
37630910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3764ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3765ed81e22dSJed Brown   PetscFunctionReturn(0);
3766ed81e22dSJed Brown }
3767ed81e22dSJed Brown 
3768ed81e22dSJed Brown #undef __FUNCT__
3769ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3770ed81e22dSJed Brown /*@C
3771ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3772ed81e22dSJed Brown 
3773ed81e22dSJed Brown    Collective on TS
3774ed81e22dSJed Brown 
3775ed81e22dSJed Brown    Input Parameters:
3776ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3777ed81e22dSJed Brown 
3778ed81e22dSJed Brown    Level: intermediate
3779ed81e22dSJed Brown 
3780ed81e22dSJed Brown    Note:
3781ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3782ed81e22dSJed Brown 
3783ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3784ed81e22dSJed Brown 
3785ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3786ed81e22dSJed Brown @*/
3787ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3788ed81e22dSJed Brown {
3789ed81e22dSJed Brown   PetscErrorCode ierr;
3790ed81e22dSJed Brown 
3791ed81e22dSJed Brown   PetscFunctionBegin;
3792ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3793fb1732b5SBarry Smith   PetscFunctionReturn(0);
3794fb1732b5SBarry Smith }
3795fb1732b5SBarry Smith 
379684df9cb4SJed Brown #undef __FUNCT__
3797ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt"
379884df9cb4SJed Brown /*@
3799ad6bc421SBarry Smith    TSGetTSAdapt - Get the adaptive controller context for the current method
380084df9cb4SJed Brown 
3801ed81e22dSJed Brown    Collective on TS if controller has not been created yet
380284df9cb4SJed Brown 
380384df9cb4SJed Brown    Input Arguments:
3804ed81e22dSJed Brown .  ts - time stepping context
380584df9cb4SJed Brown 
380684df9cb4SJed Brown    Output Arguments:
3807ed81e22dSJed Brown .  adapt - adaptive controller
380884df9cb4SJed Brown 
380984df9cb4SJed Brown    Level: intermediate
381084df9cb4SJed Brown 
3811ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
381284df9cb4SJed Brown @*/
3813ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt)
381484df9cb4SJed Brown {
381584df9cb4SJed Brown   PetscErrorCode ierr;
381684df9cb4SJed Brown 
381784df9cb4SJed Brown   PetscFunctionBegin;
381884df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
381984df9cb4SJed Brown   PetscValidPointer(adapt,2);
382084df9cb4SJed Brown   if (!ts->adapt) {
382184df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
38221c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
38231c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
382484df9cb4SJed Brown   }
382584df9cb4SJed Brown   *adapt = ts->adapt;
382684df9cb4SJed Brown   PetscFunctionReturn(0);
382784df9cb4SJed Brown }
3828d6ebe24aSShri Abhyankar 
3829d6ebe24aSShri Abhyankar #undef __FUNCT__
38301c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
38311c3436cfSJed Brown /*@
38321c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
38331c3436cfSJed Brown 
38341c3436cfSJed Brown    Logically Collective
38351c3436cfSJed Brown 
38361c3436cfSJed Brown    Input Arguments:
38371c3436cfSJed Brown +  ts - time integration context
38381c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
38391c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
38401c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
38411c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
38421c3436cfSJed Brown 
38431c3436cfSJed Brown    Level: beginner
38441c3436cfSJed Brown 
3845c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
38461c3436cfSJed Brown @*/
38471c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
38481c3436cfSJed Brown {
38491c3436cfSJed Brown   PetscErrorCode ierr;
38501c3436cfSJed Brown 
38511c3436cfSJed Brown   PetscFunctionBegin;
3852c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
38531c3436cfSJed Brown   if (vatol) {
38541c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
38551c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
3856bbd56ea5SKarl Rupp 
38571c3436cfSJed Brown     ts->vatol = vatol;
38581c3436cfSJed Brown   }
3859c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
38601c3436cfSJed Brown   if (vrtol) {
38611c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
38621c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
3863bbd56ea5SKarl Rupp 
38641c3436cfSJed Brown     ts->vrtol = vrtol;
38651c3436cfSJed Brown   }
38661c3436cfSJed Brown   PetscFunctionReturn(0);
38671c3436cfSJed Brown }
38681c3436cfSJed Brown 
38691c3436cfSJed Brown #undef __FUNCT__
3870c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3871c5033834SJed Brown /*@
3872c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3873c5033834SJed Brown 
3874c5033834SJed Brown    Logically Collective
3875c5033834SJed Brown 
3876c5033834SJed Brown    Input Arguments:
3877c5033834SJed Brown .  ts - time integration context
3878c5033834SJed Brown 
3879c5033834SJed Brown    Output Arguments:
3880c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3881c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3882c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3883c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3884c5033834SJed Brown 
3885c5033834SJed Brown    Level: beginner
3886c5033834SJed Brown 
3887c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3888c5033834SJed Brown @*/
3889c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3890c5033834SJed Brown {
3891c5033834SJed Brown   PetscFunctionBegin;
3892c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3893c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3894c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3895c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3896c5033834SJed Brown   PetscFunctionReturn(0);
3897c5033834SJed Brown }
3898c5033834SJed Brown 
3899c5033834SJed Brown #undef __FUNCT__
39001c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
39011c3436cfSJed Brown /*@
39021c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
39031c3436cfSJed Brown 
39041c3436cfSJed Brown    Collective on TS
39051c3436cfSJed Brown 
39061c3436cfSJed Brown    Input Arguments:
39071c3436cfSJed Brown +  ts - time stepping context
39081c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
39091c3436cfSJed Brown 
39101c3436cfSJed Brown    Output Arguments:
39111c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
39121c3436cfSJed Brown 
39131c3436cfSJed Brown    Level: developer
39141c3436cfSJed Brown 
39151c3436cfSJed Brown .seealso: TSSetTolerances()
39161c3436cfSJed Brown @*/
39171c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
39181c3436cfSJed Brown {
39191c3436cfSJed Brown   PetscErrorCode    ierr;
39201c3436cfSJed Brown   PetscInt          i,n,N;
39210910c330SBarry Smith   const PetscScalar *u,*y;
39220910c330SBarry Smith   Vec               U;
39231c3436cfSJed Brown   PetscReal         sum,gsum;
39241c3436cfSJed Brown 
39251c3436cfSJed Brown   PetscFunctionBegin;
39261c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39271c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
39281c3436cfSJed Brown   PetscValidPointer(norm,3);
39290910c330SBarry Smith   U = ts->vec_sol;
39300910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
39310910c330SBarry Smith   if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
39321c3436cfSJed Brown 
39330910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
39340910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
39350910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
39361c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
39371c3436cfSJed Brown   sum  = 0.;
3938ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3939ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3940ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3941ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3942ceefc113SJed Brown     for (i=0; i<n; i++) {
39430910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39440910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3945ceefc113SJed Brown     }
3946ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3947ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3948ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3949ceefc113SJed Brown     const PetscScalar *atol;
3950ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3951ceefc113SJed Brown     for (i=0; i<n; i++) {
39520910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39530910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3954ceefc113SJed Brown     }
3955ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3956ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3957ceefc113SJed Brown     const PetscScalar *rtol;
3958ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3959ceefc113SJed Brown     for (i=0; i<n; i++) {
39600910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39610910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3962ceefc113SJed Brown     }
3963ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3964ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
39651c3436cfSJed Brown     for (i=0; i<n; i++) {
39660910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39670910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
39681c3436cfSJed Brown     }
3969ceefc113SJed Brown   }
39700910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
39711c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
39721c3436cfSJed Brown 
39731c3436cfSJed Brown   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
39741c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
39751c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
39761c3436cfSJed Brown   PetscFunctionReturn(0);
39771c3436cfSJed Brown }
39781c3436cfSJed Brown 
39791c3436cfSJed Brown #undef __FUNCT__
39808d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
39818d59e960SJed Brown /*@
39828d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
39838d59e960SJed Brown 
39848d59e960SJed Brown    Logically Collective on TS
39858d59e960SJed Brown 
39868d59e960SJed Brown    Input Arguments:
39878d59e960SJed Brown +  ts - time stepping context
39888d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
39898d59e960SJed Brown 
39908d59e960SJed Brown    Note:
39918d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
39928d59e960SJed Brown 
39938d59e960SJed Brown    Level: intermediate
39948d59e960SJed Brown 
39958d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
39968d59e960SJed Brown @*/
39978d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
39988d59e960SJed Brown {
39998d59e960SJed Brown   PetscFunctionBegin;
40008d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40018d59e960SJed Brown   ts->cfltime_local = cfltime;
40028d59e960SJed Brown   ts->cfltime       = -1.;
40038d59e960SJed Brown   PetscFunctionReturn(0);
40048d59e960SJed Brown }
40058d59e960SJed Brown 
40068d59e960SJed Brown #undef __FUNCT__
40078d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
40088d59e960SJed Brown /*@
40098d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
40108d59e960SJed Brown 
40118d59e960SJed Brown    Collective on TS
40128d59e960SJed Brown 
40138d59e960SJed Brown    Input Arguments:
40148d59e960SJed Brown .  ts - time stepping context
40158d59e960SJed Brown 
40168d59e960SJed Brown    Output Arguments:
40178d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
40188d59e960SJed Brown 
40198d59e960SJed Brown    Level: advanced
40208d59e960SJed Brown 
40218d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
40228d59e960SJed Brown @*/
40238d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
40248d59e960SJed Brown {
40258d59e960SJed Brown   PetscErrorCode ierr;
40268d59e960SJed Brown 
40278d59e960SJed Brown   PetscFunctionBegin;
40288d59e960SJed Brown   if (ts->cfltime < 0) {
40298d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
40308d59e960SJed Brown   }
40318d59e960SJed Brown   *cfltime = ts->cfltime;
40328d59e960SJed Brown   PetscFunctionReturn(0);
40338d59e960SJed Brown }
40348d59e960SJed Brown 
40358d59e960SJed Brown #undef __FUNCT__
4036d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4037d6ebe24aSShri Abhyankar /*@
4038d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4039d6ebe24aSShri Abhyankar 
4040d6ebe24aSShri Abhyankar    Input Parameters:
4041d6ebe24aSShri Abhyankar .  ts   - the TS context.
4042d6ebe24aSShri Abhyankar .  xl   - lower bound.
4043d6ebe24aSShri Abhyankar .  xu   - upper bound.
4044d6ebe24aSShri Abhyankar 
4045d6ebe24aSShri Abhyankar    Notes:
4046d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
404733c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4048d6ebe24aSShri Abhyankar 
40492bd2b0e6SSatish Balay    Level: advanced
40502bd2b0e6SSatish Balay 
4051d6ebe24aSShri Abhyankar @*/
4052d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4053d6ebe24aSShri Abhyankar {
4054d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4055d6ebe24aSShri Abhyankar   SNES           snes;
4056d6ebe24aSShri Abhyankar 
4057d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4058d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4059d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4060d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4061d6ebe24aSShri Abhyankar }
4062d6ebe24aSShri Abhyankar 
4063325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4064c6db04a5SJed Brown #include <mex.h>
4065325fc9f4SBarry Smith 
4066325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4067325fc9f4SBarry Smith 
4068325fc9f4SBarry Smith #undef __FUNCT__
4069325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4070325fc9f4SBarry Smith /*
4071325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4072325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4073325fc9f4SBarry Smith 
4074325fc9f4SBarry Smith    Collective on TS
4075325fc9f4SBarry Smith 
4076325fc9f4SBarry Smith    Input Parameters:
4077325fc9f4SBarry Smith +  snes - the TS context
40780910c330SBarry Smith -  u - input vector
4079325fc9f4SBarry Smith 
4080325fc9f4SBarry Smith    Output Parameter:
4081325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4082325fc9f4SBarry Smith 
4083325fc9f4SBarry Smith    Notes:
4084325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4085325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4086325fc9f4SBarry Smith    themselves.
4087325fc9f4SBarry Smith 
4088325fc9f4SBarry Smith    Level: developer
4089325fc9f4SBarry Smith 
4090325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4091325fc9f4SBarry Smith 
4092325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4093325fc9f4SBarry Smith */
40940910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4095325fc9f4SBarry Smith {
4096325fc9f4SBarry Smith   PetscErrorCode  ierr;
4097325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4098325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4099325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4100325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4101325fc9f4SBarry Smith 
4102325fc9f4SBarry Smith   PetscFunctionBegin;
4103325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
41040910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
41050910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4106325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
41070910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4108325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4109325fc9f4SBarry Smith 
4110325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
41110910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
41120910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
41130910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4114bbd56ea5SKarl Rupp 
4115325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4116325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4117325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4118325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4119325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4120325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4121325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4122325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4123325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4124325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4125325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4126325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4127325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4128325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4129325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4130325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4131325fc9f4SBarry Smith   PetscFunctionReturn(0);
4132325fc9f4SBarry Smith }
4133325fc9f4SBarry Smith 
4134325fc9f4SBarry Smith 
4135325fc9f4SBarry Smith #undef __FUNCT__
4136325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4137325fc9f4SBarry Smith /*
4138325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4139325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4140e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4141325fc9f4SBarry Smith 
4142325fc9f4SBarry Smith    Logically Collective on TS
4143325fc9f4SBarry Smith 
4144325fc9f4SBarry Smith    Input Parameters:
4145325fc9f4SBarry Smith +  ts - the TS context
4146325fc9f4SBarry Smith -  func - function evaluation routine
4147325fc9f4SBarry Smith 
4148325fc9f4SBarry Smith    Calling sequence of func:
41490910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4150325fc9f4SBarry Smith 
4151325fc9f4SBarry Smith    Level: beginner
4152325fc9f4SBarry Smith 
4153325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4154325fc9f4SBarry Smith 
4155325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4156325fc9f4SBarry Smith */
4157cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4158325fc9f4SBarry Smith {
4159325fc9f4SBarry Smith   PetscErrorCode  ierr;
4160325fc9f4SBarry Smith   TSMatlabContext *sctx;
4161325fc9f4SBarry Smith 
4162325fc9f4SBarry Smith   PetscFunctionBegin;
4163325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4164325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4165325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4166325fc9f4SBarry Smith   /*
4167325fc9f4SBarry Smith      This should work, but it doesn't
4168325fc9f4SBarry Smith   sctx->ctx = ctx;
4169325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4170325fc9f4SBarry Smith   */
4171325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4172bbd56ea5SKarl Rupp 
4173cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4174325fc9f4SBarry Smith   PetscFunctionReturn(0);
4175325fc9f4SBarry Smith }
4176325fc9f4SBarry Smith 
4177325fc9f4SBarry Smith #undef __FUNCT__
4178325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4179325fc9f4SBarry Smith /*
4180325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4181325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4182325fc9f4SBarry Smith 
4183325fc9f4SBarry Smith    Collective on TS
4184325fc9f4SBarry Smith 
4185325fc9f4SBarry Smith    Input Parameters:
4186cdcf91faSSean Farley +  ts - the TS context
41870910c330SBarry Smith .  u - input vector
4188325fc9f4SBarry Smith .  A, B - the matrices
4189325fc9f4SBarry Smith -  ctx - user context
4190325fc9f4SBarry Smith 
4191325fc9f4SBarry Smith    Output Parameter:
4192325fc9f4SBarry Smith .  flag - structure of the matrix
4193325fc9f4SBarry Smith 
4194325fc9f4SBarry Smith    Level: developer
4195325fc9f4SBarry Smith 
4196325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4197325fc9f4SBarry Smith 
4198325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4199325fc9f4SBarry Smith @*/
42000910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4201325fc9f4SBarry Smith {
4202325fc9f4SBarry Smith   PetscErrorCode  ierr;
4203325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4204325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4205325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4206325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4207325fc9f4SBarry Smith 
4208325fc9f4SBarry Smith   PetscFunctionBegin;
4209cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42100910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4211325fc9f4SBarry Smith 
42120910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4213325fc9f4SBarry Smith 
4214cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
42150910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
42160910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
42170910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
42180910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4219bbd56ea5SKarl Rupp 
4220325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4221325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4222325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4223325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4224325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4225325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4226325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4227325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4228325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4229325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4230325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4231325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4232325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4233325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4234325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4235325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4236325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4237325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4238325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4239325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4240325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4241325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4242325fc9f4SBarry Smith   PetscFunctionReturn(0);
4243325fc9f4SBarry Smith }
4244325fc9f4SBarry Smith 
4245325fc9f4SBarry Smith 
4246325fc9f4SBarry Smith #undef __FUNCT__
4247325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4248325fc9f4SBarry Smith /*
4249325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4250e3c5b3baSBarry 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
4251325fc9f4SBarry Smith 
4252325fc9f4SBarry Smith    Logically Collective on TS
4253325fc9f4SBarry Smith 
4254325fc9f4SBarry Smith    Input Parameters:
4255cdcf91faSSean Farley +  ts - the TS context
4256325fc9f4SBarry Smith .  A,B - Jacobian matrices
4257325fc9f4SBarry Smith .  func - function evaluation routine
4258325fc9f4SBarry Smith -  ctx - user context
4259325fc9f4SBarry Smith 
4260325fc9f4SBarry Smith    Calling sequence of func:
42610910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4262325fc9f4SBarry Smith 
4263325fc9f4SBarry Smith 
4264325fc9f4SBarry Smith    Level: developer
4265325fc9f4SBarry Smith 
4266325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4267325fc9f4SBarry Smith 
4268325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4269325fc9f4SBarry Smith */
4270cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4271325fc9f4SBarry Smith {
4272325fc9f4SBarry Smith   PetscErrorCode  ierr;
4273325fc9f4SBarry Smith   TSMatlabContext *sctx;
4274325fc9f4SBarry Smith 
4275325fc9f4SBarry Smith   PetscFunctionBegin;
4276325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4277325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4278325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4279325fc9f4SBarry Smith   /*
4280325fc9f4SBarry Smith      This should work, but it doesn't
4281325fc9f4SBarry Smith   sctx->ctx = ctx;
4282325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4283325fc9f4SBarry Smith   */
4284325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4285bbd56ea5SKarl Rupp 
4286cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4287325fc9f4SBarry Smith   PetscFunctionReturn(0);
4288325fc9f4SBarry Smith }
4289325fc9f4SBarry Smith 
4290b5b1a830SBarry Smith #undef __FUNCT__
4291b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4292b5b1a830SBarry Smith /*
4293b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4294b5b1a830SBarry Smith 
4295b5b1a830SBarry Smith    Collective on TS
4296b5b1a830SBarry Smith 
4297b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4298b5b1a830SBarry Smith @*/
42990910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4300b5b1a830SBarry Smith {
4301b5b1a830SBarry Smith   PetscErrorCode  ierr;
4302b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4303a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4304b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4305b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4306b5b1a830SBarry Smith 
4307b5b1a830SBarry Smith   PetscFunctionBegin;
4308cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43090910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4310b5b1a830SBarry Smith 
4311cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
43120910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4313bbd56ea5SKarl Rupp 
4314b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4315b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4316b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4317b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4318b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4319b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4320b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4321b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4322b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4323b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4324b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4325b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4326b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4327b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4328b5b1a830SBarry Smith   PetscFunctionReturn(0);
4329b5b1a830SBarry Smith }
4330b5b1a830SBarry Smith 
4331b5b1a830SBarry Smith 
4332b5b1a830SBarry Smith #undef __FUNCT__
4333b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4334b5b1a830SBarry Smith /*
4335b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4336b5b1a830SBarry Smith 
4337b5b1a830SBarry Smith    Level: developer
4338b5b1a830SBarry Smith 
4339b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4340b5b1a830SBarry Smith 
4341b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4342b5b1a830SBarry Smith */
4343cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4344b5b1a830SBarry Smith {
4345b5b1a830SBarry Smith   PetscErrorCode  ierr;
4346b5b1a830SBarry Smith   TSMatlabContext *sctx;
4347b5b1a830SBarry Smith 
4348b5b1a830SBarry Smith   PetscFunctionBegin;
4349b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4350b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4351b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4352b5b1a830SBarry Smith   /*
4353b5b1a830SBarry Smith      This should work, but it doesn't
4354b5b1a830SBarry Smith   sctx->ctx = ctx;
4355b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4356b5b1a830SBarry Smith   */
4357b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4358bbd56ea5SKarl Rupp 
4359cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
4360b5b1a830SBarry Smith   PetscFunctionReturn(0);
4361b5b1a830SBarry Smith }
4362325fc9f4SBarry Smith #endif
4363b3603a34SBarry Smith 
43640b039ecaSBarry Smith 
43650b039ecaSBarry Smith 
4366b3603a34SBarry Smith #undef __FUNCT__
43674f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4368b3603a34SBarry Smith /*@C
43694f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4370b3603a34SBarry Smith        in a time based line graph
4371b3603a34SBarry Smith 
4372b3603a34SBarry Smith    Collective on TS
4373b3603a34SBarry Smith 
4374b3603a34SBarry Smith    Input Parameters:
4375b3603a34SBarry Smith +  ts - the TS context
4376b3603a34SBarry Smith .  step - current time-step
4377b3603a34SBarry Smith .  ptime - current time
4378b3603a34SBarry Smith -  lg - a line graph object
4379b3603a34SBarry Smith 
4380b3603a34SBarry Smith    Level: intermediate
4381b3603a34SBarry Smith 
43820b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4383b3603a34SBarry Smith 
4384b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4385b3603a34SBarry Smith 
4386b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4387b3603a34SBarry Smith @*/
43880910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4389b3603a34SBarry Smith {
4390b3603a34SBarry Smith   PetscErrorCode    ierr;
43910b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4392b3603a34SBarry Smith   const PetscScalar *yy;
439358ff32f7SBarry Smith   PetscInt          dim;
4394b3603a34SBarry Smith 
4395b3603a34SBarry Smith   PetscFunctionBegin;
439658ff32f7SBarry Smith   if (!step) {
4397a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4398a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4399a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
44000910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
44010b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
44020b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
440358ff32f7SBarry Smith   }
44040910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4405e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4406e3efe391SJed Brown   {
4407e3efe391SJed Brown     PetscReal *yreal;
4408e3efe391SJed Brown     PetscInt  i,n;
44090910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4410e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4411e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
44120b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4413e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4414e3efe391SJed Brown   }
4415e3efe391SJed Brown #else
44160b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4417e3efe391SJed Brown #endif
44180910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
44193fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
44200b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
44213923b477SBarry Smith   }
4422b3603a34SBarry Smith   PetscFunctionReturn(0);
4423b3603a34SBarry Smith }
4424b3603a34SBarry Smith 
4425b3603a34SBarry Smith #undef __FUNCT__
44264f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4427ef20d060SBarry Smith /*@C
44284f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4429ef20d060SBarry Smith        in a time based line graph
4430ef20d060SBarry Smith 
4431ef20d060SBarry Smith    Collective on TS
4432ef20d060SBarry Smith 
4433ef20d060SBarry Smith    Input Parameters:
4434ef20d060SBarry Smith +  ts - the TS context
4435ef20d060SBarry Smith .  step - current time-step
4436ef20d060SBarry Smith .  ptime - current time
4437ef20d060SBarry Smith -  lg - a line graph object
4438ef20d060SBarry Smith 
4439ef20d060SBarry Smith    Level: intermediate
4440ef20d060SBarry Smith 
4441abd5a294SJed Brown    Notes:
4442abd5a294SJed Brown    Only for sequential solves.
4443abd5a294SJed Brown 
4444abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4445abd5a294SJed Brown 
4446abd5a294SJed Brown    Options Database Keys:
44474f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4448ef20d060SBarry Smith 
4449ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4450ef20d060SBarry Smith 
4451abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4452ef20d060SBarry Smith @*/
44530910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4454ef20d060SBarry Smith {
4455ef20d060SBarry Smith   PetscErrorCode    ierr;
44560b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4457ef20d060SBarry Smith   const PetscScalar *yy;
4458ef20d060SBarry Smith   Vec               y;
4459a9f9c1f6SBarry Smith   PetscInt          dim;
4460ef20d060SBarry Smith 
4461ef20d060SBarry Smith   PetscFunctionBegin;
4462a9f9c1f6SBarry Smith   if (!step) {
4463a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4464a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
44651ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
44660910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4467a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4468a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4469a9f9c1f6SBarry Smith   }
44700910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4471ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
44720910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4473ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4474e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4475e3efe391SJed Brown   {
4476e3efe391SJed Brown     PetscReal *yreal;
4477e3efe391SJed Brown     PetscInt  i,n;
4478e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4479e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4480e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
44810b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4482e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4483e3efe391SJed Brown   }
4484e3efe391SJed Brown #else
44850b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4486e3efe391SJed Brown #endif
4487ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4488ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
44893fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
44900b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
44913923b477SBarry Smith   }
4492ef20d060SBarry Smith   PetscFunctionReturn(0);
4493ef20d060SBarry Smith }
4494ef20d060SBarry Smith 
4495201da799SBarry Smith #undef __FUNCT__
4496201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4497201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4498201da799SBarry Smith {
4499201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4500201da799SBarry Smith   PetscReal      x   = ptime,y;
4501201da799SBarry Smith   PetscErrorCode ierr;
4502201da799SBarry Smith   PetscInt       its;
4503201da799SBarry Smith 
4504201da799SBarry Smith   PetscFunctionBegin;
4505201da799SBarry Smith   if (!n) {
4506201da799SBarry Smith     PetscDrawAxis axis;
4507bbd56ea5SKarl Rupp 
4508201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4509201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4510201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4511bbd56ea5SKarl Rupp 
4512201da799SBarry Smith     ctx->snes_its = 0;
4513201da799SBarry Smith   }
4514201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4515201da799SBarry Smith   y    = its - ctx->snes_its;
4516201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
45173fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4518201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4519201da799SBarry Smith   }
4520201da799SBarry Smith   ctx->snes_its = its;
4521201da799SBarry Smith   PetscFunctionReturn(0);
4522201da799SBarry Smith }
4523201da799SBarry Smith 
4524201da799SBarry Smith #undef __FUNCT__
4525201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4526201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4527201da799SBarry Smith {
4528201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4529201da799SBarry Smith   PetscReal      x   = ptime,y;
4530201da799SBarry Smith   PetscErrorCode ierr;
4531201da799SBarry Smith   PetscInt       its;
4532201da799SBarry Smith 
4533201da799SBarry Smith   PetscFunctionBegin;
4534201da799SBarry Smith   if (!n) {
4535201da799SBarry Smith     PetscDrawAxis axis;
4536bbd56ea5SKarl Rupp 
4537201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4538201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4539201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4540bbd56ea5SKarl Rupp 
4541201da799SBarry Smith     ctx->ksp_its = 0;
4542201da799SBarry Smith   }
4543201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4544201da799SBarry Smith   y    = its - ctx->ksp_its;
4545201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
454699fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4547201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4548201da799SBarry Smith   }
4549201da799SBarry Smith   ctx->ksp_its = its;
4550201da799SBarry Smith   PetscFunctionReturn(0);
4551201da799SBarry Smith }
4552f9c1d6abSBarry Smith 
4553f9c1d6abSBarry Smith #undef __FUNCT__
4554f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4555f9c1d6abSBarry Smith /*@
4556f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4557f9c1d6abSBarry Smith 
4558f9c1d6abSBarry Smith    Collective on TS and Vec
4559f9c1d6abSBarry Smith 
4560f9c1d6abSBarry Smith    Input Parameters:
4561f9c1d6abSBarry Smith +  ts - the TS context
4562f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4563f9c1d6abSBarry Smith 
4564f9c1d6abSBarry Smith    Output Parameters:
4565f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4566f9c1d6abSBarry Smith 
4567f9c1d6abSBarry Smith    Level: developer
4568f9c1d6abSBarry Smith 
4569f9c1d6abSBarry Smith .keywords: TS, compute
4570f9c1d6abSBarry Smith 
4571f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4572f9c1d6abSBarry Smith @*/
4573f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4574f9c1d6abSBarry Smith {
4575f9c1d6abSBarry Smith   PetscErrorCode ierr;
4576f9c1d6abSBarry Smith 
4577f9c1d6abSBarry Smith   PetscFunctionBegin;
4578f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4579f9c1d6abSBarry Smith   if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4580f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4581f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4582f9c1d6abSBarry Smith }
4583