xref: /petsc/src/ts/interface/ts.c (revision bbd56ea5790821d2a217d362e8e9710702952333)
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;
34*bbd56ea5SKarl Rupp   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
35*bbd56ea5SKarl 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
72de06c3feSJed Brown .  -ts_monitor_draw_solution_binary <filename> - Save each solution to a binary file
73e817cc15SEmil Constantinescu -  -ts_monitor_draw_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
74bdad233fSMatthew Knepley 
75bdad233fSMatthew Knepley    Level: beginner
76bdad233fSMatthew Knepley 
77bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
78bdad233fSMatthew Knepley 
79a313700dSBarry Smith .seealso: TSGetType()
80bdad233fSMatthew Knepley @*/
817087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
82bdad233fSMatthew Knepley {
83ace3abfcSBarry Smith   PetscBool              opt,flg;
84dfbe8321SBarry Smith   PetscErrorCode         ierr;
85649052a6SBarry Smith   PetscViewer            monviewer;
86eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
87089b2837SJed Brown   SNES                   snes;
881c3436cfSJed Brown   TSAdapt                adapt;
8931748224SBarry Smith   PetscReal              time_step;
9049354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
91d1212d36SBarry Smith   char                   dir[16];
92bdad233fSMatthew Knepley 
93bdad233fSMatthew Knepley   PetscFunctionBegin;
940700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
953194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
96a43b19c4SJed Brown   /* Handle TS type options */
97a43b19c4SJed Brown   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
98bdad233fSMatthew Knepley 
99bdad233fSMatthew Knepley   /* Handle generic TS options */
100bdad233fSMatthew Knepley   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
1013bca7d26SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
102d8cd7023SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr);
10331748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
10431748224SBarry Smith   if (flg) {
10531748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
10631748224SBarry Smith   }
10749354f04SShri 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);
10849354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
109cef5090cSJed 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);
110cef5090cSJed 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);
111cef5090cSJed Brown   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
1121c3436cfSJed Brown   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr);
1131c3436cfSJed Brown   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr);
114bdad233fSMatthew Knepley 
115bdad233fSMatthew Knepley   /* Monitor options */
116a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
117eabae89aSBarry Smith   if (flg) {
118649052a6SBarry Smith     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
119649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
120bdad233fSMatthew Knepley   }
1215180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1225180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1235180491cSLisandro Dalcin 
1244f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
125a7cc72afSBarry Smith   if (opt) {
1260b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1273923b477SBarry Smith     PetscInt       howoften = 1;
128a80ad3e0SBarry Smith 
1294f09c107SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
1301d1e9da1SBarry Smith     ierr = TSMonitorLGCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1314f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
132b3603a34SBarry Smith   }
1334f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
134b3603a34SBarry Smith   if (opt) {
1350b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1363923b477SBarry Smith     PetscInt       howoften = 1;
137b3603a34SBarry Smith 
1384f09c107SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
13922d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1404f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
141bdad233fSMatthew Knepley   }
1424f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
143ef20d060SBarry Smith   if (opt) {
1440b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1453923b477SBarry Smith     PetscInt       howoften = 1;
146ef20d060SBarry Smith 
1474f09c107SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
14822d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1494f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
150ef20d060SBarry Smith   }
151201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
152201da799SBarry Smith   if (opt) {
153201da799SBarry Smith     TSMonitorLGCtx ctx;
154201da799SBarry Smith     PetscInt       howoften = 1;
155201da799SBarry Smith 
156201da799SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
15722d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
158201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
159201da799SBarry Smith   }
160201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
161201da799SBarry Smith   if (opt) {
162201da799SBarry Smith     TSMonitorLGCtx ctx;
163201da799SBarry Smith     PetscInt       howoften = 1;
164201da799SBarry Smith 
165201da799SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
16622d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
167201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
168201da799SBarry Smith   }
1698189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1708189c53fSBarry Smith   if (opt) {
1718189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
1728189c53fSBarry Smith     PetscInt          howoften = 1;
1738189c53fSBarry Smith 
1748189c53fSBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
17522d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1768189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
1778189c53fSBarry Smith   }
178ef20d060SBarry Smith   opt  = PETSC_FALSE;
1790dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
180a7cc72afSBarry Smith   if (opt) {
18183a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
18283a4ac43SBarry Smith     PetscInt         howoften = 1;
183a80ad3e0SBarry Smith 
18483a4ac43SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
18522d28d08SBarry Smith     ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
18683a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
187bdad233fSMatthew Knepley   }
188fb1732b5SBarry Smith   opt  = PETSC_FALSE;
1890dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
1903a471f94SBarry Smith   if (opt) {
19183a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
19283a4ac43SBarry Smith     PetscInt         howoften = 1;
1933a471f94SBarry Smith 
19483a4ac43SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
19522d28d08SBarry Smith     ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
19683a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
1973a471f94SBarry Smith   }
1983a471f94SBarry Smith   opt  = PETSC_FALSE;
199de06c3feSJed Brown   ierr = PetscOptionsString("-ts_monitor_draw_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
200fb1732b5SBarry Smith   if (flg) {
201fb1732b5SBarry Smith     PetscViewer ctx;
202fb1732b5SBarry Smith     if (monfilename[0]) {
203fb1732b5SBarry Smith       ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
204fb1732b5SBarry Smith     } else {
205fb1732b5SBarry Smith       ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
206fb1732b5SBarry Smith     }
207fb1732b5SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
208fb1732b5SBarry Smith   }
209ed81e22dSJed Brown   opt  = PETSC_FALSE;
210de06c3feSJed Brown   ierr = PetscOptionsString("-ts_monitor_draw_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
211ed81e22dSJed Brown   if (flg) {
212ed81e22dSJed Brown     const char *ptr,*ptr2;
213ed81e22dSJed Brown     char       *filetemplate;
214de06c3feSJed Brown     if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts");
215ed81e22dSJed Brown     /* Do some cursory validation of the input. */
216ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
217de06c3feSJed Brown     if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts");
218ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
219ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
220de06c3feSJed Brown       if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_draw_solution_vtk, should look like filename-%%03D.vts");
221ed81e22dSJed Brown       if (ptr2) break;
222ed81e22dSJed Brown     }
223ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
224ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
225ed81e22dSJed Brown   }
226bdad233fSMatthew Knepley 
227d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
228d1212d36SBarry Smith   if (flg) {
229d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
230d1212d36SBarry Smith     int                 ray = 0;
231d1212d36SBarry Smith     DMDADirection       ddir;
232d1212d36SBarry Smith     DM                  da;
233d1212d36SBarry Smith     PetscMPIInt         rank;
234d1212d36SBarry Smith 
235d1212d36SBarry Smith     if (dir[1] != '=') SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
236d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
237d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
238d1212d36SBarry Smith     else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
239d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
240d1212d36SBarry Smith 
241d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
242d1212d36SBarry Smith     ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr);
243d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
244d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
245d1212d36SBarry Smith     ierr = MPI_Comm_rank(((PetscObject)ts)->comm,&rank);CHKERRQ(ierr);
246d1212d36SBarry Smith     if (!rank) {
247d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
248d1212d36SBarry Smith     }
249d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
250d1212d36SBarry Smith   }
251d1212d36SBarry Smith 
252ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&adapt);CHKERRQ(ierr);
2531c3436cfSJed Brown   ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2541c3436cfSJed Brown 
255d52bd9f3SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
256d52bd9f3SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
257d52bd9f3SBarry Smith 
258bdad233fSMatthew Knepley   /* Handle specific TS options */
259abc0a331SBarry Smith   if (ts->ops->setfromoptions) {
260bdad233fSMatthew Knepley     ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
261bdad233fSMatthew Knepley   }
2625d973c19SBarry Smith 
2635d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
2645d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
265bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
266bdad233fSMatthew Knepley   PetscFunctionReturn(0);
267bdad233fSMatthew Knepley }
268bdad233fSMatthew Knepley 
269bdad233fSMatthew Knepley #undef __FUNCT__
270cdcf91faSSean Farley #undef __FUNCT__
2714a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
272a7a1495cSBarry Smith /*@
2738c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
274a7a1495cSBarry Smith       set with TSSetRHSJacobian().
275a7a1495cSBarry Smith 
276a7a1495cSBarry Smith    Collective on TS and Vec
277a7a1495cSBarry Smith 
278a7a1495cSBarry Smith    Input Parameters:
279316643e7SJed Brown +  ts - the TS context
280a7a1495cSBarry Smith .  t - current timestep
2810910c330SBarry Smith -  U - input vector
282a7a1495cSBarry Smith 
283a7a1495cSBarry Smith    Output Parameters:
284a7a1495cSBarry Smith +  A - Jacobian matrix
285a7a1495cSBarry Smith .  B - optional preconditioning matrix
286a7a1495cSBarry Smith -  flag - flag indicating matrix structure
287a7a1495cSBarry Smith 
288a7a1495cSBarry Smith    Notes:
289a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
290a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
291a7a1495cSBarry Smith 
29294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
293a7a1495cSBarry Smith    flag parameter.
294a7a1495cSBarry Smith 
295a7a1495cSBarry Smith    Level: developer
296a7a1495cSBarry Smith 
297a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
298a7a1495cSBarry Smith 
29994b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
300a7a1495cSBarry Smith @*/
3010910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
302a7a1495cSBarry Smith {
303dfbe8321SBarry Smith   PetscErrorCode ierr;
3040910c330SBarry Smith   PetscInt       Ustate;
30524989b8cSPeter Brune   DM             dm;
306942e3340SBarry Smith   DMTS           tsdm;
30724989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
30824989b8cSPeter Brune   void           *ctx;
30924989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
310a7a1495cSBarry Smith 
311a7a1495cSBarry Smith   PetscFunctionBegin;
3120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3130910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3140910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
31524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
316942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
31724989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
31824989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
3190910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
3200910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
3210e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
3220e4ef248SJed Brown     PetscFunctionReturn(0);
323f8ede8e7SJed Brown   }
324d90be118SSean Farley 
32524989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
326d90be118SSean Farley 
32724989b8cSPeter Brune   if (rhsjacobianfunc) {
3280910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
329a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
330a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3310910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
332a7a1495cSBarry Smith     PetscStackPop;
3330910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
334a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3350700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3360700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
337ef66eb69SBarry Smith   } else {
338214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
339214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
340214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
341ef66eb69SBarry Smith   }
3420e4ef248SJed Brown   ts->rhsjacobian.time       = t;
3430910c330SBarry Smith   ts->rhsjacobian.X          = U;
3440910c330SBarry Smith   ierr                       = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3450e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
346a7a1495cSBarry Smith   PetscFunctionReturn(0);
347a7a1495cSBarry Smith }
348a7a1495cSBarry Smith 
3494a2ae208SSatish Balay #undef __FUNCT__
3504a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
351316643e7SJed Brown /*@
352d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
353d763cef2SBarry Smith 
354316643e7SJed Brown    Collective on TS and Vec
355316643e7SJed Brown 
356316643e7SJed Brown    Input Parameters:
357316643e7SJed Brown +  ts - the TS context
358316643e7SJed Brown .  t - current time
3590910c330SBarry Smith -  U - state vector
360316643e7SJed Brown 
361316643e7SJed Brown    Output Parameter:
362316643e7SJed Brown .  y - right hand side
363316643e7SJed Brown 
364316643e7SJed Brown    Note:
365316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
366316643e7SJed Brown    is used internally within the nonlinear solvers.
367316643e7SJed Brown 
368316643e7SJed Brown    Level: developer
369316643e7SJed Brown 
370316643e7SJed Brown .keywords: TS, compute
371316643e7SJed Brown 
372316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
373316643e7SJed Brown @*/
3740910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
375d763cef2SBarry Smith {
376dfbe8321SBarry Smith   PetscErrorCode ierr;
37724989b8cSPeter Brune   TSRHSFunction  rhsfunction;
37824989b8cSPeter Brune   TSIFunction    ifunction;
37924989b8cSPeter Brune   void           *ctx;
38024989b8cSPeter Brune   DM             dm;
38124989b8cSPeter Brune 
382d763cef2SBarry Smith   PetscFunctionBegin;
3830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3840910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3850700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
38624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
38724989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
38824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
389d763cef2SBarry Smith 
39024989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
391d763cef2SBarry Smith 
3920910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
39324989b8cSPeter Brune   if (rhsfunction) {
394d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
3950910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
396d763cef2SBarry Smith     PetscStackPop;
397214bc6a2SJed Brown   } else {
398214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
399b2cd27e8SJed Brown   }
40044a41b28SSean Farley 
4010910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
402d763cef2SBarry Smith   PetscFunctionReturn(0);
403d763cef2SBarry Smith }
404d763cef2SBarry Smith 
4054a2ae208SSatish Balay #undef __FUNCT__
406ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
407ef20d060SBarry Smith /*@
408ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
409ef20d060SBarry Smith 
410ef20d060SBarry Smith    Collective on TS and Vec
411ef20d060SBarry Smith 
412ef20d060SBarry Smith    Input Parameters:
413ef20d060SBarry Smith +  ts - the TS context
414ef20d060SBarry Smith -  t - current time
415ef20d060SBarry Smith 
416ef20d060SBarry Smith    Output Parameter:
4170910c330SBarry Smith .  U - the solution
418ef20d060SBarry Smith 
419ef20d060SBarry Smith    Note:
420ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
421ef20d060SBarry Smith    is used internally within the nonlinear solvers.
422ef20d060SBarry Smith 
423ef20d060SBarry Smith    Level: developer
424ef20d060SBarry Smith 
425ef20d060SBarry Smith .keywords: TS, compute
426ef20d060SBarry Smith 
427abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
428ef20d060SBarry Smith @*/
4290910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
430ef20d060SBarry Smith {
431ef20d060SBarry Smith   PetscErrorCode     ierr;
432ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
433ef20d060SBarry Smith   void               *ctx;
434ef20d060SBarry Smith   DM                 dm;
435ef20d060SBarry Smith 
436ef20d060SBarry Smith   PetscFunctionBegin;
437ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4380910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
439ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
440ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
441ef20d060SBarry Smith 
442ef20d060SBarry Smith   if (solutionfunction) {
443ef20d060SBarry Smith     PetscStackPush("TS user right-hand-side function");
4440910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
445ef20d060SBarry Smith     PetscStackPop;
446ef20d060SBarry Smith   }
447ef20d060SBarry Smith   PetscFunctionReturn(0);
448ef20d060SBarry Smith }
449ef20d060SBarry Smith 
450ef20d060SBarry Smith #undef __FUNCT__
451214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
452214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
453214bc6a2SJed Brown {
4542dd45cf8SJed Brown   Vec            F;
455214bc6a2SJed Brown   PetscErrorCode ierr;
456214bc6a2SJed Brown 
457214bc6a2SJed Brown   PetscFunctionBegin;
4580da9a4f0SJed Brown   *Frhs = PETSC_NULL;
4592dd45cf8SJed Brown   ierr  = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
460214bc6a2SJed Brown   if (!ts->Frhs) {
4612dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
462214bc6a2SJed Brown   }
463214bc6a2SJed Brown   *Frhs = ts->Frhs;
464214bc6a2SJed Brown   PetscFunctionReturn(0);
465214bc6a2SJed Brown }
466214bc6a2SJed Brown 
467214bc6a2SJed Brown #undef __FUNCT__
468214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
469214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
470214bc6a2SJed Brown {
471214bc6a2SJed Brown   Mat            A,B;
4722dd45cf8SJed Brown   PetscErrorCode ierr;
473214bc6a2SJed Brown 
474214bc6a2SJed Brown   PetscFunctionBegin;
475214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
476214bc6a2SJed Brown   if (Arhs) {
477214bc6a2SJed Brown     if (!ts->Arhs) {
478214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
479214bc6a2SJed Brown     }
480214bc6a2SJed Brown     *Arhs = ts->Arhs;
481214bc6a2SJed Brown   }
482214bc6a2SJed Brown   if (Brhs) {
483214bc6a2SJed Brown     if (!ts->Brhs) {
484214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
485214bc6a2SJed Brown     }
486214bc6a2SJed Brown     *Brhs = ts->Brhs;
487214bc6a2SJed Brown   }
488214bc6a2SJed Brown   PetscFunctionReturn(0);
489214bc6a2SJed Brown }
490214bc6a2SJed Brown 
491214bc6a2SJed Brown #undef __FUNCT__
492316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
493316643e7SJed Brown /*@
4940910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
495316643e7SJed Brown 
496316643e7SJed Brown    Collective on TS and Vec
497316643e7SJed Brown 
498316643e7SJed Brown    Input Parameters:
499316643e7SJed Brown +  ts - the TS context
500316643e7SJed Brown .  t - current time
5010910c330SBarry Smith .  U - state vector
5020910c330SBarry Smith .  Udot - time derivative of state vector
503214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
504316643e7SJed Brown 
505316643e7SJed Brown    Output Parameter:
506316643e7SJed Brown .  Y - right hand side
507316643e7SJed Brown 
508316643e7SJed Brown    Note:
509316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
510316643e7SJed Brown    is used internally within the nonlinear solvers.
511316643e7SJed Brown 
512316643e7SJed Brown    If the user did did not write their equations in implicit form, this
513316643e7SJed Brown    function recasts them in implicit form.
514316643e7SJed Brown 
515316643e7SJed Brown    Level: developer
516316643e7SJed Brown 
517316643e7SJed Brown .keywords: TS, compute
518316643e7SJed Brown 
519316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
520316643e7SJed Brown @*/
5210910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
522316643e7SJed Brown {
523316643e7SJed Brown   PetscErrorCode ierr;
52424989b8cSPeter Brune   TSIFunction    ifunction;
52524989b8cSPeter Brune   TSRHSFunction  rhsfunction;
52624989b8cSPeter Brune   void           *ctx;
52724989b8cSPeter Brune   DM             dm;
528316643e7SJed Brown 
529316643e7SJed Brown   PetscFunctionBegin;
5300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5310910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5320910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
5330700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
534316643e7SJed Brown 
53524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
53624989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
53724989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
53824989b8cSPeter Brune 
53924989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
540d90be118SSean Farley 
5410910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
54224989b8cSPeter Brune   if (ifunction) {
543316643e7SJed Brown     PetscStackPush("TS user implicit function");
5440910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
545316643e7SJed Brown     PetscStackPop;
546214bc6a2SJed Brown   }
547214bc6a2SJed Brown   if (imex) {
54824989b8cSPeter Brune     if (!ifunction) {
5490910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
5502dd45cf8SJed Brown     }
55124989b8cSPeter Brune   } else if (rhsfunction) {
55224989b8cSPeter Brune     if (ifunction) {
553214bc6a2SJed Brown       Vec Frhs;
554214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
5550910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
556214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
5572dd45cf8SJed Brown     } else {
5580910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
5590910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
560316643e7SJed Brown     }
5614a6899ffSJed Brown   }
5620910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
563316643e7SJed Brown   PetscFunctionReturn(0);
564316643e7SJed Brown }
565316643e7SJed Brown 
566316643e7SJed Brown #undef __FUNCT__
567316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
568316643e7SJed Brown /*@
569316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
570316643e7SJed Brown 
571316643e7SJed Brown    Collective on TS and Vec
572316643e7SJed Brown 
573316643e7SJed Brown    Input
574316643e7SJed Brown       Input Parameters:
575316643e7SJed Brown +  ts - the TS context
576316643e7SJed Brown .  t - current timestep
5770910c330SBarry Smith .  U - state vector
5780910c330SBarry Smith .  Udot - time derivative of state vector
579214bc6a2SJed Brown .  shift - shift to apply, see note below
580214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
581316643e7SJed Brown 
582316643e7SJed Brown    Output Parameters:
583316643e7SJed Brown +  A - Jacobian matrix
584316643e7SJed Brown .  B - optional preconditioning matrix
585316643e7SJed Brown -  flag - flag indicating matrix structure
586316643e7SJed Brown 
587316643e7SJed Brown    Notes:
5880910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
589316643e7SJed Brown 
5900910c330SBarry Smith    dF/dU + shift*dF/dUdot
591316643e7SJed Brown 
592316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
593316643e7SJed Brown    is used internally within the nonlinear solvers.
594316643e7SJed Brown 
595316643e7SJed Brown    Level: developer
596316643e7SJed Brown 
597316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
598316643e7SJed Brown 
599316643e7SJed Brown .seealso:  TSSetIJacobian()
60063495f91SJed Brown @*/
6010910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
602316643e7SJed Brown {
6030910c330SBarry Smith   PetscInt       Ustate, Udotstate;
604316643e7SJed Brown   PetscErrorCode ierr;
60524989b8cSPeter Brune   TSIJacobian    ijacobian;
60624989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
60724989b8cSPeter Brune   DM             dm;
60824989b8cSPeter Brune   void           *ctx;
609316643e7SJed Brown 
610316643e7SJed Brown   PetscFunctionBegin;
6110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6120910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6130910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
614316643e7SJed Brown   PetscValidPointer(A,6);
6150700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
616316643e7SJed Brown   PetscValidPointer(B,7);
6170700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
618316643e7SJed Brown   PetscValidPointer(flg,8);
61924989b8cSPeter Brune 
62024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
62124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
62224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
62324989b8cSPeter Brune 
6240910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
6250910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
6260910c330SBarry 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))) {
6270026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
6280026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
6290026cea9SSean Farley     PetscFunctionReturn(0);
6300026cea9SSean Farley   }
631316643e7SJed Brown 
63224989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
633316643e7SJed Brown 
6344e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
6350910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
63624989b8cSPeter Brune   if (ijacobian) {
6372dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
638316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
6390910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
640316643e7SJed Brown     PetscStackPop;
641214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
642214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
643214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
6444a6899ffSJed Brown   }
645214bc6a2SJed Brown   if (imex) {
646b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
647214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
6482dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
649214bc6a2SJed Brown       if (*A != *B) {
650214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
651214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
652214bc6a2SJed Brown       }
653214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
654214bc6a2SJed Brown     }
655214bc6a2SJed Brown   } else {
65624989b8cSPeter Brune     if (!ijacobian) {
6570910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr);
658214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
659214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
660316643e7SJed Brown       if (*A != *B) {
661316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
662316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
663316643e7SJed Brown       }
66424989b8cSPeter Brune     } else if (rhsjacobian) {
665214bc6a2SJed Brown       Mat          Arhs,Brhs;
666214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
667214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
6680910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
669214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
670214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
671214bc6a2SJed Brown       if (*A != *B) {
672214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
673214bc6a2SJed Brown       }
674214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
675214bc6a2SJed Brown     }
676316643e7SJed Brown   }
6770026cea9SSean Farley 
6780026cea9SSean Farley   ts->ijacobian.time = t;
6790910c330SBarry Smith   ts->ijacobian.X    = U;
6800910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
681*bbd56ea5SKarl Rupp 
6820910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
6830910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
684*bbd56ea5SKarl Rupp 
6850026cea9SSean Farley   ts->ijacobian.shift      = shift;
6860026cea9SSean Farley   ts->ijacobian.imex       = imex;
6870026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
688*bbd56ea5SKarl Rupp 
6890910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
690316643e7SJed Brown   PetscFunctionReturn(0);
691316643e7SJed Brown }
692316643e7SJed Brown 
693316643e7SJed Brown #undef __FUNCT__
6944a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
695d763cef2SBarry Smith /*@C
696d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
697b5abc632SBarry Smith     where U_t = G(t,u).
698d763cef2SBarry Smith 
6993f9fe445SBarry Smith     Logically Collective on TS
700d763cef2SBarry Smith 
701d763cef2SBarry Smith     Input Parameters:
702d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
703ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
704d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
705d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
706d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
707d763cef2SBarry Smith 
708d763cef2SBarry Smith     Calling sequence of func:
70987828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
710d763cef2SBarry Smith 
711d763cef2SBarry Smith +   t - current timestep
712d763cef2SBarry Smith .   u - input vector
713d763cef2SBarry Smith .   F - function vector
714d763cef2SBarry Smith -   ctx - [optional] user-defined function context
715d763cef2SBarry Smith 
716d763cef2SBarry Smith     Level: beginner
717d763cef2SBarry Smith 
718d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
719d763cef2SBarry Smith 
720d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
721d763cef2SBarry Smith @*/
722089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
723d763cef2SBarry Smith {
724089b2837SJed Brown   PetscErrorCode ierr;
725089b2837SJed Brown   SNES           snes;
726e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
72724989b8cSPeter Brune   DM             dm;
728d763cef2SBarry Smith 
729089b2837SJed Brown   PetscFunctionBegin;
7300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
731ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
73224989b8cSPeter Brune 
73324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
73424989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
735089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
736e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
737e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
738e856ceecSJed Brown     r    = ralloc;
739e856ceecSJed Brown   }
740089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
741e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
742d763cef2SBarry Smith   PetscFunctionReturn(0);
743d763cef2SBarry Smith }
744d763cef2SBarry Smith 
7454a2ae208SSatish Balay #undef __FUNCT__
746ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
747ef20d060SBarry Smith /*@C
748abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
749ef20d060SBarry Smith 
750ef20d060SBarry Smith     Logically Collective on TS
751ef20d060SBarry Smith 
752ef20d060SBarry Smith     Input Parameters:
753ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
754ef20d060SBarry Smith .   f - routine for evaluating the solution
755ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
756ef20d060SBarry Smith           function evaluation routine (may be PETSC_NULL)
757ef20d060SBarry Smith 
758ef20d060SBarry Smith     Calling sequence of func:
759ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
760ef20d060SBarry Smith 
761ef20d060SBarry Smith +   t - current timestep
762ef20d060SBarry Smith .   u - output vector
763ef20d060SBarry Smith -   ctx - [optional] user-defined function context
764ef20d060SBarry Smith 
765abd5a294SJed Brown     Notes:
766abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
767abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
768abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
769abd5a294SJed Brown 
7704f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
771abd5a294SJed Brown 
772ef20d060SBarry Smith     Level: beginner
773ef20d060SBarry Smith 
774ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
775ef20d060SBarry Smith 
776abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction()
777ef20d060SBarry Smith @*/
778ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
779ef20d060SBarry Smith {
780ef20d060SBarry Smith   PetscErrorCode ierr;
781ef20d060SBarry Smith   DM             dm;
782ef20d060SBarry Smith 
783ef20d060SBarry Smith   PetscFunctionBegin;
784ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
785ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
786ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
787ef20d060SBarry Smith   PetscFunctionReturn(0);
788ef20d060SBarry Smith }
789ef20d060SBarry Smith 
790ef20d060SBarry Smith #undef __FUNCT__
7914a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
792d763cef2SBarry Smith /*@C
793d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
794b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
795d763cef2SBarry Smith 
7963f9fe445SBarry Smith    Logically Collective on TS
797d763cef2SBarry Smith 
798d763cef2SBarry Smith    Input Parameters:
799d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
800d763cef2SBarry Smith .  A   - Jacobian matrix
801d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
802d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
803d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
804d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
805d763cef2SBarry Smith 
806d763cef2SBarry Smith    Calling sequence of func:
80787828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
808d763cef2SBarry Smith 
809d763cef2SBarry Smith +  t - current timestep
810d763cef2SBarry Smith .  u - input vector
811d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
812d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
813d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
81494b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
815d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
816d763cef2SBarry Smith 
817d763cef2SBarry Smith    Notes:
81894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
819d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
820d763cef2SBarry Smith 
821d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
822d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
82356335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
824d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
825d763cef2SBarry Smith    throughout the global iterations.
826d763cef2SBarry Smith 
827d763cef2SBarry Smith    Level: beginner
828d763cef2SBarry Smith 
829d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
830d763cef2SBarry Smith 
831ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
832d763cef2SBarry Smith 
833d763cef2SBarry Smith @*/
834089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
835d763cef2SBarry Smith {
836277b19d0SLisandro Dalcin   PetscErrorCode ierr;
837089b2837SJed Brown   SNES           snes;
83824989b8cSPeter Brune   DM             dm;
83924989b8cSPeter Brune   TSIJacobian    ijacobian;
840277b19d0SLisandro Dalcin 
841d763cef2SBarry Smith   PetscFunctionBegin;
8420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
843277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
844277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
845277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
846277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
847d763cef2SBarry Smith 
84824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
84924989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
85024989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
85124989b8cSPeter Brune 
852089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
8535f659677SPeter Brune   if (!ijacobian) {
854089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
8550e4ef248SJed Brown   }
8560e4ef248SJed Brown   if (A) {
8570e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8580e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
859*bbd56ea5SKarl Rupp 
8600e4ef248SJed Brown     ts->Arhs = A;
8610e4ef248SJed Brown   }
8620e4ef248SJed Brown   if (B) {
8630e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8640e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
865*bbd56ea5SKarl Rupp 
8660e4ef248SJed Brown     ts->Brhs = B;
8670e4ef248SJed Brown   }
868d763cef2SBarry Smith   PetscFunctionReturn(0);
869d763cef2SBarry Smith }
870d763cef2SBarry Smith 
871316643e7SJed Brown 
872316643e7SJed Brown #undef __FUNCT__
873316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
874316643e7SJed Brown /*@C
875b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
876316643e7SJed Brown 
8773f9fe445SBarry Smith    Logically Collective on TS
878316643e7SJed Brown 
879316643e7SJed Brown    Input Parameters:
880316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
881ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
882316643e7SJed Brown .  f   - the function evaluation routine
883316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
884316643e7SJed Brown 
885316643e7SJed Brown    Calling sequence of f:
886316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
887316643e7SJed Brown 
888316643e7SJed Brown +  t   - time at step/stage being solved
889316643e7SJed Brown .  u   - state vector
890316643e7SJed Brown .  u_t - time derivative of state vector
891316643e7SJed Brown .  F   - function vector
892316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
893316643e7SJed Brown 
894316643e7SJed Brown    Important:
895d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
896316643e7SJed Brown 
897316643e7SJed Brown    Level: beginner
898316643e7SJed Brown 
899316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
900316643e7SJed Brown 
901d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
902316643e7SJed Brown @*/
903089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
904316643e7SJed Brown {
905089b2837SJed Brown   PetscErrorCode ierr;
906089b2837SJed Brown   SNES           snes;
907e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
90824989b8cSPeter Brune   DM             dm;
909316643e7SJed Brown 
910316643e7SJed Brown   PetscFunctionBegin;
9110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
912ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
91324989b8cSPeter Brune 
91424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
91524989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
91624989b8cSPeter Brune 
917089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
918e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
919e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
920e856ceecSJed Brown     res  = resalloc;
921e856ceecSJed Brown   }
922089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
923e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
924089b2837SJed Brown   PetscFunctionReturn(0);
925089b2837SJed Brown }
926089b2837SJed Brown 
927089b2837SJed Brown #undef __FUNCT__
928089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
929089b2837SJed Brown /*@C
930089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
931089b2837SJed Brown 
932089b2837SJed Brown    Not Collective
933089b2837SJed Brown 
934089b2837SJed Brown    Input Parameter:
935089b2837SJed Brown .  ts - the TS context
936089b2837SJed Brown 
937089b2837SJed Brown    Output Parameter:
938089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
939089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
940089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
941089b2837SJed Brown 
942089b2837SJed Brown    Level: advanced
943089b2837SJed Brown 
944089b2837SJed Brown .keywords: TS, nonlinear, get, function
945089b2837SJed Brown 
946089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
947089b2837SJed Brown @*/
948089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
949089b2837SJed Brown {
950089b2837SJed Brown   PetscErrorCode ierr;
951089b2837SJed Brown   SNES           snes;
95224989b8cSPeter Brune   DM             dm;
953089b2837SJed Brown 
954089b2837SJed Brown   PetscFunctionBegin;
955089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
956089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
957089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
95824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
95924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
960089b2837SJed Brown   PetscFunctionReturn(0);
961089b2837SJed Brown }
962089b2837SJed Brown 
963089b2837SJed Brown #undef __FUNCT__
964089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
965089b2837SJed Brown /*@C
966089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
967089b2837SJed Brown 
968089b2837SJed Brown    Not Collective
969089b2837SJed Brown 
970089b2837SJed Brown    Input Parameter:
971089b2837SJed Brown .  ts - the TS context
972089b2837SJed Brown 
973089b2837SJed Brown    Output Parameter:
974089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
975089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
976089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
977089b2837SJed Brown 
978089b2837SJed Brown    Level: advanced
979089b2837SJed Brown 
980089b2837SJed Brown .keywords: TS, nonlinear, get, function
981089b2837SJed Brown 
982089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
983089b2837SJed Brown @*/
984089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
985089b2837SJed Brown {
986089b2837SJed Brown   PetscErrorCode ierr;
987089b2837SJed Brown   SNES           snes;
98824989b8cSPeter Brune   DM             dm;
989089b2837SJed Brown 
990089b2837SJed Brown   PetscFunctionBegin;
991089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
992089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
993089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
99424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
99524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
996316643e7SJed Brown   PetscFunctionReturn(0);
997316643e7SJed Brown }
998316643e7SJed Brown 
999316643e7SJed Brown #undef __FUNCT__
1000316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1001316643e7SJed Brown /*@C
1002a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1003a4f0a591SBarry Smith         you provided with TSSetIFunction().
1004316643e7SJed Brown 
10053f9fe445SBarry Smith    Logically Collective on TS
1006316643e7SJed Brown 
1007316643e7SJed Brown    Input Parameters:
1008316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1009316643e7SJed Brown .  A   - Jacobian matrix
1010316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
1011316643e7SJed Brown .  f   - the Jacobian evaluation routine
1012316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
1013316643e7SJed Brown 
1014316643e7SJed Brown    Calling sequence of f:
10151b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
1016316643e7SJed Brown 
1017316643e7SJed Brown +  t    - time at step/stage being solved
10181b4a444bSJed Brown .  U    - state vector
10191b4a444bSJed Brown .  U_t  - time derivative of state vector
1020316643e7SJed Brown .  a    - shift
1021b5abc632SBarry Smith .  A    - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1022316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
1023316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1024316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1025316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1026316643e7SJed Brown 
1027316643e7SJed Brown    Notes:
1028316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
1029316643e7SJed Brown 
1030a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1031b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1032a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1033a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1034a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1035a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1036a4f0a591SBarry Smith 
1037316643e7SJed Brown    Level: beginner
1038316643e7SJed Brown 
1039316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1040316643e7SJed Brown 
1041ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
1042316643e7SJed Brown 
1043316643e7SJed Brown @*/
10447087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1045316643e7SJed Brown {
1046316643e7SJed Brown   PetscErrorCode ierr;
1047089b2837SJed Brown   SNES           snes;
104824989b8cSPeter Brune   DM             dm;
1049316643e7SJed Brown 
1050316643e7SJed Brown   PetscFunctionBegin;
10510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10520700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
10530700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1054316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1055316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
105624989b8cSPeter Brune 
105724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
105824989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
105924989b8cSPeter Brune 
1060089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1061089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1062316643e7SJed Brown   PetscFunctionReturn(0);
1063316643e7SJed Brown }
1064316643e7SJed Brown 
10654a2ae208SSatish Balay #undef __FUNCT__
106655849f57SBarry Smith #define __FUNCT__ "TSLoad"
106755849f57SBarry Smith /*@C
106855849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
106955849f57SBarry Smith 
107055849f57SBarry Smith   Collective on PetscViewer
107155849f57SBarry Smith 
107255849f57SBarry Smith   Input Parameters:
107355849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
107455849f57SBarry Smith            some related function before a call to TSLoad().
107555849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
107655849f57SBarry Smith 
107755849f57SBarry Smith    Level: intermediate
107855849f57SBarry Smith 
107955849f57SBarry Smith   Notes:
108055849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
108155849f57SBarry Smith 
108255849f57SBarry Smith   Notes for advanced users:
108355849f57SBarry Smith   Most users should not need to know the details of the binary storage
108455849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
108555849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
108655849f57SBarry Smith   format is
108755849f57SBarry Smith .vb
108855849f57SBarry Smith      has not yet been determined
108955849f57SBarry Smith .ve
109055849f57SBarry Smith 
109155849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
109255849f57SBarry Smith @*/
1093f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
109455849f57SBarry Smith {
109555849f57SBarry Smith   PetscErrorCode ierr;
109655849f57SBarry Smith   PetscBool      isbinary;
109755849f57SBarry Smith   PetscInt       classid;
109855849f57SBarry Smith   char           type[256];
10992d53ad75SBarry Smith   DMTS           sdm;
1100ad6bc421SBarry Smith   DM             dm;
110155849f57SBarry Smith 
110255849f57SBarry Smith   PetscFunctionBegin;
1103f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
110455849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
110555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
110655849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
110755849f57SBarry Smith 
110855849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1109f2c2a1b9SBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_ARG_WRONG,"Not TS next in file");
111055849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1111f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1112f2c2a1b9SBarry Smith   if (ts->ops->load) {
1113f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1114f2c2a1b9SBarry Smith   }
1115ad6bc421SBarry Smith   ierr = DMCreate(((PetscObject)ts)->comm,&dm);CHKERRQ(ierr);
1116ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1117ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1118f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1119f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
11202d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
11212d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
112255849f57SBarry Smith   PetscFunctionReturn(0);
112355849f57SBarry Smith }
112455849f57SBarry Smith 
112555849f57SBarry Smith #undef __FUNCT__
11264a2ae208SSatish Balay #define __FUNCT__ "TSView"
11277e2c5f70SBarry Smith /*@C
1128d763cef2SBarry Smith     TSView - Prints the TS data structure.
1129d763cef2SBarry Smith 
11304c49b128SBarry Smith     Collective on TS
1131d763cef2SBarry Smith 
1132d763cef2SBarry Smith     Input Parameters:
1133d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1134d763cef2SBarry Smith -   viewer - visualization context
1135d763cef2SBarry Smith 
1136d763cef2SBarry Smith     Options Database Key:
1137d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1138d763cef2SBarry Smith 
1139d763cef2SBarry Smith     Notes:
1140d763cef2SBarry Smith     The available visualization contexts include
1141b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1142b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1143d763cef2SBarry Smith          output where only the first processor opens
1144d763cef2SBarry Smith          the file.  All other processors send their
1145d763cef2SBarry Smith          data to the first processor to print.
1146d763cef2SBarry Smith 
1147d763cef2SBarry Smith     The user can open an alternative visualization context with
1148b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1149d763cef2SBarry Smith 
1150d763cef2SBarry Smith     Level: beginner
1151d763cef2SBarry Smith 
1152d763cef2SBarry Smith .keywords: TS, timestep, view
1153d763cef2SBarry Smith 
1154b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1155d763cef2SBarry Smith @*/
11567087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1157d763cef2SBarry Smith {
1158dfbe8321SBarry Smith   PetscErrorCode ierr;
115919fd82e9SBarry Smith   TSType         type;
11602b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
11612d53ad75SBarry Smith   DMTS           sdm;
1162d763cef2SBarry Smith 
1163d763cef2SBarry Smith   PetscFunctionBegin;
11640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11653050cee2SBarry Smith   if (!viewer) {
11667adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
11673050cee2SBarry Smith   }
11680700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1169c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1170fd16b177SBarry Smith 
1171251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1172251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
117355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
11742b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
117532077d6dSBarry Smith   if (iascii) {
1176317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
117777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1178a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1179d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
11805ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1181c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1182d763cef2SBarry Smith     }
11835ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1184193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
11852d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
11862d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1187d52bd9f3SBarry Smith     if (ts->ops->view) {
1188d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1189d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1190d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1191d52bd9f3SBarry Smith     }
11920f5bd95cSBarry Smith   } else if (isstring) {
1193a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1194b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
119555849f57SBarry Smith   } else if (isbinary) {
119655849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
119755849f57SBarry Smith     MPI_Comm    comm;
119855849f57SBarry Smith     PetscMPIInt rank;
119955849f57SBarry Smith     char        type[256];
120055849f57SBarry Smith 
120155849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
120255849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
120355849f57SBarry Smith     if (!rank) {
120455849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
120555849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
120655849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
120755849f57SBarry Smith     }
120855849f57SBarry Smith     if (ts->ops->view) {
120955849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
121055849f57SBarry Smith     }
1211f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1212f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
12132d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12142d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
12152b0a91c0SBarry Smith   } else if (isdraw) {
12162b0a91c0SBarry Smith     PetscDraw draw;
12172b0a91c0SBarry Smith     char      str[36];
121889fd9fafSBarry Smith     PetscReal x,y,bottom,h;
12192b0a91c0SBarry Smith 
12202b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
12212b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
12222b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
12232b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
122489fd9fafSBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,PETSC_NULL,&h);CHKERRQ(ierr);
122589fd9fafSBarry Smith     bottom = y - h;
12262b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
12272b0a91c0SBarry Smith     if (ts->ops->view) {
12282b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
12292b0a91c0SBarry Smith     }
12302b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1231d763cef2SBarry Smith   }
1232b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1233251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1234b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1235d763cef2SBarry Smith   PetscFunctionReturn(0);
1236d763cef2SBarry Smith }
1237d763cef2SBarry Smith 
1238d763cef2SBarry Smith 
12394a2ae208SSatish Balay #undef __FUNCT__
12404a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1241b07ff414SBarry Smith /*@
1242d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1243d763cef2SBarry Smith    the timesteppers.
1244d763cef2SBarry Smith 
12453f9fe445SBarry Smith    Logically Collective on TS
1246d763cef2SBarry Smith 
1247d763cef2SBarry Smith    Input Parameters:
1248d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1249d763cef2SBarry Smith -  usrP - optional user context
1250d763cef2SBarry Smith 
1251d763cef2SBarry Smith    Level: intermediate
1252d763cef2SBarry Smith 
1253d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1254d763cef2SBarry Smith 
1255d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1256d763cef2SBarry Smith @*/
12577087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1258d763cef2SBarry Smith {
1259d763cef2SBarry Smith   PetscFunctionBegin;
12600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1261d763cef2SBarry Smith   ts->user = usrP;
1262d763cef2SBarry Smith   PetscFunctionReturn(0);
1263d763cef2SBarry Smith }
1264d763cef2SBarry Smith 
12654a2ae208SSatish Balay #undef __FUNCT__
12664a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1267b07ff414SBarry Smith /*@
1268d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1269d763cef2SBarry Smith     timestepper.
1270d763cef2SBarry Smith 
1271d763cef2SBarry Smith     Not Collective
1272d763cef2SBarry Smith 
1273d763cef2SBarry Smith     Input Parameter:
1274d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1275d763cef2SBarry Smith 
1276d763cef2SBarry Smith     Output Parameter:
1277d763cef2SBarry Smith .   usrP - user context
1278d763cef2SBarry Smith 
1279d763cef2SBarry Smith     Level: intermediate
1280d763cef2SBarry Smith 
1281d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1282d763cef2SBarry Smith 
1283d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1284d763cef2SBarry Smith @*/
1285e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1286d763cef2SBarry Smith {
1287d763cef2SBarry Smith   PetscFunctionBegin;
12880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1289e71120c6SJed Brown   *(void**)usrP = ts->user;
1290d763cef2SBarry Smith   PetscFunctionReturn(0);
1291d763cef2SBarry Smith }
1292d763cef2SBarry Smith 
12934a2ae208SSatish Balay #undef __FUNCT__
12944a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1295d763cef2SBarry Smith /*@
1296b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1297d763cef2SBarry Smith 
1298d763cef2SBarry Smith    Not Collective
1299d763cef2SBarry Smith 
1300d763cef2SBarry Smith    Input Parameter:
1301d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1302d763cef2SBarry Smith 
1303d763cef2SBarry Smith    Output Parameter:
1304b8123daeSJed Brown .  iter - number of steps completed so far
1305d763cef2SBarry Smith 
1306d763cef2SBarry Smith    Level: intermediate
1307d763cef2SBarry Smith 
1308d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1309b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1310d763cef2SBarry Smith @*/
13117087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1312d763cef2SBarry Smith {
1313d763cef2SBarry Smith   PetscFunctionBegin;
13140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13154482741eSBarry Smith   PetscValidIntPointer(iter,2);
1316d763cef2SBarry Smith   *iter = ts->steps;
1317d763cef2SBarry Smith   PetscFunctionReturn(0);
1318d763cef2SBarry Smith }
1319d763cef2SBarry Smith 
13204a2ae208SSatish Balay #undef __FUNCT__
13214a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1322d763cef2SBarry Smith /*@
1323d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1324d763cef2SBarry Smith    as well as the initial time.
1325d763cef2SBarry Smith 
13263f9fe445SBarry Smith    Logically Collective on TS
1327d763cef2SBarry Smith 
1328d763cef2SBarry Smith    Input Parameters:
1329d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1330d763cef2SBarry Smith .  initial_time - the initial time
1331d763cef2SBarry Smith -  time_step - the size of the timestep
1332d763cef2SBarry Smith 
1333d763cef2SBarry Smith    Level: intermediate
1334d763cef2SBarry Smith 
1335d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1336d763cef2SBarry Smith 
1337d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1338d763cef2SBarry Smith @*/
13397087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1340d763cef2SBarry Smith {
1341e144a568SJed Brown   PetscErrorCode ierr;
1342e144a568SJed Brown 
1343d763cef2SBarry Smith   PetscFunctionBegin;
13440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1345e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1346d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1347d763cef2SBarry Smith   PetscFunctionReturn(0);
1348d763cef2SBarry Smith }
1349d763cef2SBarry Smith 
13504a2ae208SSatish Balay #undef __FUNCT__
13514a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1352d763cef2SBarry Smith /*@
1353d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1354d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1355d763cef2SBarry Smith 
13563f9fe445SBarry Smith    Logically Collective on TS
1357d763cef2SBarry Smith 
1358d763cef2SBarry Smith    Input Parameters:
1359d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1360d763cef2SBarry Smith -  time_step - the size of the timestep
1361d763cef2SBarry Smith 
1362d763cef2SBarry Smith    Level: intermediate
1363d763cef2SBarry Smith 
1364d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1365d763cef2SBarry Smith 
1366d763cef2SBarry Smith .keywords: TS, set, timestep
1367d763cef2SBarry Smith @*/
13687087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1369d763cef2SBarry Smith {
1370d763cef2SBarry Smith   PetscFunctionBegin;
13710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1372c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1373d763cef2SBarry Smith   ts->time_step      = time_step;
137431748224SBarry Smith   ts->time_step_orig = time_step;
1375d763cef2SBarry Smith   PetscFunctionReturn(0);
1376d763cef2SBarry Smith }
1377d763cef2SBarry Smith 
13784a2ae208SSatish Balay #undef __FUNCT__
1379a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1380a43b19c4SJed Brown /*@
138149354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
138249354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
138349354f04SShri Abhyankar      or just return at the final time TS computed.
1384a43b19c4SJed Brown 
1385a43b19c4SJed Brown   Logically Collective on TS
1386a43b19c4SJed Brown 
1387a43b19c4SJed Brown    Input Parameter:
1388a43b19c4SJed Brown +   ts - the time-step context
138949354f04SShri Abhyankar -   eftopt - exact final time option
1390a43b19c4SJed Brown 
1391a43b19c4SJed Brown    Level: beginner
1392a43b19c4SJed Brown 
1393a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1394a43b19c4SJed Brown @*/
139549354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1396a43b19c4SJed Brown {
1397a43b19c4SJed Brown   PetscFunctionBegin;
1398a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
139949354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
140049354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1401a43b19c4SJed Brown   PetscFunctionReturn(0);
1402a43b19c4SJed Brown }
1403a43b19c4SJed Brown 
1404a43b19c4SJed Brown #undef __FUNCT__
14054a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1406d763cef2SBarry Smith /*@
1407d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1408d763cef2SBarry Smith 
1409d763cef2SBarry Smith    Not Collective
1410d763cef2SBarry Smith 
1411d763cef2SBarry Smith    Input Parameter:
1412d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1413d763cef2SBarry Smith 
1414d763cef2SBarry Smith    Output Parameter:
1415d763cef2SBarry Smith .  dt - the current timestep size
1416d763cef2SBarry Smith 
1417d763cef2SBarry Smith    Level: intermediate
1418d763cef2SBarry Smith 
1419d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1420d763cef2SBarry Smith 
1421d763cef2SBarry Smith .keywords: TS, get, timestep
1422d763cef2SBarry Smith @*/
14237087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1424d763cef2SBarry Smith {
1425d763cef2SBarry Smith   PetscFunctionBegin;
14260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1427f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1428d763cef2SBarry Smith   *dt = ts->time_step;
1429d763cef2SBarry Smith   PetscFunctionReturn(0);
1430d763cef2SBarry Smith }
1431d763cef2SBarry Smith 
14324a2ae208SSatish Balay #undef __FUNCT__
14334a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1434d8e5e3e6SSatish Balay /*@
1435d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1436d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1437d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1438d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1439d763cef2SBarry Smith 
1440d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1441d763cef2SBarry Smith 
1442d763cef2SBarry Smith    Input Parameter:
1443d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1444d763cef2SBarry Smith 
1445d763cef2SBarry Smith    Output Parameter:
1446d763cef2SBarry Smith .  v - the vector containing the solution
1447d763cef2SBarry Smith 
1448d763cef2SBarry Smith    Level: intermediate
1449d763cef2SBarry Smith 
1450d763cef2SBarry Smith .seealso: TSGetTimeStep()
1451d763cef2SBarry Smith 
1452d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1453d763cef2SBarry Smith @*/
14547087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1455d763cef2SBarry Smith {
1456d763cef2SBarry Smith   PetscFunctionBegin;
14570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14584482741eSBarry Smith   PetscValidPointer(v,2);
14598737fe31SLisandro Dalcin   *v = ts->vec_sol;
1460d763cef2SBarry Smith   PetscFunctionReturn(0);
1461d763cef2SBarry Smith }
1462d763cef2SBarry Smith 
1463bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
14644a2ae208SSatish Balay #undef __FUNCT__
1465bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1466d8e5e3e6SSatish Balay /*@
1467bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1468d763cef2SBarry Smith 
1469bdad233fSMatthew Knepley   Not collective
1470d763cef2SBarry Smith 
1471bdad233fSMatthew Knepley   Input Parameters:
1472bdad233fSMatthew Knepley + ts   - The TS
1473bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1474d763cef2SBarry Smith .vb
14750910c330SBarry Smith          U_t - A U = 0      (linear)
14760910c330SBarry Smith          U_t - A(t) U = 0   (linear)
14770910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1478d763cef2SBarry Smith .ve
1479d763cef2SBarry Smith 
1480d763cef2SBarry Smith    Level: beginner
1481d763cef2SBarry Smith 
1482bdad233fSMatthew Knepley .keywords: TS, problem type
1483bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1484d763cef2SBarry Smith @*/
14857087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1486a7cc72afSBarry Smith {
14879e2a6581SJed Brown   PetscErrorCode ierr;
14889e2a6581SJed Brown 
1489d763cef2SBarry Smith   PetscFunctionBegin;
14900700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1491bdad233fSMatthew Knepley   ts->problem_type = type;
14929e2a6581SJed Brown   if (type == TS_LINEAR) {
14939e2a6581SJed Brown     SNES snes;
14949e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
14959e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
14969e2a6581SJed Brown   }
1497d763cef2SBarry Smith   PetscFunctionReturn(0);
1498d763cef2SBarry Smith }
1499d763cef2SBarry Smith 
1500bdad233fSMatthew Knepley #undef __FUNCT__
1501bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1502bdad233fSMatthew Knepley /*@C
1503bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1504bdad233fSMatthew Knepley 
1505bdad233fSMatthew Knepley   Not collective
1506bdad233fSMatthew Knepley 
1507bdad233fSMatthew Knepley   Input Parameter:
1508bdad233fSMatthew Knepley . ts   - The TS
1509bdad233fSMatthew Knepley 
1510bdad233fSMatthew Knepley   Output Parameter:
1511bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1512bdad233fSMatthew Knepley .vb
1513089b2837SJed Brown          M U_t = A U
1514089b2837SJed Brown          M(t) U_t = A(t) U
1515b5abc632SBarry Smith          F(t,U,U_t)
1516bdad233fSMatthew Knepley .ve
1517bdad233fSMatthew Knepley 
1518bdad233fSMatthew Knepley    Level: beginner
1519bdad233fSMatthew Knepley 
1520bdad233fSMatthew Knepley .keywords: TS, problem type
1521bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1522bdad233fSMatthew Knepley @*/
15237087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1524a7cc72afSBarry Smith {
1525bdad233fSMatthew Knepley   PetscFunctionBegin;
15260700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
15274482741eSBarry Smith   PetscValidIntPointer(type,2);
1528bdad233fSMatthew Knepley   *type = ts->problem_type;
1529bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1530bdad233fSMatthew Knepley }
1531d763cef2SBarry Smith 
15324a2ae208SSatish Balay #undef __FUNCT__
15334a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1534d763cef2SBarry Smith /*@
1535d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1536d763cef2SBarry Smith    of a timestepper.
1537d763cef2SBarry Smith 
1538d763cef2SBarry Smith    Collective on TS
1539d763cef2SBarry Smith 
1540d763cef2SBarry Smith    Input Parameter:
1541d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith    Notes:
1544d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1545d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1546d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1547d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1548d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1549d763cef2SBarry Smith 
1550d763cef2SBarry Smith    Level: advanced
1551d763cef2SBarry Smith 
1552d763cef2SBarry Smith .keywords: TS, timestep, setup
1553d763cef2SBarry Smith 
1554d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1555d763cef2SBarry Smith @*/
15567087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1557d763cef2SBarry Smith {
1558dfbe8321SBarry Smith   PetscErrorCode ierr;
15596c6b9e74SPeter Brune   DM             dm;
15606c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
15616c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
15626c6b9e74SPeter Brune   TSIJacobian    ijac;
15636c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1564d763cef2SBarry Smith 
1565d763cef2SBarry Smith   PetscFunctionBegin;
15660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1567277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1568277b19d0SLisandro Dalcin 
15697adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
15709596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1571d763cef2SBarry Smith   }
1572277b19d0SLisandro Dalcin 
1573277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1574277b19d0SLisandro Dalcin 
1575ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr);
15761c3436cfSJed Brown 
1577277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1578000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1579277b19d0SLisandro Dalcin   }
1580277b19d0SLisandro Dalcin 
15816c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
15826c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
15836c6b9e74SPeter Brune    */
15846c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
15856c6b9e74SPeter Brune   ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr);
15866c6b9e74SPeter Brune   if (!func) {
15876c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
15886c6b9e74SPeter Brune   }
15896c6b9e74SPeter 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.
15906c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
15916c6b9e74SPeter Brune    */
15926c6b9e74SPeter Brune   ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr);
15936c6b9e74SPeter Brune   ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr);
15946c6b9e74SPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr);
15956c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
15966c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
15976c6b9e74SPeter Brune   }
1598277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1599277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1600277b19d0SLisandro Dalcin }
1601277b19d0SLisandro Dalcin 
1602277b19d0SLisandro Dalcin #undef __FUNCT__
1603277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1604277b19d0SLisandro Dalcin /*@
1605277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1606277b19d0SLisandro Dalcin 
1607277b19d0SLisandro Dalcin    Collective on TS
1608277b19d0SLisandro Dalcin 
1609277b19d0SLisandro Dalcin    Input Parameter:
1610277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1611277b19d0SLisandro Dalcin 
1612277b19d0SLisandro Dalcin    Level: beginner
1613277b19d0SLisandro Dalcin 
1614277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1615277b19d0SLisandro Dalcin 
1616277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1617277b19d0SLisandro Dalcin @*/
1618277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1619277b19d0SLisandro Dalcin {
1620277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1621277b19d0SLisandro Dalcin 
1622277b19d0SLisandro Dalcin   PetscFunctionBegin;
1623277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1624277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1625277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1626277b19d0SLisandro Dalcin   }
1627277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1628*bbd56ea5SKarl Rupp 
16294e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
16304e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1631214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
16326bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1633e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1634e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
163538637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1636*bbd56ea5SKarl Rupp 
1637277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1638d763cef2SBarry Smith   PetscFunctionReturn(0);
1639d763cef2SBarry Smith }
1640d763cef2SBarry Smith 
16414a2ae208SSatish Balay #undef __FUNCT__
16424a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1643d8e5e3e6SSatish Balay /*@
1644d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1645d763cef2SBarry Smith    with TSCreate().
1646d763cef2SBarry Smith 
1647d763cef2SBarry Smith    Collective on TS
1648d763cef2SBarry Smith 
1649d763cef2SBarry Smith    Input Parameter:
1650d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1651d763cef2SBarry Smith 
1652d763cef2SBarry Smith    Level: beginner
1653d763cef2SBarry Smith 
1654d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1655d763cef2SBarry Smith 
1656d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1657d763cef2SBarry Smith @*/
16586bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1659d763cef2SBarry Smith {
16606849ba73SBarry Smith   PetscErrorCode ierr;
1661d763cef2SBarry Smith 
1662d763cef2SBarry Smith   PetscFunctionBegin;
16636bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
16646bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
16656bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1666d763cef2SBarry Smith 
16676bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1668277b19d0SLisandro Dalcin 
1669be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
16706bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
16716bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
16726d4c513bSLisandro Dalcin 
167384df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
16746bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
16756bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
16766bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
16776d4c513bSLisandro Dalcin 
1678a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1679d763cef2SBarry Smith   PetscFunctionReturn(0);
1680d763cef2SBarry Smith }
1681d763cef2SBarry Smith 
16824a2ae208SSatish Balay #undef __FUNCT__
16834a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1684d8e5e3e6SSatish Balay /*@
1685d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1686d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1687d763cef2SBarry Smith 
1688d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1689d763cef2SBarry Smith 
1690d763cef2SBarry Smith    Input Parameter:
1691d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1692d763cef2SBarry Smith 
1693d763cef2SBarry Smith    Output Parameter:
1694d763cef2SBarry Smith .  snes - the nonlinear solver context
1695d763cef2SBarry Smith 
1696d763cef2SBarry Smith    Notes:
1697d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1698d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
169994b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1700d763cef2SBarry Smith 
1701d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1702d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1703d763cef2SBarry Smith 
1704d763cef2SBarry Smith    Level: beginner
1705d763cef2SBarry Smith 
1706d763cef2SBarry Smith .keywords: timestep, get, SNES
1707d763cef2SBarry Smith @*/
17087087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1709d763cef2SBarry Smith {
1710d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1711d372ba47SLisandro Dalcin 
1712d763cef2SBarry Smith   PetscFunctionBegin;
17130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17144482741eSBarry Smith   PetscValidPointer(snes,2);
1715d372ba47SLisandro Dalcin   if (!ts->snes) {
1716d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
17176c6b9e74SPeter Brune     ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1718d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1719d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1720496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
17219e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
17229e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
17239e2a6581SJed Brown     }
1724d372ba47SLisandro Dalcin   }
1725d763cef2SBarry Smith   *snes = ts->snes;
1726d763cef2SBarry Smith   PetscFunctionReturn(0);
1727d763cef2SBarry Smith }
1728d763cef2SBarry Smith 
17294a2ae208SSatish Balay #undef __FUNCT__
1730deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1731deb2cd25SJed Brown /*@
1732deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1733deb2cd25SJed Brown 
1734deb2cd25SJed Brown    Collective
1735deb2cd25SJed Brown 
1736deb2cd25SJed Brown    Input Parameter:
1737deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1738deb2cd25SJed Brown -  snes - the nonlinear solver context
1739deb2cd25SJed Brown 
1740deb2cd25SJed Brown    Notes:
1741deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1742deb2cd25SJed Brown 
1743deb2cd25SJed Brown    Level: developer
1744deb2cd25SJed Brown 
1745deb2cd25SJed Brown .keywords: timestep, set, SNES
1746deb2cd25SJed Brown @*/
1747deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1748deb2cd25SJed Brown {
1749deb2cd25SJed Brown   PetscErrorCode ierr;
1750740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1751deb2cd25SJed Brown 
1752deb2cd25SJed Brown   PetscFunctionBegin;
1753deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1754deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1755deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1756deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1757*bbd56ea5SKarl Rupp 
1758deb2cd25SJed Brown   ts->snes = snes;
1759*bbd56ea5SKarl Rupp 
1760740132f1SEmil Constantinescu   ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1761740132f1SEmil Constantinescu   ierr = SNESGetJacobian(ts->snes,PETSC_NULL,PETSC_NULL,&func,PETSC_NULL);CHKERRQ(ierr);
1762740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
1763740132f1SEmil Constantinescu     ierr = SNESSetJacobian(ts->snes,PETSC_NULL,PETSC_NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1764740132f1SEmil Constantinescu   }
1765deb2cd25SJed Brown   PetscFunctionReturn(0);
1766deb2cd25SJed Brown }
1767deb2cd25SJed Brown 
1768deb2cd25SJed Brown #undef __FUNCT__
176994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1770d8e5e3e6SSatish Balay /*@
177194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1772d763cef2SBarry Smith    a TS (timestepper) context.
1773d763cef2SBarry Smith 
177494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1775d763cef2SBarry Smith 
1776d763cef2SBarry Smith    Input Parameter:
1777d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1778d763cef2SBarry Smith 
1779d763cef2SBarry Smith    Output Parameter:
178094b7f48cSBarry Smith .  ksp - the nonlinear solver context
1781d763cef2SBarry Smith 
1782d763cef2SBarry Smith    Notes:
178394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1784d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1785d763cef2SBarry Smith    KSP and PC contexts as well.
1786d763cef2SBarry Smith 
178794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
178894b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1789d763cef2SBarry Smith 
1790d763cef2SBarry Smith    Level: beginner
1791d763cef2SBarry Smith 
179294b7f48cSBarry Smith .keywords: timestep, get, KSP
1793d763cef2SBarry Smith @*/
17947087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1795d763cef2SBarry Smith {
1796d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1797089b2837SJed Brown   SNES           snes;
1798d372ba47SLisandro Dalcin 
1799d763cef2SBarry Smith   PetscFunctionBegin;
18000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18014482741eSBarry Smith   PetscValidPointer(ksp,2);
180217186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1803e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1804089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1805089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1806d763cef2SBarry Smith   PetscFunctionReturn(0);
1807d763cef2SBarry Smith }
1808d763cef2SBarry Smith 
1809d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1810d763cef2SBarry Smith 
18114a2ae208SSatish Balay #undef __FUNCT__
1812adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1813adb62b0dSMatthew Knepley /*@
1814adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1815adb62b0dSMatthew Knepley    maximum time for iteration.
1816adb62b0dSMatthew Knepley 
18173f9fe445SBarry Smith    Not Collective
1818adb62b0dSMatthew Knepley 
1819adb62b0dSMatthew Knepley    Input Parameters:
1820adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1821adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1822adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1823adb62b0dSMatthew Knepley 
1824adb62b0dSMatthew Knepley    Level: intermediate
1825adb62b0dSMatthew Knepley 
1826adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1827adb62b0dSMatthew Knepley @*/
18287087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1829adb62b0dSMatthew Knepley {
1830adb62b0dSMatthew Knepley   PetscFunctionBegin;
18310700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1832abc0a331SBarry Smith   if (maxsteps) {
18334482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1834adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1835adb62b0dSMatthew Knepley   }
1836abc0a331SBarry Smith   if (maxtime) {
18374482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1838adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
1839adb62b0dSMatthew Knepley   }
1840adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1841adb62b0dSMatthew Knepley }
1842adb62b0dSMatthew Knepley 
1843adb62b0dSMatthew Knepley #undef __FUNCT__
18444a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1845d763cef2SBarry Smith /*@
1846d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1847d763cef2SBarry Smith    maximum time for iteration.
1848d763cef2SBarry Smith 
18493f9fe445SBarry Smith    Logically Collective on TS
1850d763cef2SBarry Smith 
1851d763cef2SBarry Smith    Input Parameters:
1852d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1853d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1854d763cef2SBarry Smith -  maxtime - final time to iterate to
1855d763cef2SBarry Smith 
1856d763cef2SBarry Smith    Options Database Keys:
1857d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
18583bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1859d763cef2SBarry Smith 
1860d763cef2SBarry Smith    Notes:
1861d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1862d763cef2SBarry Smith 
1863d763cef2SBarry Smith    Level: intermediate
1864d763cef2SBarry Smith 
1865d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1866a43b19c4SJed Brown 
1867a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1868d763cef2SBarry Smith @*/
18697087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1870d763cef2SBarry Smith {
1871d763cef2SBarry Smith   PetscFunctionBegin;
18720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1873c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1874c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
187539b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
187639b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
1877d763cef2SBarry Smith   PetscFunctionReturn(0);
1878d763cef2SBarry Smith }
1879d763cef2SBarry Smith 
18804a2ae208SSatish Balay #undef __FUNCT__
18814a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1882d763cef2SBarry Smith /*@
1883d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1884d763cef2SBarry Smith    for use by the TS routines.
1885d763cef2SBarry Smith 
18863f9fe445SBarry Smith    Logically Collective on TS and Vec
1887d763cef2SBarry Smith 
1888d763cef2SBarry Smith    Input Parameters:
1889d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
18900910c330SBarry Smith -  u - the solution vector
1891d763cef2SBarry Smith 
1892d763cef2SBarry Smith    Level: beginner
1893d763cef2SBarry Smith 
1894d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1895d763cef2SBarry Smith @*/
18960910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
1897d763cef2SBarry Smith {
18988737fe31SLisandro Dalcin   PetscErrorCode ierr;
1899496e6a7aSJed Brown   DM             dm;
19008737fe31SLisandro Dalcin 
1901d763cef2SBarry Smith   PetscFunctionBegin;
19020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19030910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
19040910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
19056bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1906*bbd56ea5SKarl Rupp 
19070910c330SBarry Smith   ts->vec_sol = u;
1908*bbd56ea5SKarl Rupp 
1909496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
19100910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
1911d763cef2SBarry Smith   PetscFunctionReturn(0);
1912d763cef2SBarry Smith }
1913d763cef2SBarry Smith 
1914e74ef692SMatthew Knepley #undef __FUNCT__
1915e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1916ac226902SBarry Smith /*@C
1917000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
19183f2090d5SJed Brown   called once at the beginning of each time step.
1919000e7ae3SMatthew Knepley 
19203f9fe445SBarry Smith   Logically Collective on TS
1921000e7ae3SMatthew Knepley 
1922000e7ae3SMatthew Knepley   Input Parameters:
1923000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1924000e7ae3SMatthew Knepley - func - The function
1925000e7ae3SMatthew Knepley 
1926000e7ae3SMatthew Knepley   Calling sequence of func:
1927000e7ae3SMatthew Knepley . func (TS ts);
1928000e7ae3SMatthew Knepley 
1929000e7ae3SMatthew Knepley   Level: intermediate
1930000e7ae3SMatthew Knepley 
1931b8123daeSJed Brown   Note:
1932b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1933b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1934b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1935b8123daeSJed Brown 
1936000e7ae3SMatthew Knepley .keywords: TS, timestep
1937b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1938000e7ae3SMatthew Knepley @*/
19397087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1940000e7ae3SMatthew Knepley {
1941000e7ae3SMatthew Knepley   PetscFunctionBegin;
19420700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1943000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1944000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1945000e7ae3SMatthew Knepley }
1946000e7ae3SMatthew Knepley 
1947e74ef692SMatthew Knepley #undef __FUNCT__
19483f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
194909ee8438SJed Brown /*@
19503f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
19513f2090d5SJed Brown 
19523f2090d5SJed Brown   Collective on TS
19533f2090d5SJed Brown 
19543f2090d5SJed Brown   Input Parameters:
19553f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
19563f2090d5SJed Brown 
19573f2090d5SJed Brown   Notes:
19583f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
19593f2090d5SJed Brown   so most users would not generally call this routine themselves.
19603f2090d5SJed Brown 
19613f2090d5SJed Brown   Level: developer
19623f2090d5SJed Brown 
19633f2090d5SJed Brown .keywords: TS, timestep
1964b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
19653f2090d5SJed Brown @*/
19667087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
19673f2090d5SJed Brown {
19683f2090d5SJed Brown   PetscErrorCode ierr;
19693f2090d5SJed Brown 
19703f2090d5SJed Brown   PetscFunctionBegin;
19710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
197272ac3e02SJed Brown   if (ts->ops->prestep) {
19733f2090d5SJed Brown     PetscStackPush("TS PreStep function");
19743f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
19753f2090d5SJed Brown     PetscStackPop;
1976312ce896SJed Brown   }
19773f2090d5SJed Brown   PetscFunctionReturn(0);
19783f2090d5SJed Brown }
19793f2090d5SJed Brown 
19803f2090d5SJed Brown #undef __FUNCT__
1981b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1982b8123daeSJed Brown /*@C
1983b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1984b8123daeSJed Brown   called once at the beginning of each stage.
1985b8123daeSJed Brown 
1986b8123daeSJed Brown   Logically Collective on TS
1987b8123daeSJed Brown 
1988b8123daeSJed Brown   Input Parameters:
1989b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1990b8123daeSJed Brown - func - The function
1991b8123daeSJed Brown 
1992b8123daeSJed Brown   Calling sequence of func:
1993b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1994b8123daeSJed Brown 
1995b8123daeSJed Brown   Level: intermediate
1996b8123daeSJed Brown 
1997b8123daeSJed Brown   Note:
1998b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
1999b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2000b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2001b8123daeSJed Brown 
2002b8123daeSJed Brown .keywords: TS, timestep
2003b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2004b8123daeSJed Brown @*/
2005b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2006b8123daeSJed Brown {
2007b8123daeSJed Brown   PetscFunctionBegin;
2008b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2009b8123daeSJed Brown   ts->ops->prestage = func;
2010b8123daeSJed Brown   PetscFunctionReturn(0);
2011b8123daeSJed Brown }
2012b8123daeSJed Brown 
2013b8123daeSJed Brown #undef __FUNCT__
2014b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2015b8123daeSJed Brown /*@
2016b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2017b8123daeSJed Brown 
2018b8123daeSJed Brown   Collective on TS
2019b8123daeSJed Brown 
2020b8123daeSJed Brown   Input Parameters:
2021b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2022b8123daeSJed Brown 
2023b8123daeSJed Brown   Notes:
2024b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2025b8123daeSJed Brown   most users would not generally call this routine themselves.
2026b8123daeSJed Brown 
2027b8123daeSJed Brown   Level: developer
2028b8123daeSJed Brown 
2029b8123daeSJed Brown .keywords: TS, timestep
2030b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2031b8123daeSJed Brown @*/
2032b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2033b8123daeSJed Brown {
2034b8123daeSJed Brown   PetscErrorCode ierr;
2035b8123daeSJed Brown 
2036b8123daeSJed Brown   PetscFunctionBegin;
2037b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2038b8123daeSJed Brown   if (ts->ops->prestage) {
2039b8123daeSJed Brown     PetscStackPush("TS PreStage function");
2040b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
2041b8123daeSJed Brown     PetscStackPop;
2042b8123daeSJed Brown   }
2043b8123daeSJed Brown   PetscFunctionReturn(0);
2044b8123daeSJed Brown }
2045b8123daeSJed Brown 
2046b8123daeSJed Brown #undef __FUNCT__
2047e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2048ac226902SBarry Smith /*@C
2049000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
20503f2090d5SJed Brown   called once at the end of each time step.
2051000e7ae3SMatthew Knepley 
20523f9fe445SBarry Smith   Logically Collective on TS
2053000e7ae3SMatthew Knepley 
2054000e7ae3SMatthew Knepley   Input Parameters:
2055000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2056000e7ae3SMatthew Knepley - func - The function
2057000e7ae3SMatthew Knepley 
2058000e7ae3SMatthew Knepley   Calling sequence of func:
2059b8123daeSJed Brown $ func (TS ts);
2060000e7ae3SMatthew Knepley 
2061000e7ae3SMatthew Knepley   Level: intermediate
2062000e7ae3SMatthew Knepley 
2063000e7ae3SMatthew Knepley .keywords: TS, timestep
2064b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2065000e7ae3SMatthew Knepley @*/
20667087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2067000e7ae3SMatthew Knepley {
2068000e7ae3SMatthew Knepley   PetscFunctionBegin;
20690700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2070000e7ae3SMatthew Knepley   ts->ops->poststep = func;
2071000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2072000e7ae3SMatthew Knepley }
2073000e7ae3SMatthew Knepley 
2074e74ef692SMatthew Knepley #undef __FUNCT__
20753f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
207609ee8438SJed Brown /*@
20773f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
20783f2090d5SJed Brown 
20793f2090d5SJed Brown   Collective on TS
20803f2090d5SJed Brown 
20813f2090d5SJed Brown   Input Parameters:
20823f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
20833f2090d5SJed Brown 
20843f2090d5SJed Brown   Notes:
20853f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
20863f2090d5SJed Brown   so most users would not generally call this routine themselves.
20873f2090d5SJed Brown 
20883f2090d5SJed Brown   Level: developer
20893f2090d5SJed Brown 
20903f2090d5SJed Brown .keywords: TS, timestep
20913f2090d5SJed Brown @*/
20927087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
20933f2090d5SJed Brown {
20943f2090d5SJed Brown   PetscErrorCode ierr;
20953f2090d5SJed Brown 
20963f2090d5SJed Brown   PetscFunctionBegin;
20970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
209872ac3e02SJed Brown   if (ts->ops->poststep) {
20993f2090d5SJed Brown     PetscStackPush("TS PostStep function");
21003f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
21013f2090d5SJed Brown     PetscStackPop;
210272ac3e02SJed Brown   }
21033f2090d5SJed Brown   PetscFunctionReturn(0);
21043f2090d5SJed Brown }
21053f2090d5SJed Brown 
2106d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2107d763cef2SBarry Smith 
21084a2ae208SSatish Balay #undef __FUNCT__
2109a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2110d763cef2SBarry Smith /*@C
2111a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2112d763cef2SBarry Smith    timestep to display the iteration's  progress.
2113d763cef2SBarry Smith 
21143f9fe445SBarry Smith    Logically Collective on TS
2115d763cef2SBarry Smith 
2116d763cef2SBarry Smith    Input Parameters:
2117d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2118e213d8f1SJed Brown .  monitor - monitoring routine
2119329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
2120b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
2121b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2122b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
2123d763cef2SBarry Smith 
2124e213d8f1SJed Brown    Calling sequence of monitor:
21250910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2126d763cef2SBarry Smith 
2127d763cef2SBarry Smith +    ts - the TS context
212888c05cc5SBarry 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
212988c05cc5SBarry Smith                                been interpolated to)
21301f06c33eSBarry Smith .    time - current time
21310910c330SBarry Smith .    u - current iterate
2132d763cef2SBarry Smith -    mctx - [optional] monitoring context
2133d763cef2SBarry Smith 
2134d763cef2SBarry Smith    Notes:
2135d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2136d763cef2SBarry Smith    already has been loaded.
2137d763cef2SBarry Smith 
2138025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2139025f1a04SBarry Smith 
2140d763cef2SBarry Smith    Level: intermediate
2141d763cef2SBarry Smith 
2142d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2143d763cef2SBarry Smith 
2144a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2145d763cef2SBarry Smith @*/
2146c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2147d763cef2SBarry Smith {
2148d763cef2SBarry Smith   PetscFunctionBegin;
21490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
215017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2151d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
21528704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2153d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2154d763cef2SBarry Smith   PetscFunctionReturn(0);
2155d763cef2SBarry Smith }
2156d763cef2SBarry Smith 
21574a2ae208SSatish Balay #undef __FUNCT__
2158a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2159d763cef2SBarry Smith /*@C
2160a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2161d763cef2SBarry Smith 
21623f9fe445SBarry Smith    Logically Collective on TS
2163d763cef2SBarry Smith 
2164d763cef2SBarry Smith    Input Parameters:
2165d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2166d763cef2SBarry Smith 
2167d763cef2SBarry Smith    Notes:
2168d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2169d763cef2SBarry Smith 
2170d763cef2SBarry Smith    Level: intermediate
2171d763cef2SBarry Smith 
2172d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2173d763cef2SBarry Smith 
2174a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2175d763cef2SBarry Smith @*/
21767087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2177d763cef2SBarry Smith {
2178d952e501SBarry Smith   PetscErrorCode ierr;
2179d952e501SBarry Smith   PetscInt       i;
2180d952e501SBarry Smith 
2181d763cef2SBarry Smith   PetscFunctionBegin;
21820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2183d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
21848704b422SBarry Smith     if (ts->monitordestroy[i]) {
21858704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2186d952e501SBarry Smith     }
2187d952e501SBarry Smith   }
2188d763cef2SBarry Smith   ts->numbermonitors = 0;
2189d763cef2SBarry Smith   PetscFunctionReturn(0);
2190d763cef2SBarry Smith }
2191d763cef2SBarry Smith 
21924a2ae208SSatish Balay #undef __FUNCT__
2193a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2194d8e5e3e6SSatish Balay /*@
2195a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
21965516499fSSatish Balay 
21975516499fSSatish Balay    Level: intermediate
219841251cbbSSatish Balay 
21995516499fSSatish Balay .keywords: TS, set, monitor
22005516499fSSatish Balay 
220141251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
220241251cbbSSatish Balay @*/
2203649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2204d763cef2SBarry Smith {
2205dfbe8321SBarry Smith   PetscErrorCode ierr;
2206649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
2207d132466eSBarry Smith 
2208d763cef2SBarry Smith   PetscFunctionBegin;
2209649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2210971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2211649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2212d763cef2SBarry Smith   PetscFunctionReturn(0);
2213d763cef2SBarry Smith }
2214d763cef2SBarry Smith 
22154a2ae208SSatish Balay #undef __FUNCT__
2216cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2217cd652676SJed Brown /*@
2218cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2219cd652676SJed Brown 
2220cd652676SJed Brown    Logically Collective on TS
2221cd652676SJed Brown 
2222cd652676SJed Brown    Input Argument:
2223cd652676SJed Brown .  ts - time stepping context
2224cd652676SJed Brown 
2225cd652676SJed Brown    Output Argument:
2226cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2227cd652676SJed Brown 
2228cd652676SJed Brown    Level: intermediate
2229cd652676SJed Brown 
2230cd652676SJed Brown .keywords: TS, set
2231cd652676SJed Brown 
2232cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2233cd652676SJed Brown @*/
2234cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2235cd652676SJed Brown {
2236cd652676SJed Brown   PetscFunctionBegin;
2237cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2238cd652676SJed Brown   ts->retain_stages = flg;
2239cd652676SJed Brown   PetscFunctionReturn(0);
2240cd652676SJed Brown }
2241cd652676SJed Brown 
2242cd652676SJed Brown #undef __FUNCT__
2243cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2244cd652676SJed Brown /*@
2245cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2246cd652676SJed Brown 
2247cd652676SJed Brown    Collective on TS
2248cd652676SJed Brown 
2249cd652676SJed Brown    Input Argument:
2250cd652676SJed Brown +  ts - time stepping context
2251cd652676SJed Brown -  t - time to interpolate to
2252cd652676SJed Brown 
2253cd652676SJed Brown    Output Argument:
22540910c330SBarry Smith .  U - state at given time
2255cd652676SJed Brown 
2256cd652676SJed Brown    Notes:
2257cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2258cd652676SJed Brown 
2259cd652676SJed Brown    Level: intermediate
2260cd652676SJed Brown 
2261cd652676SJed Brown    Developer Notes:
2262cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2263cd652676SJed Brown 
2264cd652676SJed Brown .keywords: TS, set
2265cd652676SJed Brown 
2266cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2267cd652676SJed Brown @*/
22680910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2269cd652676SJed Brown {
2270cd652676SJed Brown   PetscErrorCode ierr;
2271cd652676SJed Brown 
2272cd652676SJed Brown   PetscFunctionBegin;
2273cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2274d8cd7023SBarry 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);
2275cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
22760910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2277cd652676SJed Brown   PetscFunctionReturn(0);
2278cd652676SJed Brown }
2279cd652676SJed Brown 
2280cd652676SJed Brown #undef __FUNCT__
22814a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2282d763cef2SBarry Smith /*@
22836d9e5789SSean Farley    TSStep - Steps one time step
2284d763cef2SBarry Smith 
2285d763cef2SBarry Smith    Collective on TS
2286d763cef2SBarry Smith 
2287d763cef2SBarry Smith    Input Parameter:
2288d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2289d763cef2SBarry Smith 
22906d9e5789SSean Farley    Level: intermediate
2291d763cef2SBarry Smith 
2292b8123daeSJed Brown    Notes:
2293b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2294b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2295b8123daeSJed Brown 
229625cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
229725cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
229825cb2221SBarry Smith 
2299d763cef2SBarry Smith .keywords: TS, timestep, solve
2300d763cef2SBarry Smith 
230125cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2302d763cef2SBarry Smith @*/
2303193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2304d763cef2SBarry Smith {
2305362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2306dfbe8321SBarry Smith   PetscErrorCode ierr;
2307d763cef2SBarry Smith 
2308d763cef2SBarry Smith   PetscFunctionBegin;
23090700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2310d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2311d405a339SMatthew Knepley 
2312362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2313362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2314*bbd56ea5SKarl Rupp 
2315d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2316193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2317d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2318*bbd56ea5SKarl Rupp 
2319362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2320362cd11cSLisandro Dalcin 
2321362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2322cef5090cSJed Brown     if (ts->errorifstepfailed) {
2323cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2324cef5090cSJed 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]);
2325cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2326cef5090cSJed Brown     }
2327362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2328db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2329db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2330362cd11cSLisandro Dalcin   }
2331d763cef2SBarry Smith   PetscFunctionReturn(0);
2332d763cef2SBarry Smith }
2333d763cef2SBarry Smith 
23344a2ae208SSatish Balay #undef __FUNCT__
233505175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
233605175c85SJed Brown /*@
233705175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
233805175c85SJed Brown 
23391c3436cfSJed Brown    Collective on TS
234005175c85SJed Brown 
234105175c85SJed Brown    Input Arguments:
23421c3436cfSJed Brown +  ts - time stepping context
23431c3436cfSJed Brown .  order - desired order of accuracy
23441c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
234505175c85SJed Brown 
234605175c85SJed Brown    Output Arguments:
23470910c330SBarry Smith .  U - state at the end of the current step
234805175c85SJed Brown 
234905175c85SJed Brown    Level: advanced
235005175c85SJed Brown 
2351108c343cSJed Brown    Notes:
2352108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2353108c343cSJed 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.
2354108c343cSJed Brown 
23551c3436cfSJed Brown .seealso: TSStep(), TSAdapt
235605175c85SJed Brown @*/
23570910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
235805175c85SJed Brown {
235905175c85SJed Brown   PetscErrorCode ierr;
236005175c85SJed Brown 
236105175c85SJed Brown   PetscFunctionBegin;
236205175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
236305175c85SJed Brown   PetscValidType(ts,1);
23640910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
23651c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
23660910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
236705175c85SJed Brown   PetscFunctionReturn(0);
236805175c85SJed Brown }
236905175c85SJed Brown 
237005175c85SJed Brown #undef __FUNCT__
23716a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
23726a4d4014SLisandro Dalcin /*@
23736a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
23746a4d4014SLisandro Dalcin 
23756a4d4014SLisandro Dalcin    Collective on TS
23766a4d4014SLisandro Dalcin 
23776a4d4014SLisandro Dalcin    Input Parameter:
23786a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2379cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
23805a3a76d0SJed Brown 
23816a4d4014SLisandro Dalcin    Level: beginner
23826a4d4014SLisandro Dalcin 
23835a3a76d0SJed Brown    Notes:
23845a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
23855a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
23865a3a76d0SJed Brown    stepped over the final time.
23875a3a76d0SJed Brown 
23886a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
23896a4d4014SLisandro Dalcin 
23906a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
23916a4d4014SLisandro Dalcin @*/
2392cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
23936a4d4014SLisandro Dalcin {
23944d7d938eSLisandro Dalcin   PetscBool         flg;
23954d7d938eSLisandro Dalcin   PetscViewer       viewer;
23966a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2397cffb1e40SBarry Smith   PetscViewerFormat format;
2398f22f69f0SBarry Smith 
23996a4d4014SLisandro Dalcin   PetscFunctionBegin;
24000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2401f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
240249354f04SShri 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 */
24030910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
24045a3a76d0SJed Brown       Vec y;
24050910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
24065a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
24075a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
24085a3a76d0SJed Brown     }
2409f2c2a1b9SBarry Smith     if (u) {
24100910c330SBarry Smith       ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2411f2c2a1b9SBarry Smith     }
2412*bbd56ea5SKarl Rupp   } else if (u) {
24130910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
24145a3a76d0SJed Brown   }
2415b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
24166a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2417193ac0bcSJed Brown   ts->steps             = 0;
24185ef26d82SJed Brown   ts->ksp_its           = 0;
24195ef26d82SJed Brown   ts->snes_its          = 0;
2420c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2421c610991cSLisandro Dalcin   ts->reject            = 0;
2422193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2423193ac0bcSJed Brown 
2424193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2425193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
24260910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2427*bbd56ea5SKarl Rupp 
2428cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2429193ac0bcSJed Brown   } else {
24306a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2431362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2432db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2433db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2434e1a7a14fSJed Brown     while (!ts->reason) {
2435193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2436193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2437193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2438193ac0bcSJed Brown     }
243949354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
24400910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2441*bbd56ea5SKarl Rupp 
2442cc708dedSBarry Smith       ts->solvetime = ts->max_time;
24430574a7fbSJed Brown     } else {
2444ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2445cc708dedSBarry Smith       ts->solvetime = ts->ptime;
24460574a7fbSJed Brown     }
2447193ac0bcSJed Brown   }
24483923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2449cffb1e40SBarry Smith   ierr = PetscOptionsGetViewer(((PetscObject)ts)->comm,((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
24504d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2451cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
24524d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2453cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2454cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
24552b0a91c0SBarry Smith   }
24566a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
24576a4d4014SLisandro Dalcin }
24586a4d4014SLisandro Dalcin 
24596a4d4014SLisandro Dalcin #undef __FUNCT__
24604a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2461228d79bcSJed Brown /*@
2462228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2463228d79bcSJed Brown 
2464228d79bcSJed Brown    Collective on TS
2465228d79bcSJed Brown 
2466228d79bcSJed Brown    Input Parameters:
2467228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2468228d79bcSJed Brown .  step - step number that has just completed
2469228d79bcSJed Brown .  ptime - model time of the state
24700910c330SBarry Smith -  u - state at the current model time
2471228d79bcSJed Brown 
2472228d79bcSJed Brown    Notes:
2473228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2474228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2475228d79bcSJed Brown 
2476228d79bcSJed Brown    Level: advanced
2477228d79bcSJed Brown 
2478228d79bcSJed Brown .keywords: TS, timestep
2479228d79bcSJed Brown @*/
24800910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2481d763cef2SBarry Smith {
24826849ba73SBarry Smith   PetscErrorCode ierr;
2483a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2484d763cef2SBarry Smith 
2485d763cef2SBarry Smith   PetscFunctionBegin;
2486d763cef2SBarry Smith   for (i=0; i<n; i++) {
24870910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2488d763cef2SBarry Smith   }
2489d763cef2SBarry Smith   PetscFunctionReturn(0);
2490d763cef2SBarry Smith }
2491d763cef2SBarry Smith 
2492d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
24930b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
24940b039ecaSBarry Smith   PetscDrawLG lg;
24950b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2496201da799SBarry Smith   PetscInt    ksp_its,snes_its;
24970b039ecaSBarry Smith };
24980b039ecaSBarry Smith 
2499d763cef2SBarry Smith 
25004a2ae208SSatish Balay #undef __FUNCT__
2501a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2502d763cef2SBarry Smith /*@C
2503a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2504a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2505d763cef2SBarry Smith 
2506d763cef2SBarry Smith    Collective on TS
2507d763cef2SBarry Smith 
2508d763cef2SBarry Smith    Input Parameters:
2509d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2510d763cef2SBarry Smith .  label - the title to put in the title bar
25117c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2512a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2513a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2514d763cef2SBarry Smith 
2515d763cef2SBarry Smith    Output Parameter:
25160b039ecaSBarry Smith .  ctx - the context
2517d763cef2SBarry Smith 
2518d763cef2SBarry Smith    Options Database Key:
25194f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
25204f09c107SBarry Smith .  -ts_monitor_lg_solution -
252199fdda47SBarry Smith .  -ts_monitor_lg_error -
252299fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
252399fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
252499fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2525d763cef2SBarry Smith 
2526d763cef2SBarry Smith    Notes:
2527a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2528d763cef2SBarry Smith 
2529d763cef2SBarry Smith    Level: intermediate
2530d763cef2SBarry Smith 
25317c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2532d763cef2SBarry Smith 
25334f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
25347c922b88SBarry Smith 
2535d763cef2SBarry Smith @*/
2536a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2537d763cef2SBarry Smith {
2538b0a32e0cSBarry Smith   PetscDraw      win;
2539dfbe8321SBarry Smith   PetscErrorCode ierr;
254099fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2541d763cef2SBarry Smith 
2542d763cef2SBarry Smith   PetscFunctionBegin;
2543201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2544a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
25453fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
25460b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
254799fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr);
254899fdda47SBarry Smith   if (flg) {
25490b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
255099fdda47SBarry Smith   }
25510b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2552a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2553d763cef2SBarry Smith   PetscFunctionReturn(0);
2554d763cef2SBarry Smith }
2555d763cef2SBarry Smith 
25564a2ae208SSatish Balay #undef __FUNCT__
25574f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
25584f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2559d763cef2SBarry Smith {
25600b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2561c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2562dfbe8321SBarry Smith   PetscErrorCode ierr;
2563d763cef2SBarry Smith 
2564d763cef2SBarry Smith   PetscFunctionBegin;
2565a9f9c1f6SBarry Smith   if (!n) {
2566a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2567a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2568a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2569a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2570a9f9c1f6SBarry Smith   }
2571c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
25720b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
257399fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))) {
25740b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
25753923b477SBarry Smith   }
2576d763cef2SBarry Smith   PetscFunctionReturn(0);
2577d763cef2SBarry Smith }
2578d763cef2SBarry Smith 
25794a2ae208SSatish Balay #undef __FUNCT__
2580a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2581d763cef2SBarry Smith /*@C
2582a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2583a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2584d763cef2SBarry Smith 
25850b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2586d763cef2SBarry Smith 
2587d763cef2SBarry Smith    Input Parameter:
25880b039ecaSBarry Smith .  ctx - the monitor context
2589d763cef2SBarry Smith 
2590d763cef2SBarry Smith    Level: intermediate
2591d763cef2SBarry Smith 
2592d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2593d763cef2SBarry Smith 
25944f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2595d763cef2SBarry Smith @*/
2596a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2597d763cef2SBarry Smith {
2598b0a32e0cSBarry Smith   PetscDraw      draw;
2599dfbe8321SBarry Smith   PetscErrorCode ierr;
2600d763cef2SBarry Smith 
2601d763cef2SBarry Smith   PetscFunctionBegin;
26020b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
26036bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
26040b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
26050b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2606d763cef2SBarry Smith   PetscFunctionReturn(0);
2607d763cef2SBarry Smith }
2608d763cef2SBarry Smith 
26094a2ae208SSatish Balay #undef __FUNCT__
26104a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2611d763cef2SBarry Smith /*@
2612b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2613d763cef2SBarry Smith 
2614d763cef2SBarry Smith    Not Collective
2615d763cef2SBarry Smith 
2616d763cef2SBarry Smith    Input Parameter:
2617d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2618d763cef2SBarry Smith 
2619d763cef2SBarry Smith    Output Parameter:
2620d763cef2SBarry Smith .  t  - the current time
2621d763cef2SBarry Smith 
2622d763cef2SBarry Smith    Level: beginner
2623d763cef2SBarry Smith 
2624b8123daeSJed Brown    Note:
2625b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2626b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2627b8123daeSJed Brown 
2628d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2629d763cef2SBarry Smith 
2630d763cef2SBarry Smith .keywords: TS, get, time
2631d763cef2SBarry Smith @*/
26327087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2633d763cef2SBarry Smith {
2634d763cef2SBarry Smith   PetscFunctionBegin;
26350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2636f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2637d763cef2SBarry Smith   *t = ts->ptime;
2638d763cef2SBarry Smith   PetscFunctionReturn(0);
2639d763cef2SBarry Smith }
2640d763cef2SBarry Smith 
26414a2ae208SSatish Balay #undef __FUNCT__
26426a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
26436a4d4014SLisandro Dalcin /*@
26446a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
26456a4d4014SLisandro Dalcin 
26463f9fe445SBarry Smith    Logically Collective on TS
26476a4d4014SLisandro Dalcin 
26486a4d4014SLisandro Dalcin    Input Parameters:
26496a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
26506a4d4014SLisandro Dalcin -  time - the time
26516a4d4014SLisandro Dalcin 
26526a4d4014SLisandro Dalcin    Level: intermediate
26536a4d4014SLisandro Dalcin 
26546a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
26556a4d4014SLisandro Dalcin 
26566a4d4014SLisandro Dalcin .keywords: TS, set, time
26576a4d4014SLisandro Dalcin @*/
26587087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
26596a4d4014SLisandro Dalcin {
26606a4d4014SLisandro Dalcin   PetscFunctionBegin;
26610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2662c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
26636a4d4014SLisandro Dalcin   ts->ptime = t;
26646a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
26656a4d4014SLisandro Dalcin }
26666a4d4014SLisandro Dalcin 
26676a4d4014SLisandro Dalcin #undef __FUNCT__
26684a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2669d763cef2SBarry Smith /*@C
2670d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2671d763cef2SBarry Smith    TS options in the database.
2672d763cef2SBarry Smith 
26733f9fe445SBarry Smith    Logically Collective on TS
2674d763cef2SBarry Smith 
2675d763cef2SBarry Smith    Input Parameter:
2676d763cef2SBarry Smith +  ts     - The TS context
2677d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2678d763cef2SBarry Smith 
2679d763cef2SBarry Smith    Notes:
2680d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2681d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2682d763cef2SBarry Smith    hyphen.
2683d763cef2SBarry Smith 
2684d763cef2SBarry Smith    Level: advanced
2685d763cef2SBarry Smith 
2686d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2687d763cef2SBarry Smith 
2688d763cef2SBarry Smith .seealso: TSSetFromOptions()
2689d763cef2SBarry Smith 
2690d763cef2SBarry Smith @*/
26917087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2692d763cef2SBarry Smith {
2693dfbe8321SBarry Smith   PetscErrorCode ierr;
2694089b2837SJed Brown   SNES           snes;
2695d763cef2SBarry Smith 
2696d763cef2SBarry Smith   PetscFunctionBegin;
26970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2698d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2699089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2700089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2701d763cef2SBarry Smith   PetscFunctionReturn(0);
2702d763cef2SBarry Smith }
2703d763cef2SBarry Smith 
2704d763cef2SBarry Smith 
27054a2ae208SSatish Balay #undef __FUNCT__
27064a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2707d763cef2SBarry Smith /*@C
2708d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2709d763cef2SBarry Smith    TS options in the database.
2710d763cef2SBarry Smith 
27113f9fe445SBarry Smith    Logically Collective on TS
2712d763cef2SBarry Smith 
2713d763cef2SBarry Smith    Input Parameter:
2714d763cef2SBarry Smith +  ts     - The TS context
2715d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2716d763cef2SBarry Smith 
2717d763cef2SBarry Smith    Notes:
2718d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2719d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2720d763cef2SBarry Smith    hyphen.
2721d763cef2SBarry Smith 
2722d763cef2SBarry Smith    Level: advanced
2723d763cef2SBarry Smith 
2724d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2725d763cef2SBarry Smith 
2726d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2727d763cef2SBarry Smith 
2728d763cef2SBarry Smith @*/
27297087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2730d763cef2SBarry Smith {
2731dfbe8321SBarry Smith   PetscErrorCode ierr;
2732089b2837SJed Brown   SNES           snes;
2733d763cef2SBarry Smith 
2734d763cef2SBarry Smith   PetscFunctionBegin;
27350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2736d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2737089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2738089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2739d763cef2SBarry Smith   PetscFunctionReturn(0);
2740d763cef2SBarry Smith }
2741d763cef2SBarry Smith 
27424a2ae208SSatish Balay #undef __FUNCT__
27434a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2744d763cef2SBarry Smith /*@C
2745d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2746d763cef2SBarry Smith    TS options in the database.
2747d763cef2SBarry Smith 
2748d763cef2SBarry Smith    Not Collective
2749d763cef2SBarry Smith 
2750d763cef2SBarry Smith    Input Parameter:
2751d763cef2SBarry Smith .  ts - The TS context
2752d763cef2SBarry Smith 
2753d763cef2SBarry Smith    Output Parameter:
2754d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2755d763cef2SBarry Smith 
2756d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2757d763cef2SBarry Smith    sufficient length to hold the prefix.
2758d763cef2SBarry Smith 
2759d763cef2SBarry Smith    Level: intermediate
2760d763cef2SBarry Smith 
2761d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2762d763cef2SBarry Smith 
2763d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2764d763cef2SBarry Smith @*/
27657087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2766d763cef2SBarry Smith {
2767dfbe8321SBarry Smith   PetscErrorCode ierr;
2768d763cef2SBarry Smith 
2769d763cef2SBarry Smith   PetscFunctionBegin;
27700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27714482741eSBarry Smith   PetscValidPointer(prefix,2);
2772d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2773d763cef2SBarry Smith   PetscFunctionReturn(0);
2774d763cef2SBarry Smith }
2775d763cef2SBarry Smith 
27764a2ae208SSatish Balay #undef __FUNCT__
27774a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2778d763cef2SBarry Smith /*@C
2779d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2780d763cef2SBarry Smith 
2781d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2782d763cef2SBarry Smith 
2783d763cef2SBarry Smith    Input Parameter:
2784d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2785d763cef2SBarry Smith 
2786d763cef2SBarry Smith    Output Parameters:
2787b5abc632SBarry Smith +  J   - The Jacobian J of F, where U_t = G(U,t)
2788d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2789089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2790d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2791d763cef2SBarry Smith 
2792d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2793d763cef2SBarry Smith 
2794d763cef2SBarry Smith    Level: intermediate
2795d763cef2SBarry Smith 
279626d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2797d763cef2SBarry Smith 
2798d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2799d763cef2SBarry Smith @*/
2800089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2801d763cef2SBarry Smith {
2802089b2837SJed Brown   PetscErrorCode ierr;
2803089b2837SJed Brown   SNES           snes;
280424989b8cSPeter Brune   DM             dm;
2805089b2837SJed Brown 
2806d763cef2SBarry Smith   PetscFunctionBegin;
2807089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2808089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
280924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
281024989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2811d763cef2SBarry Smith   PetscFunctionReturn(0);
2812d763cef2SBarry Smith }
2813d763cef2SBarry Smith 
28141713a123SBarry Smith #undef __FUNCT__
28152eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
28162eca1d9cSJed Brown /*@C
28172eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
28182eca1d9cSJed Brown 
28192eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
28202eca1d9cSJed Brown 
28212eca1d9cSJed Brown    Input Parameter:
28222eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
28232eca1d9cSJed Brown 
28242eca1d9cSJed Brown    Output Parameters:
28252eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
28262eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
28272eca1d9cSJed Brown .  f   - The function to compute the matrices
28282eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
28292eca1d9cSJed Brown 
28302eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
28312eca1d9cSJed Brown 
28322eca1d9cSJed Brown    Level: advanced
28332eca1d9cSJed Brown 
28342eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
28352eca1d9cSJed Brown 
28362eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
28372eca1d9cSJed Brown @*/
28387087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
28392eca1d9cSJed Brown {
2840089b2837SJed Brown   PetscErrorCode ierr;
2841089b2837SJed Brown   SNES           snes;
284224989b8cSPeter Brune   DM             dm;
28430910c330SBarry Smith 
28442eca1d9cSJed Brown   PetscFunctionBegin;
2845089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2846f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
2847089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
284824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
284924989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
28502eca1d9cSJed Brown   PetscFunctionReturn(0);
28512eca1d9cSJed Brown }
28522eca1d9cSJed Brown 
285383a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
28546083293cSBarry Smith   PetscViewer viewer;
28556083293cSBarry Smith   Vec         initialsolution;
28566083293cSBarry Smith   PetscBool   showinitial;
285783a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
285883a4ac43SBarry Smith };
28596083293cSBarry Smith 
28602eca1d9cSJed Brown #undef __FUNCT__
286183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
28621713a123SBarry Smith /*@C
286383a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
28641713a123SBarry Smith    VecView() for the solution at each timestep
28651713a123SBarry Smith 
28661713a123SBarry Smith    Collective on TS
28671713a123SBarry Smith 
28681713a123SBarry Smith    Input Parameters:
28691713a123SBarry Smith +  ts - the TS context
28701713a123SBarry Smith .  step - current time-step
2871142b95e3SSatish Balay .  ptime - current time
28721713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
28731713a123SBarry Smith 
287499fdda47SBarry Smith    Options Database:
287599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
287699fdda47SBarry Smith 
287799fdda47SBarry 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
287899fdda47SBarry Smith        will look bad
287999fdda47SBarry Smith 
28801713a123SBarry Smith    Level: intermediate
28811713a123SBarry Smith 
28821713a123SBarry Smith .keywords: TS,  vector, monitor, view
28831713a123SBarry Smith 
2884a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
28851713a123SBarry Smith @*/
28860910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
28871713a123SBarry Smith {
2888dfbe8321SBarry Smith   PetscErrorCode   ierr;
288983a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
28901713a123SBarry Smith 
28911713a123SBarry Smith   PetscFunctionBegin;
28926083293cSBarry Smith   if (!step && ictx->showinitial) {
28936083293cSBarry Smith     if (!ictx->initialsolution) {
28940910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
28951713a123SBarry Smith     }
28960910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
28976083293cSBarry Smith   }
28983fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
28990dcf80beSBarry Smith 
29006083293cSBarry Smith   if (ictx->showinitial) {
29016083293cSBarry Smith     PetscReal pause;
29026083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
29036083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
29046083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
29056083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
29066083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
29076083293cSBarry Smith   }
29080910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
29096083293cSBarry Smith   if (ictx->showinitial) {
29106083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
29116083293cSBarry Smith   }
29121713a123SBarry Smith   PetscFunctionReturn(0);
29131713a123SBarry Smith }
29141713a123SBarry Smith 
29151713a123SBarry Smith 
29166c699258SBarry Smith #undef __FUNCT__
291783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
29186083293cSBarry Smith /*@C
291983a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
29206083293cSBarry Smith 
29216083293cSBarry Smith    Collective on TS
29226083293cSBarry Smith 
29236083293cSBarry Smith    Input Parameters:
29246083293cSBarry Smith .    ctx - the monitor context
29256083293cSBarry Smith 
29266083293cSBarry Smith    Level: intermediate
29276083293cSBarry Smith 
29286083293cSBarry Smith .keywords: TS,  vector, monitor, view
29296083293cSBarry Smith 
293083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
29316083293cSBarry Smith @*/
293283a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
29336083293cSBarry Smith {
29346083293cSBarry Smith   PetscErrorCode ierr;
29356083293cSBarry Smith 
29366083293cSBarry Smith   PetscFunctionBegin;
293783a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
293883a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
293983a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
29406083293cSBarry Smith   PetscFunctionReturn(0);
29416083293cSBarry Smith }
29426083293cSBarry Smith 
29436083293cSBarry Smith #undef __FUNCT__
294483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
29456083293cSBarry Smith /*@C
294683a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
29476083293cSBarry Smith 
29486083293cSBarry Smith    Collective on TS
29496083293cSBarry Smith 
29506083293cSBarry Smith    Input Parameter:
29516083293cSBarry Smith .    ts - time-step context
29526083293cSBarry Smith 
29536083293cSBarry Smith    Output Patameter:
29546083293cSBarry Smith .    ctx - the monitor context
29556083293cSBarry Smith 
295699fdda47SBarry Smith    Options Database:
295799fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
295899fdda47SBarry Smith 
29596083293cSBarry Smith    Level: intermediate
29606083293cSBarry Smith 
29616083293cSBarry Smith .keywords: TS,  vector, monitor, view
29626083293cSBarry Smith 
296383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
29646083293cSBarry Smith @*/
296583a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
29666083293cSBarry Smith {
29676083293cSBarry Smith   PetscErrorCode   ierr;
29686083293cSBarry Smith 
29696083293cSBarry Smith   PetscFunctionBegin;
297083a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
297183a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
2972*bbd56ea5SKarl Rupp 
297383a4ac43SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
297483a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
2975*bbd56ea5SKarl Rupp 
297699fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr);
29776083293cSBarry Smith   PetscFunctionReturn(0);
29786083293cSBarry Smith }
29796083293cSBarry Smith 
29806083293cSBarry Smith #undef __FUNCT__
298183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
29823a471f94SBarry Smith /*@C
298383a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
29843a471f94SBarry Smith    VecView() for the error at each timestep
29853a471f94SBarry Smith 
29863a471f94SBarry Smith    Collective on TS
29873a471f94SBarry Smith 
29883a471f94SBarry Smith    Input Parameters:
29893a471f94SBarry Smith +  ts - the TS context
29903a471f94SBarry Smith .  step - current time-step
29913a471f94SBarry Smith .  ptime - current time
29923a471f94SBarry Smith -  dummy - either a viewer or PETSC_NULL
29933a471f94SBarry Smith 
29943a471f94SBarry Smith    Level: intermediate
29953a471f94SBarry Smith 
29963a471f94SBarry Smith .keywords: TS,  vector, monitor, view
29973a471f94SBarry Smith 
29983a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
29993a471f94SBarry Smith @*/
30000910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
30013a471f94SBarry Smith {
30023a471f94SBarry Smith   PetscErrorCode   ierr;
300383a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
300483a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
30053a471f94SBarry Smith   Vec              work;
30063a471f94SBarry Smith 
30073a471f94SBarry Smith   PetscFunctionBegin;
30083fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
30090910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
30103a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
30110910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
30123a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
30133a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
30143a471f94SBarry Smith   PetscFunctionReturn(0);
30153a471f94SBarry Smith }
30163a471f94SBarry Smith 
30172a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
30183a471f94SBarry Smith #undef __FUNCT__
30196c699258SBarry Smith #define __FUNCT__ "TSSetDM"
30206c699258SBarry Smith /*@
30216c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
30226c699258SBarry Smith 
30233f9fe445SBarry Smith    Logically Collective on TS and DM
30246c699258SBarry Smith 
30256c699258SBarry Smith    Input Parameters:
30266c699258SBarry Smith +  ts - the preconditioner context
30276c699258SBarry Smith -  dm - the dm
30286c699258SBarry Smith 
30296c699258SBarry Smith    Level: intermediate
30306c699258SBarry Smith 
30316c699258SBarry Smith 
30326c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
30336c699258SBarry Smith @*/
30347087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
30356c699258SBarry Smith {
30366c699258SBarry Smith   PetscErrorCode ierr;
3037089b2837SJed Brown   SNES           snes;
3038942e3340SBarry Smith   DMTS           tsdm;
30396c699258SBarry Smith 
30406c699258SBarry Smith   PetscFunctionBegin;
30410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
304270663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3043942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
30442a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3045942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3046942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
304724989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
304824989b8cSPeter Brune         tsdm->originaldm = dm;
304924989b8cSPeter Brune       }
305024989b8cSPeter Brune     }
30516bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
305224989b8cSPeter Brune   }
30536c699258SBarry Smith   ts->dm = dm;
3054*bbd56ea5SKarl Rupp 
3055089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3056089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
30576c699258SBarry Smith   PetscFunctionReturn(0);
30586c699258SBarry Smith }
30596c699258SBarry Smith 
30606c699258SBarry Smith #undef __FUNCT__
30616c699258SBarry Smith #define __FUNCT__ "TSGetDM"
30626c699258SBarry Smith /*@
30636c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
30646c699258SBarry Smith 
30653f9fe445SBarry Smith    Not Collective
30666c699258SBarry Smith 
30676c699258SBarry Smith    Input Parameter:
30686c699258SBarry Smith . ts - the preconditioner context
30696c699258SBarry Smith 
30706c699258SBarry Smith    Output Parameter:
30716c699258SBarry Smith .  dm - the dm
30726c699258SBarry Smith 
30736c699258SBarry Smith    Level: intermediate
30746c699258SBarry Smith 
30756c699258SBarry Smith 
30766c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
30776c699258SBarry Smith @*/
30787087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
30796c699258SBarry Smith {
3080496e6a7aSJed Brown   PetscErrorCode ierr;
3081496e6a7aSJed Brown 
30826c699258SBarry Smith   PetscFunctionBegin;
30830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3084496e6a7aSJed Brown   if (!ts->dm) {
3085496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
3086496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3087496e6a7aSJed Brown   }
30886c699258SBarry Smith   *dm = ts->dm;
30896c699258SBarry Smith   PetscFunctionReturn(0);
30906c699258SBarry Smith }
30911713a123SBarry Smith 
30920f5c6efeSJed Brown #undef __FUNCT__
30930f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
30940f5c6efeSJed Brown /*@
30950f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
30960f5c6efeSJed Brown 
30973f9fe445SBarry Smith    Logically Collective on SNES
30980f5c6efeSJed Brown 
30990f5c6efeSJed Brown    Input Parameter:
3100d42a1c89SJed Brown + snes - nonlinear solver
31010910c330SBarry Smith . U - the current state at which to evaluate the residual
3102d42a1c89SJed Brown - ctx - user context, must be a TS
31030f5c6efeSJed Brown 
31040f5c6efeSJed Brown    Output Parameter:
31050f5c6efeSJed Brown . F - the nonlinear residual
31060f5c6efeSJed Brown 
31070f5c6efeSJed Brown    Notes:
31080f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
31090f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
31100f5c6efeSJed Brown 
31110f5c6efeSJed Brown    Level: advanced
31120f5c6efeSJed Brown 
31130f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
31140f5c6efeSJed Brown @*/
31150910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
31160f5c6efeSJed Brown {
31170f5c6efeSJed Brown   TS             ts = (TS)ctx;
31180f5c6efeSJed Brown   PetscErrorCode ierr;
31190f5c6efeSJed Brown 
31200f5c6efeSJed Brown   PetscFunctionBegin;
31210f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31220910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
31230f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
31240f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
31250910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
31260f5c6efeSJed Brown   PetscFunctionReturn(0);
31270f5c6efeSJed Brown }
31280f5c6efeSJed Brown 
31290f5c6efeSJed Brown #undef __FUNCT__
31300f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
31310f5c6efeSJed Brown /*@
31320f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
31330f5c6efeSJed Brown 
31340f5c6efeSJed Brown    Collective on SNES
31350f5c6efeSJed Brown 
31360f5c6efeSJed Brown    Input Parameter:
31370f5c6efeSJed Brown + snes - nonlinear solver
31380910c330SBarry Smith . U - the current state at which to evaluate the residual
31390f5c6efeSJed Brown - ctx - user context, must be a TS
31400f5c6efeSJed Brown 
31410f5c6efeSJed Brown    Output Parameter:
31420f5c6efeSJed Brown + A - the Jacobian
31430f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
31440f5c6efeSJed Brown - flag - indicates any structure change in the matrix
31450f5c6efeSJed Brown 
31460f5c6efeSJed Brown    Notes:
31470f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
31480f5c6efeSJed Brown 
31490f5c6efeSJed Brown    Level: developer
31500f5c6efeSJed Brown 
31510f5c6efeSJed Brown .seealso: SNESSetJacobian()
31520f5c6efeSJed Brown @*/
31530910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
31540f5c6efeSJed Brown {
31550f5c6efeSJed Brown   TS             ts = (TS)ctx;
31560f5c6efeSJed Brown   PetscErrorCode ierr;
31570f5c6efeSJed Brown 
31580f5c6efeSJed Brown   PetscFunctionBegin;
31590f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31600910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
31610f5c6efeSJed Brown   PetscValidPointer(A,3);
31620f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
31630f5c6efeSJed Brown   PetscValidPointer(B,4);
31640f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
31650f5c6efeSJed Brown   PetscValidPointer(flag,5);
31660f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
31670910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
31680f5c6efeSJed Brown   PetscFunctionReturn(0);
31690f5c6efeSJed Brown }
3170325fc9f4SBarry Smith 
31710e4ef248SJed Brown #undef __FUNCT__
31720e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
31730e4ef248SJed Brown /*@C
31740e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
31750e4ef248SJed Brown 
31760e4ef248SJed Brown    Collective on TS
31770e4ef248SJed Brown 
31780e4ef248SJed Brown    Input Arguments:
31790e4ef248SJed Brown +  ts - time stepping context
31800e4ef248SJed Brown .  t - time at which to evaluate
31810910c330SBarry Smith .  U - state at which to evaluate
31820e4ef248SJed Brown -  ctx - context
31830e4ef248SJed Brown 
31840e4ef248SJed Brown    Output Arguments:
31850e4ef248SJed Brown .  F - right hand side
31860e4ef248SJed Brown 
31870e4ef248SJed Brown    Level: intermediate
31880e4ef248SJed Brown 
31890e4ef248SJed Brown    Notes:
31900e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
31910e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
31920e4ef248SJed Brown 
31930e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
31940e4ef248SJed Brown @*/
31950910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
31960e4ef248SJed Brown {
31970e4ef248SJed Brown   PetscErrorCode ierr;
31980e4ef248SJed Brown   Mat            Arhs,Brhs;
31990e4ef248SJed Brown   MatStructure   flg2;
32000e4ef248SJed Brown 
32010e4ef248SJed Brown   PetscFunctionBegin;
32020e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
32030910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
32040910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
32050e4ef248SJed Brown   PetscFunctionReturn(0);
32060e4ef248SJed Brown }
32070e4ef248SJed Brown 
32080e4ef248SJed Brown #undef __FUNCT__
32090e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
32100e4ef248SJed Brown /*@C
32110e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
32120e4ef248SJed Brown 
32130e4ef248SJed Brown    Collective on TS
32140e4ef248SJed Brown 
32150e4ef248SJed Brown    Input Arguments:
32160e4ef248SJed Brown +  ts - time stepping context
32170e4ef248SJed Brown .  t - time at which to evaluate
32180910c330SBarry Smith .  U - state at which to evaluate
32190e4ef248SJed Brown -  ctx - context
32200e4ef248SJed Brown 
32210e4ef248SJed Brown    Output Arguments:
32220e4ef248SJed Brown +  A - pointer to operator
32230e4ef248SJed Brown .  B - pointer to preconditioning matrix
32240e4ef248SJed Brown -  flg - matrix structure flag
32250e4ef248SJed Brown 
32260e4ef248SJed Brown    Level: intermediate
32270e4ef248SJed Brown 
32280e4ef248SJed Brown    Notes:
32290e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
32300e4ef248SJed Brown 
32310e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
32320e4ef248SJed Brown @*/
32330910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
32340e4ef248SJed Brown {
32350e4ef248SJed Brown   PetscFunctionBegin;
32360e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
32370e4ef248SJed Brown   PetscFunctionReturn(0);
32380e4ef248SJed Brown }
32390e4ef248SJed Brown 
32400026cea9SSean Farley #undef __FUNCT__
32410026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
32420026cea9SSean Farley /*@C
32430026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
32440026cea9SSean Farley 
32450026cea9SSean Farley    Collective on TS
32460026cea9SSean Farley 
32470026cea9SSean Farley    Input Arguments:
32480026cea9SSean Farley +  ts - time stepping context
32490026cea9SSean Farley .  t - time at which to evaluate
32500910c330SBarry Smith .  U - state at which to evaluate
32510910c330SBarry Smith .  Udot - time derivative of state vector
32520026cea9SSean Farley -  ctx - context
32530026cea9SSean Farley 
32540026cea9SSean Farley    Output Arguments:
32550026cea9SSean Farley .  F - left hand side
32560026cea9SSean Farley 
32570026cea9SSean Farley    Level: intermediate
32580026cea9SSean Farley 
32590026cea9SSean Farley    Notes:
32600910c330SBarry 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
32610026cea9SSean Farley    user is required to write their own TSComputeIFunction.
32620026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
32630026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
32640026cea9SSean Farley 
32650026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
32660026cea9SSean Farley @*/
32670910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
32680026cea9SSean Farley {
32690026cea9SSean Farley   PetscErrorCode ierr;
32700026cea9SSean Farley   Mat            A,B;
32710026cea9SSean Farley   MatStructure   flg2;
32720026cea9SSean Farley 
32730026cea9SSean Farley   PetscFunctionBegin;
32740026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
32750910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
32760910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
32770026cea9SSean Farley   PetscFunctionReturn(0);
32780026cea9SSean Farley }
32790026cea9SSean Farley 
32800026cea9SSean Farley #undef __FUNCT__
32810026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
32820026cea9SSean Farley /*@C
328324e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
32840026cea9SSean Farley 
32850026cea9SSean Farley    Collective on TS
32860026cea9SSean Farley 
32870026cea9SSean Farley    Input Arguments:
32880026cea9SSean Farley +  ts - time stepping context
32890026cea9SSean Farley .  t - time at which to evaluate
32900910c330SBarry Smith .  U - state at which to evaluate
32910910c330SBarry Smith .  Udot - time derivative of state vector
32920026cea9SSean Farley .  shift - shift to apply
32930026cea9SSean Farley -  ctx - context
32940026cea9SSean Farley 
32950026cea9SSean Farley    Output Arguments:
32960026cea9SSean Farley +  A - pointer to operator
32970026cea9SSean Farley .  B - pointer to preconditioning matrix
32980026cea9SSean Farley -  flg - matrix structure flag
32990026cea9SSean Farley 
33000026cea9SSean Farley    Level: intermediate
33010026cea9SSean Farley 
33020026cea9SSean Farley    Notes:
33030026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
33040026cea9SSean Farley 
33050026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
33060026cea9SSean Farley @*/
33070910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
33080026cea9SSean Farley {
33090026cea9SSean Farley   PetscFunctionBegin;
33100026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
33110026cea9SSean Farley   PetscFunctionReturn(0);
33120026cea9SSean Farley }
3313e817cc15SEmil Constantinescu #undef __FUNCT__
3314e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3315e817cc15SEmil Constantinescu /*@
3316e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3317e817cc15SEmil Constantinescu 
3318e817cc15SEmil Constantinescu    Not Collective
3319e817cc15SEmil Constantinescu 
3320e817cc15SEmil Constantinescu    Input Parameter:
3321e817cc15SEmil Constantinescu .  ts - the TS context
3322e817cc15SEmil Constantinescu 
3323e817cc15SEmil Constantinescu    Output Parameter:
3324e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3325e817cc15SEmil Constantinescu 
3326e817cc15SEmil Constantinescu    Level: beginner
3327e817cc15SEmil Constantinescu 
3328e817cc15SEmil Constantinescu .keywords: TS, equation type
3329e817cc15SEmil Constantinescu 
3330e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3331e817cc15SEmil Constantinescu @*/
3332e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3333e817cc15SEmil Constantinescu {
3334e817cc15SEmil Constantinescu   PetscFunctionBegin;
3335e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3336e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3337e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3338e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3339e817cc15SEmil Constantinescu }
3340e817cc15SEmil Constantinescu 
3341e817cc15SEmil Constantinescu #undef __FUNCT__
3342e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3343e817cc15SEmil Constantinescu /*@
3344e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3345e817cc15SEmil Constantinescu 
3346e817cc15SEmil Constantinescu    Not Collective
3347e817cc15SEmil Constantinescu 
3348e817cc15SEmil Constantinescu    Input Parameter:
3349e817cc15SEmil Constantinescu +  ts - the TS context
3350e817cc15SEmil Constantinescu .  equation_type - see TSEquatioType
3351e817cc15SEmil Constantinescu 
3352e817cc15SEmil Constantinescu    Level: advanced
3353e817cc15SEmil Constantinescu 
3354e817cc15SEmil Constantinescu .keywords: TS, equation type
3355e817cc15SEmil Constantinescu 
3356e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3357e817cc15SEmil Constantinescu @*/
3358e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3359e817cc15SEmil Constantinescu {
3360e817cc15SEmil Constantinescu   PetscFunctionBegin;
3361e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3362e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3363e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3364e817cc15SEmil Constantinescu }
33650026cea9SSean Farley 
33664af1b03aSJed Brown #undef __FUNCT__
33674af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
33684af1b03aSJed Brown /*@
33694af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
33704af1b03aSJed Brown 
33714af1b03aSJed Brown    Not Collective
33724af1b03aSJed Brown 
33734af1b03aSJed Brown    Input Parameter:
33744af1b03aSJed Brown .  ts - the TS context
33754af1b03aSJed Brown 
33764af1b03aSJed Brown    Output Parameter:
33774af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
33784af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
33794af1b03aSJed Brown 
3380487e0bb9SJed Brown    Level: beginner
33814af1b03aSJed Brown 
3382cd652676SJed Brown    Notes:
3383cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
33844af1b03aSJed Brown 
33854af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
33864af1b03aSJed Brown 
33874af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
33884af1b03aSJed Brown @*/
33894af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
33904af1b03aSJed Brown {
33914af1b03aSJed Brown   PetscFunctionBegin;
33924af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33934af1b03aSJed Brown   PetscValidPointer(reason,2);
33944af1b03aSJed Brown   *reason = ts->reason;
33954af1b03aSJed Brown   PetscFunctionReturn(0);
33964af1b03aSJed Brown }
33974af1b03aSJed Brown 
3398fb1732b5SBarry Smith #undef __FUNCT__
3399d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3400d6ad946cSShri Abhyankar /*@
3401d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3402d6ad946cSShri Abhyankar 
3403d6ad946cSShri Abhyankar    Not Collective
3404d6ad946cSShri Abhyankar 
3405d6ad946cSShri Abhyankar    Input Parameter:
3406d6ad946cSShri Abhyankar +  ts - the TS context
3407d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3408d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3409d6ad946cSShri Abhyankar 
3410f5abba47SShri Abhyankar    Level: advanced
3411d6ad946cSShri Abhyankar 
3412d6ad946cSShri Abhyankar    Notes:
3413d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3414d6ad946cSShri Abhyankar 
3415d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3416d6ad946cSShri Abhyankar 
3417d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3418d6ad946cSShri Abhyankar @*/
3419d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3420d6ad946cSShri Abhyankar {
3421d6ad946cSShri Abhyankar   PetscFunctionBegin;
3422d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3423d6ad946cSShri Abhyankar   ts->reason = reason;
3424d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3425d6ad946cSShri Abhyankar }
3426d6ad946cSShri Abhyankar 
3427d6ad946cSShri Abhyankar #undef __FUNCT__
3428cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3429cc708dedSBarry Smith /*@
3430cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3431cc708dedSBarry Smith 
3432cc708dedSBarry Smith    Not Collective
3433cc708dedSBarry Smith 
3434cc708dedSBarry Smith    Input Parameter:
3435cc708dedSBarry Smith .  ts - the TS context
3436cc708dedSBarry Smith 
3437cc708dedSBarry Smith    Output Parameter:
3438cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3439cc708dedSBarry Smith 
3440487e0bb9SJed Brown    Level: beginner
3441cc708dedSBarry Smith 
3442cc708dedSBarry Smith    Notes:
3443cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3444cc708dedSBarry Smith 
3445cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3446cc708dedSBarry Smith 
3447cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3448cc708dedSBarry Smith @*/
3449cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3450cc708dedSBarry Smith {
3451cc708dedSBarry Smith   PetscFunctionBegin;
3452cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3453cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3454cc708dedSBarry Smith   *ftime = ts->solvetime;
3455cc708dedSBarry Smith   PetscFunctionReturn(0);
3456cc708dedSBarry Smith }
3457cc708dedSBarry Smith 
3458cc708dedSBarry Smith #undef __FUNCT__
34595ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
34609f67acb7SJed Brown /*@
34615ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
34629f67acb7SJed Brown    used by the time integrator.
34639f67acb7SJed Brown 
34649f67acb7SJed Brown    Not Collective
34659f67acb7SJed Brown 
34669f67acb7SJed Brown    Input Parameter:
34679f67acb7SJed Brown .  ts - TS context
34689f67acb7SJed Brown 
34699f67acb7SJed Brown    Output Parameter:
34709f67acb7SJed Brown .  nits - number of nonlinear iterations
34719f67acb7SJed Brown 
34729f67acb7SJed Brown    Notes:
34739f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
34749f67acb7SJed Brown 
34759f67acb7SJed Brown    Level: intermediate
34769f67acb7SJed Brown 
34779f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
34789f67acb7SJed Brown 
34795ef26d82SJed Brown .seealso:  TSGetKSPIterations()
34809f67acb7SJed Brown @*/
34815ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
34829f67acb7SJed Brown {
34839f67acb7SJed Brown   PetscFunctionBegin;
34849f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34859f67acb7SJed Brown   PetscValidIntPointer(nits,2);
34865ef26d82SJed Brown   *nits = ts->snes_its;
34879f67acb7SJed Brown   PetscFunctionReturn(0);
34889f67acb7SJed Brown }
34899f67acb7SJed Brown 
34909f67acb7SJed Brown #undef __FUNCT__
34915ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
34929f67acb7SJed Brown /*@
34935ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
34949f67acb7SJed Brown    used by the time integrator.
34959f67acb7SJed Brown 
34969f67acb7SJed Brown    Not Collective
34979f67acb7SJed Brown 
34989f67acb7SJed Brown    Input Parameter:
34999f67acb7SJed Brown .  ts - TS context
35009f67acb7SJed Brown 
35019f67acb7SJed Brown    Output Parameter:
35029f67acb7SJed Brown .  lits - number of linear iterations
35039f67acb7SJed Brown 
35049f67acb7SJed Brown    Notes:
35059f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
35069f67acb7SJed Brown 
35079f67acb7SJed Brown    Level: intermediate
35089f67acb7SJed Brown 
35099f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
35109f67acb7SJed Brown 
35115ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
35129f67acb7SJed Brown @*/
35135ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
35149f67acb7SJed Brown {
35159f67acb7SJed Brown   PetscFunctionBegin;
35169f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35179f67acb7SJed Brown   PetscValidIntPointer(lits,2);
35185ef26d82SJed Brown   *lits = ts->ksp_its;
35199f67acb7SJed Brown   PetscFunctionReturn(0);
35209f67acb7SJed Brown }
35219f67acb7SJed Brown 
35229f67acb7SJed Brown #undef __FUNCT__
3523cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3524cef5090cSJed Brown /*@
3525cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3526cef5090cSJed Brown 
3527cef5090cSJed Brown    Not Collective
3528cef5090cSJed Brown 
3529cef5090cSJed Brown    Input Parameter:
3530cef5090cSJed Brown .  ts - TS context
3531cef5090cSJed Brown 
3532cef5090cSJed Brown    Output Parameter:
3533cef5090cSJed Brown .  rejects - number of steps rejected
3534cef5090cSJed Brown 
3535cef5090cSJed Brown    Notes:
3536cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3537cef5090cSJed Brown 
3538cef5090cSJed Brown    Level: intermediate
3539cef5090cSJed Brown 
3540cef5090cSJed Brown .keywords: TS, get, number
3541cef5090cSJed Brown 
35425ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3543cef5090cSJed Brown @*/
3544cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3545cef5090cSJed Brown {
3546cef5090cSJed Brown   PetscFunctionBegin;
3547cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3548cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3549cef5090cSJed Brown   *rejects = ts->reject;
3550cef5090cSJed Brown   PetscFunctionReturn(0);
3551cef5090cSJed Brown }
3552cef5090cSJed Brown 
3553cef5090cSJed Brown #undef __FUNCT__
3554cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3555cef5090cSJed Brown /*@
3556cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3557cef5090cSJed Brown 
3558cef5090cSJed Brown    Not Collective
3559cef5090cSJed Brown 
3560cef5090cSJed Brown    Input Parameter:
3561cef5090cSJed Brown .  ts - TS context
3562cef5090cSJed Brown 
3563cef5090cSJed Brown    Output Parameter:
3564cef5090cSJed Brown .  fails - number of failed nonlinear solves
3565cef5090cSJed Brown 
3566cef5090cSJed Brown    Notes:
3567cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3568cef5090cSJed Brown 
3569cef5090cSJed Brown    Level: intermediate
3570cef5090cSJed Brown 
3571cef5090cSJed Brown .keywords: TS, get, number
3572cef5090cSJed Brown 
35735ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3574cef5090cSJed Brown @*/
3575cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3576cef5090cSJed Brown {
3577cef5090cSJed Brown   PetscFunctionBegin;
3578cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3579cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3580cef5090cSJed Brown   *fails = ts->num_snes_failures;
3581cef5090cSJed Brown   PetscFunctionReturn(0);
3582cef5090cSJed Brown }
3583cef5090cSJed Brown 
3584cef5090cSJed Brown #undef __FUNCT__
3585cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3586cef5090cSJed Brown /*@
3587cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3588cef5090cSJed Brown 
3589cef5090cSJed Brown    Not Collective
3590cef5090cSJed Brown 
3591cef5090cSJed Brown    Input Parameter:
3592cef5090cSJed Brown +  ts - TS context
3593cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3594cef5090cSJed Brown 
3595cef5090cSJed Brown    Notes:
3596cef5090cSJed Brown    The counter is reset to zero for each step
3597cef5090cSJed Brown 
3598cef5090cSJed Brown    Options Database Key:
3599cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3600cef5090cSJed Brown 
3601cef5090cSJed Brown    Level: intermediate
3602cef5090cSJed Brown 
3603cef5090cSJed Brown .keywords: TS, set, maximum, number
3604cef5090cSJed Brown 
36055ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3606cef5090cSJed Brown @*/
3607cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3608cef5090cSJed Brown {
3609cef5090cSJed Brown   PetscFunctionBegin;
3610cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3611cef5090cSJed Brown   ts->max_reject = rejects;
3612cef5090cSJed Brown   PetscFunctionReturn(0);
3613cef5090cSJed Brown }
3614cef5090cSJed Brown 
3615cef5090cSJed Brown #undef __FUNCT__
3616cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3617cef5090cSJed Brown /*@
3618cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3619cef5090cSJed Brown 
3620cef5090cSJed Brown    Not Collective
3621cef5090cSJed Brown 
3622cef5090cSJed Brown    Input Parameter:
3623cef5090cSJed Brown +  ts - TS context
3624cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3625cef5090cSJed Brown 
3626cef5090cSJed Brown    Notes:
3627cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3628cef5090cSJed Brown 
3629cef5090cSJed Brown    Options Database Key:
3630cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3631cef5090cSJed Brown 
3632cef5090cSJed Brown    Level: intermediate
3633cef5090cSJed Brown 
3634cef5090cSJed Brown .keywords: TS, set, maximum, number
3635cef5090cSJed Brown 
36365ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3637cef5090cSJed Brown @*/
3638cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3639cef5090cSJed Brown {
3640cef5090cSJed Brown   PetscFunctionBegin;
3641cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3642cef5090cSJed Brown   ts->max_snes_failures = fails;
3643cef5090cSJed Brown   PetscFunctionReturn(0);
3644cef5090cSJed Brown }
3645cef5090cSJed Brown 
3646cef5090cSJed Brown #undef __FUNCT__
3647cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3648cef5090cSJed Brown /*@
3649cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3650cef5090cSJed Brown 
3651cef5090cSJed Brown    Not Collective
3652cef5090cSJed Brown 
3653cef5090cSJed Brown    Input Parameter:
3654cef5090cSJed Brown +  ts - TS context
3655cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3656cef5090cSJed Brown 
3657cef5090cSJed Brown    Options Database Key:
3658cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3659cef5090cSJed Brown 
3660cef5090cSJed Brown    Level: intermediate
3661cef5090cSJed Brown 
3662cef5090cSJed Brown .keywords: TS, set, error
3663cef5090cSJed Brown 
36645ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3665cef5090cSJed Brown @*/
3666cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3667cef5090cSJed Brown {
3668cef5090cSJed Brown   PetscFunctionBegin;
3669cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3670cef5090cSJed Brown   ts->errorifstepfailed = err;
3671cef5090cSJed Brown   PetscFunctionReturn(0);
3672cef5090cSJed Brown }
3673cef5090cSJed Brown 
3674cef5090cSJed Brown #undef __FUNCT__
3675fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3676fb1732b5SBarry Smith /*@C
3677fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3678fb1732b5SBarry Smith 
3679fb1732b5SBarry Smith    Collective on TS
3680fb1732b5SBarry Smith 
3681fb1732b5SBarry Smith    Input Parameters:
3682fb1732b5SBarry Smith +  ts - the TS context
3683fb1732b5SBarry Smith .  step - current time-step
3684fb1732b5SBarry Smith .  ptime - current time
36850910c330SBarry Smith .  u - current state
3686fb1732b5SBarry Smith -  viewer - binary viewer
3687fb1732b5SBarry Smith 
3688fb1732b5SBarry Smith    Level: intermediate
3689fb1732b5SBarry Smith 
3690fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3691fb1732b5SBarry Smith 
3692fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3693fb1732b5SBarry Smith @*/
36940910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3695fb1732b5SBarry Smith {
3696fb1732b5SBarry Smith   PetscErrorCode ierr;
3697ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3698fb1732b5SBarry Smith 
3699fb1732b5SBarry Smith   PetscFunctionBegin;
37000910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3701ed81e22dSJed Brown   PetscFunctionReturn(0);
3702ed81e22dSJed Brown }
3703ed81e22dSJed Brown 
3704ed81e22dSJed Brown #undef __FUNCT__
3705ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3706ed81e22dSJed Brown /*@C
3707ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3708ed81e22dSJed Brown 
3709ed81e22dSJed Brown    Collective on TS
3710ed81e22dSJed Brown 
3711ed81e22dSJed Brown    Input Parameters:
3712ed81e22dSJed Brown +  ts - the TS context
3713ed81e22dSJed Brown .  step - current time-step
3714ed81e22dSJed Brown .  ptime - current time
37150910c330SBarry Smith .  u - current state
3716ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3717ed81e22dSJed Brown 
3718ed81e22dSJed Brown    Level: intermediate
3719ed81e22dSJed Brown 
3720ed81e22dSJed Brown    Notes:
3721ed81e22dSJed 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.
3722ed81e22dSJed Brown    These are named according to the file name template.
3723ed81e22dSJed Brown 
3724ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3725ed81e22dSJed Brown 
3726ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3727ed81e22dSJed Brown 
3728ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3729ed81e22dSJed Brown @*/
37300910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3731ed81e22dSJed Brown {
3732ed81e22dSJed Brown   PetscErrorCode ierr;
3733ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3734ed81e22dSJed Brown   PetscViewer    viewer;
3735ed81e22dSJed Brown 
3736ed81e22dSJed Brown   PetscFunctionBegin;
37378caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3738ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
37390910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3740ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3741ed81e22dSJed Brown   PetscFunctionReturn(0);
3742ed81e22dSJed Brown }
3743ed81e22dSJed Brown 
3744ed81e22dSJed Brown #undef __FUNCT__
3745ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3746ed81e22dSJed Brown /*@C
3747ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3748ed81e22dSJed Brown 
3749ed81e22dSJed Brown    Collective on TS
3750ed81e22dSJed Brown 
3751ed81e22dSJed Brown    Input Parameters:
3752ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3753ed81e22dSJed Brown 
3754ed81e22dSJed Brown    Level: intermediate
3755ed81e22dSJed Brown 
3756ed81e22dSJed Brown    Note:
3757ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3758ed81e22dSJed Brown 
3759ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3760ed81e22dSJed Brown 
3761ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3762ed81e22dSJed Brown @*/
3763ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3764ed81e22dSJed Brown {
3765ed81e22dSJed Brown   PetscErrorCode ierr;
3766ed81e22dSJed Brown 
3767ed81e22dSJed Brown   PetscFunctionBegin;
3768ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3769fb1732b5SBarry Smith   PetscFunctionReturn(0);
3770fb1732b5SBarry Smith }
3771fb1732b5SBarry Smith 
377284df9cb4SJed Brown #undef __FUNCT__
3773ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt"
377484df9cb4SJed Brown /*@
3775ad6bc421SBarry Smith    TSGetTSAdapt - Get the adaptive controller context for the current method
377684df9cb4SJed Brown 
3777ed81e22dSJed Brown    Collective on TS if controller has not been created yet
377884df9cb4SJed Brown 
377984df9cb4SJed Brown    Input Arguments:
3780ed81e22dSJed Brown .  ts - time stepping context
378184df9cb4SJed Brown 
378284df9cb4SJed Brown    Output Arguments:
3783ed81e22dSJed Brown .  adapt - adaptive controller
378484df9cb4SJed Brown 
378584df9cb4SJed Brown    Level: intermediate
378684df9cb4SJed Brown 
3787ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
378884df9cb4SJed Brown @*/
3789ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt)
379084df9cb4SJed Brown {
379184df9cb4SJed Brown   PetscErrorCode ierr;
379284df9cb4SJed Brown 
379384df9cb4SJed Brown   PetscFunctionBegin;
379484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
379584df9cb4SJed Brown   PetscValidPointer(adapt,2);
379684df9cb4SJed Brown   if (!ts->adapt) {
379784df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
37981c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
37991c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
380084df9cb4SJed Brown   }
380184df9cb4SJed Brown   *adapt = ts->adapt;
380284df9cb4SJed Brown   PetscFunctionReturn(0);
380384df9cb4SJed Brown }
3804d6ebe24aSShri Abhyankar 
3805d6ebe24aSShri Abhyankar #undef __FUNCT__
38061c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
38071c3436cfSJed Brown /*@
38081c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
38091c3436cfSJed Brown 
38101c3436cfSJed Brown    Logically Collective
38111c3436cfSJed Brown 
38121c3436cfSJed Brown    Input Arguments:
38131c3436cfSJed Brown +  ts - time integration context
38141c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
38151c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
38161c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
38171c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
38181c3436cfSJed Brown 
38191c3436cfSJed Brown    Level: beginner
38201c3436cfSJed Brown 
3821c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
38221c3436cfSJed Brown @*/
38231c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
38241c3436cfSJed Brown {
38251c3436cfSJed Brown   PetscErrorCode ierr;
38261c3436cfSJed Brown 
38271c3436cfSJed Brown   PetscFunctionBegin;
3828c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
38291c3436cfSJed Brown   if (vatol) {
38301c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
38311c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
3832*bbd56ea5SKarl Rupp 
38331c3436cfSJed Brown     ts->vatol = vatol;
38341c3436cfSJed Brown   }
3835c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
38361c3436cfSJed Brown   if (vrtol) {
38371c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
38381c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
3839*bbd56ea5SKarl Rupp 
38401c3436cfSJed Brown     ts->vrtol = vrtol;
38411c3436cfSJed Brown   }
38421c3436cfSJed Brown   PetscFunctionReturn(0);
38431c3436cfSJed Brown }
38441c3436cfSJed Brown 
38451c3436cfSJed Brown #undef __FUNCT__
3846c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3847c5033834SJed Brown /*@
3848c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3849c5033834SJed Brown 
3850c5033834SJed Brown    Logically Collective
3851c5033834SJed Brown 
3852c5033834SJed Brown    Input Arguments:
3853c5033834SJed Brown .  ts - time integration context
3854c5033834SJed Brown 
3855c5033834SJed Brown    Output Arguments:
3856c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3857c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3858c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3859c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3860c5033834SJed Brown 
3861c5033834SJed Brown    Level: beginner
3862c5033834SJed Brown 
3863c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3864c5033834SJed Brown @*/
3865c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3866c5033834SJed Brown {
3867c5033834SJed Brown   PetscFunctionBegin;
3868c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3869c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3870c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3871c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3872c5033834SJed Brown   PetscFunctionReturn(0);
3873c5033834SJed Brown }
3874c5033834SJed Brown 
3875c5033834SJed Brown #undef __FUNCT__
38761c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
38771c3436cfSJed Brown /*@
38781c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
38791c3436cfSJed Brown 
38801c3436cfSJed Brown    Collective on TS
38811c3436cfSJed Brown 
38821c3436cfSJed Brown    Input Arguments:
38831c3436cfSJed Brown +  ts - time stepping context
38841c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
38851c3436cfSJed Brown 
38861c3436cfSJed Brown    Output Arguments:
38871c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
38881c3436cfSJed Brown 
38891c3436cfSJed Brown    Level: developer
38901c3436cfSJed Brown 
38911c3436cfSJed Brown .seealso: TSSetTolerances()
38921c3436cfSJed Brown @*/
38931c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
38941c3436cfSJed Brown {
38951c3436cfSJed Brown   PetscErrorCode    ierr;
38961c3436cfSJed Brown   PetscInt          i,n,N;
38970910c330SBarry Smith   const PetscScalar *u,*y;
38980910c330SBarry Smith   Vec               U;
38991c3436cfSJed Brown   PetscReal         sum,gsum;
39001c3436cfSJed Brown 
39011c3436cfSJed Brown   PetscFunctionBegin;
39021c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39031c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
39041c3436cfSJed Brown   PetscValidPointer(norm,3);
39050910c330SBarry Smith   U = ts->vec_sol;
39060910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
39070910c330SBarry Smith   if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
39081c3436cfSJed Brown 
39090910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
39100910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
39110910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
39121c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
39131c3436cfSJed Brown   sum  = 0.;
3914ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3915ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3916ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3917ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3918ceefc113SJed Brown     for (i=0; i<n; i++) {
39190910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39200910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3921ceefc113SJed Brown     }
3922ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3923ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3924ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3925ceefc113SJed Brown     const PetscScalar *atol;
3926ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3927ceefc113SJed Brown     for (i=0; i<n; i++) {
39280910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39290910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3930ceefc113SJed Brown     }
3931ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3932ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3933ceefc113SJed Brown     const PetscScalar *rtol;
3934ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3935ceefc113SJed Brown     for (i=0; i<n; i++) {
39360910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39370910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3938ceefc113SJed Brown     }
3939ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3940ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
39411c3436cfSJed Brown     for (i=0; i<n; i++) {
39420910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
39430910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
39441c3436cfSJed Brown     }
3945ceefc113SJed Brown   }
39460910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
39471c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
39481c3436cfSJed Brown 
39491c3436cfSJed Brown   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
39501c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
39511c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
39521c3436cfSJed Brown   PetscFunctionReturn(0);
39531c3436cfSJed Brown }
39541c3436cfSJed Brown 
39551c3436cfSJed Brown #undef __FUNCT__
39568d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
39578d59e960SJed Brown /*@
39588d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
39598d59e960SJed Brown 
39608d59e960SJed Brown    Logically Collective on TS
39618d59e960SJed Brown 
39628d59e960SJed Brown    Input Arguments:
39638d59e960SJed Brown +  ts - time stepping context
39648d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
39658d59e960SJed Brown 
39668d59e960SJed Brown    Note:
39678d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
39688d59e960SJed Brown 
39698d59e960SJed Brown    Level: intermediate
39708d59e960SJed Brown 
39718d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
39728d59e960SJed Brown @*/
39738d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
39748d59e960SJed Brown {
39758d59e960SJed Brown   PetscFunctionBegin;
39768d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39778d59e960SJed Brown   ts->cfltime_local = cfltime;
39788d59e960SJed Brown   ts->cfltime       = -1.;
39798d59e960SJed Brown   PetscFunctionReturn(0);
39808d59e960SJed Brown }
39818d59e960SJed Brown 
39828d59e960SJed Brown #undef __FUNCT__
39838d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
39848d59e960SJed Brown /*@
39858d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
39868d59e960SJed Brown 
39878d59e960SJed Brown    Collective on TS
39888d59e960SJed Brown 
39898d59e960SJed Brown    Input Arguments:
39908d59e960SJed Brown .  ts - time stepping context
39918d59e960SJed Brown 
39928d59e960SJed Brown    Output Arguments:
39938d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
39948d59e960SJed Brown 
39958d59e960SJed Brown    Level: advanced
39968d59e960SJed Brown 
39978d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
39988d59e960SJed Brown @*/
39998d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
40008d59e960SJed Brown {
40018d59e960SJed Brown   PetscErrorCode ierr;
40028d59e960SJed Brown 
40038d59e960SJed Brown   PetscFunctionBegin;
40048d59e960SJed Brown   if (ts->cfltime < 0) {
40058d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
40068d59e960SJed Brown   }
40078d59e960SJed Brown   *cfltime = ts->cfltime;
40088d59e960SJed Brown   PetscFunctionReturn(0);
40098d59e960SJed Brown }
40108d59e960SJed Brown 
40118d59e960SJed Brown #undef __FUNCT__
4012d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4013d6ebe24aSShri Abhyankar /*@
4014d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4015d6ebe24aSShri Abhyankar 
4016d6ebe24aSShri Abhyankar    Input Parameters:
4017d6ebe24aSShri Abhyankar .  ts   - the TS context.
4018d6ebe24aSShri Abhyankar .  xl   - lower bound.
4019d6ebe24aSShri Abhyankar .  xu   - upper bound.
4020d6ebe24aSShri Abhyankar 
4021d6ebe24aSShri Abhyankar    Notes:
4022d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
402333c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4024d6ebe24aSShri Abhyankar 
40252bd2b0e6SSatish Balay    Level: advanced
40262bd2b0e6SSatish Balay 
4027d6ebe24aSShri Abhyankar @*/
4028d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4029d6ebe24aSShri Abhyankar {
4030d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4031d6ebe24aSShri Abhyankar   SNES           snes;
4032d6ebe24aSShri Abhyankar 
4033d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4034d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4035d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4036d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4037d6ebe24aSShri Abhyankar }
4038d6ebe24aSShri Abhyankar 
4039325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4040c6db04a5SJed Brown #include <mex.h>
4041325fc9f4SBarry Smith 
4042325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4043325fc9f4SBarry Smith 
4044325fc9f4SBarry Smith #undef __FUNCT__
4045325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4046325fc9f4SBarry Smith /*
4047325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4048325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4049325fc9f4SBarry Smith 
4050325fc9f4SBarry Smith    Collective on TS
4051325fc9f4SBarry Smith 
4052325fc9f4SBarry Smith    Input Parameters:
4053325fc9f4SBarry Smith +  snes - the TS context
40540910c330SBarry Smith -  u - input vector
4055325fc9f4SBarry Smith 
4056325fc9f4SBarry Smith    Output Parameter:
4057325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4058325fc9f4SBarry Smith 
4059325fc9f4SBarry Smith    Notes:
4060325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4061325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4062325fc9f4SBarry Smith    themselves.
4063325fc9f4SBarry Smith 
4064325fc9f4SBarry Smith    Level: developer
4065325fc9f4SBarry Smith 
4066325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4067325fc9f4SBarry Smith 
4068325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4069325fc9f4SBarry Smith */
40700910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4071325fc9f4SBarry Smith {
4072325fc9f4SBarry Smith   PetscErrorCode  ierr;
4073325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4074325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4075325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4076325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4077325fc9f4SBarry Smith 
4078325fc9f4SBarry Smith   PetscFunctionBegin;
4079325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
40800910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
40810910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4082325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
40830910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4084325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4085325fc9f4SBarry Smith 
4086325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
40870910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
40880910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
40890910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4090*bbd56ea5SKarl Rupp 
4091325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4092325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4093325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4094325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4095325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4096325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4097325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4098325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4099325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4100325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4101325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4102325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4103325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4104325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4105325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4106325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4107325fc9f4SBarry Smith   PetscFunctionReturn(0);
4108325fc9f4SBarry Smith }
4109325fc9f4SBarry Smith 
4110325fc9f4SBarry Smith 
4111325fc9f4SBarry Smith #undef __FUNCT__
4112325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4113325fc9f4SBarry Smith /*
4114325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4115325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4116e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4117325fc9f4SBarry Smith 
4118325fc9f4SBarry Smith    Logically Collective on TS
4119325fc9f4SBarry Smith 
4120325fc9f4SBarry Smith    Input Parameters:
4121325fc9f4SBarry Smith +  ts - the TS context
4122325fc9f4SBarry Smith -  func - function evaluation routine
4123325fc9f4SBarry Smith 
4124325fc9f4SBarry Smith    Calling sequence of func:
41250910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4126325fc9f4SBarry Smith 
4127325fc9f4SBarry Smith    Level: beginner
4128325fc9f4SBarry Smith 
4129325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4130325fc9f4SBarry Smith 
4131325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4132325fc9f4SBarry Smith */
4133cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4134325fc9f4SBarry Smith {
4135325fc9f4SBarry Smith   PetscErrorCode  ierr;
4136325fc9f4SBarry Smith   TSMatlabContext *sctx;
4137325fc9f4SBarry Smith 
4138325fc9f4SBarry Smith   PetscFunctionBegin;
4139325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4140325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4141325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4142325fc9f4SBarry Smith   /*
4143325fc9f4SBarry Smith      This should work, but it doesn't
4144325fc9f4SBarry Smith   sctx->ctx = ctx;
4145325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4146325fc9f4SBarry Smith   */
4147325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4148*bbd56ea5SKarl Rupp 
4149cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4150325fc9f4SBarry Smith   PetscFunctionReturn(0);
4151325fc9f4SBarry Smith }
4152325fc9f4SBarry Smith 
4153325fc9f4SBarry Smith #undef __FUNCT__
4154325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4155325fc9f4SBarry Smith /*
4156325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4157325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4158325fc9f4SBarry Smith 
4159325fc9f4SBarry Smith    Collective on TS
4160325fc9f4SBarry Smith 
4161325fc9f4SBarry Smith    Input Parameters:
4162cdcf91faSSean Farley +  ts - the TS context
41630910c330SBarry Smith .  u - input vector
4164325fc9f4SBarry Smith .  A, B - the matrices
4165325fc9f4SBarry Smith -  ctx - user context
4166325fc9f4SBarry Smith 
4167325fc9f4SBarry Smith    Output Parameter:
4168325fc9f4SBarry Smith .  flag - structure of the matrix
4169325fc9f4SBarry Smith 
4170325fc9f4SBarry Smith    Level: developer
4171325fc9f4SBarry Smith 
4172325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4173325fc9f4SBarry Smith 
4174325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4175325fc9f4SBarry Smith @*/
41760910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4177325fc9f4SBarry Smith {
4178325fc9f4SBarry Smith   PetscErrorCode  ierr;
4179325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4180325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4181325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4182325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4183325fc9f4SBarry Smith 
4184325fc9f4SBarry Smith   PetscFunctionBegin;
4185cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41860910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4187325fc9f4SBarry Smith 
41880910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4189325fc9f4SBarry Smith 
4190cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
41910910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
41920910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
41930910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
41940910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4195*bbd56ea5SKarl Rupp 
4196325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4197325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4198325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4199325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4200325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4201325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4202325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4203325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4204325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4205325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4206325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4207325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4208325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4209325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4210325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4211325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4212325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4213325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4214325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4215325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4216325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4217325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4218325fc9f4SBarry Smith   PetscFunctionReturn(0);
4219325fc9f4SBarry Smith }
4220325fc9f4SBarry Smith 
4221325fc9f4SBarry Smith 
4222325fc9f4SBarry Smith #undef __FUNCT__
4223325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4224325fc9f4SBarry Smith /*
4225325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4226e3c5b3baSBarry 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
4227325fc9f4SBarry Smith 
4228325fc9f4SBarry Smith    Logically Collective on TS
4229325fc9f4SBarry Smith 
4230325fc9f4SBarry Smith    Input Parameters:
4231cdcf91faSSean Farley +  ts - the TS context
4232325fc9f4SBarry Smith .  A,B - Jacobian matrices
4233325fc9f4SBarry Smith .  func - function evaluation routine
4234325fc9f4SBarry Smith -  ctx - user context
4235325fc9f4SBarry Smith 
4236325fc9f4SBarry Smith    Calling sequence of func:
42370910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4238325fc9f4SBarry Smith 
4239325fc9f4SBarry Smith 
4240325fc9f4SBarry Smith    Level: developer
4241325fc9f4SBarry Smith 
4242325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4243325fc9f4SBarry Smith 
4244325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4245325fc9f4SBarry Smith */
4246cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4247325fc9f4SBarry Smith {
4248325fc9f4SBarry Smith   PetscErrorCode  ierr;
4249325fc9f4SBarry Smith   TSMatlabContext *sctx;
4250325fc9f4SBarry Smith 
4251325fc9f4SBarry Smith   PetscFunctionBegin;
4252325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4253325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4254325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4255325fc9f4SBarry Smith   /*
4256325fc9f4SBarry Smith      This should work, but it doesn't
4257325fc9f4SBarry Smith   sctx->ctx = ctx;
4258325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4259325fc9f4SBarry Smith   */
4260325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4261*bbd56ea5SKarl Rupp 
4262cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4263325fc9f4SBarry Smith   PetscFunctionReturn(0);
4264325fc9f4SBarry Smith }
4265325fc9f4SBarry Smith 
4266b5b1a830SBarry Smith #undef __FUNCT__
4267b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4268b5b1a830SBarry Smith /*
4269b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4270b5b1a830SBarry Smith 
4271b5b1a830SBarry Smith    Collective on TS
4272b5b1a830SBarry Smith 
4273b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4274b5b1a830SBarry Smith @*/
42750910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4276b5b1a830SBarry Smith {
4277b5b1a830SBarry Smith   PetscErrorCode  ierr;
4278b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4279a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4280b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4281b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4282b5b1a830SBarry Smith 
4283b5b1a830SBarry Smith   PetscFunctionBegin;
4284cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42850910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4286b5b1a830SBarry Smith 
4287cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
42880910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4289*bbd56ea5SKarl Rupp 
4290b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4291b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4292b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4293b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4294b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4295b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4296b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4297b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4298b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4299b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4300b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4301b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4302b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4303b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4304b5b1a830SBarry Smith   PetscFunctionReturn(0);
4305b5b1a830SBarry Smith }
4306b5b1a830SBarry Smith 
4307b5b1a830SBarry Smith 
4308b5b1a830SBarry Smith #undef __FUNCT__
4309b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4310b5b1a830SBarry Smith /*
4311b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4312b5b1a830SBarry Smith 
4313b5b1a830SBarry Smith    Level: developer
4314b5b1a830SBarry Smith 
4315b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4316b5b1a830SBarry Smith 
4317b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4318b5b1a830SBarry Smith */
4319cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4320b5b1a830SBarry Smith {
4321b5b1a830SBarry Smith   PetscErrorCode  ierr;
4322b5b1a830SBarry Smith   TSMatlabContext *sctx;
4323b5b1a830SBarry Smith 
4324b5b1a830SBarry Smith   PetscFunctionBegin;
4325b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4326b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4327b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4328b5b1a830SBarry Smith   /*
4329b5b1a830SBarry Smith      This should work, but it doesn't
4330b5b1a830SBarry Smith   sctx->ctx = ctx;
4331b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4332b5b1a830SBarry Smith   */
4333b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4334*bbd56ea5SKarl Rupp 
4335cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
4336b5b1a830SBarry Smith   PetscFunctionReturn(0);
4337b5b1a830SBarry Smith }
4338325fc9f4SBarry Smith #endif
4339b3603a34SBarry Smith 
43400b039ecaSBarry Smith 
43410b039ecaSBarry Smith 
4342b3603a34SBarry Smith #undef __FUNCT__
43434f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4344b3603a34SBarry Smith /*@C
43454f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4346b3603a34SBarry Smith        in a time based line graph
4347b3603a34SBarry Smith 
4348b3603a34SBarry Smith    Collective on TS
4349b3603a34SBarry Smith 
4350b3603a34SBarry Smith    Input Parameters:
4351b3603a34SBarry Smith +  ts - the TS context
4352b3603a34SBarry Smith .  step - current time-step
4353b3603a34SBarry Smith .  ptime - current time
4354b3603a34SBarry Smith -  lg - a line graph object
4355b3603a34SBarry Smith 
4356b3603a34SBarry Smith    Level: intermediate
4357b3603a34SBarry Smith 
43580b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4359b3603a34SBarry Smith 
4360b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4361b3603a34SBarry Smith 
4362b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4363b3603a34SBarry Smith @*/
43640910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4365b3603a34SBarry Smith {
4366b3603a34SBarry Smith   PetscErrorCode    ierr;
43670b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4368b3603a34SBarry Smith   const PetscScalar *yy;
436958ff32f7SBarry Smith   PetscInt          dim;
4370b3603a34SBarry Smith 
4371b3603a34SBarry Smith   PetscFunctionBegin;
437258ff32f7SBarry Smith   if (!step) {
4373a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4374a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4375a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
43760910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
43770b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
43780b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
437958ff32f7SBarry Smith   }
43800910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4381e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4382e3efe391SJed Brown   {
4383e3efe391SJed Brown     PetscReal *yreal;
4384e3efe391SJed Brown     PetscInt  i,n;
43850910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4386e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4387e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
43880b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4389e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4390e3efe391SJed Brown   }
4391e3efe391SJed Brown #else
43920b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4393e3efe391SJed Brown #endif
43940910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
43953fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
43960b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43973923b477SBarry Smith   }
4398b3603a34SBarry Smith   PetscFunctionReturn(0);
4399b3603a34SBarry Smith }
4400b3603a34SBarry Smith 
4401b3603a34SBarry Smith #undef __FUNCT__
44024f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4403ef20d060SBarry Smith /*@C
44044f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4405ef20d060SBarry Smith        in a time based line graph
4406ef20d060SBarry Smith 
4407ef20d060SBarry Smith    Collective on TS
4408ef20d060SBarry Smith 
4409ef20d060SBarry Smith    Input Parameters:
4410ef20d060SBarry Smith +  ts - the TS context
4411ef20d060SBarry Smith .  step - current time-step
4412ef20d060SBarry Smith .  ptime - current time
4413ef20d060SBarry Smith -  lg - a line graph object
4414ef20d060SBarry Smith 
4415ef20d060SBarry Smith    Level: intermediate
4416ef20d060SBarry Smith 
4417abd5a294SJed Brown    Notes:
4418abd5a294SJed Brown    Only for sequential solves.
4419abd5a294SJed Brown 
4420abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4421abd5a294SJed Brown 
4422abd5a294SJed Brown    Options Database Keys:
44234f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4424ef20d060SBarry Smith 
4425ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4426ef20d060SBarry Smith 
4427abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4428ef20d060SBarry Smith @*/
44290910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4430ef20d060SBarry Smith {
4431ef20d060SBarry Smith   PetscErrorCode    ierr;
44320b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4433ef20d060SBarry Smith   const PetscScalar *yy;
4434ef20d060SBarry Smith   Vec               y;
4435a9f9c1f6SBarry Smith   PetscInt          dim;
4436ef20d060SBarry Smith 
4437ef20d060SBarry Smith   PetscFunctionBegin;
4438a9f9c1f6SBarry Smith   if (!step) {
4439a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4440a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
44411ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
44420910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4443a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4444a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4445a9f9c1f6SBarry Smith   }
44460910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4447ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
44480910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4449ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4450e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4451e3efe391SJed Brown   {
4452e3efe391SJed Brown     PetscReal *yreal;
4453e3efe391SJed Brown     PetscInt  i,n;
4454e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4455e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4456e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
44570b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4458e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4459e3efe391SJed Brown   }
4460e3efe391SJed Brown #else
44610b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4462e3efe391SJed Brown #endif
4463ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4464ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
44653fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
44660b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
44673923b477SBarry Smith   }
4468ef20d060SBarry Smith   PetscFunctionReturn(0);
4469ef20d060SBarry Smith }
4470ef20d060SBarry Smith 
4471201da799SBarry Smith #undef __FUNCT__
4472201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4473201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4474201da799SBarry Smith {
4475201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4476201da799SBarry Smith   PetscReal      x   = ptime,y;
4477201da799SBarry Smith   PetscErrorCode ierr;
4478201da799SBarry Smith   PetscInt       its;
4479201da799SBarry Smith 
4480201da799SBarry Smith   PetscFunctionBegin;
4481201da799SBarry Smith   if (!n) {
4482201da799SBarry Smith     PetscDrawAxis axis;
4483*bbd56ea5SKarl Rupp 
4484201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4485201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4486201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4487*bbd56ea5SKarl Rupp 
4488201da799SBarry Smith     ctx->snes_its = 0;
4489201da799SBarry Smith   }
4490201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4491201da799SBarry Smith   y    = its - ctx->snes_its;
4492201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
44933fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4494201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4495201da799SBarry Smith   }
4496201da799SBarry Smith   ctx->snes_its = its;
4497201da799SBarry Smith   PetscFunctionReturn(0);
4498201da799SBarry Smith }
4499201da799SBarry Smith 
4500201da799SBarry Smith #undef __FUNCT__
4501201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4502201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4503201da799SBarry Smith {
4504201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4505201da799SBarry Smith   PetscReal      x   = ptime,y;
4506201da799SBarry Smith   PetscErrorCode ierr;
4507201da799SBarry Smith   PetscInt       its;
4508201da799SBarry Smith 
4509201da799SBarry Smith   PetscFunctionBegin;
4510201da799SBarry Smith   if (!n) {
4511201da799SBarry Smith     PetscDrawAxis axis;
4512*bbd56ea5SKarl Rupp 
4513201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4514201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4515201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4516*bbd56ea5SKarl Rupp 
4517201da799SBarry Smith     ctx->ksp_its = 0;
4518201da799SBarry Smith   }
4519201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4520201da799SBarry Smith   y    = its - ctx->ksp_its;
4521201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
452299fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4523201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4524201da799SBarry Smith   }
4525201da799SBarry Smith   ctx->ksp_its = its;
4526201da799SBarry Smith   PetscFunctionReturn(0);
4527201da799SBarry Smith }
4528f9c1d6abSBarry Smith 
4529f9c1d6abSBarry Smith #undef __FUNCT__
4530f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4531f9c1d6abSBarry Smith /*@
4532f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4533f9c1d6abSBarry Smith 
4534f9c1d6abSBarry Smith    Collective on TS and Vec
4535f9c1d6abSBarry Smith 
4536f9c1d6abSBarry Smith    Input Parameters:
4537f9c1d6abSBarry Smith +  ts - the TS context
4538f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4539f9c1d6abSBarry Smith 
4540f9c1d6abSBarry Smith    Output Parameters:
4541f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4542f9c1d6abSBarry Smith 
4543f9c1d6abSBarry Smith    Level: developer
4544f9c1d6abSBarry Smith 
4545f9c1d6abSBarry Smith .keywords: TS, compute
4546f9c1d6abSBarry Smith 
4547f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4548f9c1d6abSBarry Smith @*/
4549f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4550f9c1d6abSBarry Smith {
4551f9c1d6abSBarry Smith   PetscErrorCode ierr;
4552f9c1d6abSBarry Smith 
4553f9c1d6abSBarry Smith   PetscFunctionBegin;
4554f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4555f9c1d6abSBarry Smith   if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4556f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4557f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4558f9c1d6abSBarry Smith }
4559