xref: /petsc/src/ts/interface/ts.c (revision 55849f575624b3890dfd9b38e2c3ba42c5d7d18f)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
4d763cef2SBarry Smith 
5d5ba7fb7SMatthew Knepley /* Logging support */
67087cfbeSBarry Smith PetscClassId  TS_CLASSID;
7166c7f25SBarry Smith PetscLogEvent  TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
8d405a339SMatthew Knepley 
94a2ae208SSatish Balay #undef __FUNCT__
10bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
11bdad233fSMatthew Knepley /*
12bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
13bdad233fSMatthew Knepley 
14bdad233fSMatthew Knepley   Collective on TS
15bdad233fSMatthew Knepley 
16bdad233fSMatthew Knepley   Input Parameter:
17bdad233fSMatthew Knepley . ts - The ts
18bdad233fSMatthew Knepley 
19bdad233fSMatthew Knepley   Level: intermediate
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
22bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
23bdad233fSMatthew Knepley */
246849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
25bdad233fSMatthew Knepley {
26ace3abfcSBarry Smith   PetscBool      opt;
272fc52814SBarry Smith   const char     *defaultType;
28bdad233fSMatthew Knepley   char           typeName[256];
29dfbe8321SBarry Smith   PetscErrorCode ierr;
30bdad233fSMatthew Knepley 
31bdad233fSMatthew Knepley   PetscFunctionBegin;
327adad957SLisandro Dalcin   if (((PetscObject)ts)->type_name) {
337adad957SLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
34bdad233fSMatthew Knepley   } else {
359596e0b4SJed Brown     defaultType = TSEULER;
36bdad233fSMatthew Knepley   }
37bdad233fSMatthew Knepley 
38cce0b1b2SLisandro Dalcin   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
39bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
40a7cc72afSBarry Smith   if (opt) {
41bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
42bdad233fSMatthew Knepley   } else {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   }
45bdad233fSMatthew Knepley   PetscFunctionReturn(0);
46bdad233fSMatthew Knepley }
47bdad233fSMatthew Knepley 
48bdad233fSMatthew Knepley #undef __FUNCT__
49bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
50bdad233fSMatthew Knepley /*@
51bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
52bdad233fSMatthew Knepley 
53bdad233fSMatthew Knepley    Collective on TS
54bdad233fSMatthew Knepley 
55bdad233fSMatthew Knepley    Input Parameter:
56bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
57bdad233fSMatthew Knepley 
58bdad233fSMatthew Knepley    Options Database Keys:
594d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
60bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
613bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
62bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
63bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
64de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
65de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
66de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
67de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
68de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
69de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
70de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
71de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
72de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
73de06c3feSJed Brown .  -ts_monitor_draw_solution_binary <filename> - Save each solution to a binary file
74de06c3feSJed Brown -  -ts_monitor_draw_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
75bdad233fSMatthew Knepley 
76bdad233fSMatthew Knepley    Level: beginner
77bdad233fSMatthew Knepley 
78bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
79bdad233fSMatthew Knepley 
80a313700dSBarry Smith .seealso: TSGetType()
81bdad233fSMatthew Knepley @*/
827087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
83bdad233fSMatthew Knepley {
84ace3abfcSBarry Smith   PetscBool      opt,flg;
85dfbe8321SBarry Smith   PetscErrorCode ierr;
86649052a6SBarry Smith   PetscViewer    monviewer;
87eabae89aSBarry Smith   char           monfilename[PETSC_MAX_PATH_LEN];
88089b2837SJed Brown   SNES           snes;
891c3436cfSJed Brown   TSAdapt        adapt;
9031748224SBarry Smith   PetscReal      time_step;
91bdad233fSMatthew Knepley 
92bdad233fSMatthew Knepley   PetscFunctionBegin;
930700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
943194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
95a43b19c4SJed Brown     /* Handle TS type options */
96a43b19c4SJed Brown     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
97bdad233fSMatthew Knepley 
98bdad233fSMatthew Knepley     /* Handle generic TS options */
99bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
1003bca7d26SBarry Smith     ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
101d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr);
10231748224SBarry Smith     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
10331748224SBarry Smith     if (flg) {
10431748224SBarry Smith       ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
10531748224SBarry Smith     }
106a43b19c4SJed Brown     opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time;
107a43b19c4SJed Brown     ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr);
108461e00b3SSean Farley     if (flg) {ierr = TSSetExactFinalTime(ts,opt);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);
1393923b477SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
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);
1483923b477SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
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);
157201da799SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
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);
166201da799SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
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);
1758189c53fSBarry Smith       ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
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);
18583a4ac43SBarry Smith       ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
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);
19583a4ac43SBarry Smith       ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
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 
2271c3436cfSJed Brown     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
2281c3436cfSJed Brown     ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2291c3436cfSJed Brown 
230d52bd9f3SBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
231d52bd9f3SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
232d52bd9f3SBarry Smith 
233bdad233fSMatthew Knepley     /* Handle specific TS options */
234abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
235bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
236bdad233fSMatthew Knepley     }
2375d973c19SBarry Smith 
2385d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
2395d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
240bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
241bdad233fSMatthew Knepley   PetscFunctionReturn(0);
242bdad233fSMatthew Knepley }
243bdad233fSMatthew Knepley 
244bdad233fSMatthew Knepley #undef __FUNCT__
245cdcf91faSSean Farley #undef __FUNCT__
2464a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
247a7a1495cSBarry Smith /*@
2488c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
249a7a1495cSBarry Smith       set with TSSetRHSJacobian().
250a7a1495cSBarry Smith 
251a7a1495cSBarry Smith    Collective on TS and Vec
252a7a1495cSBarry Smith 
253a7a1495cSBarry Smith    Input Parameters:
254316643e7SJed Brown +  ts - the TS context
255a7a1495cSBarry Smith .  t - current timestep
2560910c330SBarry Smith -  U - input vector
257a7a1495cSBarry Smith 
258a7a1495cSBarry Smith    Output Parameters:
259a7a1495cSBarry Smith +  A - Jacobian matrix
260a7a1495cSBarry Smith .  B - optional preconditioning matrix
261a7a1495cSBarry Smith -  flag - flag indicating matrix structure
262a7a1495cSBarry Smith 
263a7a1495cSBarry Smith    Notes:
264a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
265a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
266a7a1495cSBarry Smith 
26794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
268a7a1495cSBarry Smith    flag parameter.
269a7a1495cSBarry Smith 
270a7a1495cSBarry Smith    Level: developer
271a7a1495cSBarry Smith 
272a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
273a7a1495cSBarry Smith 
27494b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
275a7a1495cSBarry Smith @*/
2760910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
277a7a1495cSBarry Smith {
278dfbe8321SBarry Smith   PetscErrorCode ierr;
2790910c330SBarry Smith   PetscInt       Ustate;
28024989b8cSPeter Brune   DM             dm;
28124989b8cSPeter Brune   TSDM           tsdm;
28224989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
28324989b8cSPeter Brune   void           *ctx;
28424989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
285a7a1495cSBarry Smith 
286a7a1495cSBarry Smith   PetscFunctionBegin;
2870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2880910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2890910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
29024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
29124989b8cSPeter Brune   ierr = DMTSGetContext(dm,&tsdm);CHKERRQ(ierr);
29224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
29324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
2940910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
2950910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
2960e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
2970e4ef248SJed Brown     PetscFunctionReturn(0);
298f8ede8e7SJed Brown   }
299d90be118SSean Farley 
30024989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
301d90be118SSean Farley 
30224989b8cSPeter Brune   if (rhsjacobianfunc) {
3030910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
304a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
305a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3060910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
307a7a1495cSBarry Smith     PetscStackPop;
3080910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
309a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3100700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3110700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
312ef66eb69SBarry Smith   } else {
313214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
314214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
315214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
316ef66eb69SBarry Smith   }
3170e4ef248SJed Brown   ts->rhsjacobian.time = t;
3180910c330SBarry Smith   ts->rhsjacobian.X = U;
3190910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3200e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
321a7a1495cSBarry Smith   PetscFunctionReturn(0);
322a7a1495cSBarry Smith }
323a7a1495cSBarry Smith 
3244a2ae208SSatish Balay #undef __FUNCT__
3254a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
326316643e7SJed Brown /*@
327d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
328d763cef2SBarry Smith 
329316643e7SJed Brown    Collective on TS and Vec
330316643e7SJed Brown 
331316643e7SJed Brown    Input Parameters:
332316643e7SJed Brown +  ts - the TS context
333316643e7SJed Brown .  t - current time
3340910c330SBarry Smith -  U - state vector
335316643e7SJed Brown 
336316643e7SJed Brown    Output Parameter:
337316643e7SJed Brown .  y - right hand side
338316643e7SJed Brown 
339316643e7SJed Brown    Note:
340316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
341316643e7SJed Brown    is used internally within the nonlinear solvers.
342316643e7SJed Brown 
343316643e7SJed Brown    Level: developer
344316643e7SJed Brown 
345316643e7SJed Brown .keywords: TS, compute
346316643e7SJed Brown 
347316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
348316643e7SJed Brown @*/
3490910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
350d763cef2SBarry Smith {
351dfbe8321SBarry Smith   PetscErrorCode ierr;
35224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
35324989b8cSPeter Brune   TSIFunction    ifunction;
35424989b8cSPeter Brune   void           *ctx;
35524989b8cSPeter Brune   DM             dm;
35624989b8cSPeter Brune 
357d763cef2SBarry Smith   PetscFunctionBegin;
3580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3590910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3600700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
36124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
36224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
36324989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
364d763cef2SBarry Smith 
36524989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
366d763cef2SBarry Smith 
3670910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
36824989b8cSPeter Brune   if (rhsfunction) {
369d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
3700910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
371d763cef2SBarry Smith     PetscStackPop;
372214bc6a2SJed Brown   } else {
373214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
374b2cd27e8SJed Brown   }
37544a41b28SSean Farley 
3760910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
377d763cef2SBarry Smith   PetscFunctionReturn(0);
378d763cef2SBarry Smith }
379d763cef2SBarry Smith 
3804a2ae208SSatish Balay #undef __FUNCT__
381ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
382ef20d060SBarry Smith /*@
383ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
384ef20d060SBarry Smith 
385ef20d060SBarry Smith    Collective on TS and Vec
386ef20d060SBarry Smith 
387ef20d060SBarry Smith    Input Parameters:
388ef20d060SBarry Smith +  ts - the TS context
389ef20d060SBarry Smith -  t - current time
390ef20d060SBarry Smith 
391ef20d060SBarry Smith    Output Parameter:
3920910c330SBarry Smith .  U - the solution
393ef20d060SBarry Smith 
394ef20d060SBarry Smith    Note:
395ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
396ef20d060SBarry Smith    is used internally within the nonlinear solvers.
397ef20d060SBarry Smith 
398ef20d060SBarry Smith    Level: developer
399ef20d060SBarry Smith 
400ef20d060SBarry Smith .keywords: TS, compute
401ef20d060SBarry Smith 
402abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
403ef20d060SBarry Smith @*/
4040910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
405ef20d060SBarry Smith {
406ef20d060SBarry Smith   PetscErrorCode     ierr;
407ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
408ef20d060SBarry Smith   void               *ctx;
409ef20d060SBarry Smith   DM                 dm;
410ef20d060SBarry Smith 
411ef20d060SBarry Smith   PetscFunctionBegin;
412ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4130910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
414ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
415ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
416ef20d060SBarry Smith 
417ef20d060SBarry Smith   if (solutionfunction) {
418ef20d060SBarry Smith     PetscStackPush("TS user right-hand-side function");
4190910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
420ef20d060SBarry Smith     PetscStackPop;
421ef20d060SBarry Smith   }
422ef20d060SBarry Smith   PetscFunctionReturn(0);
423ef20d060SBarry Smith }
424ef20d060SBarry Smith 
425ef20d060SBarry Smith #undef __FUNCT__
426214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
427214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
428214bc6a2SJed Brown {
4292dd45cf8SJed Brown   Vec            F;
430214bc6a2SJed Brown   PetscErrorCode ierr;
431214bc6a2SJed Brown 
432214bc6a2SJed Brown   PetscFunctionBegin;
4330da9a4f0SJed Brown   *Frhs = PETSC_NULL;
4342dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
435214bc6a2SJed Brown   if (!ts->Frhs) {
4362dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
437214bc6a2SJed Brown   }
438214bc6a2SJed Brown   *Frhs = ts->Frhs;
439214bc6a2SJed Brown   PetscFunctionReturn(0);
440214bc6a2SJed Brown }
441214bc6a2SJed Brown 
442214bc6a2SJed Brown #undef __FUNCT__
443214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
444214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
445214bc6a2SJed Brown {
446214bc6a2SJed Brown   Mat            A,B;
4472dd45cf8SJed Brown   PetscErrorCode ierr;
448214bc6a2SJed Brown 
449214bc6a2SJed Brown   PetscFunctionBegin;
450214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
451214bc6a2SJed Brown   if (Arhs) {
452214bc6a2SJed Brown     if (!ts->Arhs) {
453214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
454214bc6a2SJed Brown     }
455214bc6a2SJed Brown     *Arhs = ts->Arhs;
456214bc6a2SJed Brown   }
457214bc6a2SJed Brown   if (Brhs) {
458214bc6a2SJed Brown     if (!ts->Brhs) {
459214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
460214bc6a2SJed Brown     }
461214bc6a2SJed Brown     *Brhs = ts->Brhs;
462214bc6a2SJed Brown   }
463214bc6a2SJed Brown   PetscFunctionReturn(0);
464214bc6a2SJed Brown }
465214bc6a2SJed Brown 
466214bc6a2SJed Brown #undef __FUNCT__
467316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
468316643e7SJed Brown /*@
4690910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
470316643e7SJed Brown 
471316643e7SJed Brown    Collective on TS and Vec
472316643e7SJed Brown 
473316643e7SJed Brown    Input Parameters:
474316643e7SJed Brown +  ts - the TS context
475316643e7SJed Brown .  t - current time
4760910c330SBarry Smith .  U - state vector
4770910c330SBarry Smith .  Udot - time derivative of state vector
478214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
479316643e7SJed Brown 
480316643e7SJed Brown    Output Parameter:
481316643e7SJed Brown .  Y - right hand side
482316643e7SJed Brown 
483316643e7SJed Brown    Note:
484316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
485316643e7SJed Brown    is used internally within the nonlinear solvers.
486316643e7SJed Brown 
487316643e7SJed Brown    If the user did did not write their equations in implicit form, this
488316643e7SJed Brown    function recasts them in implicit form.
489316643e7SJed Brown 
490316643e7SJed Brown    Level: developer
491316643e7SJed Brown 
492316643e7SJed Brown .keywords: TS, compute
493316643e7SJed Brown 
494316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
495316643e7SJed Brown @*/
4960910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
497316643e7SJed Brown {
498316643e7SJed Brown   PetscErrorCode ierr;
49924989b8cSPeter Brune   TSIFunction    ifunction;
50024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
50124989b8cSPeter Brune   void           *ctx;
50224989b8cSPeter Brune   DM             dm;
503316643e7SJed Brown 
504316643e7SJed Brown   PetscFunctionBegin;
5050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5060910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5070910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
5080700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
509316643e7SJed Brown 
51024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
51124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
51224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
51324989b8cSPeter Brune 
51424989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
515d90be118SSean Farley 
5160910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
51724989b8cSPeter Brune   if (ifunction) {
518316643e7SJed Brown     PetscStackPush("TS user implicit function");
5190910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
520316643e7SJed Brown     PetscStackPop;
521214bc6a2SJed Brown   }
522214bc6a2SJed Brown   if (imex) {
52324989b8cSPeter Brune     if (!ifunction) {
5240910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
5252dd45cf8SJed Brown     }
52624989b8cSPeter Brune   } else if (rhsfunction) {
52724989b8cSPeter Brune     if (ifunction) {
528214bc6a2SJed Brown       Vec Frhs;
529214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
5300910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
531214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
5322dd45cf8SJed Brown     } else {
5330910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
5340910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
535316643e7SJed Brown     }
5364a6899ffSJed Brown   }
5370910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
538316643e7SJed Brown   PetscFunctionReturn(0);
539316643e7SJed Brown }
540316643e7SJed Brown 
541316643e7SJed Brown #undef __FUNCT__
542316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
543316643e7SJed Brown /*@
544316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
545316643e7SJed Brown 
546316643e7SJed Brown    Collective on TS and Vec
547316643e7SJed Brown 
548316643e7SJed Brown    Input
549316643e7SJed Brown       Input Parameters:
550316643e7SJed Brown +  ts - the TS context
551316643e7SJed Brown .  t - current timestep
5520910c330SBarry Smith .  U - state vector
5530910c330SBarry Smith .  Udot - time derivative of state vector
554214bc6a2SJed Brown .  shift - shift to apply, see note below
555214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
556316643e7SJed Brown 
557316643e7SJed Brown    Output Parameters:
558316643e7SJed Brown +  A - Jacobian matrix
559316643e7SJed Brown .  B - optional preconditioning matrix
560316643e7SJed Brown -  flag - flag indicating matrix structure
561316643e7SJed Brown 
562316643e7SJed Brown    Notes:
5630910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
564316643e7SJed Brown 
5650910c330SBarry Smith    dF/dU + shift*dF/dUdot
566316643e7SJed Brown 
567316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
568316643e7SJed Brown    is used internally within the nonlinear solvers.
569316643e7SJed Brown 
570316643e7SJed Brown    Level: developer
571316643e7SJed Brown 
572316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
573316643e7SJed Brown 
574316643e7SJed Brown .seealso:  TSSetIJacobian()
57563495f91SJed Brown @*/
5760910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
577316643e7SJed Brown {
5780910c330SBarry Smith   PetscInt       Ustate, Udotstate;
579316643e7SJed Brown   PetscErrorCode ierr;
58024989b8cSPeter Brune   TSIJacobian    ijacobian;
58124989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
58224989b8cSPeter Brune   DM             dm;
58324989b8cSPeter Brune   void           *ctx;
584316643e7SJed Brown 
585316643e7SJed Brown   PetscFunctionBegin;
58624989b8cSPeter Brune 
5870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5880910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5890910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
590316643e7SJed Brown   PetscValidPointer(A,6);
5910700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
592316643e7SJed Brown   PetscValidPointer(B,7);
5930700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
594316643e7SJed Brown   PetscValidPointer(flg,8);
59524989b8cSPeter Brune 
59624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
59724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
59824989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
59924989b8cSPeter Brune 
6000910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
6010910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
6020910c330SBarry 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))) {
6030026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
6040026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
6050026cea9SSean Farley     PetscFunctionReturn(0);
6060026cea9SSean Farley   }
607316643e7SJed Brown 
60824989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
609316643e7SJed Brown 
6104e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
6110910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
61224989b8cSPeter Brune   if (ijacobian) {
6132dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
614316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
6150910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
616316643e7SJed Brown     PetscStackPop;
617214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
618214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
619214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
6204a6899ffSJed Brown   }
621214bc6a2SJed Brown   if (imex) {
622b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
623214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
6242dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
625214bc6a2SJed Brown       if (*A != *B) {
626214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
627214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
628214bc6a2SJed Brown       }
629214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
630214bc6a2SJed Brown     }
631214bc6a2SJed Brown   } else {
63224989b8cSPeter Brune     if (!ijacobian) {
6330910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr);
634214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
635214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
636316643e7SJed Brown       if (*A != *B) {
637316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
638316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
639316643e7SJed Brown       }
64024989b8cSPeter Brune     } else if (rhsjacobian) {
641214bc6a2SJed Brown       Mat Arhs,Brhs;
642214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
643214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
6440910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
645214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
646214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
647214bc6a2SJed Brown       if (*A != *B) {
648214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
649214bc6a2SJed Brown       }
650214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
651214bc6a2SJed Brown     }
652316643e7SJed Brown   }
6530026cea9SSean Farley 
6540026cea9SSean Farley   ts->ijacobian.time = t;
6550910c330SBarry Smith   ts->ijacobian.X = U;
6560910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
6570910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
6580910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
6590026cea9SSean Farley   ts->ijacobian.shift = shift;
6600026cea9SSean Farley   ts->ijacobian.imex = imex;
6610026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
6620910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
663316643e7SJed Brown   PetscFunctionReturn(0);
664316643e7SJed Brown }
665316643e7SJed Brown 
666316643e7SJed Brown #undef __FUNCT__
6674a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
668d763cef2SBarry Smith /*@C
669d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
670b5abc632SBarry Smith     where U_t = G(t,u).
671d763cef2SBarry Smith 
6723f9fe445SBarry Smith     Logically Collective on TS
673d763cef2SBarry Smith 
674d763cef2SBarry Smith     Input Parameters:
675d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
676ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
677d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
678d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
679d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
680d763cef2SBarry Smith 
681d763cef2SBarry Smith     Calling sequence of func:
68287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
683d763cef2SBarry Smith 
684d763cef2SBarry Smith +   t - current timestep
685d763cef2SBarry Smith .   u - input vector
686d763cef2SBarry Smith .   F - function vector
687d763cef2SBarry Smith -   ctx - [optional] user-defined function context
688d763cef2SBarry Smith 
689d763cef2SBarry Smith     Level: beginner
690d763cef2SBarry Smith 
691d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
692d763cef2SBarry Smith 
693d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
694d763cef2SBarry Smith @*/
695089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
696d763cef2SBarry Smith {
697089b2837SJed Brown   PetscErrorCode ierr;
698089b2837SJed Brown   SNES           snes;
699e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
70024989b8cSPeter Brune   DM             dm;
701d763cef2SBarry Smith 
702089b2837SJed Brown   PetscFunctionBegin;
7030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
704ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
70524989b8cSPeter Brune 
70624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
70724989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
708089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
709e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
710e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
711e856ceecSJed Brown     r = ralloc;
712e856ceecSJed Brown   }
713089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
714e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
715d763cef2SBarry Smith   PetscFunctionReturn(0);
716d763cef2SBarry Smith }
717d763cef2SBarry Smith 
7184a2ae208SSatish Balay #undef __FUNCT__
719ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
720ef20d060SBarry Smith /*@C
721abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
722ef20d060SBarry Smith 
723ef20d060SBarry Smith     Logically Collective on TS
724ef20d060SBarry Smith 
725ef20d060SBarry Smith     Input Parameters:
726ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
727ef20d060SBarry Smith .   f - routine for evaluating the solution
728ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
729ef20d060SBarry Smith           function evaluation routine (may be PETSC_NULL)
730ef20d060SBarry Smith 
731ef20d060SBarry Smith     Calling sequence of func:
732ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
733ef20d060SBarry Smith 
734ef20d060SBarry Smith +   t - current timestep
735ef20d060SBarry Smith .   u - output vector
736ef20d060SBarry Smith -   ctx - [optional] user-defined function context
737ef20d060SBarry Smith 
738abd5a294SJed Brown     Notes:
739abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
740abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
741abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
742abd5a294SJed Brown 
7434f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
744abd5a294SJed Brown 
745ef20d060SBarry Smith     Level: beginner
746ef20d060SBarry Smith 
747ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
748ef20d060SBarry Smith 
749abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction()
750ef20d060SBarry Smith @*/
751ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
752ef20d060SBarry Smith {
753ef20d060SBarry Smith   PetscErrorCode ierr;
754ef20d060SBarry Smith   DM             dm;
755ef20d060SBarry Smith 
756ef20d060SBarry Smith   PetscFunctionBegin;
757ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
758ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
759ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
760ef20d060SBarry Smith   PetscFunctionReturn(0);
761ef20d060SBarry Smith }
762ef20d060SBarry Smith 
763ef20d060SBarry Smith #undef __FUNCT__
7644a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
765d763cef2SBarry Smith /*@C
766d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
767b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
768d763cef2SBarry Smith 
7693f9fe445SBarry Smith    Logically Collective on TS
770d763cef2SBarry Smith 
771d763cef2SBarry Smith    Input Parameters:
772d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
773d763cef2SBarry Smith .  A   - Jacobian matrix
774d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
775d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
776d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
777d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
778d763cef2SBarry Smith 
779d763cef2SBarry Smith    Calling sequence of func:
78087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
781d763cef2SBarry Smith 
782d763cef2SBarry Smith +  t - current timestep
783d763cef2SBarry Smith .  u - input vector
784d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
785d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
786d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
78794b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
788d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
789d763cef2SBarry Smith 
790d763cef2SBarry Smith    Notes:
79194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
792d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
793d763cef2SBarry Smith 
794d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
795d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
79656335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
797d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
798d763cef2SBarry Smith    throughout the global iterations.
799d763cef2SBarry Smith 
800d763cef2SBarry Smith    Level: beginner
801d763cef2SBarry Smith 
802d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
803d763cef2SBarry Smith 
804ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
805d763cef2SBarry Smith 
806d763cef2SBarry Smith @*/
807089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
808d763cef2SBarry Smith {
809277b19d0SLisandro Dalcin   PetscErrorCode ierr;
810089b2837SJed Brown   SNES           snes;
81124989b8cSPeter Brune   DM             dm;
81224989b8cSPeter Brune   TSIJacobian    ijacobian;
813277b19d0SLisandro Dalcin 
814d763cef2SBarry Smith   PetscFunctionBegin;
8150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
816277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
817277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
818277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
819277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
820d763cef2SBarry Smith 
82124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
82224989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
82324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
82424989b8cSPeter Brune 
825089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
8265f659677SPeter Brune   if (!ijacobian) {
827089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
8280e4ef248SJed Brown   }
8290e4ef248SJed Brown   if (A) {
8300e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8310e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
8320e4ef248SJed Brown     ts->Arhs = A;
8330e4ef248SJed Brown   }
8340e4ef248SJed Brown   if (B) {
8350e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8360e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
8370e4ef248SJed Brown     ts->Brhs = B;
8380e4ef248SJed Brown   }
839d763cef2SBarry Smith   PetscFunctionReturn(0);
840d763cef2SBarry Smith }
841d763cef2SBarry Smith 
842316643e7SJed Brown 
843316643e7SJed Brown #undef __FUNCT__
844316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
845316643e7SJed Brown /*@C
846b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
847316643e7SJed Brown 
8483f9fe445SBarry Smith    Logically Collective on TS
849316643e7SJed Brown 
850316643e7SJed Brown    Input Parameters:
851316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
852ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
853316643e7SJed Brown .  f   - the function evaluation routine
854316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
855316643e7SJed Brown 
856316643e7SJed Brown    Calling sequence of f:
857316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
858316643e7SJed Brown 
859316643e7SJed Brown +  t   - time at step/stage being solved
860316643e7SJed Brown .  u   - state vector
861316643e7SJed Brown .  u_t - time derivative of state vector
862316643e7SJed Brown .  F   - function vector
863316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
864316643e7SJed Brown 
865316643e7SJed Brown    Important:
866d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
867316643e7SJed Brown 
868316643e7SJed Brown    Level: beginner
869316643e7SJed Brown 
870316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
871316643e7SJed Brown 
872d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
873316643e7SJed Brown @*/
874089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
875316643e7SJed Brown {
876089b2837SJed Brown   PetscErrorCode ierr;
877089b2837SJed Brown   SNES           snes;
878e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
87924989b8cSPeter Brune   DM             dm;
880316643e7SJed Brown 
881316643e7SJed Brown   PetscFunctionBegin;
8820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
883ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
88424989b8cSPeter Brune 
88524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88624989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
88724989b8cSPeter Brune 
888089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
889e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
890e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
891e856ceecSJed Brown     res = resalloc;
892e856ceecSJed Brown   }
893089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
894e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
89524989b8cSPeter Brune 
896089b2837SJed Brown   PetscFunctionReturn(0);
897089b2837SJed Brown }
898089b2837SJed Brown 
899089b2837SJed Brown #undef __FUNCT__
900089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
901089b2837SJed Brown /*@C
902089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
903089b2837SJed Brown 
904089b2837SJed Brown    Not Collective
905089b2837SJed Brown 
906089b2837SJed Brown    Input Parameter:
907089b2837SJed Brown .  ts - the TS context
908089b2837SJed Brown 
909089b2837SJed Brown    Output Parameter:
910089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
911089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
912089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
913089b2837SJed Brown 
914089b2837SJed Brown    Level: advanced
915089b2837SJed Brown 
916089b2837SJed Brown .keywords: TS, nonlinear, get, function
917089b2837SJed Brown 
918089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
919089b2837SJed Brown @*/
920089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
921089b2837SJed Brown {
922089b2837SJed Brown   PetscErrorCode ierr;
923089b2837SJed Brown   SNES           snes;
92424989b8cSPeter Brune   DM             dm;
925089b2837SJed Brown 
926089b2837SJed Brown   PetscFunctionBegin;
927089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
928089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
929089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
93024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
93124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
932089b2837SJed Brown   PetscFunctionReturn(0);
933089b2837SJed Brown }
934089b2837SJed Brown 
935089b2837SJed Brown #undef __FUNCT__
936089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
937089b2837SJed Brown /*@C
938089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
939089b2837SJed Brown 
940089b2837SJed Brown    Not Collective
941089b2837SJed Brown 
942089b2837SJed Brown    Input Parameter:
943089b2837SJed Brown .  ts - the TS context
944089b2837SJed Brown 
945089b2837SJed Brown    Output Parameter:
946089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
947089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
948089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
949089b2837SJed Brown 
950089b2837SJed Brown    Level: advanced
951089b2837SJed Brown 
952089b2837SJed Brown .keywords: TS, nonlinear, get, function
953089b2837SJed Brown 
954089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
955089b2837SJed Brown @*/
956089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
957089b2837SJed Brown {
958089b2837SJed Brown   PetscErrorCode ierr;
959089b2837SJed Brown   SNES           snes;
96024989b8cSPeter Brune   DM             dm;
961089b2837SJed Brown 
962089b2837SJed Brown   PetscFunctionBegin;
963089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
964089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
965089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
96624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96724989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
968316643e7SJed Brown   PetscFunctionReturn(0);
969316643e7SJed Brown }
970316643e7SJed Brown 
971316643e7SJed Brown #undef __FUNCT__
972316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
973316643e7SJed Brown /*@C
974a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
975a4f0a591SBarry Smith         you provided with TSSetIFunction().
976316643e7SJed Brown 
9773f9fe445SBarry Smith    Logically Collective on TS
978316643e7SJed Brown 
979316643e7SJed Brown    Input Parameters:
980316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
981316643e7SJed Brown .  A   - Jacobian matrix
982316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
983316643e7SJed Brown .  f   - the Jacobian evaluation routine
984316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
985316643e7SJed Brown 
986316643e7SJed Brown    Calling sequence of f:
9871b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
988316643e7SJed Brown 
989316643e7SJed Brown +  t    - time at step/stage being solved
9901b4a444bSJed Brown .  U    - state vector
9911b4a444bSJed Brown .  U_t  - time derivative of state vector
992316643e7SJed Brown .  a    - shift
993b5abc632SBarry Smith .  A    - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
994316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
995316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
996316643e7SJed Brown           structure (same as flag in KSPSetOperators())
997316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
998316643e7SJed Brown 
999316643e7SJed Brown    Notes:
1000316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
1001316643e7SJed Brown 
1002a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1003b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1004a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1005a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1006a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1007a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1008a4f0a591SBarry Smith 
1009316643e7SJed Brown    Level: beginner
1010316643e7SJed Brown 
1011316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1012316643e7SJed Brown 
1013ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
1014316643e7SJed Brown 
1015316643e7SJed Brown @*/
10167087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1017316643e7SJed Brown {
1018316643e7SJed Brown   PetscErrorCode ierr;
1019089b2837SJed Brown   SNES           snes;
102024989b8cSPeter Brune   DM             dm;
1021316643e7SJed Brown 
1022316643e7SJed Brown   PetscFunctionBegin;
10230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10240700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
10250700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1026316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1027316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
102824989b8cSPeter Brune 
102924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
103024989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
103124989b8cSPeter Brune 
1032089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1033089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1034316643e7SJed Brown   PetscFunctionReturn(0);
1035316643e7SJed Brown }
1036316643e7SJed Brown 
10374a2ae208SSatish Balay #undef __FUNCT__
1038*55849f57SBarry Smith #define __FUNCT__ "TSLoad"
1039*55849f57SBarry Smith /*@C
1040*55849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
1041*55849f57SBarry Smith 
1042*55849f57SBarry Smith   Collective on PetscViewer
1043*55849f57SBarry Smith 
1044*55849f57SBarry Smith   Input Parameters:
1045*55849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
1046*55849f57SBarry Smith            some related function before a call to TSLoad().
1047*55849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
1048*55849f57SBarry Smith 
1049*55849f57SBarry Smith    Level: intermediate
1050*55849f57SBarry Smith 
1051*55849f57SBarry Smith   Notes:
1052*55849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
1053*55849f57SBarry Smith 
1054*55849f57SBarry Smith   Notes for advanced users:
1055*55849f57SBarry Smith   Most users should not need to know the details of the binary storage
1056*55849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
1057*55849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
1058*55849f57SBarry Smith   format is
1059*55849f57SBarry Smith .vb
1060*55849f57SBarry Smith      has not yet been determined
1061*55849f57SBarry Smith .ve
1062*55849f57SBarry Smith 
1063*55849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
1064*55849f57SBarry Smith @*/
1065*55849f57SBarry Smith PetscErrorCode  TSLoad(TS newdm, PetscViewer viewer)
1066*55849f57SBarry Smith {
1067*55849f57SBarry Smith   PetscErrorCode ierr;
1068*55849f57SBarry Smith   PetscBool      isbinary;
1069*55849f57SBarry Smith   PetscInt       classid;
1070*55849f57SBarry Smith   char           type[256];
1071*55849f57SBarry Smith   SNES           snes;
1072*55849f57SBarry Smith 
1073*55849f57SBarry Smith   PetscFunctionBegin;
1074*55849f57SBarry Smith   PetscValidHeaderSpecific(newdm,TS_CLASSID,1);
1075*55849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1076*55849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
1077*55849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
1078*55849f57SBarry Smith 
1079*55849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1080*55849f57SBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(((PetscObject)newdm)->comm,PETSC_ERR_ARG_WRONG,"Not TS next in file");
1081*55849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1082*55849f57SBarry Smith   ierr = TSSetType(newdm, type);CHKERRQ(ierr);
1083*55849f57SBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
1084*55849f57SBarry Smith   ierr = TSGetSNES(newdm,&snes);CHKERRQ(ierr);
1085*55849f57SBarry Smith   ierr = SNESLoad(snes,viewer);CHKERRQ(ierr);
1086*55849f57SBarry Smith   PetscFunctionReturn(0);
1087*55849f57SBarry Smith }
1088*55849f57SBarry Smith 
1089*55849f57SBarry Smith #undef __FUNCT__
10904a2ae208SSatish Balay #define __FUNCT__ "TSView"
10917e2c5f70SBarry Smith /*@C
1092d763cef2SBarry Smith     TSView - Prints the TS data structure.
1093d763cef2SBarry Smith 
10944c49b128SBarry Smith     Collective on TS
1095d763cef2SBarry Smith 
1096d763cef2SBarry Smith     Input Parameters:
1097d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1098d763cef2SBarry Smith -   viewer - visualization context
1099d763cef2SBarry Smith 
1100d763cef2SBarry Smith     Options Database Key:
1101d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1102d763cef2SBarry Smith 
1103d763cef2SBarry Smith     Notes:
1104d763cef2SBarry Smith     The available visualization contexts include
1105b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1106b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1107d763cef2SBarry Smith          output where only the first processor opens
1108d763cef2SBarry Smith          the file.  All other processors send their
1109d763cef2SBarry Smith          data to the first processor to print.
1110d763cef2SBarry Smith 
1111d763cef2SBarry Smith     The user can open an alternative visualization context with
1112b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1113d763cef2SBarry Smith 
1114d763cef2SBarry Smith     Level: beginner
1115d763cef2SBarry Smith 
1116d763cef2SBarry Smith .keywords: TS, timestep, view
1117d763cef2SBarry Smith 
1118b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1119d763cef2SBarry Smith @*/
11207087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1121d763cef2SBarry Smith {
1122dfbe8321SBarry Smith   PetscErrorCode ierr;
112319fd82e9SBarry Smith   TSType         type;
1124*55849f57SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary;
1125d763cef2SBarry Smith 
1126d763cef2SBarry Smith   PetscFunctionBegin;
11270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11283050cee2SBarry Smith   if (!viewer) {
11297adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
11303050cee2SBarry Smith   }
11310700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1132c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1133fd16b177SBarry Smith 
1134251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1135251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
1136*55849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
113732077d6dSBarry Smith   if (iascii) {
1138317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
113977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1140a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1141d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
11425ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1143c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1144d763cef2SBarry Smith     }
11455ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1146193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1147d52bd9f3SBarry Smith     if (ts->ops->view) {
1148d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1149d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1150d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1151d52bd9f3SBarry Smith     }
11520f5bd95cSBarry Smith   } else if (isstring) {
1153a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1154b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
1155*55849f57SBarry Smith   } else if (isbinary) {
1156*55849f57SBarry Smith     PetscInt         classid = TS_FILE_CLASSID;
1157*55849f57SBarry Smith     MPI_Comm         comm;
1158*55849f57SBarry Smith     PetscMPIInt      rank;
1159*55849f57SBarry Smith     char             type[256];
1160*55849f57SBarry Smith 
1161*55849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
1162*55849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
1163*55849f57SBarry Smith     if (!rank) {
1164*55849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
1165*55849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
1166*55849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
1167*55849f57SBarry Smith     }
1168*55849f57SBarry Smith     if (ts->ops->view) {
1169*55849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1170*55849f57SBarry Smith     }
1171d763cef2SBarry Smith   }
1172b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1173251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1174b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1175d763cef2SBarry Smith   PetscFunctionReturn(0);
1176d763cef2SBarry Smith }
1177d763cef2SBarry Smith 
1178d763cef2SBarry Smith 
11794a2ae208SSatish Balay #undef __FUNCT__
11804a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1181b07ff414SBarry Smith /*@
1182d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1183d763cef2SBarry Smith    the timesteppers.
1184d763cef2SBarry Smith 
11853f9fe445SBarry Smith    Logically Collective on TS
1186d763cef2SBarry Smith 
1187d763cef2SBarry Smith    Input Parameters:
1188d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1189d763cef2SBarry Smith -  usrP - optional user context
1190d763cef2SBarry Smith 
1191d763cef2SBarry Smith    Level: intermediate
1192d763cef2SBarry Smith 
1193d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1194d763cef2SBarry Smith 
1195d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1196d763cef2SBarry Smith @*/
11977087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1198d763cef2SBarry Smith {
1199d763cef2SBarry Smith   PetscFunctionBegin;
12000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1201d763cef2SBarry Smith   ts->user = usrP;
1202d763cef2SBarry Smith   PetscFunctionReturn(0);
1203d763cef2SBarry Smith }
1204d763cef2SBarry Smith 
12054a2ae208SSatish Balay #undef __FUNCT__
12064a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1207b07ff414SBarry Smith /*@
1208d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1209d763cef2SBarry Smith     timestepper.
1210d763cef2SBarry Smith 
1211d763cef2SBarry Smith     Not Collective
1212d763cef2SBarry Smith 
1213d763cef2SBarry Smith     Input Parameter:
1214d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1215d763cef2SBarry Smith 
1216d763cef2SBarry Smith     Output Parameter:
1217d763cef2SBarry Smith .   usrP - user context
1218d763cef2SBarry Smith 
1219d763cef2SBarry Smith     Level: intermediate
1220d763cef2SBarry Smith 
1221d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1222d763cef2SBarry Smith 
1223d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1224d763cef2SBarry Smith @*/
1225e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1226d763cef2SBarry Smith {
1227d763cef2SBarry Smith   PetscFunctionBegin;
12280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1229e71120c6SJed Brown   *(void**)usrP = ts->user;
1230d763cef2SBarry Smith   PetscFunctionReturn(0);
1231d763cef2SBarry Smith }
1232d763cef2SBarry Smith 
12334a2ae208SSatish Balay #undef __FUNCT__
12344a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1235d763cef2SBarry Smith /*@
1236b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1237d763cef2SBarry Smith 
1238d763cef2SBarry Smith    Not Collective
1239d763cef2SBarry Smith 
1240d763cef2SBarry Smith    Input Parameter:
1241d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1242d763cef2SBarry Smith 
1243d763cef2SBarry Smith    Output Parameter:
1244b8123daeSJed Brown .  iter - number of steps completed so far
1245d763cef2SBarry Smith 
1246d763cef2SBarry Smith    Level: intermediate
1247d763cef2SBarry Smith 
1248d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1249b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1250d763cef2SBarry Smith @*/
12517087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
1252d763cef2SBarry Smith {
1253d763cef2SBarry Smith   PetscFunctionBegin;
12540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12554482741eSBarry Smith   PetscValidIntPointer(iter,2);
1256d763cef2SBarry Smith   *iter = ts->steps;
1257d763cef2SBarry Smith   PetscFunctionReturn(0);
1258d763cef2SBarry Smith }
1259d763cef2SBarry Smith 
12604a2ae208SSatish Balay #undef __FUNCT__
12614a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1262d763cef2SBarry Smith /*@
1263d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1264d763cef2SBarry Smith    as well as the initial time.
1265d763cef2SBarry Smith 
12663f9fe445SBarry Smith    Logically Collective on TS
1267d763cef2SBarry Smith 
1268d763cef2SBarry Smith    Input Parameters:
1269d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1270d763cef2SBarry Smith .  initial_time - the initial time
1271d763cef2SBarry Smith -  time_step - the size of the timestep
1272d763cef2SBarry Smith 
1273d763cef2SBarry Smith    Level: intermediate
1274d763cef2SBarry Smith 
1275d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1276d763cef2SBarry Smith 
1277d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1278d763cef2SBarry Smith @*/
12797087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1280d763cef2SBarry Smith {
1281e144a568SJed Brown   PetscErrorCode ierr;
1282e144a568SJed Brown 
1283d763cef2SBarry Smith   PetscFunctionBegin;
12840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1285e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1286d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1287d763cef2SBarry Smith   PetscFunctionReturn(0);
1288d763cef2SBarry Smith }
1289d763cef2SBarry Smith 
12904a2ae208SSatish Balay #undef __FUNCT__
12914a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1292d763cef2SBarry Smith /*@
1293d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1294d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1295d763cef2SBarry Smith 
12963f9fe445SBarry Smith    Logically Collective on TS
1297d763cef2SBarry Smith 
1298d763cef2SBarry Smith    Input Parameters:
1299d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1300d763cef2SBarry Smith -  time_step - the size of the timestep
1301d763cef2SBarry Smith 
1302d763cef2SBarry Smith    Level: intermediate
1303d763cef2SBarry Smith 
1304d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1305d763cef2SBarry Smith 
1306d763cef2SBarry Smith .keywords: TS, set, timestep
1307d763cef2SBarry Smith @*/
13087087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1309d763cef2SBarry Smith {
1310d763cef2SBarry Smith   PetscFunctionBegin;
13110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1312c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1313d763cef2SBarry Smith   ts->time_step = time_step;
131431748224SBarry Smith   ts->time_step_orig = time_step;
1315d763cef2SBarry Smith   PetscFunctionReturn(0);
1316d763cef2SBarry Smith }
1317d763cef2SBarry Smith 
13184a2ae208SSatish Balay #undef __FUNCT__
1319a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1320a43b19c4SJed Brown /*@
1321a43b19c4SJed Brown    TSSetExactFinalTime - Determines whether to interpolate solution to the
1322a43b19c4SJed Brown       exact final time requested by the user or just returns it at the final time
1323a43b19c4SJed Brown       it computed.
1324a43b19c4SJed Brown 
1325a43b19c4SJed Brown   Logically Collective on TS
1326a43b19c4SJed Brown 
1327a43b19c4SJed Brown    Input Parameter:
1328a43b19c4SJed Brown +   ts - the time-step context
1329a43b19c4SJed Brown -   ft - PETSC_TRUE if interpolates, else PETSC_FALSE
1330a43b19c4SJed Brown 
1331a43b19c4SJed Brown    Level: beginner
1332a43b19c4SJed Brown 
1333a43b19c4SJed Brown .seealso: TSSetDuration()
1334a43b19c4SJed Brown @*/
1335a43b19c4SJed Brown PetscErrorCode  TSSetExactFinalTime(TS ts,PetscBool flg)
1336a43b19c4SJed Brown {
1337a43b19c4SJed Brown   PetscFunctionBegin;
1338a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1339d8cd7023SBarry Smith   PetscValidLogicalCollectiveBool(ts,flg,2);
1340a43b19c4SJed Brown   ts->exact_final_time = flg;
1341a43b19c4SJed Brown   PetscFunctionReturn(0);
1342a43b19c4SJed Brown }
1343a43b19c4SJed Brown 
1344a43b19c4SJed Brown #undef __FUNCT__
13454a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1346d763cef2SBarry Smith /*@
1347d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1348d763cef2SBarry Smith 
1349d763cef2SBarry Smith    Not Collective
1350d763cef2SBarry Smith 
1351d763cef2SBarry Smith    Input Parameter:
1352d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1353d763cef2SBarry Smith 
1354d763cef2SBarry Smith    Output Parameter:
1355d763cef2SBarry Smith .  dt - the current timestep size
1356d763cef2SBarry Smith 
1357d763cef2SBarry Smith    Level: intermediate
1358d763cef2SBarry Smith 
1359d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1360d763cef2SBarry Smith 
1361d763cef2SBarry Smith .keywords: TS, get, timestep
1362d763cef2SBarry Smith @*/
13637087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1364d763cef2SBarry Smith {
1365d763cef2SBarry Smith   PetscFunctionBegin;
13660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1367f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1368d763cef2SBarry Smith   *dt = ts->time_step;
1369d763cef2SBarry Smith   PetscFunctionReturn(0);
1370d763cef2SBarry Smith }
1371d763cef2SBarry Smith 
13724a2ae208SSatish Balay #undef __FUNCT__
13734a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1374d8e5e3e6SSatish Balay /*@
1375d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1376d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1377d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1378d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1379d763cef2SBarry Smith 
1380d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1381d763cef2SBarry Smith 
1382d763cef2SBarry Smith    Input Parameter:
1383d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1384d763cef2SBarry Smith 
1385d763cef2SBarry Smith    Output Parameter:
1386d763cef2SBarry Smith .  v - the vector containing the solution
1387d763cef2SBarry Smith 
1388d763cef2SBarry Smith    Level: intermediate
1389d763cef2SBarry Smith 
1390d763cef2SBarry Smith .seealso: TSGetTimeStep()
1391d763cef2SBarry Smith 
1392d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1393d763cef2SBarry Smith @*/
13947087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1395d763cef2SBarry Smith {
1396d763cef2SBarry Smith   PetscFunctionBegin;
13970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13984482741eSBarry Smith   PetscValidPointer(v,2);
13998737fe31SLisandro Dalcin   *v = ts->vec_sol;
1400d763cef2SBarry Smith   PetscFunctionReturn(0);
1401d763cef2SBarry Smith }
1402d763cef2SBarry Smith 
1403bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
14044a2ae208SSatish Balay #undef __FUNCT__
1405bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1406d8e5e3e6SSatish Balay /*@
1407bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1408d763cef2SBarry Smith 
1409bdad233fSMatthew Knepley   Not collective
1410d763cef2SBarry Smith 
1411bdad233fSMatthew Knepley   Input Parameters:
1412bdad233fSMatthew Knepley + ts   - The TS
1413bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1414d763cef2SBarry Smith .vb
14150910c330SBarry Smith          U_t - A U = 0      (linear)
14160910c330SBarry Smith          U_t - A(t) U = 0   (linear)
14170910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1418d763cef2SBarry Smith .ve
1419d763cef2SBarry Smith 
1420d763cef2SBarry Smith    Level: beginner
1421d763cef2SBarry Smith 
1422bdad233fSMatthew Knepley .keywords: TS, problem type
1423bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1424d763cef2SBarry Smith @*/
14257087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1426a7cc72afSBarry Smith {
14279e2a6581SJed Brown   PetscErrorCode ierr;
14289e2a6581SJed Brown 
1429d763cef2SBarry Smith   PetscFunctionBegin;
14300700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1431bdad233fSMatthew Knepley   ts->problem_type = type;
14329e2a6581SJed Brown   if (type == TS_LINEAR) {
14339e2a6581SJed Brown     SNES snes;
14349e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
14359e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
14369e2a6581SJed Brown   }
1437d763cef2SBarry Smith   PetscFunctionReturn(0);
1438d763cef2SBarry Smith }
1439d763cef2SBarry Smith 
1440bdad233fSMatthew Knepley #undef __FUNCT__
1441bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1442bdad233fSMatthew Knepley /*@C
1443bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1444bdad233fSMatthew Knepley 
1445bdad233fSMatthew Knepley   Not collective
1446bdad233fSMatthew Knepley 
1447bdad233fSMatthew Knepley   Input Parameter:
1448bdad233fSMatthew Knepley . ts   - The TS
1449bdad233fSMatthew Knepley 
1450bdad233fSMatthew Knepley   Output Parameter:
1451bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1452bdad233fSMatthew Knepley .vb
1453089b2837SJed Brown          M U_t = A U
1454089b2837SJed Brown          M(t) U_t = A(t) U
1455b5abc632SBarry Smith          F(t,U,U_t)
1456bdad233fSMatthew Knepley .ve
1457bdad233fSMatthew Knepley 
1458bdad233fSMatthew Knepley    Level: beginner
1459bdad233fSMatthew Knepley 
1460bdad233fSMatthew Knepley .keywords: TS, problem type
1461bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1462bdad233fSMatthew Knepley @*/
14637087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1464a7cc72afSBarry Smith {
1465bdad233fSMatthew Knepley   PetscFunctionBegin;
14660700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
14674482741eSBarry Smith   PetscValidIntPointer(type,2);
1468bdad233fSMatthew Knepley   *type = ts->problem_type;
1469bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1470bdad233fSMatthew Knepley }
1471d763cef2SBarry Smith 
14724a2ae208SSatish Balay #undef __FUNCT__
14734a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1474d763cef2SBarry Smith /*@
1475d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1476d763cef2SBarry Smith    of a timestepper.
1477d763cef2SBarry Smith 
1478d763cef2SBarry Smith    Collective on TS
1479d763cef2SBarry Smith 
1480d763cef2SBarry Smith    Input Parameter:
1481d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1482d763cef2SBarry Smith 
1483d763cef2SBarry Smith    Notes:
1484d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1485d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1486d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1487d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1488d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1489d763cef2SBarry Smith 
1490d763cef2SBarry Smith    Level: advanced
1491d763cef2SBarry Smith 
1492d763cef2SBarry Smith .keywords: TS, timestep, setup
1493d763cef2SBarry Smith 
1494d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1495d763cef2SBarry Smith @*/
14967087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1497d763cef2SBarry Smith {
1498dfbe8321SBarry Smith   PetscErrorCode ierr;
14996c6b9e74SPeter Brune   DM             dm;
15006c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
15016c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
15026c6b9e74SPeter Brune   TSIJacobian    ijac;
15036c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1504d763cef2SBarry Smith 
1505d763cef2SBarry Smith   PetscFunctionBegin;
15060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1507277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1508277b19d0SLisandro Dalcin 
15097adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
15109596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1511d763cef2SBarry Smith   }
1512a43b19c4SJed Brown   if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE;
1513277b19d0SLisandro Dalcin 
1514277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1515277b19d0SLisandro Dalcin 
15161c3436cfSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
15171c3436cfSJed Brown 
1518277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1519000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1520277b19d0SLisandro Dalcin   }
1521277b19d0SLisandro Dalcin 
15226c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
15236c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
15246c6b9e74SPeter Brune    */
15256c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
15266c6b9e74SPeter Brune   ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr);
15276c6b9e74SPeter Brune   if (!func) {
15286c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
15296c6b9e74SPeter Brune   }
15306c6b9e74SPeter 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.
15316c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
15326c6b9e74SPeter Brune    */
15336c6b9e74SPeter Brune   ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr);
15346c6b9e74SPeter Brune   ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr);
15356c6b9e74SPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr);
15366c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
15376c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
15386c6b9e74SPeter Brune   }
1539277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1540277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1541277b19d0SLisandro Dalcin }
1542277b19d0SLisandro Dalcin 
1543277b19d0SLisandro Dalcin #undef __FUNCT__
1544277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1545277b19d0SLisandro Dalcin /*@
1546277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1547277b19d0SLisandro Dalcin 
1548277b19d0SLisandro Dalcin    Collective on TS
1549277b19d0SLisandro Dalcin 
1550277b19d0SLisandro Dalcin    Input Parameter:
1551277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1552277b19d0SLisandro Dalcin 
1553277b19d0SLisandro Dalcin    Level: beginner
1554277b19d0SLisandro Dalcin 
1555277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1556277b19d0SLisandro Dalcin 
1557277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1558277b19d0SLisandro Dalcin @*/
1559277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1560277b19d0SLisandro Dalcin {
1561277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1562277b19d0SLisandro Dalcin 
1563277b19d0SLisandro Dalcin   PetscFunctionBegin;
1564277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1565277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1566277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1567277b19d0SLisandro Dalcin   }
1568277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
15694e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
15704e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1571214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
15726bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1573e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1574e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
157538637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1576277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1577d763cef2SBarry Smith   PetscFunctionReturn(0);
1578d763cef2SBarry Smith }
1579d763cef2SBarry Smith 
15804a2ae208SSatish Balay #undef __FUNCT__
15814a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1582d8e5e3e6SSatish Balay /*@
1583d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1584d763cef2SBarry Smith    with TSCreate().
1585d763cef2SBarry Smith 
1586d763cef2SBarry Smith    Collective on TS
1587d763cef2SBarry Smith 
1588d763cef2SBarry Smith    Input Parameter:
1589d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1590d763cef2SBarry Smith 
1591d763cef2SBarry Smith    Level: beginner
1592d763cef2SBarry Smith 
1593d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1594d763cef2SBarry Smith 
1595d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1596d763cef2SBarry Smith @*/
15976bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1598d763cef2SBarry Smith {
15996849ba73SBarry Smith   PetscErrorCode ierr;
1600d763cef2SBarry Smith 
1601d763cef2SBarry Smith   PetscFunctionBegin;
16026bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
16036bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
16046bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1605d763cef2SBarry Smith 
16066bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1607277b19d0SLisandro Dalcin 
1608be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
16096bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
16106bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
16116d4c513bSLisandro Dalcin 
161284df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
16136bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
16146bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
16156bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
16166d4c513bSLisandro Dalcin 
1617a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1618d763cef2SBarry Smith   PetscFunctionReturn(0);
1619d763cef2SBarry Smith }
1620d763cef2SBarry Smith 
16214a2ae208SSatish Balay #undef __FUNCT__
16224a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1623d8e5e3e6SSatish Balay /*@
1624d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1625d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1626d763cef2SBarry Smith 
1627d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1628d763cef2SBarry Smith 
1629d763cef2SBarry Smith    Input Parameter:
1630d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1631d763cef2SBarry Smith 
1632d763cef2SBarry Smith    Output Parameter:
1633d763cef2SBarry Smith .  snes - the nonlinear solver context
1634d763cef2SBarry Smith 
1635d763cef2SBarry Smith    Notes:
1636d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1637d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
163894b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1639d763cef2SBarry Smith 
1640d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1641d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1642d763cef2SBarry Smith 
1643d763cef2SBarry Smith    Level: beginner
1644d763cef2SBarry Smith 
1645d763cef2SBarry Smith .keywords: timestep, get, SNES
1646d763cef2SBarry Smith @*/
16477087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1648d763cef2SBarry Smith {
1649d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1650d372ba47SLisandro Dalcin 
1651d763cef2SBarry Smith   PetscFunctionBegin;
16520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16534482741eSBarry Smith   PetscValidPointer(snes,2);
1654d372ba47SLisandro Dalcin   if (!ts->snes) {
1655d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
16566c6b9e74SPeter Brune     ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1657d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1658d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1659496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
16609e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
16619e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
16629e2a6581SJed Brown     }
1663d372ba47SLisandro Dalcin   }
1664d763cef2SBarry Smith   *snes = ts->snes;
1665d763cef2SBarry Smith   PetscFunctionReturn(0);
1666d763cef2SBarry Smith }
1667d763cef2SBarry Smith 
16684a2ae208SSatish Balay #undef __FUNCT__
166994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1670d8e5e3e6SSatish Balay /*@
167194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1672d763cef2SBarry Smith    a TS (timestepper) context.
1673d763cef2SBarry Smith 
167494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1675d763cef2SBarry Smith 
1676d763cef2SBarry Smith    Input Parameter:
1677d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1678d763cef2SBarry Smith 
1679d763cef2SBarry Smith    Output Parameter:
168094b7f48cSBarry Smith .  ksp - the nonlinear solver context
1681d763cef2SBarry Smith 
1682d763cef2SBarry Smith    Notes:
168394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1684d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1685d763cef2SBarry Smith    KSP and PC contexts as well.
1686d763cef2SBarry Smith 
168794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
168894b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1689d763cef2SBarry Smith 
1690d763cef2SBarry Smith    Level: beginner
1691d763cef2SBarry Smith 
169294b7f48cSBarry Smith .keywords: timestep, get, KSP
1693d763cef2SBarry Smith @*/
16947087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1695d763cef2SBarry Smith {
1696d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1697089b2837SJed Brown   SNES           snes;
1698d372ba47SLisandro Dalcin 
1699d763cef2SBarry Smith   PetscFunctionBegin;
17000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17014482741eSBarry Smith   PetscValidPointer(ksp,2);
170217186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1703e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1704089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1705089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1706d763cef2SBarry Smith   PetscFunctionReturn(0);
1707d763cef2SBarry Smith }
1708d763cef2SBarry Smith 
1709d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1710d763cef2SBarry Smith 
17114a2ae208SSatish Balay #undef __FUNCT__
1712adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1713adb62b0dSMatthew Knepley /*@
1714adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1715adb62b0dSMatthew Knepley    maximum time for iteration.
1716adb62b0dSMatthew Knepley 
17173f9fe445SBarry Smith    Not Collective
1718adb62b0dSMatthew Knepley 
1719adb62b0dSMatthew Knepley    Input Parameters:
1720adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1721adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1722adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1723adb62b0dSMatthew Knepley 
1724adb62b0dSMatthew Knepley    Level: intermediate
1725adb62b0dSMatthew Knepley 
1726adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1727adb62b0dSMatthew Knepley @*/
17287087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1729adb62b0dSMatthew Knepley {
1730adb62b0dSMatthew Knepley   PetscFunctionBegin;
17310700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1732abc0a331SBarry Smith   if (maxsteps) {
17334482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1734adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1735adb62b0dSMatthew Knepley   }
1736abc0a331SBarry Smith   if (maxtime) {
17374482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1738adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1739adb62b0dSMatthew Knepley   }
1740adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1741adb62b0dSMatthew Knepley }
1742adb62b0dSMatthew Knepley 
1743adb62b0dSMatthew Knepley #undef __FUNCT__
17444a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1745d763cef2SBarry Smith /*@
1746d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1747d763cef2SBarry Smith    maximum time for iteration.
1748d763cef2SBarry Smith 
17493f9fe445SBarry Smith    Logically Collective on TS
1750d763cef2SBarry Smith 
1751d763cef2SBarry Smith    Input Parameters:
1752d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1753d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1754d763cef2SBarry Smith -  maxtime - final time to iterate to
1755d763cef2SBarry Smith 
1756d763cef2SBarry Smith    Options Database Keys:
1757d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
17583bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1759d763cef2SBarry Smith 
1760d763cef2SBarry Smith    Notes:
1761d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1762d763cef2SBarry Smith 
1763d763cef2SBarry Smith    Level: intermediate
1764d763cef2SBarry Smith 
1765d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1766a43b19c4SJed Brown 
1767a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1768d763cef2SBarry Smith @*/
17697087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1770d763cef2SBarry Smith {
1771d763cef2SBarry Smith   PetscFunctionBegin;
17720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1773c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1774c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
177539b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
177639b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1777d763cef2SBarry Smith   PetscFunctionReturn(0);
1778d763cef2SBarry Smith }
1779d763cef2SBarry Smith 
17804a2ae208SSatish Balay #undef __FUNCT__
17814a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1782d763cef2SBarry Smith /*@
1783d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1784d763cef2SBarry Smith    for use by the TS routines.
1785d763cef2SBarry Smith 
17863f9fe445SBarry Smith    Logically Collective on TS and Vec
1787d763cef2SBarry Smith 
1788d763cef2SBarry Smith    Input Parameters:
1789d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
17900910c330SBarry Smith -  u - the solution vector
1791d763cef2SBarry Smith 
1792d763cef2SBarry Smith    Level: beginner
1793d763cef2SBarry Smith 
1794d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1795d763cef2SBarry Smith @*/
17960910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
1797d763cef2SBarry Smith {
17988737fe31SLisandro Dalcin   PetscErrorCode ierr;
1799496e6a7aSJed Brown   DM             dm;
18008737fe31SLisandro Dalcin 
1801d763cef2SBarry Smith   PetscFunctionBegin;
18020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18030910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
18040910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
18056bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
18060910c330SBarry Smith   ts->vec_sol = u;
1807496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
18080910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
1809d763cef2SBarry Smith   PetscFunctionReturn(0);
1810d763cef2SBarry Smith }
1811d763cef2SBarry Smith 
1812e74ef692SMatthew Knepley #undef __FUNCT__
1813e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1814ac226902SBarry Smith /*@C
1815000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
18163f2090d5SJed Brown   called once at the beginning of each time step.
1817000e7ae3SMatthew Knepley 
18183f9fe445SBarry Smith   Logically Collective on TS
1819000e7ae3SMatthew Knepley 
1820000e7ae3SMatthew Knepley   Input Parameters:
1821000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1822000e7ae3SMatthew Knepley - func - The function
1823000e7ae3SMatthew Knepley 
1824000e7ae3SMatthew Knepley   Calling sequence of func:
1825000e7ae3SMatthew Knepley . func (TS ts);
1826000e7ae3SMatthew Knepley 
1827000e7ae3SMatthew Knepley   Level: intermediate
1828000e7ae3SMatthew Knepley 
1829b8123daeSJed Brown   Note:
1830b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1831b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1832b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1833b8123daeSJed Brown 
1834000e7ae3SMatthew Knepley .keywords: TS, timestep
1835b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1836000e7ae3SMatthew Knepley @*/
18377087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1838000e7ae3SMatthew Knepley {
1839000e7ae3SMatthew Knepley   PetscFunctionBegin;
18400700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1841000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1842000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1843000e7ae3SMatthew Knepley }
1844000e7ae3SMatthew Knepley 
1845e74ef692SMatthew Knepley #undef __FUNCT__
18463f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
184709ee8438SJed Brown /*@
18483f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
18493f2090d5SJed Brown 
18503f2090d5SJed Brown   Collective on TS
18513f2090d5SJed Brown 
18523f2090d5SJed Brown   Input Parameters:
18533f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
18543f2090d5SJed Brown 
18553f2090d5SJed Brown   Notes:
18563f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
18573f2090d5SJed Brown   so most users would not generally call this routine themselves.
18583f2090d5SJed Brown 
18593f2090d5SJed Brown   Level: developer
18603f2090d5SJed Brown 
18613f2090d5SJed Brown .keywords: TS, timestep
1862b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
18633f2090d5SJed Brown @*/
18647087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
18653f2090d5SJed Brown {
18663f2090d5SJed Brown   PetscErrorCode ierr;
18673f2090d5SJed Brown 
18683f2090d5SJed Brown   PetscFunctionBegin;
18690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
187072ac3e02SJed Brown   if (ts->ops->prestep) {
18713f2090d5SJed Brown     PetscStackPush("TS PreStep function");
18723f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
18733f2090d5SJed Brown     PetscStackPop;
1874312ce896SJed Brown   }
18753f2090d5SJed Brown   PetscFunctionReturn(0);
18763f2090d5SJed Brown }
18773f2090d5SJed Brown 
18783f2090d5SJed Brown #undef __FUNCT__
1879b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1880b8123daeSJed Brown /*@C
1881b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1882b8123daeSJed Brown   called once at the beginning of each stage.
1883b8123daeSJed Brown 
1884b8123daeSJed Brown   Logically Collective on TS
1885b8123daeSJed Brown 
1886b8123daeSJed Brown   Input Parameters:
1887b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1888b8123daeSJed Brown - func - The function
1889b8123daeSJed Brown 
1890b8123daeSJed Brown   Calling sequence of func:
1891b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1892b8123daeSJed Brown 
1893b8123daeSJed Brown   Level: intermediate
1894b8123daeSJed Brown 
1895b8123daeSJed Brown   Note:
1896b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
1897b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
1898b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
1899b8123daeSJed Brown 
1900b8123daeSJed Brown .keywords: TS, timestep
1901b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
1902b8123daeSJed Brown @*/
1903b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
1904b8123daeSJed Brown {
1905b8123daeSJed Brown   PetscFunctionBegin;
1906b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1907b8123daeSJed Brown   ts->ops->prestage = func;
1908b8123daeSJed Brown   PetscFunctionReturn(0);
1909b8123daeSJed Brown }
1910b8123daeSJed Brown 
1911b8123daeSJed Brown #undef __FUNCT__
1912b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
1913b8123daeSJed Brown /*@
1914b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
1915b8123daeSJed Brown 
1916b8123daeSJed Brown   Collective on TS
1917b8123daeSJed Brown 
1918b8123daeSJed Brown   Input Parameters:
1919b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
1920b8123daeSJed Brown 
1921b8123daeSJed Brown   Notes:
1922b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
1923b8123daeSJed Brown   most users would not generally call this routine themselves.
1924b8123daeSJed Brown 
1925b8123daeSJed Brown   Level: developer
1926b8123daeSJed Brown 
1927b8123daeSJed Brown .keywords: TS, timestep
1928b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
1929b8123daeSJed Brown @*/
1930b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
1931b8123daeSJed Brown {
1932b8123daeSJed Brown   PetscErrorCode ierr;
1933b8123daeSJed Brown 
1934b8123daeSJed Brown   PetscFunctionBegin;
1935b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1936b8123daeSJed Brown   if (ts->ops->prestage) {
1937b8123daeSJed Brown     PetscStackPush("TS PreStage function");
1938b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
1939b8123daeSJed Brown     PetscStackPop;
1940b8123daeSJed Brown   }
1941b8123daeSJed Brown   PetscFunctionReturn(0);
1942b8123daeSJed Brown }
1943b8123daeSJed Brown 
1944b8123daeSJed Brown #undef __FUNCT__
1945e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1946ac226902SBarry Smith /*@C
1947000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
19483f2090d5SJed Brown   called once at the end of each time step.
1949000e7ae3SMatthew Knepley 
19503f9fe445SBarry Smith   Logically Collective on TS
1951000e7ae3SMatthew Knepley 
1952000e7ae3SMatthew Knepley   Input Parameters:
1953000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1954000e7ae3SMatthew Knepley - func - The function
1955000e7ae3SMatthew Knepley 
1956000e7ae3SMatthew Knepley   Calling sequence of func:
1957b8123daeSJed Brown $ func (TS ts);
1958000e7ae3SMatthew Knepley 
1959000e7ae3SMatthew Knepley   Level: intermediate
1960000e7ae3SMatthew Knepley 
1961000e7ae3SMatthew Knepley .keywords: TS, timestep
1962b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
1963000e7ae3SMatthew Knepley @*/
19647087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1965000e7ae3SMatthew Knepley {
1966000e7ae3SMatthew Knepley   PetscFunctionBegin;
19670700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1968000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1969000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1970000e7ae3SMatthew Knepley }
1971000e7ae3SMatthew Knepley 
1972e74ef692SMatthew Knepley #undef __FUNCT__
19733f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
197409ee8438SJed Brown /*@
19753f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
19763f2090d5SJed Brown 
19773f2090d5SJed Brown   Collective on TS
19783f2090d5SJed Brown 
19793f2090d5SJed Brown   Input Parameters:
19803f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
19813f2090d5SJed Brown 
19823f2090d5SJed Brown   Notes:
19833f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
19843f2090d5SJed Brown   so most users would not generally call this routine themselves.
19853f2090d5SJed Brown 
19863f2090d5SJed Brown   Level: developer
19873f2090d5SJed Brown 
19883f2090d5SJed Brown .keywords: TS, timestep
19893f2090d5SJed Brown @*/
19907087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
19913f2090d5SJed Brown {
19923f2090d5SJed Brown   PetscErrorCode ierr;
19933f2090d5SJed Brown 
19943f2090d5SJed Brown   PetscFunctionBegin;
19950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
199672ac3e02SJed Brown   if (ts->ops->poststep) {
19973f2090d5SJed Brown     PetscStackPush("TS PostStep function");
19983f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
19993f2090d5SJed Brown     PetscStackPop;
200072ac3e02SJed Brown   }
20013f2090d5SJed Brown   PetscFunctionReturn(0);
20023f2090d5SJed Brown }
20033f2090d5SJed Brown 
2004d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2005d763cef2SBarry Smith 
20064a2ae208SSatish Balay #undef __FUNCT__
2007a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2008d763cef2SBarry Smith /*@C
2009a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2010d763cef2SBarry Smith    timestep to display the iteration's  progress.
2011d763cef2SBarry Smith 
20123f9fe445SBarry Smith    Logically Collective on TS
2013d763cef2SBarry Smith 
2014d763cef2SBarry Smith    Input Parameters:
2015d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2016e213d8f1SJed Brown .  monitor - monitoring routine
2017329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
2018b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
2019b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2020b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
2021d763cef2SBarry Smith 
2022e213d8f1SJed Brown    Calling sequence of monitor:
20230910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2024d763cef2SBarry Smith 
2025d763cef2SBarry Smith +    ts - the TS context
202688c05cc5SBarry 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
202788c05cc5SBarry Smith                                been interpolated to)
20281f06c33eSBarry Smith .    time - current time
20290910c330SBarry Smith .    u - current iterate
2030d763cef2SBarry Smith -    mctx - [optional] monitoring context
2031d763cef2SBarry Smith 
2032d763cef2SBarry Smith    Notes:
2033d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2034d763cef2SBarry Smith    already has been loaded.
2035d763cef2SBarry Smith 
2036025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2037025f1a04SBarry Smith 
2038d763cef2SBarry Smith    Level: intermediate
2039d763cef2SBarry Smith 
2040d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2041d763cef2SBarry Smith 
2042a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2043d763cef2SBarry Smith @*/
2044c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2045d763cef2SBarry Smith {
2046d763cef2SBarry Smith   PetscFunctionBegin;
20470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
204817186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2049d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
20508704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]    = mdestroy;
2051d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
2052d763cef2SBarry Smith   PetscFunctionReturn(0);
2053d763cef2SBarry Smith }
2054d763cef2SBarry Smith 
20554a2ae208SSatish Balay #undef __FUNCT__
2056a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2057d763cef2SBarry Smith /*@C
2058a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2059d763cef2SBarry Smith 
20603f9fe445SBarry Smith    Logically Collective on TS
2061d763cef2SBarry Smith 
2062d763cef2SBarry Smith    Input Parameters:
2063d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2064d763cef2SBarry Smith 
2065d763cef2SBarry Smith    Notes:
2066d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2067d763cef2SBarry Smith 
2068d763cef2SBarry Smith    Level: intermediate
2069d763cef2SBarry Smith 
2070d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2071d763cef2SBarry Smith 
2072a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2073d763cef2SBarry Smith @*/
20747087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2075d763cef2SBarry Smith {
2076d952e501SBarry Smith   PetscErrorCode ierr;
2077d952e501SBarry Smith   PetscInt       i;
2078d952e501SBarry Smith 
2079d763cef2SBarry Smith   PetscFunctionBegin;
20800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2081d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
20828704b422SBarry Smith     if (ts->monitordestroy[i]) {
20838704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2084d952e501SBarry Smith     }
2085d952e501SBarry Smith   }
2086d763cef2SBarry Smith   ts->numbermonitors = 0;
2087d763cef2SBarry Smith   PetscFunctionReturn(0);
2088d763cef2SBarry Smith }
2089d763cef2SBarry Smith 
20904a2ae208SSatish Balay #undef __FUNCT__
2091a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2092d8e5e3e6SSatish Balay /*@
2093a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
20945516499fSSatish Balay 
20955516499fSSatish Balay    Level: intermediate
209641251cbbSSatish Balay 
20975516499fSSatish Balay .keywords: TS, set, monitor
20985516499fSSatish Balay 
209941251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
210041251cbbSSatish Balay @*/
2101649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2102d763cef2SBarry Smith {
2103dfbe8321SBarry Smith   PetscErrorCode ierr;
2104649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
2105d132466eSBarry Smith 
2106d763cef2SBarry Smith   PetscFunctionBegin;
2107649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2108971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2109649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2110d763cef2SBarry Smith   PetscFunctionReturn(0);
2111d763cef2SBarry Smith }
2112d763cef2SBarry Smith 
21134a2ae208SSatish Balay #undef __FUNCT__
2114cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2115cd652676SJed Brown /*@
2116cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2117cd652676SJed Brown 
2118cd652676SJed Brown    Logically Collective on TS
2119cd652676SJed Brown 
2120cd652676SJed Brown    Input Argument:
2121cd652676SJed Brown .  ts - time stepping context
2122cd652676SJed Brown 
2123cd652676SJed Brown    Output Argument:
2124cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2125cd652676SJed Brown 
2126cd652676SJed Brown    Level: intermediate
2127cd652676SJed Brown 
2128cd652676SJed Brown .keywords: TS, set
2129cd652676SJed Brown 
2130cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2131cd652676SJed Brown @*/
2132cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2133cd652676SJed Brown {
2134cd652676SJed Brown   PetscFunctionBegin;
2135cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2136cd652676SJed Brown   ts->retain_stages = flg;
2137cd652676SJed Brown   PetscFunctionReturn(0);
2138cd652676SJed Brown }
2139cd652676SJed Brown 
2140cd652676SJed Brown #undef __FUNCT__
2141cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2142cd652676SJed Brown /*@
2143cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2144cd652676SJed Brown 
2145cd652676SJed Brown    Collective on TS
2146cd652676SJed Brown 
2147cd652676SJed Brown    Input Argument:
2148cd652676SJed Brown +  ts - time stepping context
2149cd652676SJed Brown -  t - time to interpolate to
2150cd652676SJed Brown 
2151cd652676SJed Brown    Output Argument:
21520910c330SBarry Smith .  U - state at given time
2153cd652676SJed Brown 
2154cd652676SJed Brown    Notes:
2155cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2156cd652676SJed Brown 
2157cd652676SJed Brown    Level: intermediate
2158cd652676SJed Brown 
2159cd652676SJed Brown    Developer Notes:
2160cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2161cd652676SJed Brown 
2162cd652676SJed Brown .keywords: TS, set
2163cd652676SJed Brown 
2164cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2165cd652676SJed Brown @*/
21660910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2167cd652676SJed Brown {
2168cd652676SJed Brown   PetscErrorCode ierr;
2169cd652676SJed Brown 
2170cd652676SJed Brown   PetscFunctionBegin;
2171cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2172d8cd7023SBarry 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);
2173cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
21740910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2175cd652676SJed Brown   PetscFunctionReturn(0);
2176cd652676SJed Brown }
2177cd652676SJed Brown 
2178cd652676SJed Brown #undef __FUNCT__
21794a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2180d763cef2SBarry Smith /*@
21816d9e5789SSean Farley    TSStep - Steps one time step
2182d763cef2SBarry Smith 
2183d763cef2SBarry Smith    Collective on TS
2184d763cef2SBarry Smith 
2185d763cef2SBarry Smith    Input Parameter:
2186d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2187d763cef2SBarry Smith 
21886d9e5789SSean Farley    Level: intermediate
2189d763cef2SBarry Smith 
2190b8123daeSJed Brown    Notes:
2191b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2192b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2193b8123daeSJed Brown 
219425cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
219525cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
219625cb2221SBarry Smith 
2197d763cef2SBarry Smith .keywords: TS, timestep, solve
2198d763cef2SBarry Smith 
219925cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2200d763cef2SBarry Smith @*/
2201193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2202d763cef2SBarry Smith {
2203362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2204dfbe8321SBarry Smith   PetscErrorCode ierr;
2205d763cef2SBarry Smith 
2206d763cef2SBarry Smith   PetscFunctionBegin;
22070700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2208d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2209d405a339SMatthew Knepley 
2210362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2211362cd11cSLisandro Dalcin 
2212362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2213d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2214193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2215d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2216362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2217362cd11cSLisandro Dalcin 
2218362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2219cef5090cSJed Brown     if (ts->errorifstepfailed) {
2220cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2221cef5090cSJed 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]);
2222cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2223cef5090cSJed Brown     }
2224362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2225362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2226362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2227362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2228362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2229362cd11cSLisandro Dalcin   }
2230362cd11cSLisandro Dalcin 
2231d763cef2SBarry Smith   PetscFunctionReturn(0);
2232d763cef2SBarry Smith }
2233d763cef2SBarry Smith 
22344a2ae208SSatish Balay #undef __FUNCT__
223505175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
223605175c85SJed Brown /*@
223705175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
223805175c85SJed Brown 
22391c3436cfSJed Brown    Collective on TS
224005175c85SJed Brown 
224105175c85SJed Brown    Input Arguments:
22421c3436cfSJed Brown +  ts - time stepping context
22431c3436cfSJed Brown .  order - desired order of accuracy
22441c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
224505175c85SJed Brown 
224605175c85SJed Brown    Output Arguments:
22470910c330SBarry Smith .  U - state at the end of the current step
224805175c85SJed Brown 
224905175c85SJed Brown    Level: advanced
225005175c85SJed Brown 
2251108c343cSJed Brown    Notes:
2252108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2253108c343cSJed 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.
2254108c343cSJed Brown 
22551c3436cfSJed Brown .seealso: TSStep(), TSAdapt
225605175c85SJed Brown @*/
22570910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
225805175c85SJed Brown {
225905175c85SJed Brown   PetscErrorCode ierr;
226005175c85SJed Brown 
226105175c85SJed Brown   PetscFunctionBegin;
226205175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
226305175c85SJed Brown   PetscValidType(ts,1);
22640910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
22651c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
22660910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
226705175c85SJed Brown   PetscFunctionReturn(0);
226805175c85SJed Brown }
226905175c85SJed Brown 
227005175c85SJed Brown #undef __FUNCT__
22716a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
22726a4d4014SLisandro Dalcin /*@
22736a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
22746a4d4014SLisandro Dalcin 
22756a4d4014SLisandro Dalcin    Collective on TS
22766a4d4014SLisandro Dalcin 
22776a4d4014SLisandro Dalcin    Input Parameter:
22786a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2279cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
22805a3a76d0SJed Brown 
22816a4d4014SLisandro Dalcin    Level: beginner
22826a4d4014SLisandro Dalcin 
22835a3a76d0SJed Brown    Notes:
22845a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
22855a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
22865a3a76d0SJed Brown    stepped over the final time.
22875a3a76d0SJed Brown 
22886a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
22896a4d4014SLisandro Dalcin 
22906a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
22916a4d4014SLisandro Dalcin @*/
2292cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
22936a4d4014SLisandro Dalcin {
22944d7d938eSLisandro Dalcin   PetscBool      flg;
22954d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
22964d7d938eSLisandro Dalcin   PetscViewer    viewer;
22976a4d4014SLisandro Dalcin   PetscErrorCode ierr;
2298f22f69f0SBarry Smith 
22996a4d4014SLisandro Dalcin   PetscFunctionBegin;
23000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23010910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
23025a3a76d0SJed Brown   if (ts->exact_final_time) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
23030910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
23045a3a76d0SJed Brown       Vec y;
23050910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
23065a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
23075a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
23085a3a76d0SJed Brown     }
23090910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
23105a3a76d0SJed Brown   } else {
23110910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
23125a3a76d0SJed Brown   }
2313b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
23146a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2315193ac0bcSJed Brown   ts->steps = 0;
23165ef26d82SJed Brown   ts->ksp_its = 0;
23175ef26d82SJed Brown   ts->snes_its = 0;
2318c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2319c610991cSLisandro Dalcin   ts->reject = 0;
2320193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
2321193ac0bcSJed Brown 
2322193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2323193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
23240910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2325cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2326193ac0bcSJed Brown   } else {
23276a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2328362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2329362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2330362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2331362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2332362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2333e1a7a14fSJed Brown     while (!ts->reason) {
2334193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2335193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2336193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2337193ac0bcSJed Brown     }
2338362cd11cSLisandro Dalcin     if (ts->exact_final_time && ts->ptime > ts->max_time) {
23390910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2340cc708dedSBarry Smith       ts->solvetime = ts->max_time;
23410574a7fbSJed Brown     } else {
23420910c330SBarry Smith       ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2343cc708dedSBarry Smith       ts->solvetime = ts->ptime;
23440574a7fbSJed Brown     }
2345193ac0bcSJed Brown   }
23463923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
23474d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
23484d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
23494d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
23504d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
23514d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2352193ac0bcSJed Brown   }
23536a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
23546a4d4014SLisandro Dalcin }
23556a4d4014SLisandro Dalcin 
23566a4d4014SLisandro Dalcin #undef __FUNCT__
23574a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2358228d79bcSJed Brown /*@
2359228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2360228d79bcSJed Brown 
2361228d79bcSJed Brown    Collective on TS
2362228d79bcSJed Brown 
2363228d79bcSJed Brown    Input Parameters:
2364228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2365228d79bcSJed Brown .  step - step number that has just completed
2366228d79bcSJed Brown .  ptime - model time of the state
23670910c330SBarry Smith -  u - state at the current model time
2368228d79bcSJed Brown 
2369228d79bcSJed Brown    Notes:
2370228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2371228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2372228d79bcSJed Brown 
2373228d79bcSJed Brown    Level: advanced
2374228d79bcSJed Brown 
2375228d79bcSJed Brown .keywords: TS, timestep
2376228d79bcSJed Brown @*/
23770910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2378d763cef2SBarry Smith {
23796849ba73SBarry Smith   PetscErrorCode ierr;
2380a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2381d763cef2SBarry Smith 
2382d763cef2SBarry Smith   PetscFunctionBegin;
2383d763cef2SBarry Smith   for (i=0; i<n; i++) {
23840910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2385d763cef2SBarry Smith   }
2386d763cef2SBarry Smith   PetscFunctionReturn(0);
2387d763cef2SBarry Smith }
2388d763cef2SBarry Smith 
2389d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
23900b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
23910b039ecaSBarry Smith   PetscDrawLG lg;
23920b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2393201da799SBarry Smith   PetscInt    ksp_its,snes_its;
23940b039ecaSBarry Smith };
23950b039ecaSBarry Smith 
2396d763cef2SBarry Smith 
23974a2ae208SSatish Balay #undef __FUNCT__
2398a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2399d763cef2SBarry Smith /*@C
2400a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2401a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2402d763cef2SBarry Smith 
2403d763cef2SBarry Smith    Collective on TS
2404d763cef2SBarry Smith 
2405d763cef2SBarry Smith    Input Parameters:
2406d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2407d763cef2SBarry Smith .  label - the title to put in the title bar
24087c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2409a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2410a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2411d763cef2SBarry Smith 
2412d763cef2SBarry Smith    Output Parameter:
24130b039ecaSBarry Smith .  ctx - the context
2414d763cef2SBarry Smith 
2415d763cef2SBarry Smith    Options Database Key:
24164f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
24174f09c107SBarry Smith .  -ts_monitor_lg_solution -
241899fdda47SBarry Smith .  -ts_monitor_lg_error -
241999fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
242099fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
242199fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2422d763cef2SBarry Smith 
2423d763cef2SBarry Smith    Notes:
2424a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2425d763cef2SBarry Smith 
2426d763cef2SBarry Smith    Level: intermediate
2427d763cef2SBarry Smith 
24287c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2429d763cef2SBarry Smith 
24304f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
24317c922b88SBarry Smith 
2432d763cef2SBarry Smith @*/
2433a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2434d763cef2SBarry Smith {
2435b0a32e0cSBarry Smith   PetscDraw      win;
2436dfbe8321SBarry Smith   PetscErrorCode ierr;
243799fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2438d763cef2SBarry Smith 
2439d763cef2SBarry Smith   PetscFunctionBegin;
2440201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2441a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
24423fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
24430b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
244499fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr);
244599fdda47SBarry Smith   if (flg) {
24460b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
244799fdda47SBarry Smith   }
24480b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2449a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2450d763cef2SBarry Smith   PetscFunctionReturn(0);
2451d763cef2SBarry Smith }
2452d763cef2SBarry Smith 
24534a2ae208SSatish Balay #undef __FUNCT__
24544f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
24554f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2456d763cef2SBarry Smith {
24570b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2458c32365f1SBarry Smith   PetscReal      x = ptime,y;
2459dfbe8321SBarry Smith   PetscErrorCode ierr;
2460d763cef2SBarry Smith 
2461d763cef2SBarry Smith   PetscFunctionBegin;
2462a9f9c1f6SBarry Smith   if (!n) {
2463a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
2464a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2465a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2466a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2467a9f9c1f6SBarry Smith   }
2468c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
24690b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
247099fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){
24710b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
24723923b477SBarry Smith   }
2473d763cef2SBarry Smith   PetscFunctionReturn(0);
2474d763cef2SBarry Smith }
2475d763cef2SBarry Smith 
24764a2ae208SSatish Balay #undef __FUNCT__
2477a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2478d763cef2SBarry Smith /*@C
2479a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2480a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2481d763cef2SBarry Smith 
24820b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2483d763cef2SBarry Smith 
2484d763cef2SBarry Smith    Input Parameter:
24850b039ecaSBarry Smith .  ctx - the monitor context
2486d763cef2SBarry Smith 
2487d763cef2SBarry Smith    Level: intermediate
2488d763cef2SBarry Smith 
2489d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2490d763cef2SBarry Smith 
24914f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2492d763cef2SBarry Smith @*/
2493a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2494d763cef2SBarry Smith {
2495b0a32e0cSBarry Smith   PetscDraw      draw;
2496dfbe8321SBarry Smith   PetscErrorCode ierr;
2497d763cef2SBarry Smith 
2498d763cef2SBarry Smith   PetscFunctionBegin;
24990b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
25006bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
25010b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
25020b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2503d763cef2SBarry Smith   PetscFunctionReturn(0);
2504d763cef2SBarry Smith }
2505d763cef2SBarry Smith 
25064a2ae208SSatish Balay #undef __FUNCT__
25074a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2508d763cef2SBarry Smith /*@
2509b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2510d763cef2SBarry Smith 
2511d763cef2SBarry Smith    Not Collective
2512d763cef2SBarry Smith 
2513d763cef2SBarry Smith    Input Parameter:
2514d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2515d763cef2SBarry Smith 
2516d763cef2SBarry Smith    Output Parameter:
2517d763cef2SBarry Smith .  t  - the current time
2518d763cef2SBarry Smith 
2519d763cef2SBarry Smith    Level: beginner
2520d763cef2SBarry Smith 
2521b8123daeSJed Brown    Note:
2522b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2523b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2524b8123daeSJed Brown 
2525d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2526d763cef2SBarry Smith 
2527d763cef2SBarry Smith .keywords: TS, get, time
2528d763cef2SBarry Smith @*/
25297087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2530d763cef2SBarry Smith {
2531d763cef2SBarry Smith   PetscFunctionBegin;
25320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2533f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2534d763cef2SBarry Smith   *t = ts->ptime;
2535d763cef2SBarry Smith   PetscFunctionReturn(0);
2536d763cef2SBarry Smith }
2537d763cef2SBarry Smith 
25384a2ae208SSatish Balay #undef __FUNCT__
25396a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
25406a4d4014SLisandro Dalcin /*@
25416a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
25426a4d4014SLisandro Dalcin 
25433f9fe445SBarry Smith    Logically Collective on TS
25446a4d4014SLisandro Dalcin 
25456a4d4014SLisandro Dalcin    Input Parameters:
25466a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
25476a4d4014SLisandro Dalcin -  time - the time
25486a4d4014SLisandro Dalcin 
25496a4d4014SLisandro Dalcin    Level: intermediate
25506a4d4014SLisandro Dalcin 
25516a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
25526a4d4014SLisandro Dalcin 
25536a4d4014SLisandro Dalcin .keywords: TS, set, time
25546a4d4014SLisandro Dalcin @*/
25557087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
25566a4d4014SLisandro Dalcin {
25576a4d4014SLisandro Dalcin   PetscFunctionBegin;
25580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2559c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
25606a4d4014SLisandro Dalcin   ts->ptime = t;
25616a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
25626a4d4014SLisandro Dalcin }
25636a4d4014SLisandro Dalcin 
25646a4d4014SLisandro Dalcin #undef __FUNCT__
25654a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2566d763cef2SBarry Smith /*@C
2567d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2568d763cef2SBarry Smith    TS options in the database.
2569d763cef2SBarry Smith 
25703f9fe445SBarry Smith    Logically Collective on TS
2571d763cef2SBarry Smith 
2572d763cef2SBarry Smith    Input Parameter:
2573d763cef2SBarry Smith +  ts     - The TS context
2574d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2575d763cef2SBarry Smith 
2576d763cef2SBarry Smith    Notes:
2577d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2578d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2579d763cef2SBarry Smith    hyphen.
2580d763cef2SBarry Smith 
2581d763cef2SBarry Smith    Level: advanced
2582d763cef2SBarry Smith 
2583d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2584d763cef2SBarry Smith 
2585d763cef2SBarry Smith .seealso: TSSetFromOptions()
2586d763cef2SBarry Smith 
2587d763cef2SBarry Smith @*/
25887087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2589d763cef2SBarry Smith {
2590dfbe8321SBarry Smith   PetscErrorCode ierr;
2591089b2837SJed Brown   SNES           snes;
2592d763cef2SBarry Smith 
2593d763cef2SBarry Smith   PetscFunctionBegin;
25940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2595d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2596089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2597089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2598d763cef2SBarry Smith   PetscFunctionReturn(0);
2599d763cef2SBarry Smith }
2600d763cef2SBarry Smith 
2601d763cef2SBarry Smith 
26024a2ae208SSatish Balay #undef __FUNCT__
26034a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2604d763cef2SBarry Smith /*@C
2605d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2606d763cef2SBarry Smith    TS options in the database.
2607d763cef2SBarry Smith 
26083f9fe445SBarry Smith    Logically Collective on TS
2609d763cef2SBarry Smith 
2610d763cef2SBarry Smith    Input Parameter:
2611d763cef2SBarry Smith +  ts     - The TS context
2612d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2613d763cef2SBarry Smith 
2614d763cef2SBarry Smith    Notes:
2615d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2616d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2617d763cef2SBarry Smith    hyphen.
2618d763cef2SBarry Smith 
2619d763cef2SBarry Smith    Level: advanced
2620d763cef2SBarry Smith 
2621d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2622d763cef2SBarry Smith 
2623d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2624d763cef2SBarry Smith 
2625d763cef2SBarry Smith @*/
26267087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2627d763cef2SBarry Smith {
2628dfbe8321SBarry Smith   PetscErrorCode ierr;
2629089b2837SJed Brown   SNES           snes;
2630d763cef2SBarry Smith 
2631d763cef2SBarry Smith   PetscFunctionBegin;
26320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2633d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2634089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2635089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2636d763cef2SBarry Smith   PetscFunctionReturn(0);
2637d763cef2SBarry Smith }
2638d763cef2SBarry Smith 
26394a2ae208SSatish Balay #undef __FUNCT__
26404a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2641d763cef2SBarry Smith /*@C
2642d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2643d763cef2SBarry Smith    TS options in the database.
2644d763cef2SBarry Smith 
2645d763cef2SBarry Smith    Not Collective
2646d763cef2SBarry Smith 
2647d763cef2SBarry Smith    Input Parameter:
2648d763cef2SBarry Smith .  ts - The TS context
2649d763cef2SBarry Smith 
2650d763cef2SBarry Smith    Output Parameter:
2651d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2652d763cef2SBarry Smith 
2653d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2654d763cef2SBarry Smith    sufficient length to hold the prefix.
2655d763cef2SBarry Smith 
2656d763cef2SBarry Smith    Level: intermediate
2657d763cef2SBarry Smith 
2658d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2659d763cef2SBarry Smith 
2660d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2661d763cef2SBarry Smith @*/
26627087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2663d763cef2SBarry Smith {
2664dfbe8321SBarry Smith   PetscErrorCode ierr;
2665d763cef2SBarry Smith 
2666d763cef2SBarry Smith   PetscFunctionBegin;
26670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26684482741eSBarry Smith   PetscValidPointer(prefix,2);
2669d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2670d763cef2SBarry Smith   PetscFunctionReturn(0);
2671d763cef2SBarry Smith }
2672d763cef2SBarry Smith 
26734a2ae208SSatish Balay #undef __FUNCT__
26744a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2675d763cef2SBarry Smith /*@C
2676d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2677d763cef2SBarry Smith 
2678d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2679d763cef2SBarry Smith 
2680d763cef2SBarry Smith    Input Parameter:
2681d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2682d763cef2SBarry Smith 
2683d763cef2SBarry Smith    Output Parameters:
2684b5abc632SBarry Smith +  J   - The Jacobian J of F, where U_t = G(U,t)
2685d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2686089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2687d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2688d763cef2SBarry Smith 
2689d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2690d763cef2SBarry Smith 
2691d763cef2SBarry Smith    Level: intermediate
2692d763cef2SBarry Smith 
269326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2694d763cef2SBarry Smith 
2695d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2696d763cef2SBarry Smith @*/
2697089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2698d763cef2SBarry Smith {
2699089b2837SJed Brown   PetscErrorCode ierr;
2700089b2837SJed Brown   SNES           snes;
270124989b8cSPeter Brune   DM             dm;
2702089b2837SJed Brown 
2703d763cef2SBarry Smith   PetscFunctionBegin;
2704089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2705089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
270624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
270724989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2708d763cef2SBarry Smith   PetscFunctionReturn(0);
2709d763cef2SBarry Smith }
2710d763cef2SBarry Smith 
27111713a123SBarry Smith #undef __FUNCT__
27122eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
27132eca1d9cSJed Brown /*@C
27142eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
27152eca1d9cSJed Brown 
27162eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
27172eca1d9cSJed Brown 
27182eca1d9cSJed Brown    Input Parameter:
27192eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
27202eca1d9cSJed Brown 
27212eca1d9cSJed Brown    Output Parameters:
27222eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
27232eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
27242eca1d9cSJed Brown .  f   - The function to compute the matrices
27252eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
27262eca1d9cSJed Brown 
27272eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
27282eca1d9cSJed Brown 
27292eca1d9cSJed Brown    Level: advanced
27302eca1d9cSJed Brown 
27312eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
27322eca1d9cSJed Brown 
27332eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
27342eca1d9cSJed Brown @*/
27357087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
27362eca1d9cSJed Brown {
2737089b2837SJed Brown   PetscErrorCode ierr;
2738089b2837SJed Brown   SNES           snes;
273924989b8cSPeter Brune   DM             dm;
27400910c330SBarry Smith 
27412eca1d9cSJed Brown   PetscFunctionBegin;
2742089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2743089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
274424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
274524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
27462eca1d9cSJed Brown   PetscFunctionReturn(0);
27472eca1d9cSJed Brown }
27482eca1d9cSJed Brown 
274983a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
27506083293cSBarry Smith   PetscViewer viewer;
27516083293cSBarry Smith   Vec         initialsolution;
27526083293cSBarry Smith   PetscBool   showinitial;
275383a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
275483a4ac43SBarry Smith };
27556083293cSBarry Smith 
27562eca1d9cSJed Brown #undef __FUNCT__
275783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
27581713a123SBarry Smith /*@C
275983a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
27601713a123SBarry Smith    VecView() for the solution at each timestep
27611713a123SBarry Smith 
27621713a123SBarry Smith    Collective on TS
27631713a123SBarry Smith 
27641713a123SBarry Smith    Input Parameters:
27651713a123SBarry Smith +  ts - the TS context
27661713a123SBarry Smith .  step - current time-step
2767142b95e3SSatish Balay .  ptime - current time
27681713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
27691713a123SBarry Smith 
277099fdda47SBarry Smith    Options Database:
277199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
277299fdda47SBarry Smith 
277399fdda47SBarry 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
277499fdda47SBarry Smith        will look bad
277599fdda47SBarry Smith 
27761713a123SBarry Smith    Level: intermediate
27771713a123SBarry Smith 
27781713a123SBarry Smith .keywords: TS,  vector, monitor, view
27791713a123SBarry Smith 
2780a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
27811713a123SBarry Smith @*/
27820910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
27831713a123SBarry Smith {
2784dfbe8321SBarry Smith   PetscErrorCode   ierr;
278583a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
27861713a123SBarry Smith 
27871713a123SBarry Smith   PetscFunctionBegin;
27886083293cSBarry Smith   if (!step && ictx->showinitial) {
27896083293cSBarry Smith     if (!ictx->initialsolution) {
27900910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
27911713a123SBarry Smith     }
27920910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
27936083293cSBarry Smith   }
27943fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
27950dcf80beSBarry Smith 
27966083293cSBarry Smith   if (ictx->showinitial) {
27976083293cSBarry Smith     PetscReal pause;
27986083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
27996083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
28006083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
28016083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
28026083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
28036083293cSBarry Smith   }
28040910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
28056083293cSBarry Smith   if (ictx->showinitial) {
28066083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
28076083293cSBarry Smith   }
28081713a123SBarry Smith   PetscFunctionReturn(0);
28091713a123SBarry Smith }
28101713a123SBarry Smith 
28111713a123SBarry Smith 
28126c699258SBarry Smith #undef __FUNCT__
281383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
28146083293cSBarry Smith /*@C
281583a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
28166083293cSBarry Smith 
28176083293cSBarry Smith    Collective on TS
28186083293cSBarry Smith 
28196083293cSBarry Smith    Input Parameters:
28206083293cSBarry Smith .    ctx - the monitor context
28216083293cSBarry Smith 
28226083293cSBarry Smith    Level: intermediate
28236083293cSBarry Smith 
28246083293cSBarry Smith .keywords: TS,  vector, monitor, view
28256083293cSBarry Smith 
282683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
28276083293cSBarry Smith @*/
282883a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
28296083293cSBarry Smith {
28306083293cSBarry Smith   PetscErrorCode       ierr;
28316083293cSBarry Smith 
28326083293cSBarry Smith   PetscFunctionBegin;
283383a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
283483a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
283583a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
28366083293cSBarry Smith   PetscFunctionReturn(0);
28376083293cSBarry Smith }
28386083293cSBarry Smith 
28396083293cSBarry Smith #undef __FUNCT__
284083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
28416083293cSBarry Smith /*@C
284283a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
28436083293cSBarry Smith 
28446083293cSBarry Smith    Collective on TS
28456083293cSBarry Smith 
28466083293cSBarry Smith    Input Parameter:
28476083293cSBarry Smith .    ts - time-step context
28486083293cSBarry Smith 
28496083293cSBarry Smith    Output Patameter:
28506083293cSBarry Smith .    ctx - the monitor context
28516083293cSBarry Smith 
285299fdda47SBarry Smith    Options Database:
285399fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
285499fdda47SBarry Smith 
28556083293cSBarry Smith    Level: intermediate
28566083293cSBarry Smith 
28576083293cSBarry Smith .keywords: TS,  vector, monitor, view
28586083293cSBarry Smith 
285983a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
28606083293cSBarry Smith @*/
286183a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
28626083293cSBarry Smith {
28636083293cSBarry Smith   PetscErrorCode   ierr;
28646083293cSBarry Smith 
28656083293cSBarry Smith   PetscFunctionBegin;
286683a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
286783a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
286883a4ac43SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
286983a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
287099fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr);
28716083293cSBarry Smith   PetscFunctionReturn(0);
28726083293cSBarry Smith }
28736083293cSBarry Smith 
28746083293cSBarry Smith #undef __FUNCT__
287583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
28763a471f94SBarry Smith /*@C
287783a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
28783a471f94SBarry Smith    VecView() for the error at each timestep
28793a471f94SBarry Smith 
28803a471f94SBarry Smith    Collective on TS
28813a471f94SBarry Smith 
28823a471f94SBarry Smith    Input Parameters:
28833a471f94SBarry Smith +  ts - the TS context
28843a471f94SBarry Smith .  step - current time-step
28853a471f94SBarry Smith .  ptime - current time
28863a471f94SBarry Smith -  dummy - either a viewer or PETSC_NULL
28873a471f94SBarry Smith 
28883a471f94SBarry Smith    Level: intermediate
28893a471f94SBarry Smith 
28903a471f94SBarry Smith .keywords: TS,  vector, monitor, view
28913a471f94SBarry Smith 
28923a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
28933a471f94SBarry Smith @*/
28940910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
28953a471f94SBarry Smith {
28963a471f94SBarry Smith   PetscErrorCode   ierr;
289783a4ac43SBarry Smith   TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy;
289883a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
28993a471f94SBarry Smith   Vec              work;
29003a471f94SBarry Smith 
29013a471f94SBarry Smith   PetscFunctionBegin;
29023fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
29030910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
29043a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
29050910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
29063a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
29073a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
29083a471f94SBarry Smith   PetscFunctionReturn(0);
29093a471f94SBarry Smith }
29103a471f94SBarry Smith 
29113a471f94SBarry Smith #undef __FUNCT__
29126c699258SBarry Smith #define __FUNCT__ "TSSetDM"
29136c699258SBarry Smith /*@
29146c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
29156c699258SBarry Smith 
29163f9fe445SBarry Smith    Logically Collective on TS and DM
29176c699258SBarry Smith 
29186c699258SBarry Smith    Input Parameters:
29196c699258SBarry Smith +  ts - the preconditioner context
29206c699258SBarry Smith -  dm - the dm
29216c699258SBarry Smith 
29226c699258SBarry Smith    Level: intermediate
29236c699258SBarry Smith 
29246c699258SBarry Smith 
29256c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
29266c699258SBarry Smith @*/
29277087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
29286c699258SBarry Smith {
29296c699258SBarry Smith   PetscErrorCode ierr;
2930089b2837SJed Brown   SNES           snes;
293124989b8cSPeter Brune   TSDM           tsdm;
29326c699258SBarry Smith 
29336c699258SBarry Smith   PetscFunctionBegin;
29340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
293570663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
293624989b8cSPeter Brune   if (ts->dm) {               /* Move the TSDM context over to the new DM unless the new DM already has one */
293724989b8cSPeter Brune     PetscContainer oldcontainer,container;
293824989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr);
293924989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr);
294024989b8cSPeter Brune     if (oldcontainer && !container) {
294124989b8cSPeter Brune       ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr);
294224989b8cSPeter Brune       ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr);
294324989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
294424989b8cSPeter Brune         tsdm->originaldm = dm;
294524989b8cSPeter Brune       }
294624989b8cSPeter Brune     }
29476bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
294824989b8cSPeter Brune   }
29496c699258SBarry Smith   ts->dm = dm;
2950089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2951089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
29526c699258SBarry Smith   PetscFunctionReturn(0);
29536c699258SBarry Smith }
29546c699258SBarry Smith 
29556c699258SBarry Smith #undef __FUNCT__
29566c699258SBarry Smith #define __FUNCT__ "TSGetDM"
29576c699258SBarry Smith /*@
29586c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
29596c699258SBarry Smith 
29603f9fe445SBarry Smith    Not Collective
29616c699258SBarry Smith 
29626c699258SBarry Smith    Input Parameter:
29636c699258SBarry Smith . ts - the preconditioner context
29646c699258SBarry Smith 
29656c699258SBarry Smith    Output Parameter:
29666c699258SBarry Smith .  dm - the dm
29676c699258SBarry Smith 
29686c699258SBarry Smith    Level: intermediate
29696c699258SBarry Smith 
29706c699258SBarry Smith 
29716c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
29726c699258SBarry Smith @*/
29737087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
29746c699258SBarry Smith {
2975496e6a7aSJed Brown   PetscErrorCode ierr;
2976496e6a7aSJed Brown 
29776c699258SBarry Smith   PetscFunctionBegin;
29780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2979496e6a7aSJed Brown   if (!ts->dm) {
2980496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
2981496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
2982496e6a7aSJed Brown   }
29836c699258SBarry Smith   *dm = ts->dm;
29846c699258SBarry Smith   PetscFunctionReturn(0);
29856c699258SBarry Smith }
29861713a123SBarry Smith 
29870f5c6efeSJed Brown #undef __FUNCT__
29880f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
29890f5c6efeSJed Brown /*@
29900f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
29910f5c6efeSJed Brown 
29923f9fe445SBarry Smith    Logically Collective on SNES
29930f5c6efeSJed Brown 
29940f5c6efeSJed Brown    Input Parameter:
2995d42a1c89SJed Brown + snes - nonlinear solver
29960910c330SBarry Smith . U - the current state at which to evaluate the residual
2997d42a1c89SJed Brown - ctx - user context, must be a TS
29980f5c6efeSJed Brown 
29990f5c6efeSJed Brown    Output Parameter:
30000f5c6efeSJed Brown . F - the nonlinear residual
30010f5c6efeSJed Brown 
30020f5c6efeSJed Brown    Notes:
30030f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
30040f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
30050f5c6efeSJed Brown 
30060f5c6efeSJed Brown    Level: advanced
30070f5c6efeSJed Brown 
30080f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
30090f5c6efeSJed Brown @*/
30100910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
30110f5c6efeSJed Brown {
30120f5c6efeSJed Brown   TS             ts = (TS)ctx;
30130f5c6efeSJed Brown   PetscErrorCode ierr;
30140f5c6efeSJed Brown 
30150f5c6efeSJed Brown   PetscFunctionBegin;
30160f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
30170910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
30180f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
30190f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
30200910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
30210f5c6efeSJed Brown   PetscFunctionReturn(0);
30220f5c6efeSJed Brown }
30230f5c6efeSJed Brown 
30240f5c6efeSJed Brown #undef __FUNCT__
30250f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
30260f5c6efeSJed Brown /*@
30270f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
30280f5c6efeSJed Brown 
30290f5c6efeSJed Brown    Collective on SNES
30300f5c6efeSJed Brown 
30310f5c6efeSJed Brown    Input Parameter:
30320f5c6efeSJed Brown + snes - nonlinear solver
30330910c330SBarry Smith . U - the current state at which to evaluate the residual
30340f5c6efeSJed Brown - ctx - user context, must be a TS
30350f5c6efeSJed Brown 
30360f5c6efeSJed Brown    Output Parameter:
30370f5c6efeSJed Brown + A - the Jacobian
30380f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
30390f5c6efeSJed Brown - flag - indicates any structure change in the matrix
30400f5c6efeSJed Brown 
30410f5c6efeSJed Brown    Notes:
30420f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
30430f5c6efeSJed Brown 
30440f5c6efeSJed Brown    Level: developer
30450f5c6efeSJed Brown 
30460f5c6efeSJed Brown .seealso: SNESSetJacobian()
30470f5c6efeSJed Brown @*/
30480910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
30490f5c6efeSJed Brown {
30500f5c6efeSJed Brown   TS             ts = (TS)ctx;
30510f5c6efeSJed Brown   PetscErrorCode ierr;
30520f5c6efeSJed Brown 
30530f5c6efeSJed Brown   PetscFunctionBegin;
30540f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
30550910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
30560f5c6efeSJed Brown   PetscValidPointer(A,3);
30570f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
30580f5c6efeSJed Brown   PetscValidPointer(B,4);
30590f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
30600f5c6efeSJed Brown   PetscValidPointer(flag,5);
30610f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
30620910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
30630f5c6efeSJed Brown   PetscFunctionReturn(0);
30640f5c6efeSJed Brown }
3065325fc9f4SBarry Smith 
30660e4ef248SJed Brown #undef __FUNCT__
30670e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
30680e4ef248SJed Brown /*@C
30690e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
30700e4ef248SJed Brown 
30710e4ef248SJed Brown    Collective on TS
30720e4ef248SJed Brown 
30730e4ef248SJed Brown    Input Arguments:
30740e4ef248SJed Brown +  ts - time stepping context
30750e4ef248SJed Brown .  t - time at which to evaluate
30760910c330SBarry Smith .  U - state at which to evaluate
30770e4ef248SJed Brown -  ctx - context
30780e4ef248SJed Brown 
30790e4ef248SJed Brown    Output Arguments:
30800e4ef248SJed Brown .  F - right hand side
30810e4ef248SJed Brown 
30820e4ef248SJed Brown    Level: intermediate
30830e4ef248SJed Brown 
30840e4ef248SJed Brown    Notes:
30850e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
30860e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
30870e4ef248SJed Brown 
30880e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
30890e4ef248SJed Brown @*/
30900910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
30910e4ef248SJed Brown {
30920e4ef248SJed Brown   PetscErrorCode ierr;
30930e4ef248SJed Brown   Mat            Arhs,Brhs;
30940e4ef248SJed Brown   MatStructure   flg2;
30950e4ef248SJed Brown 
30960e4ef248SJed Brown   PetscFunctionBegin;
30970e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
30980910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
30990910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
31000e4ef248SJed Brown   PetscFunctionReturn(0);
31010e4ef248SJed Brown }
31020e4ef248SJed Brown 
31030e4ef248SJed Brown #undef __FUNCT__
31040e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
31050e4ef248SJed Brown /*@C
31060e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
31070e4ef248SJed Brown 
31080e4ef248SJed Brown    Collective on TS
31090e4ef248SJed Brown 
31100e4ef248SJed Brown    Input Arguments:
31110e4ef248SJed Brown +  ts - time stepping context
31120e4ef248SJed Brown .  t - time at which to evaluate
31130910c330SBarry Smith .  U - state at which to evaluate
31140e4ef248SJed Brown -  ctx - context
31150e4ef248SJed Brown 
31160e4ef248SJed Brown    Output Arguments:
31170e4ef248SJed Brown +  A - pointer to operator
31180e4ef248SJed Brown .  B - pointer to preconditioning matrix
31190e4ef248SJed Brown -  flg - matrix structure flag
31200e4ef248SJed Brown 
31210e4ef248SJed Brown    Level: intermediate
31220e4ef248SJed Brown 
31230e4ef248SJed Brown    Notes:
31240e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
31250e4ef248SJed Brown 
31260e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
31270e4ef248SJed Brown @*/
31280910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
31290e4ef248SJed Brown {
31300e4ef248SJed Brown   PetscFunctionBegin;
31310e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
31320e4ef248SJed Brown   PetscFunctionReturn(0);
31330e4ef248SJed Brown }
31340e4ef248SJed Brown 
31350026cea9SSean Farley #undef __FUNCT__
31360026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
31370026cea9SSean Farley /*@C
31380026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
31390026cea9SSean Farley 
31400026cea9SSean Farley    Collective on TS
31410026cea9SSean Farley 
31420026cea9SSean Farley    Input Arguments:
31430026cea9SSean Farley +  ts - time stepping context
31440026cea9SSean Farley .  t - time at which to evaluate
31450910c330SBarry Smith .  U - state at which to evaluate
31460910c330SBarry Smith .  Udot - time derivative of state vector
31470026cea9SSean Farley -  ctx - context
31480026cea9SSean Farley 
31490026cea9SSean Farley    Output Arguments:
31500026cea9SSean Farley .  F - left hand side
31510026cea9SSean Farley 
31520026cea9SSean Farley    Level: intermediate
31530026cea9SSean Farley 
31540026cea9SSean Farley    Notes:
31550910c330SBarry 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
31560026cea9SSean Farley    user is required to write their own TSComputeIFunction.
31570026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
31580026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
31590026cea9SSean Farley 
31600026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
31610026cea9SSean Farley @*/
31620910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
31630026cea9SSean Farley {
31640026cea9SSean Farley   PetscErrorCode ierr;
31650026cea9SSean Farley   Mat            A,B;
31660026cea9SSean Farley   MatStructure   flg2;
31670026cea9SSean Farley 
31680026cea9SSean Farley   PetscFunctionBegin;
31690026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
31700910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
31710910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
31720026cea9SSean Farley   PetscFunctionReturn(0);
31730026cea9SSean Farley }
31740026cea9SSean Farley 
31750026cea9SSean Farley #undef __FUNCT__
31760026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
31770026cea9SSean Farley /*@C
317824e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
31790026cea9SSean Farley 
31800026cea9SSean Farley    Collective on TS
31810026cea9SSean Farley 
31820026cea9SSean Farley    Input Arguments:
31830026cea9SSean Farley +  ts - time stepping context
31840026cea9SSean Farley .  t - time at which to evaluate
31850910c330SBarry Smith .  U - state at which to evaluate
31860910c330SBarry Smith .  Udot - time derivative of state vector
31870026cea9SSean Farley .  shift - shift to apply
31880026cea9SSean Farley -  ctx - context
31890026cea9SSean Farley 
31900026cea9SSean Farley    Output Arguments:
31910026cea9SSean Farley +  A - pointer to operator
31920026cea9SSean Farley .  B - pointer to preconditioning matrix
31930026cea9SSean Farley -  flg - matrix structure flag
31940026cea9SSean Farley 
31950026cea9SSean Farley    Level: intermediate
31960026cea9SSean Farley 
31970026cea9SSean Farley    Notes:
31980026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
31990026cea9SSean Farley 
32000026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
32010026cea9SSean Farley @*/
32020910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
32030026cea9SSean Farley {
32040026cea9SSean Farley   PetscFunctionBegin;
32050026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
32060026cea9SSean Farley   PetscFunctionReturn(0);
32070026cea9SSean Farley }
32080026cea9SSean Farley 
32094af1b03aSJed Brown #undef __FUNCT__
32104af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
32114af1b03aSJed Brown /*@
32124af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
32134af1b03aSJed Brown 
32144af1b03aSJed Brown    Not Collective
32154af1b03aSJed Brown 
32164af1b03aSJed Brown    Input Parameter:
32174af1b03aSJed Brown .  ts - the TS context
32184af1b03aSJed Brown 
32194af1b03aSJed Brown    Output Parameter:
32204af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
32214af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
32224af1b03aSJed Brown 
3223487e0bb9SJed Brown    Level: beginner
32244af1b03aSJed Brown 
3225cd652676SJed Brown    Notes:
3226cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
32274af1b03aSJed Brown 
32284af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
32294af1b03aSJed Brown 
32304af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
32314af1b03aSJed Brown @*/
32324af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
32334af1b03aSJed Brown {
32344af1b03aSJed Brown   PetscFunctionBegin;
32354af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32364af1b03aSJed Brown   PetscValidPointer(reason,2);
32374af1b03aSJed Brown   *reason = ts->reason;
32384af1b03aSJed Brown   PetscFunctionReturn(0);
32394af1b03aSJed Brown }
32404af1b03aSJed Brown 
3241fb1732b5SBarry Smith #undef __FUNCT__
3242cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3243cc708dedSBarry Smith /*@
3244cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3245cc708dedSBarry Smith 
3246cc708dedSBarry Smith    Not Collective
3247cc708dedSBarry Smith 
3248cc708dedSBarry Smith    Input Parameter:
3249cc708dedSBarry Smith .  ts - the TS context
3250cc708dedSBarry Smith 
3251cc708dedSBarry Smith    Output Parameter:
3252cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3253cc708dedSBarry Smith 
3254487e0bb9SJed Brown    Level: beginner
3255cc708dedSBarry Smith 
3256cc708dedSBarry Smith    Notes:
3257cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3258cc708dedSBarry Smith 
3259cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3260cc708dedSBarry Smith 
3261cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3262cc708dedSBarry Smith @*/
3263cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3264cc708dedSBarry Smith {
3265cc708dedSBarry Smith   PetscFunctionBegin;
3266cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3267cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3268cc708dedSBarry Smith   *ftime = ts->solvetime;
3269cc708dedSBarry Smith   PetscFunctionReturn(0);
3270cc708dedSBarry Smith }
3271cc708dedSBarry Smith 
3272cc708dedSBarry Smith #undef __FUNCT__
32735ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
32749f67acb7SJed Brown /*@
32755ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
32769f67acb7SJed Brown    used by the time integrator.
32779f67acb7SJed Brown 
32789f67acb7SJed Brown    Not Collective
32799f67acb7SJed Brown 
32809f67acb7SJed Brown    Input Parameter:
32819f67acb7SJed Brown .  ts - TS context
32829f67acb7SJed Brown 
32839f67acb7SJed Brown    Output Parameter:
32849f67acb7SJed Brown .  nits - number of nonlinear iterations
32859f67acb7SJed Brown 
32869f67acb7SJed Brown    Notes:
32879f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
32889f67acb7SJed Brown 
32899f67acb7SJed Brown    Level: intermediate
32909f67acb7SJed Brown 
32919f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
32929f67acb7SJed Brown 
32935ef26d82SJed Brown .seealso:  TSGetKSPIterations()
32949f67acb7SJed Brown @*/
32955ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
32969f67acb7SJed Brown {
32979f67acb7SJed Brown   PetscFunctionBegin;
32989f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32999f67acb7SJed Brown   PetscValidIntPointer(nits,2);
33005ef26d82SJed Brown   *nits = ts->snes_its;
33019f67acb7SJed Brown   PetscFunctionReturn(0);
33029f67acb7SJed Brown }
33039f67acb7SJed Brown 
33049f67acb7SJed Brown #undef __FUNCT__
33055ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
33069f67acb7SJed Brown /*@
33075ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
33089f67acb7SJed Brown    used by the time integrator.
33099f67acb7SJed Brown 
33109f67acb7SJed Brown    Not Collective
33119f67acb7SJed Brown 
33129f67acb7SJed Brown    Input Parameter:
33139f67acb7SJed Brown .  ts - TS context
33149f67acb7SJed Brown 
33159f67acb7SJed Brown    Output Parameter:
33169f67acb7SJed Brown .  lits - number of linear iterations
33179f67acb7SJed Brown 
33189f67acb7SJed Brown    Notes:
33199f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
33209f67acb7SJed Brown 
33219f67acb7SJed Brown    Level: intermediate
33229f67acb7SJed Brown 
33239f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
33249f67acb7SJed Brown 
33255ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
33269f67acb7SJed Brown @*/
33275ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
33289f67acb7SJed Brown {
33299f67acb7SJed Brown   PetscFunctionBegin;
33309f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33319f67acb7SJed Brown   PetscValidIntPointer(lits,2);
33325ef26d82SJed Brown   *lits = ts->ksp_its;
33339f67acb7SJed Brown   PetscFunctionReturn(0);
33349f67acb7SJed Brown }
33359f67acb7SJed Brown 
33369f67acb7SJed Brown #undef __FUNCT__
3337cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3338cef5090cSJed Brown /*@
3339cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3340cef5090cSJed Brown 
3341cef5090cSJed Brown    Not Collective
3342cef5090cSJed Brown 
3343cef5090cSJed Brown    Input Parameter:
3344cef5090cSJed Brown .  ts - TS context
3345cef5090cSJed Brown 
3346cef5090cSJed Brown    Output Parameter:
3347cef5090cSJed Brown .  rejects - number of steps rejected
3348cef5090cSJed Brown 
3349cef5090cSJed Brown    Notes:
3350cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3351cef5090cSJed Brown 
3352cef5090cSJed Brown    Level: intermediate
3353cef5090cSJed Brown 
3354cef5090cSJed Brown .keywords: TS, get, number
3355cef5090cSJed Brown 
33565ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3357cef5090cSJed Brown @*/
3358cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3359cef5090cSJed Brown {
3360cef5090cSJed Brown   PetscFunctionBegin;
3361cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3362cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3363cef5090cSJed Brown   *rejects = ts->reject;
3364cef5090cSJed Brown   PetscFunctionReturn(0);
3365cef5090cSJed Brown }
3366cef5090cSJed Brown 
3367cef5090cSJed Brown #undef __FUNCT__
3368cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3369cef5090cSJed Brown /*@
3370cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3371cef5090cSJed Brown 
3372cef5090cSJed Brown    Not Collective
3373cef5090cSJed Brown 
3374cef5090cSJed Brown    Input Parameter:
3375cef5090cSJed Brown .  ts - TS context
3376cef5090cSJed Brown 
3377cef5090cSJed Brown    Output Parameter:
3378cef5090cSJed Brown .  fails - number of failed nonlinear solves
3379cef5090cSJed Brown 
3380cef5090cSJed Brown    Notes:
3381cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3382cef5090cSJed Brown 
3383cef5090cSJed Brown    Level: intermediate
3384cef5090cSJed Brown 
3385cef5090cSJed Brown .keywords: TS, get, number
3386cef5090cSJed Brown 
33875ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3388cef5090cSJed Brown @*/
3389cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3390cef5090cSJed Brown {
3391cef5090cSJed Brown   PetscFunctionBegin;
3392cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3393cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3394cef5090cSJed Brown   *fails = ts->num_snes_failures;
3395cef5090cSJed Brown   PetscFunctionReturn(0);
3396cef5090cSJed Brown }
3397cef5090cSJed Brown 
3398cef5090cSJed Brown #undef __FUNCT__
3399cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3400cef5090cSJed Brown /*@
3401cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3402cef5090cSJed Brown 
3403cef5090cSJed Brown    Not Collective
3404cef5090cSJed Brown 
3405cef5090cSJed Brown    Input Parameter:
3406cef5090cSJed Brown +  ts - TS context
3407cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3408cef5090cSJed Brown 
3409cef5090cSJed Brown    Notes:
3410cef5090cSJed Brown    The counter is reset to zero for each step
3411cef5090cSJed Brown 
3412cef5090cSJed Brown    Options Database Key:
3413cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3414cef5090cSJed Brown 
3415cef5090cSJed Brown    Level: intermediate
3416cef5090cSJed Brown 
3417cef5090cSJed Brown .keywords: TS, set, maximum, number
3418cef5090cSJed Brown 
34195ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3420cef5090cSJed Brown @*/
3421cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3422cef5090cSJed Brown {
3423cef5090cSJed Brown   PetscFunctionBegin;
3424cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3425cef5090cSJed Brown   ts->max_reject = rejects;
3426cef5090cSJed Brown   PetscFunctionReturn(0);
3427cef5090cSJed Brown }
3428cef5090cSJed Brown 
3429cef5090cSJed Brown #undef __FUNCT__
3430cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3431cef5090cSJed Brown /*@
3432cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3433cef5090cSJed Brown 
3434cef5090cSJed Brown    Not Collective
3435cef5090cSJed Brown 
3436cef5090cSJed Brown    Input Parameter:
3437cef5090cSJed Brown +  ts - TS context
3438cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3439cef5090cSJed Brown 
3440cef5090cSJed Brown    Notes:
3441cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3442cef5090cSJed Brown 
3443cef5090cSJed Brown    Options Database Key:
3444cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3445cef5090cSJed Brown 
3446cef5090cSJed Brown    Level: intermediate
3447cef5090cSJed Brown 
3448cef5090cSJed Brown .keywords: TS, set, maximum, number
3449cef5090cSJed Brown 
34505ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3451cef5090cSJed Brown @*/
3452cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3453cef5090cSJed Brown {
3454cef5090cSJed Brown   PetscFunctionBegin;
3455cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3456cef5090cSJed Brown   ts->max_snes_failures = fails;
3457cef5090cSJed Brown   PetscFunctionReturn(0);
3458cef5090cSJed Brown }
3459cef5090cSJed Brown 
3460cef5090cSJed Brown #undef __FUNCT__
3461cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3462cef5090cSJed Brown /*@
3463cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3464cef5090cSJed Brown 
3465cef5090cSJed Brown    Not Collective
3466cef5090cSJed Brown 
3467cef5090cSJed Brown    Input Parameter:
3468cef5090cSJed Brown +  ts - TS context
3469cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3470cef5090cSJed Brown 
3471cef5090cSJed Brown    Options Database Key:
3472cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3473cef5090cSJed Brown 
3474cef5090cSJed Brown    Level: intermediate
3475cef5090cSJed Brown 
3476cef5090cSJed Brown .keywords: TS, set, error
3477cef5090cSJed Brown 
34785ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3479cef5090cSJed Brown @*/
3480cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3481cef5090cSJed Brown {
3482cef5090cSJed Brown   PetscFunctionBegin;
3483cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3484cef5090cSJed Brown   ts->errorifstepfailed = err;
3485cef5090cSJed Brown   PetscFunctionReturn(0);
3486cef5090cSJed Brown }
3487cef5090cSJed Brown 
3488cef5090cSJed Brown #undef __FUNCT__
3489fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3490fb1732b5SBarry Smith /*@C
3491fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3492fb1732b5SBarry Smith 
3493fb1732b5SBarry Smith    Collective on TS
3494fb1732b5SBarry Smith 
3495fb1732b5SBarry Smith    Input Parameters:
3496fb1732b5SBarry Smith +  ts - the TS context
3497fb1732b5SBarry Smith .  step - current time-step
3498fb1732b5SBarry Smith .  ptime - current time
34990910c330SBarry Smith .  u - current state
3500fb1732b5SBarry Smith -  viewer - binary viewer
3501fb1732b5SBarry Smith 
3502fb1732b5SBarry Smith    Level: intermediate
3503fb1732b5SBarry Smith 
3504fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3505fb1732b5SBarry Smith 
3506fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3507fb1732b5SBarry Smith @*/
35080910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3509fb1732b5SBarry Smith {
3510fb1732b5SBarry Smith   PetscErrorCode ierr;
3511ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3512fb1732b5SBarry Smith 
3513fb1732b5SBarry Smith   PetscFunctionBegin;
35140910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3515ed81e22dSJed Brown   PetscFunctionReturn(0);
3516ed81e22dSJed Brown }
3517ed81e22dSJed Brown 
3518ed81e22dSJed Brown #undef __FUNCT__
3519ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3520ed81e22dSJed Brown /*@C
3521ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3522ed81e22dSJed Brown 
3523ed81e22dSJed Brown    Collective on TS
3524ed81e22dSJed Brown 
3525ed81e22dSJed Brown    Input Parameters:
3526ed81e22dSJed Brown +  ts - the TS context
3527ed81e22dSJed Brown .  step - current time-step
3528ed81e22dSJed Brown .  ptime - current time
35290910c330SBarry Smith .  u - current state
3530ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3531ed81e22dSJed Brown 
3532ed81e22dSJed Brown    Level: intermediate
3533ed81e22dSJed Brown 
3534ed81e22dSJed Brown    Notes:
3535ed81e22dSJed 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.
3536ed81e22dSJed Brown    These are named according to the file name template.
3537ed81e22dSJed Brown 
3538ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3539ed81e22dSJed Brown 
3540ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3541ed81e22dSJed Brown 
3542ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3543ed81e22dSJed Brown @*/
35440910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3545ed81e22dSJed Brown {
3546ed81e22dSJed Brown   PetscErrorCode ierr;
3547ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3548ed81e22dSJed Brown   PetscViewer    viewer;
3549ed81e22dSJed Brown 
3550ed81e22dSJed Brown   PetscFunctionBegin;
35518caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3552ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
35530910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3554ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3555ed81e22dSJed Brown   PetscFunctionReturn(0);
3556ed81e22dSJed Brown }
3557ed81e22dSJed Brown 
3558ed81e22dSJed Brown #undef __FUNCT__
3559ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3560ed81e22dSJed Brown /*@C
3561ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3562ed81e22dSJed Brown 
3563ed81e22dSJed Brown    Collective on TS
3564ed81e22dSJed Brown 
3565ed81e22dSJed Brown    Input Parameters:
3566ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3567ed81e22dSJed Brown 
3568ed81e22dSJed Brown    Level: intermediate
3569ed81e22dSJed Brown 
3570ed81e22dSJed Brown    Note:
3571ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3572ed81e22dSJed Brown 
3573ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3574ed81e22dSJed Brown 
3575ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3576ed81e22dSJed Brown @*/
3577ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3578ed81e22dSJed Brown {
3579ed81e22dSJed Brown   PetscErrorCode ierr;
3580ed81e22dSJed Brown 
3581ed81e22dSJed Brown   PetscFunctionBegin;
3582ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3583fb1732b5SBarry Smith   PetscFunctionReturn(0);
3584fb1732b5SBarry Smith }
3585fb1732b5SBarry Smith 
358684df9cb4SJed Brown #undef __FUNCT__
358784df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt"
358884df9cb4SJed Brown /*@
358984df9cb4SJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
359084df9cb4SJed Brown 
3591ed81e22dSJed Brown    Collective on TS if controller has not been created yet
359284df9cb4SJed Brown 
359384df9cb4SJed Brown    Input Arguments:
3594ed81e22dSJed Brown .  ts - time stepping context
359584df9cb4SJed Brown 
359684df9cb4SJed Brown    Output Arguments:
3597ed81e22dSJed Brown .  adapt - adaptive controller
359884df9cb4SJed Brown 
359984df9cb4SJed Brown    Level: intermediate
360084df9cb4SJed Brown 
3601ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
360284df9cb4SJed Brown @*/
360384df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
360484df9cb4SJed Brown {
360584df9cb4SJed Brown   PetscErrorCode ierr;
360684df9cb4SJed Brown 
360784df9cb4SJed Brown   PetscFunctionBegin;
360884df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
360984df9cb4SJed Brown   PetscValidPointer(adapt,2);
361084df9cb4SJed Brown   if (!ts->adapt) {
361184df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
36121c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
36131c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
361484df9cb4SJed Brown   }
361584df9cb4SJed Brown   *adapt = ts->adapt;
361684df9cb4SJed Brown   PetscFunctionReturn(0);
361784df9cb4SJed Brown }
3618d6ebe24aSShri Abhyankar 
3619d6ebe24aSShri Abhyankar #undef __FUNCT__
36201c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
36211c3436cfSJed Brown /*@
36221c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
36231c3436cfSJed Brown 
36241c3436cfSJed Brown    Logically Collective
36251c3436cfSJed Brown 
36261c3436cfSJed Brown    Input Arguments:
36271c3436cfSJed Brown +  ts - time integration context
36281c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
36291c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
36301c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
36311c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
36321c3436cfSJed Brown 
36331c3436cfSJed Brown    Level: beginner
36341c3436cfSJed Brown 
3635c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
36361c3436cfSJed Brown @*/
36371c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
36381c3436cfSJed Brown {
36391c3436cfSJed Brown   PetscErrorCode ierr;
36401c3436cfSJed Brown 
36411c3436cfSJed Brown   PetscFunctionBegin;
3642c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
36431c3436cfSJed Brown   if (vatol) {
36441c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
36451c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
36461c3436cfSJed Brown     ts->vatol = vatol;
36471c3436cfSJed Brown   }
3648c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
36491c3436cfSJed Brown   if (vrtol) {
36501c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
36511c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
36521c3436cfSJed Brown     ts->vrtol = vrtol;
36531c3436cfSJed Brown   }
36541c3436cfSJed Brown   PetscFunctionReturn(0);
36551c3436cfSJed Brown }
36561c3436cfSJed Brown 
36571c3436cfSJed Brown #undef __FUNCT__
3658c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3659c5033834SJed Brown /*@
3660c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3661c5033834SJed Brown 
3662c5033834SJed Brown    Logically Collective
3663c5033834SJed Brown 
3664c5033834SJed Brown    Input Arguments:
3665c5033834SJed Brown .  ts - time integration context
3666c5033834SJed Brown 
3667c5033834SJed Brown    Output Arguments:
3668c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3669c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3670c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3671c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3672c5033834SJed Brown 
3673c5033834SJed Brown    Level: beginner
3674c5033834SJed Brown 
3675c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3676c5033834SJed Brown @*/
3677c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3678c5033834SJed Brown {
3679c5033834SJed Brown   PetscFunctionBegin;
3680c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3681c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3682c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3683c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3684c5033834SJed Brown   PetscFunctionReturn(0);
3685c5033834SJed Brown }
3686c5033834SJed Brown 
3687c5033834SJed Brown #undef __FUNCT__
36881c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
36891c3436cfSJed Brown /*@
36901c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
36911c3436cfSJed Brown 
36921c3436cfSJed Brown    Collective on TS
36931c3436cfSJed Brown 
36941c3436cfSJed Brown    Input Arguments:
36951c3436cfSJed Brown +  ts - time stepping context
36961c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
36971c3436cfSJed Brown 
36981c3436cfSJed Brown    Output Arguments:
36991c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
37001c3436cfSJed Brown 
37011c3436cfSJed Brown    Level: developer
37021c3436cfSJed Brown 
37031c3436cfSJed Brown .seealso: TSSetTolerances()
37041c3436cfSJed Brown @*/
37051c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
37061c3436cfSJed Brown {
37071c3436cfSJed Brown   PetscErrorCode    ierr;
37081c3436cfSJed Brown   PetscInt          i,n,N;
37090910c330SBarry Smith   const PetscScalar *u,*y;
37100910c330SBarry Smith   Vec               U;
37111c3436cfSJed Brown   PetscReal         sum,gsum;
37121c3436cfSJed Brown 
37131c3436cfSJed Brown   PetscFunctionBegin;
37141c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37151c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
37161c3436cfSJed Brown   PetscValidPointer(norm,3);
37170910c330SBarry Smith   U = ts->vec_sol;
37180910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
37190910c330SBarry Smith   if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
37201c3436cfSJed Brown 
37210910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
37220910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
37230910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
37241c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
37251c3436cfSJed Brown   sum = 0.;
3726ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3727ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3728ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3729ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3730ceefc113SJed Brown     for (i=0; i<n; i++) {
37310910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
37320910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3733ceefc113SJed Brown     }
3734ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3735ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3736ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3737ceefc113SJed Brown     const PetscScalar *atol;
3738ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3739ceefc113SJed Brown     for (i=0; i<n; i++) {
37400910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
37410910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3742ceefc113SJed Brown     }
3743ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3744ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3745ceefc113SJed Brown     const PetscScalar *rtol;
3746ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3747ceefc113SJed Brown     for (i=0; i<n; i++) {
37480910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
37490910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3750ceefc113SJed Brown     }
3751ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3752ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
37531c3436cfSJed Brown     for (i=0; i<n; i++) {
37540910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
37550910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
37561c3436cfSJed Brown     }
3757ceefc113SJed Brown   }
37580910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
37591c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
37601c3436cfSJed Brown 
37611c3436cfSJed Brown   ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
37621c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
37631c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
37641c3436cfSJed Brown   PetscFunctionReturn(0);
37651c3436cfSJed Brown }
37661c3436cfSJed Brown 
37671c3436cfSJed Brown #undef __FUNCT__
37688d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
37698d59e960SJed Brown /*@
37708d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
37718d59e960SJed Brown 
37728d59e960SJed Brown    Logically Collective on TS
37738d59e960SJed Brown 
37748d59e960SJed Brown    Input Arguments:
37758d59e960SJed Brown +  ts - time stepping context
37768d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
37778d59e960SJed Brown 
37788d59e960SJed Brown    Note:
37798d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
37808d59e960SJed Brown 
37818d59e960SJed Brown    Level: intermediate
37828d59e960SJed Brown 
37838d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
37848d59e960SJed Brown @*/
37858d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
37868d59e960SJed Brown {
37878d59e960SJed Brown   PetscFunctionBegin;
37888d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37898d59e960SJed Brown   ts->cfltime_local = cfltime;
37908d59e960SJed Brown   ts->cfltime = -1.;
37918d59e960SJed Brown   PetscFunctionReturn(0);
37928d59e960SJed Brown }
37938d59e960SJed Brown 
37948d59e960SJed Brown #undef __FUNCT__
37958d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
37968d59e960SJed Brown /*@
37978d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
37988d59e960SJed Brown 
37998d59e960SJed Brown    Collective on TS
38008d59e960SJed Brown 
38018d59e960SJed Brown    Input Arguments:
38028d59e960SJed Brown .  ts - time stepping context
38038d59e960SJed Brown 
38048d59e960SJed Brown    Output Arguments:
38058d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
38068d59e960SJed Brown 
38078d59e960SJed Brown    Level: advanced
38088d59e960SJed Brown 
38098d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
38108d59e960SJed Brown @*/
38118d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
38128d59e960SJed Brown {
38138d59e960SJed Brown   PetscErrorCode ierr;
38148d59e960SJed Brown 
38158d59e960SJed Brown   PetscFunctionBegin;
38168d59e960SJed Brown   if (ts->cfltime < 0) {
38178d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
38188d59e960SJed Brown   }
38198d59e960SJed Brown   *cfltime = ts->cfltime;
38208d59e960SJed Brown   PetscFunctionReturn(0);
38218d59e960SJed Brown }
38228d59e960SJed Brown 
38238d59e960SJed Brown #undef __FUNCT__
3824d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
3825d6ebe24aSShri Abhyankar /*@
3826d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
3827d6ebe24aSShri Abhyankar 
3828d6ebe24aSShri Abhyankar    Input Parameters:
3829d6ebe24aSShri Abhyankar .  ts   - the TS context.
3830d6ebe24aSShri Abhyankar .  xl   - lower bound.
3831d6ebe24aSShri Abhyankar .  xu   - upper bound.
3832d6ebe24aSShri Abhyankar 
3833d6ebe24aSShri Abhyankar    Notes:
3834d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
383533c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
3836d6ebe24aSShri Abhyankar 
38372bd2b0e6SSatish Balay    Level: advanced
38382bd2b0e6SSatish Balay 
3839d6ebe24aSShri Abhyankar @*/
3840d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
3841d6ebe24aSShri Abhyankar {
3842d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
3843d6ebe24aSShri Abhyankar   SNES           snes;
3844d6ebe24aSShri Abhyankar 
3845d6ebe24aSShri Abhyankar   PetscFunctionBegin;
3846d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3847d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
3848d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
3849d6ebe24aSShri Abhyankar }
3850d6ebe24aSShri Abhyankar 
3851325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3852c6db04a5SJed Brown #include <mex.h>
3853325fc9f4SBarry Smith 
3854325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
3855325fc9f4SBarry Smith 
3856325fc9f4SBarry Smith #undef __FUNCT__
3857325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
3858325fc9f4SBarry Smith /*
3859325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
3860325fc9f4SBarry Smith                          TSSetFunctionMatlab().
3861325fc9f4SBarry Smith 
3862325fc9f4SBarry Smith    Collective on TS
3863325fc9f4SBarry Smith 
3864325fc9f4SBarry Smith    Input Parameters:
3865325fc9f4SBarry Smith +  snes - the TS context
38660910c330SBarry Smith -  u - input vector
3867325fc9f4SBarry Smith 
3868325fc9f4SBarry Smith    Output Parameter:
3869325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
3870325fc9f4SBarry Smith 
3871325fc9f4SBarry Smith    Notes:
3872325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
3873325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
3874325fc9f4SBarry Smith    themselves.
3875325fc9f4SBarry Smith 
3876325fc9f4SBarry Smith    Level: developer
3877325fc9f4SBarry Smith 
3878325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3879325fc9f4SBarry Smith 
3880325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3881325fc9f4SBarry Smith */
38820910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
3883325fc9f4SBarry Smith {
3884325fc9f4SBarry Smith   PetscErrorCode   ierr;
3885325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3886325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
3887325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
3888325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
3889325fc9f4SBarry Smith 
3890325fc9f4SBarry Smith   PetscFunctionBegin;
3891325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
38920910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
38930910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
3894325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
38950910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
3896325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
3897325fc9f4SBarry Smith 
3898325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
38990910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
39000910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
39010910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
3902325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3903325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
3904325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3905325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3906325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
3907325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
3908325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
3909325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
3910325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3911325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3912325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3913325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3914325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3915325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3916325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3917325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3918325fc9f4SBarry Smith   PetscFunctionReturn(0);
3919325fc9f4SBarry Smith }
3920325fc9f4SBarry Smith 
3921325fc9f4SBarry Smith 
3922325fc9f4SBarry Smith #undef __FUNCT__
3923325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
3924325fc9f4SBarry Smith /*
3925325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
3926325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
3927e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
3928325fc9f4SBarry Smith 
3929325fc9f4SBarry Smith    Logically Collective on TS
3930325fc9f4SBarry Smith 
3931325fc9f4SBarry Smith    Input Parameters:
3932325fc9f4SBarry Smith +  ts - the TS context
3933325fc9f4SBarry Smith -  func - function evaluation routine
3934325fc9f4SBarry Smith 
3935325fc9f4SBarry Smith    Calling sequence of func:
39360910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
3937325fc9f4SBarry Smith 
3938325fc9f4SBarry Smith    Level: beginner
3939325fc9f4SBarry Smith 
3940325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3941325fc9f4SBarry Smith 
3942325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3943325fc9f4SBarry Smith */
3944cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
3945325fc9f4SBarry Smith {
3946325fc9f4SBarry Smith   PetscErrorCode  ierr;
3947325fc9f4SBarry Smith   TSMatlabContext *sctx;
3948325fc9f4SBarry Smith 
3949325fc9f4SBarry Smith   PetscFunctionBegin;
3950325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3951325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3952325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3953325fc9f4SBarry Smith   /*
3954325fc9f4SBarry Smith      This should work, but it doesn't
3955325fc9f4SBarry Smith   sctx->ctx = ctx;
3956325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3957325fc9f4SBarry Smith   */
3958325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3959cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
3960325fc9f4SBarry Smith   PetscFunctionReturn(0);
3961325fc9f4SBarry Smith }
3962325fc9f4SBarry Smith 
3963325fc9f4SBarry Smith #undef __FUNCT__
3964325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
3965325fc9f4SBarry Smith /*
3966325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
3967325fc9f4SBarry Smith                          TSSetJacobianMatlab().
3968325fc9f4SBarry Smith 
3969325fc9f4SBarry Smith    Collective on TS
3970325fc9f4SBarry Smith 
3971325fc9f4SBarry Smith    Input Parameters:
3972cdcf91faSSean Farley +  ts - the TS context
39730910c330SBarry Smith .  u - input vector
3974325fc9f4SBarry Smith .  A, B - the matrices
3975325fc9f4SBarry Smith -  ctx - user context
3976325fc9f4SBarry Smith 
3977325fc9f4SBarry Smith    Output Parameter:
3978325fc9f4SBarry Smith .  flag - structure of the matrix
3979325fc9f4SBarry Smith 
3980325fc9f4SBarry Smith    Level: developer
3981325fc9f4SBarry Smith 
3982325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3983325fc9f4SBarry Smith 
3984325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3985325fc9f4SBarry Smith @*/
39860910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
3987325fc9f4SBarry Smith {
3988325fc9f4SBarry Smith   PetscErrorCode  ierr;
3989325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3990325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
3991325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
3992325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
3993325fc9f4SBarry Smith 
3994325fc9f4SBarry Smith   PetscFunctionBegin;
3995cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39960910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
3997325fc9f4SBarry Smith 
39980910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
3999325fc9f4SBarry Smith 
4000cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
40010910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
40020910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
40030910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
40040910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4005325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4006325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4007325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4008325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4009325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4010325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4011325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4012325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4013325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4014325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4015325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4016325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4017325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4018325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4019325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4020325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4021325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4022325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4023325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4024325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4025325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4026325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4027325fc9f4SBarry Smith   PetscFunctionReturn(0);
4028325fc9f4SBarry Smith }
4029325fc9f4SBarry Smith 
4030325fc9f4SBarry Smith 
4031325fc9f4SBarry Smith #undef __FUNCT__
4032325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4033325fc9f4SBarry Smith /*
4034325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4035e3c5b3baSBarry 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
4036325fc9f4SBarry Smith 
4037325fc9f4SBarry Smith    Logically Collective on TS
4038325fc9f4SBarry Smith 
4039325fc9f4SBarry Smith    Input Parameters:
4040cdcf91faSSean Farley +  ts - the TS context
4041325fc9f4SBarry Smith .  A,B - Jacobian matrices
4042325fc9f4SBarry Smith .  func - function evaluation routine
4043325fc9f4SBarry Smith -  ctx - user context
4044325fc9f4SBarry Smith 
4045325fc9f4SBarry Smith    Calling sequence of func:
40460910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4047325fc9f4SBarry Smith 
4048325fc9f4SBarry Smith 
4049325fc9f4SBarry Smith    Level: developer
4050325fc9f4SBarry Smith 
4051325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4052325fc9f4SBarry Smith 
4053325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4054325fc9f4SBarry Smith */
4055cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4056325fc9f4SBarry Smith {
4057325fc9f4SBarry Smith   PetscErrorCode    ierr;
4058325fc9f4SBarry Smith   TSMatlabContext *sctx;
4059325fc9f4SBarry Smith 
4060325fc9f4SBarry Smith   PetscFunctionBegin;
4061325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4062325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4063325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4064325fc9f4SBarry Smith   /*
4065325fc9f4SBarry Smith      This should work, but it doesn't
4066325fc9f4SBarry Smith   sctx->ctx = ctx;
4067325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4068325fc9f4SBarry Smith   */
4069325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4070cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4071325fc9f4SBarry Smith   PetscFunctionReturn(0);
4072325fc9f4SBarry Smith }
4073325fc9f4SBarry Smith 
4074b5b1a830SBarry Smith #undef __FUNCT__
4075b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4076b5b1a830SBarry Smith /*
4077b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4078b5b1a830SBarry Smith 
4079b5b1a830SBarry Smith    Collective on TS
4080b5b1a830SBarry Smith 
4081b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4082b5b1a830SBarry Smith @*/
40830910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4084b5b1a830SBarry Smith {
4085b5b1a830SBarry Smith   PetscErrorCode  ierr;
4086b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
4087a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
4088b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4089b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4090b5b1a830SBarry Smith 
4091b5b1a830SBarry Smith   PetscFunctionBegin;
4092cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40930910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4094b5b1a830SBarry Smith 
4095cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
40960910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4097b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4098b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4099b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4100b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4101b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4102b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4103b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4104b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4105b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4106b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4107b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4108b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4109b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4110b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4111b5b1a830SBarry Smith   PetscFunctionReturn(0);
4112b5b1a830SBarry Smith }
4113b5b1a830SBarry Smith 
4114b5b1a830SBarry Smith 
4115b5b1a830SBarry Smith #undef __FUNCT__
4116b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4117b5b1a830SBarry Smith /*
4118b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4119b5b1a830SBarry Smith 
4120b5b1a830SBarry Smith    Level: developer
4121b5b1a830SBarry Smith 
4122b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4123b5b1a830SBarry Smith 
4124b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4125b5b1a830SBarry Smith */
4126cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4127b5b1a830SBarry Smith {
4128b5b1a830SBarry Smith   PetscErrorCode    ierr;
4129b5b1a830SBarry Smith   TSMatlabContext *sctx;
4130b5b1a830SBarry Smith 
4131b5b1a830SBarry Smith   PetscFunctionBegin;
4132b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4133b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4134b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4135b5b1a830SBarry Smith   /*
4136b5b1a830SBarry Smith      This should work, but it doesn't
4137b5b1a830SBarry Smith   sctx->ctx = ctx;
4138b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4139b5b1a830SBarry Smith   */
4140b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4141cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
4142b5b1a830SBarry Smith   PetscFunctionReturn(0);
4143b5b1a830SBarry Smith }
4144325fc9f4SBarry Smith #endif
4145b3603a34SBarry Smith 
41460b039ecaSBarry Smith 
41470b039ecaSBarry Smith 
4148b3603a34SBarry Smith #undef __FUNCT__
41494f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4150b3603a34SBarry Smith /*@C
41514f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4152b3603a34SBarry Smith        in a time based line graph
4153b3603a34SBarry Smith 
4154b3603a34SBarry Smith    Collective on TS
4155b3603a34SBarry Smith 
4156b3603a34SBarry Smith    Input Parameters:
4157b3603a34SBarry Smith +  ts - the TS context
4158b3603a34SBarry Smith .  step - current time-step
4159b3603a34SBarry Smith .  ptime - current time
4160b3603a34SBarry Smith -  lg - a line graph object
4161b3603a34SBarry Smith 
4162b3603a34SBarry Smith    Level: intermediate
4163b3603a34SBarry Smith 
41640b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4165b3603a34SBarry Smith 
4166b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4167b3603a34SBarry Smith 
4168b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4169b3603a34SBarry Smith @*/
41700910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4171b3603a34SBarry Smith {
4172b3603a34SBarry Smith   PetscErrorCode    ierr;
41730b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4174b3603a34SBarry Smith   const PetscScalar *yy;
417558ff32f7SBarry Smith   PetscInt          dim;
4176b3603a34SBarry Smith 
4177b3603a34SBarry Smith   PetscFunctionBegin;
417858ff32f7SBarry Smith   if (!step) {
4179a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4180a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4181a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
41820910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
41830b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
41840b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
418558ff32f7SBarry Smith   }
41860910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4187e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4188e3efe391SJed Brown   {
4189e3efe391SJed Brown     PetscReal *yreal;
4190e3efe391SJed Brown     PetscInt i,n;
41910910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4192e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4193e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
41940b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4195e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4196e3efe391SJed Brown   }
4197e3efe391SJed Brown #else
41980b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4199e3efe391SJed Brown #endif
42000910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
42013fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
42020b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
42033923b477SBarry Smith   }
4204b3603a34SBarry Smith   PetscFunctionReturn(0);
4205b3603a34SBarry Smith }
4206b3603a34SBarry Smith 
4207b3603a34SBarry Smith #undef __FUNCT__
42084f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4209ef20d060SBarry Smith /*@C
42104f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4211ef20d060SBarry Smith        in a time based line graph
4212ef20d060SBarry Smith 
4213ef20d060SBarry Smith    Collective on TS
4214ef20d060SBarry Smith 
4215ef20d060SBarry Smith    Input Parameters:
4216ef20d060SBarry Smith +  ts - the TS context
4217ef20d060SBarry Smith .  step - current time-step
4218ef20d060SBarry Smith .  ptime - current time
4219ef20d060SBarry Smith -  lg - a line graph object
4220ef20d060SBarry Smith 
4221ef20d060SBarry Smith    Level: intermediate
4222ef20d060SBarry Smith 
4223abd5a294SJed Brown    Notes:
4224abd5a294SJed Brown    Only for sequential solves.
4225abd5a294SJed Brown 
4226abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4227abd5a294SJed Brown 
4228abd5a294SJed Brown    Options Database Keys:
42294f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4230ef20d060SBarry Smith 
4231ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4232ef20d060SBarry Smith 
4233abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4234ef20d060SBarry Smith @*/
42350910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4236ef20d060SBarry Smith {
4237ef20d060SBarry Smith   PetscErrorCode    ierr;
42380b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4239ef20d060SBarry Smith   const PetscScalar *yy;
4240ef20d060SBarry Smith   Vec               y;
4241a9f9c1f6SBarry Smith   PetscInt          dim;
4242ef20d060SBarry Smith 
4243ef20d060SBarry Smith   PetscFunctionBegin;
4244a9f9c1f6SBarry Smith   if (!step) {
4245a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4246a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
42471ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
42480910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4249a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4250a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4251a9f9c1f6SBarry Smith   }
42520910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4253ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
42540910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4255ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4256e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4257e3efe391SJed Brown   {
4258e3efe391SJed Brown     PetscReal *yreal;
4259e3efe391SJed Brown     PetscInt  i,n;
4260e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4261e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4262e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
42630b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4264e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4265e3efe391SJed Brown   }
4266e3efe391SJed Brown #else
42670b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4268e3efe391SJed Brown #endif
4269ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4270ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
42713fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
42720b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
42733923b477SBarry Smith   }
4274ef20d060SBarry Smith   PetscFunctionReturn(0);
4275ef20d060SBarry Smith }
4276ef20d060SBarry Smith 
4277201da799SBarry Smith #undef __FUNCT__
4278201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4279201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4280201da799SBarry Smith {
4281201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4282201da799SBarry Smith   PetscReal      x = ptime,y;
4283201da799SBarry Smith   PetscErrorCode ierr;
4284201da799SBarry Smith   PetscInt       its;
4285201da799SBarry Smith 
4286201da799SBarry Smith   PetscFunctionBegin;
4287201da799SBarry Smith   if (!n) {
4288201da799SBarry Smith     PetscDrawAxis  axis;
4289201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4290201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4291201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4292201da799SBarry Smith     ctx->snes_its  = 0;
4293201da799SBarry Smith   }
4294201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4295201da799SBarry Smith   y    = its - ctx->snes_its;
4296201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
42973fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){
4298201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4299201da799SBarry Smith   }
4300201da799SBarry Smith   ctx->snes_its = its;
4301201da799SBarry Smith   PetscFunctionReturn(0);
4302201da799SBarry Smith }
4303201da799SBarry Smith 
4304201da799SBarry Smith #undef __FUNCT__
4305201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4306201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4307201da799SBarry Smith {
4308201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4309201da799SBarry Smith   PetscReal      x = ptime,y;
4310201da799SBarry Smith   PetscErrorCode ierr;
4311201da799SBarry Smith   PetscInt       its;
4312201da799SBarry Smith 
4313201da799SBarry Smith   PetscFunctionBegin;
4314201da799SBarry Smith   if (!n) {
4315201da799SBarry Smith     PetscDrawAxis  axis;
4316201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4317201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4318201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4319201da799SBarry Smith     ctx->ksp_its = 0;
4320201da799SBarry Smith   }
4321201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4322201da799SBarry Smith   y    = its - ctx->ksp_its;
4323201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
432499fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){
4325201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4326201da799SBarry Smith   }
4327201da799SBarry Smith   ctx->ksp_its = its;
4328201da799SBarry Smith   PetscFunctionReturn(0);
4329201da799SBarry Smith }
4330f9c1d6abSBarry Smith 
4331f9c1d6abSBarry Smith #undef __FUNCT__
4332f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4333f9c1d6abSBarry Smith /*@
4334f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4335f9c1d6abSBarry Smith 
4336f9c1d6abSBarry Smith    Collective on TS and Vec
4337f9c1d6abSBarry Smith 
4338f9c1d6abSBarry Smith    Input Parameters:
4339f9c1d6abSBarry Smith +  ts - the TS context
4340f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4341f9c1d6abSBarry Smith 
4342f9c1d6abSBarry Smith    Output Parameters:
4343f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4344f9c1d6abSBarry Smith 
4345f9c1d6abSBarry Smith    Level: developer
4346f9c1d6abSBarry Smith 
4347f9c1d6abSBarry Smith .keywords: TS, compute
4348f9c1d6abSBarry Smith 
4349f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4350f9c1d6abSBarry Smith @*/
4351f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4352f9c1d6abSBarry Smith {
4353f9c1d6abSBarry Smith   PetscErrorCode ierr;
4354f9c1d6abSBarry Smith 
4355f9c1d6abSBarry Smith   PetscFunctionBegin;
4356f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4357f9c1d6abSBarry Smith   if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4358f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4359f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4360f9c1d6abSBarry Smith }
4361