xref: /petsc/src/ts/interface/ts.c (revision aa39b21e6cd01458f69edf88db3746c321abb490)
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
64a6570f20SBarry Smith -  -ts_monitor_draw - plot information at each timestep
65bdad233fSMatthew Knepley 
66bdad233fSMatthew Knepley    Level: beginner
67bdad233fSMatthew Knepley 
68bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
69bdad233fSMatthew Knepley 
70a313700dSBarry Smith .seealso: TSGetType()
71bdad233fSMatthew Knepley @*/
727087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
73bdad233fSMatthew Knepley {
74ace3abfcSBarry Smith   PetscBool      opt,flg;
75dfbe8321SBarry Smith   PetscErrorCode ierr;
76649052a6SBarry Smith   PetscViewer    monviewer;
77eabae89aSBarry Smith   char           monfilename[PETSC_MAX_PATH_LEN];
78089b2837SJed Brown   SNES           snes;
791c3436cfSJed Brown   TSAdapt        adapt;
8031748224SBarry Smith   PetscReal      time_step;
81bdad233fSMatthew Knepley 
82bdad233fSMatthew Knepley   PetscFunctionBegin;
830700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
843194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
85a43b19c4SJed Brown     /* Handle TS type options */
86a43b19c4SJed Brown     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
87bdad233fSMatthew Knepley 
88bdad233fSMatthew Knepley     /* Handle generic TS options */
89bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
903bca7d26SBarry Smith     ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
91d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr);
9231748224SBarry Smith     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
9331748224SBarry Smith     if (flg) {
9431748224SBarry Smith       ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
9531748224SBarry Smith     }
96a43b19c4SJed Brown     opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time;
97a43b19c4SJed Brown     ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr);
98461e00b3SSean Farley     if (flg) {ierr = TSSetExactFinalTime(ts,opt);CHKERRQ(ierr);}
99cef5090cSJed 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);
100cef5090cSJed 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);
101cef5090cSJed Brown     ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
1021c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr);
1031c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr);
104bdad233fSMatthew Knepley 
105bdad233fSMatthew Knepley     /* Monitor options */
106a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
107eabae89aSBarry Smith     if (flg) {
108649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
109649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
110bdad233fSMatthew Knepley     }
1115180491cSLisandro Dalcin     ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1125180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1135180491cSLisandro Dalcin 
11490d69ab7SBarry Smith     opt  = PETSC_FALSE;
115acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
116a7cc72afSBarry Smith     if (opt) {
117a80ad3e0SBarry Smith       PetscDrawLG lg;
118a80ad3e0SBarry Smith 
119a80ad3e0SBarry Smith       ierr = TSMonitorLGCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,&lg);
120b3603a34SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLG,lg,(PetscErrorCode (*)(void**))TSMonitorLGDestroy);CHKERRQ(ierr);
121b3603a34SBarry Smith     }
122b3603a34SBarry Smith     opt  = PETSC_FALSE;
123b3603a34SBarry Smith     ierr = PetscOptionsBool("-ts_monitor_solutionode","Monitor solution graphically","TSMonitorSolutionODE",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
124b3603a34SBarry Smith     if (opt) {
125b3603a34SBarry Smith       PetscDrawLG lg;
126b3603a34SBarry Smith 
127ab352700SBarry Smith       ierr = TSMonitorSolutionODECreate(((PetscObject)ts)->comm,3,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,&lg);
128b3603a34SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionODE,lg,(PetscErrorCode (*)(void**))TSMonitorSolutionODEDestroy);CHKERRQ(ierr);
129bdad233fSMatthew Knepley     }
13090d69ab7SBarry Smith     opt  = PETSC_FALSE;
131acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
132a7cc72afSBarry Smith     if (opt) {
1336083293cSBarry Smith       void        *ctx;
134a80ad3e0SBarry Smith       PetscViewer viewer;
135a80ad3e0SBarry Smith 
136ab352700SBarry Smith       ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,&viewer);
137a80ad3e0SBarry Smith       ierr = TSMonitorSolutionCreate(ts,viewer,&ctx);CHKERRQ(ierr);
138a80ad3e0SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1396083293cSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,ctx,TSMonitorSolutionDestroy);CHKERRQ(ierr);
140bdad233fSMatthew Knepley     }
141fb1732b5SBarry Smith     opt  = PETSC_FALSE;
142fb1732b5SBarry Smith     ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
143fb1732b5SBarry Smith     if (flg) {
144fb1732b5SBarry Smith       PetscViewer ctx;
145fb1732b5SBarry Smith       if (monfilename[0]) {
146fb1732b5SBarry Smith         ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
147fb1732b5SBarry Smith       } else {
148fb1732b5SBarry Smith         ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
149fb1732b5SBarry Smith       }
150fb1732b5SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
151fb1732b5SBarry Smith     }
152ed81e22dSJed Brown     opt  = PETSC_FALSE;
153ed81e22dSJed Brown     ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
154ed81e22dSJed Brown     if (flg) {
155ed81e22dSJed Brown       const char *ptr,*ptr2;
156ed81e22dSJed Brown       char *filetemplate;
157ed81e22dSJed Brown       if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
158ed81e22dSJed Brown       /* Do some cursory validation of the input. */
159ed81e22dSJed Brown       ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
160ed81e22dSJed Brown       if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
161ed81e22dSJed Brown       for (ptr++ ; ptr && *ptr; ptr++) {
162ed81e22dSJed Brown         ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
163ed81e22dSJed Brown         if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
164ed81e22dSJed Brown         if (ptr2) break;
165ed81e22dSJed Brown       }
166ed81e22dSJed Brown       ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
167ed81e22dSJed Brown       ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
168ed81e22dSJed Brown     }
169bdad233fSMatthew Knepley 
1701c3436cfSJed Brown     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
1711c3436cfSJed Brown     ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
1721c3436cfSJed Brown 
173d52bd9f3SBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
174d52bd9f3SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
175d52bd9f3SBarry Smith 
176bdad233fSMatthew Knepley     /* Handle specific TS options */
177abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
178bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
179bdad233fSMatthew Knepley     }
1805d973c19SBarry Smith 
1815d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
1825d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
183bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
184bdad233fSMatthew Knepley   PetscFunctionReturn(0);
185bdad233fSMatthew Knepley }
186bdad233fSMatthew Knepley 
187bdad233fSMatthew Knepley #undef __FUNCT__
188cdcf91faSSean Farley #undef __FUNCT__
1894a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
190a7a1495cSBarry Smith /*@
1918c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
192a7a1495cSBarry Smith       set with TSSetRHSJacobian().
193a7a1495cSBarry Smith 
194a7a1495cSBarry Smith    Collective on TS and Vec
195a7a1495cSBarry Smith 
196a7a1495cSBarry Smith    Input Parameters:
197316643e7SJed Brown +  ts - the TS context
198a7a1495cSBarry Smith .  t - current timestep
199a7a1495cSBarry Smith -  x - input vector
200a7a1495cSBarry Smith 
201a7a1495cSBarry Smith    Output Parameters:
202a7a1495cSBarry Smith +  A - Jacobian matrix
203a7a1495cSBarry Smith .  B - optional preconditioning matrix
204a7a1495cSBarry Smith -  flag - flag indicating matrix structure
205a7a1495cSBarry Smith 
206a7a1495cSBarry Smith    Notes:
207a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
208a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
209a7a1495cSBarry Smith 
21094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
211a7a1495cSBarry Smith    flag parameter.
212a7a1495cSBarry Smith 
213a7a1495cSBarry Smith    Level: developer
214a7a1495cSBarry Smith 
215a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
216a7a1495cSBarry Smith 
21794b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
218a7a1495cSBarry Smith @*/
2197087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
220a7a1495cSBarry Smith {
221dfbe8321SBarry Smith   PetscErrorCode ierr;
2220e4ef248SJed Brown   PetscInt       Xstate;
22324989b8cSPeter Brune   DM             dm;
22424989b8cSPeter Brune   TSDM           tsdm;
22524989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
22624989b8cSPeter Brune   void           *ctx;
22724989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
228a7a1495cSBarry Smith 
229a7a1495cSBarry Smith   PetscFunctionBegin;
2300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2310700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
232c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
23324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
23424989b8cSPeter Brune   ierr = DMTSGetContext(dm,&tsdm);CHKERRQ(ierr);
23524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
23624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
2370e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
2380e4ef248SJed Brown   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) {
2390e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
2400e4ef248SJed Brown     PetscFunctionReturn(0);
241f8ede8e7SJed Brown   }
242d90be118SSean Farley 
24324989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
244d90be118SSean Farley 
24524989b8cSPeter Brune   if (rhsjacobianfunc) {
246d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
247a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
248a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
24924989b8cSPeter Brune     ierr = (*rhsjacobianfunc)(ts,t,X,A,B,flg,ctx);CHKERRQ(ierr);
250a7a1495cSBarry Smith     PetscStackPop;
251d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
252a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
2530700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
2540700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
255ef66eb69SBarry Smith   } else {
256214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
257214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
258214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
259ef66eb69SBarry Smith   }
2600e4ef248SJed Brown   ts->rhsjacobian.time = t;
2610e4ef248SJed Brown   ts->rhsjacobian.X = X;
2620e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
2630e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
264a7a1495cSBarry Smith   PetscFunctionReturn(0);
265a7a1495cSBarry Smith }
266a7a1495cSBarry Smith 
2674a2ae208SSatish Balay #undef __FUNCT__
2684a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
269316643e7SJed Brown /*@
270d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
271d763cef2SBarry Smith 
272316643e7SJed Brown    Collective on TS and Vec
273316643e7SJed Brown 
274316643e7SJed Brown    Input Parameters:
275316643e7SJed Brown +  ts - the TS context
276316643e7SJed Brown .  t - current time
277316643e7SJed Brown -  x - state vector
278316643e7SJed Brown 
279316643e7SJed Brown    Output Parameter:
280316643e7SJed Brown .  y - right hand side
281316643e7SJed Brown 
282316643e7SJed Brown    Note:
283316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
284316643e7SJed Brown    is used internally within the nonlinear solvers.
285316643e7SJed Brown 
286316643e7SJed Brown    Level: developer
287316643e7SJed Brown 
288316643e7SJed Brown .keywords: TS, compute
289316643e7SJed Brown 
290316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
291316643e7SJed Brown @*/
292dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
293d763cef2SBarry Smith {
294dfbe8321SBarry Smith   PetscErrorCode ierr;
295d763cef2SBarry Smith 
29624989b8cSPeter Brune   TSRHSFunction rhsfunction;
29724989b8cSPeter Brune   TSIFunction   ifunction;
29824989b8cSPeter Brune   void          *ctx;
29924989b8cSPeter Brune   DM            dm;
30024989b8cSPeter Brune 
301d763cef2SBarry Smith   PetscFunctionBegin;
3020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3030700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3040700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
30524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
30624989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
30724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
308d763cef2SBarry Smith 
30924989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
310d763cef2SBarry Smith 
311d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
31224989b8cSPeter Brune   if (rhsfunction) {
313d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
31424989b8cSPeter Brune     ierr = (*rhsfunction)(ts,t,x,y,ctx);CHKERRQ(ierr);
315d763cef2SBarry Smith     PetscStackPop;
316214bc6a2SJed Brown   } else {
317214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
318b2cd27e8SJed Brown   }
31944a41b28SSean Farley 
320d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
321d763cef2SBarry Smith   PetscFunctionReturn(0);
322d763cef2SBarry Smith }
323d763cef2SBarry Smith 
3244a2ae208SSatish Balay #undef __FUNCT__
325214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
326214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
327214bc6a2SJed Brown {
3282dd45cf8SJed Brown   Vec            F;
329214bc6a2SJed Brown   PetscErrorCode ierr;
330214bc6a2SJed Brown 
331214bc6a2SJed Brown   PetscFunctionBegin;
3320da9a4f0SJed Brown   *Frhs = PETSC_NULL;
3332dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
334214bc6a2SJed Brown   if (!ts->Frhs) {
3352dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
336214bc6a2SJed Brown   }
337214bc6a2SJed Brown   *Frhs = ts->Frhs;
338214bc6a2SJed Brown   PetscFunctionReturn(0);
339214bc6a2SJed Brown }
340214bc6a2SJed Brown 
341214bc6a2SJed Brown #undef __FUNCT__
342214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
343214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
344214bc6a2SJed Brown {
345214bc6a2SJed Brown   Mat            A,B;
3462dd45cf8SJed Brown   PetscErrorCode ierr;
347214bc6a2SJed Brown 
348214bc6a2SJed Brown   PetscFunctionBegin;
349214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
350214bc6a2SJed Brown   if (Arhs) {
351214bc6a2SJed Brown     if (!ts->Arhs) {
352214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
353214bc6a2SJed Brown     }
354214bc6a2SJed Brown     *Arhs = ts->Arhs;
355214bc6a2SJed Brown   }
356214bc6a2SJed Brown   if (Brhs) {
357214bc6a2SJed Brown     if (!ts->Brhs) {
358214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
359214bc6a2SJed Brown     }
360214bc6a2SJed Brown     *Brhs = ts->Brhs;
361214bc6a2SJed Brown   }
362214bc6a2SJed Brown   PetscFunctionReturn(0);
363214bc6a2SJed Brown }
364214bc6a2SJed Brown 
365214bc6a2SJed Brown #undef __FUNCT__
366316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
367316643e7SJed Brown /*@
368316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
369316643e7SJed Brown 
370316643e7SJed Brown    Collective on TS and Vec
371316643e7SJed Brown 
372316643e7SJed Brown    Input Parameters:
373316643e7SJed Brown +  ts - the TS context
374316643e7SJed Brown .  t - current time
375316643e7SJed Brown .  X - state vector
376214bc6a2SJed Brown .  Xdot - time derivative of state vector
377214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
378316643e7SJed Brown 
379316643e7SJed Brown    Output Parameter:
380316643e7SJed Brown .  Y - right hand side
381316643e7SJed Brown 
382316643e7SJed Brown    Note:
383316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
384316643e7SJed Brown    is used internally within the nonlinear solvers.
385316643e7SJed Brown 
386316643e7SJed Brown    If the user did did not write their equations in implicit form, this
387316643e7SJed Brown    function recasts them in implicit form.
388316643e7SJed Brown 
389316643e7SJed Brown    Level: developer
390316643e7SJed Brown 
391316643e7SJed Brown .keywords: TS, compute
392316643e7SJed Brown 
393316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
394316643e7SJed Brown @*/
395214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex)
396316643e7SJed Brown {
397316643e7SJed Brown   PetscErrorCode ierr;
39824989b8cSPeter Brune   TSIFunction    ifunction;
39924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
40024989b8cSPeter Brune   void           *ctx;
40124989b8cSPeter Brune   DM             dm;
402316643e7SJed Brown 
403316643e7SJed Brown   PetscFunctionBegin;
4040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4050700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4060700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
4070700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
408316643e7SJed Brown 
40924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
41024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
41124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
41224989b8cSPeter Brune 
41324989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
414d90be118SSean Farley 
415316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
41624989b8cSPeter Brune   if (ifunction) {
417316643e7SJed Brown     PetscStackPush("TS user implicit function");
41824989b8cSPeter Brune     ierr = (*ifunction)(ts,t,X,Xdot,Y,ctx);CHKERRQ(ierr);
419316643e7SJed Brown     PetscStackPop;
420214bc6a2SJed Brown   }
421214bc6a2SJed Brown   if (imex) {
42224989b8cSPeter Brune     if (!ifunction) {
4232dd45cf8SJed Brown       ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);
4242dd45cf8SJed Brown     }
42524989b8cSPeter Brune   } else if (rhsfunction) {
42624989b8cSPeter Brune     if (ifunction) {
427214bc6a2SJed Brown       Vec Frhs;
428214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
429214bc6a2SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr);
430214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
4312dd45cf8SJed Brown     } else {
4322dd45cf8SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
4332dd45cf8SJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
434316643e7SJed Brown     }
4354a6899ffSJed Brown   }
436316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
437316643e7SJed Brown   PetscFunctionReturn(0);
438316643e7SJed Brown }
439316643e7SJed Brown 
440316643e7SJed Brown #undef __FUNCT__
441316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
442316643e7SJed Brown /*@
443316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
444316643e7SJed Brown 
445316643e7SJed Brown    Collective on TS and Vec
446316643e7SJed Brown 
447316643e7SJed Brown    Input
448316643e7SJed Brown       Input Parameters:
449316643e7SJed Brown +  ts - the TS context
450316643e7SJed Brown .  t - current timestep
451316643e7SJed Brown .  X - state vector
452316643e7SJed Brown .  Xdot - time derivative of state vector
453214bc6a2SJed Brown .  shift - shift to apply, see note below
454214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
455316643e7SJed Brown 
456316643e7SJed Brown    Output Parameters:
457316643e7SJed Brown +  A - Jacobian matrix
458316643e7SJed Brown .  B - optional preconditioning matrix
459316643e7SJed Brown -  flag - flag indicating matrix structure
460316643e7SJed Brown 
461316643e7SJed Brown    Notes:
462316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
463316643e7SJed Brown 
464316643e7SJed Brown    dF/dX + shift*dF/dXdot
465316643e7SJed Brown 
466316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
467316643e7SJed Brown    is used internally within the nonlinear solvers.
468316643e7SJed Brown 
469316643e7SJed Brown    Level: developer
470316643e7SJed Brown 
471316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
472316643e7SJed Brown 
473316643e7SJed Brown .seealso:  TSSetIJacobian()
47463495f91SJed Brown @*/
475214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
476316643e7SJed Brown {
4770026cea9SSean Farley   PetscInt Xstate, Xdotstate;
478316643e7SJed Brown   PetscErrorCode ierr;
47924989b8cSPeter Brune   TSIJacobian    ijacobian;
48024989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
48124989b8cSPeter Brune   DM             dm;
48224989b8cSPeter Brune   void           *ctx;
483316643e7SJed Brown 
484316643e7SJed Brown   PetscFunctionBegin;
48524989b8cSPeter Brune 
4860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4870700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4880700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
489316643e7SJed Brown   PetscValidPointer(A,6);
4900700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
491316643e7SJed Brown   PetscValidPointer(B,7);
4920700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
493316643e7SJed Brown   PetscValidPointer(flg,8);
49424989b8cSPeter Brune 
49524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
49624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
49724989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
49824989b8cSPeter Brune 
4990026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
5000026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr);
5010026cea9SSean Farley   if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == X && ts->ijacobian.Xstate == Xstate && ts->ijacobian.Xdot == Xdot && ts->ijacobian.Xdotstate == Xdotstate && ts->ijacobian.imex == imex))) {
5020026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
5030026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5040026cea9SSean Farley     PetscFunctionReturn(0);
5050026cea9SSean Farley   }
506316643e7SJed Brown 
50724989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
508316643e7SJed Brown 
5094e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
510316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
51124989b8cSPeter Brune   if (ijacobian) {
5122dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
513316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
51424989b8cSPeter Brune     ierr = (*ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ctx);CHKERRQ(ierr);
515316643e7SJed Brown     PetscStackPop;
516214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
517214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
518214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
5194a6899ffSJed Brown   }
520214bc6a2SJed Brown   if (imex) {
52124989b8cSPeter Brune     if (!ijacobian) {  /* system was written as Xdot = F(t,X) */
522214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
5232dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
524214bc6a2SJed Brown       if (*A != *B) {
525214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
526214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
527214bc6a2SJed Brown       }
528214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
529214bc6a2SJed Brown     }
530214bc6a2SJed Brown   } else {
53124989b8cSPeter Brune     if (!ijacobian) {
532214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
533214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
534214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
535316643e7SJed Brown       if (*A != *B) {
536316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
537316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
538316643e7SJed Brown       }
53924989b8cSPeter Brune     } else if (rhsjacobian) {
540214bc6a2SJed Brown       Mat Arhs,Brhs;
541214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
542214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
543214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
544214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
545214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
546214bc6a2SJed Brown       if (*A != *B) {
547214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
548214bc6a2SJed Brown       }
549214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
550214bc6a2SJed Brown     }
551316643e7SJed Brown   }
5520026cea9SSean Farley 
5530026cea9SSean Farley   ts->ijacobian.time = t;
5540026cea9SSean Farley   ts->ijacobian.X = X;
5550026cea9SSean Farley   ts->ijacobian.Xdot = Xdot;
5560026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr);
5570026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
5580026cea9SSean Farley   ts->ijacobian.shift = shift;
5590026cea9SSean Farley   ts->ijacobian.imex = imex;
5600026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
561316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
562316643e7SJed Brown   PetscFunctionReturn(0);
563316643e7SJed Brown }
564316643e7SJed Brown 
565316643e7SJed Brown #undef __FUNCT__
5664a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
567d763cef2SBarry Smith /*@C
568d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
569d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
570d763cef2SBarry Smith 
5713f9fe445SBarry Smith     Logically Collective on TS
572d763cef2SBarry Smith 
573d763cef2SBarry Smith     Input Parameters:
574d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
575ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
576d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
577d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
578d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
579d763cef2SBarry Smith 
580d763cef2SBarry Smith     Calling sequence of func:
58187828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
582d763cef2SBarry Smith 
583d763cef2SBarry Smith +   t - current timestep
584d763cef2SBarry Smith .   u - input vector
585d763cef2SBarry Smith .   F - function vector
586d763cef2SBarry Smith -   ctx - [optional] user-defined function context
587d763cef2SBarry Smith 
588d763cef2SBarry Smith     Level: beginner
589d763cef2SBarry Smith 
590d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
591d763cef2SBarry Smith 
592d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
593d763cef2SBarry Smith @*/
594089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
595d763cef2SBarry Smith {
596089b2837SJed Brown   PetscErrorCode ierr;
597089b2837SJed Brown   SNES           snes;
598e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
59924989b8cSPeter Brune   DM             dm;
600d763cef2SBarry Smith 
601089b2837SJed Brown   PetscFunctionBegin;
6020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
603ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
60424989b8cSPeter Brune 
60524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
60624989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
607089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
608e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
609e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
610e856ceecSJed Brown     r = ralloc;
611e856ceecSJed Brown   }
612089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
613e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
614d763cef2SBarry Smith   PetscFunctionReturn(0);
615d763cef2SBarry Smith }
616d763cef2SBarry Smith 
6174a2ae208SSatish Balay #undef __FUNCT__
6184a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
619d763cef2SBarry Smith /*@C
620d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
621d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
622d763cef2SBarry Smith 
6233f9fe445SBarry Smith    Logically Collective on TS
624d763cef2SBarry Smith 
625d763cef2SBarry Smith    Input Parameters:
626d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
627d763cef2SBarry Smith .  A   - Jacobian matrix
628d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
629d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
630d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
631d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
632d763cef2SBarry Smith 
633d763cef2SBarry Smith    Calling sequence of func:
63487828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
635d763cef2SBarry Smith 
636d763cef2SBarry Smith +  t - current timestep
637d763cef2SBarry Smith .  u - input vector
638d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
639d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
640d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
64194b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
642d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
643d763cef2SBarry Smith 
644d763cef2SBarry Smith    Notes:
64594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
646d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
647d763cef2SBarry Smith 
648d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
649d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
65056335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
651d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
652d763cef2SBarry Smith    throughout the global iterations.
653d763cef2SBarry Smith 
654d763cef2SBarry Smith    Level: beginner
655d763cef2SBarry Smith 
656d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
657d763cef2SBarry Smith 
658ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
659d763cef2SBarry Smith 
660d763cef2SBarry Smith @*/
661089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
662d763cef2SBarry Smith {
663277b19d0SLisandro Dalcin   PetscErrorCode ierr;
664089b2837SJed Brown   SNES           snes;
66524989b8cSPeter Brune   DM             dm;
66624989b8cSPeter Brune   TSIJacobian    ijacobian;
667277b19d0SLisandro Dalcin 
668d763cef2SBarry Smith   PetscFunctionBegin;
6690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
670277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
671277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
672277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
673277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
674d763cef2SBarry Smith 
67524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
67624989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
67724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
67824989b8cSPeter Brune 
679089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6805f659677SPeter Brune   if (!ijacobian) {
681089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
6820e4ef248SJed Brown   }
6830e4ef248SJed Brown   if (A) {
6840e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
6850e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
6860e4ef248SJed Brown     ts->Arhs = A;
6870e4ef248SJed Brown   }
6880e4ef248SJed Brown   if (B) {
6890e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
6900e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
6910e4ef248SJed Brown     ts->Brhs = B;
6920e4ef248SJed Brown   }
693d763cef2SBarry Smith   PetscFunctionReturn(0);
694d763cef2SBarry Smith }
695d763cef2SBarry Smith 
696316643e7SJed Brown 
697316643e7SJed Brown #undef __FUNCT__
698316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
699316643e7SJed Brown /*@C
700316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
701316643e7SJed Brown 
7023f9fe445SBarry Smith    Logically Collective on TS
703316643e7SJed Brown 
704316643e7SJed Brown    Input Parameters:
705316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
706ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
707316643e7SJed Brown .  f   - the function evaluation routine
708316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
709316643e7SJed Brown 
710316643e7SJed Brown    Calling sequence of f:
711316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
712316643e7SJed Brown 
713316643e7SJed Brown +  t   - time at step/stage being solved
714316643e7SJed Brown .  u   - state vector
715316643e7SJed Brown .  u_t - time derivative of state vector
716316643e7SJed Brown .  F   - function vector
717316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
718316643e7SJed Brown 
719316643e7SJed Brown    Important:
720d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
721316643e7SJed Brown 
722316643e7SJed Brown    Level: beginner
723316643e7SJed Brown 
724316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
725316643e7SJed Brown 
726d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
727316643e7SJed Brown @*/
728089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
729316643e7SJed Brown {
730089b2837SJed Brown   PetscErrorCode ierr;
731089b2837SJed Brown   SNES           snes;
732e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
73324989b8cSPeter Brune   DM             dm;
734316643e7SJed Brown 
735316643e7SJed Brown   PetscFunctionBegin;
7360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
737ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
73824989b8cSPeter Brune 
73924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
74024989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
74124989b8cSPeter Brune 
742089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
743e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
744e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
745e856ceecSJed Brown     res = resalloc;
746e856ceecSJed Brown   }
747089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
748e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
74924989b8cSPeter Brune 
750089b2837SJed Brown   PetscFunctionReturn(0);
751089b2837SJed Brown }
752089b2837SJed Brown 
753089b2837SJed Brown #undef __FUNCT__
754089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
755089b2837SJed Brown /*@C
756089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
757089b2837SJed Brown 
758089b2837SJed Brown    Not Collective
759089b2837SJed Brown 
760089b2837SJed Brown    Input Parameter:
761089b2837SJed Brown .  ts - the TS context
762089b2837SJed Brown 
763089b2837SJed Brown    Output Parameter:
764089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
765089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
766089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
767089b2837SJed Brown 
768089b2837SJed Brown    Level: advanced
769089b2837SJed Brown 
770089b2837SJed Brown .keywords: TS, nonlinear, get, function
771089b2837SJed Brown 
772089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
773089b2837SJed Brown @*/
774089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
775089b2837SJed Brown {
776089b2837SJed Brown   PetscErrorCode ierr;
777089b2837SJed Brown   SNES snes;
77824989b8cSPeter Brune   DM   dm;
779089b2837SJed Brown 
780089b2837SJed Brown   PetscFunctionBegin;
781089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
782089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
783089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
78424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
78524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
786089b2837SJed Brown   PetscFunctionReturn(0);
787089b2837SJed Brown }
788089b2837SJed Brown 
789089b2837SJed Brown #undef __FUNCT__
790089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
791089b2837SJed Brown /*@C
792089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
793089b2837SJed Brown 
794089b2837SJed Brown    Not Collective
795089b2837SJed Brown 
796089b2837SJed Brown    Input Parameter:
797089b2837SJed Brown .  ts - the TS context
798089b2837SJed Brown 
799089b2837SJed Brown    Output Parameter:
800089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
801089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
802089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
803089b2837SJed Brown 
804089b2837SJed Brown    Level: advanced
805089b2837SJed Brown 
806089b2837SJed Brown .keywords: TS, nonlinear, get, function
807089b2837SJed Brown 
808089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
809089b2837SJed Brown @*/
810089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
811089b2837SJed Brown {
812089b2837SJed Brown   PetscErrorCode ierr;
813089b2837SJed Brown   SNES snes;
81424989b8cSPeter Brune   DM   dm;
815089b2837SJed Brown 
816089b2837SJed Brown   PetscFunctionBegin;
817089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
818089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
819089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
82024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
82124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
822316643e7SJed Brown   PetscFunctionReturn(0);
823316643e7SJed Brown }
824316643e7SJed Brown 
825316643e7SJed Brown #undef __FUNCT__
826316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
827316643e7SJed Brown /*@C
828a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
829a4f0a591SBarry Smith         you provided with TSSetIFunction().
830316643e7SJed Brown 
8313f9fe445SBarry Smith    Logically Collective on TS
832316643e7SJed Brown 
833316643e7SJed Brown    Input Parameters:
834316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
835316643e7SJed Brown .  A   - Jacobian matrix
836316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
837316643e7SJed Brown .  f   - the Jacobian evaluation routine
838316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
839316643e7SJed Brown 
840316643e7SJed Brown    Calling sequence of f:
8411b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
842316643e7SJed Brown 
843316643e7SJed Brown +  t    - time at step/stage being solved
8441b4a444bSJed Brown .  U    - state vector
8451b4a444bSJed Brown .  U_t  - time derivative of state vector
846316643e7SJed Brown .  a    - shift
8471b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
848316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
849316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
850316643e7SJed Brown           structure (same as flag in KSPSetOperators())
851316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
852316643e7SJed Brown 
853316643e7SJed Brown    Notes:
854316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
855316643e7SJed Brown 
856a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
857a4f0a591SBarry Smith    the Jacobian of G(U) = F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
858a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
859a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
860a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
861a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
862a4f0a591SBarry Smith 
863316643e7SJed Brown    Level: beginner
864316643e7SJed Brown 
865316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
866316643e7SJed Brown 
867ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
868316643e7SJed Brown 
869316643e7SJed Brown @*/
8707087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
871316643e7SJed Brown {
872316643e7SJed Brown   PetscErrorCode ierr;
873089b2837SJed Brown   SNES           snes;
87424989b8cSPeter Brune   DM             dm;
875316643e7SJed Brown 
876316643e7SJed Brown   PetscFunctionBegin;
8770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8780700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
8790700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
880316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
881316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
88224989b8cSPeter Brune 
88324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88424989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
88524989b8cSPeter Brune 
886089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
887089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
888316643e7SJed Brown   PetscFunctionReturn(0);
889316643e7SJed Brown }
890316643e7SJed Brown 
8914a2ae208SSatish Balay #undef __FUNCT__
8924a2ae208SSatish Balay #define __FUNCT__ "TSView"
8937e2c5f70SBarry Smith /*@C
894d763cef2SBarry Smith     TSView - Prints the TS data structure.
895d763cef2SBarry Smith 
8964c49b128SBarry Smith     Collective on TS
897d763cef2SBarry Smith 
898d763cef2SBarry Smith     Input Parameters:
899d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
900d763cef2SBarry Smith -   viewer - visualization context
901d763cef2SBarry Smith 
902d763cef2SBarry Smith     Options Database Key:
903d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
904d763cef2SBarry Smith 
905d763cef2SBarry Smith     Notes:
906d763cef2SBarry Smith     The available visualization contexts include
907b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
908b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
909d763cef2SBarry Smith          output where only the first processor opens
910d763cef2SBarry Smith          the file.  All other processors send their
911d763cef2SBarry Smith          data to the first processor to print.
912d763cef2SBarry Smith 
913d763cef2SBarry Smith     The user can open an alternative visualization context with
914b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
915d763cef2SBarry Smith 
916d763cef2SBarry Smith     Level: beginner
917d763cef2SBarry Smith 
918d763cef2SBarry Smith .keywords: TS, timestep, view
919d763cef2SBarry Smith 
920b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
921d763cef2SBarry Smith @*/
9227087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
923d763cef2SBarry Smith {
924dfbe8321SBarry Smith   PetscErrorCode ierr;
92519fd82e9SBarry Smith   TSType         type;
926089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
927d763cef2SBarry Smith 
928d763cef2SBarry Smith   PetscFunctionBegin;
9290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9303050cee2SBarry Smith   if (!viewer) {
9317adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
9323050cee2SBarry Smith   }
9330700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
934c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
935fd16b177SBarry Smith 
936251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
937251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
93832077d6dSBarry Smith   if (iascii) {
939317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
94077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
941a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
942d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
9435ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
944c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
945d763cef2SBarry Smith     }
9465ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
947193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
948d52bd9f3SBarry Smith     if (ts->ops->view) {
949d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
950d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
951d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
952d52bd9f3SBarry Smith     }
9530f5bd95cSBarry Smith   } else if (isstring) {
954a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
955b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
956d763cef2SBarry Smith   }
957b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
958251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
959b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
960d763cef2SBarry Smith   PetscFunctionReturn(0);
961d763cef2SBarry Smith }
962d763cef2SBarry Smith 
963d763cef2SBarry Smith 
9644a2ae208SSatish Balay #undef __FUNCT__
9654a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
966b07ff414SBarry Smith /*@
967d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
968d763cef2SBarry Smith    the timesteppers.
969d763cef2SBarry Smith 
9703f9fe445SBarry Smith    Logically Collective on TS
971d763cef2SBarry Smith 
972d763cef2SBarry Smith    Input Parameters:
973d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
974d763cef2SBarry Smith -  usrP - optional user context
975d763cef2SBarry Smith 
976d763cef2SBarry Smith    Level: intermediate
977d763cef2SBarry Smith 
978d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
979d763cef2SBarry Smith 
980d763cef2SBarry Smith .seealso: TSGetApplicationContext()
981d763cef2SBarry Smith @*/
9827087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
983d763cef2SBarry Smith {
984d763cef2SBarry Smith   PetscFunctionBegin;
9850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
986d763cef2SBarry Smith   ts->user = usrP;
987d763cef2SBarry Smith   PetscFunctionReturn(0);
988d763cef2SBarry Smith }
989d763cef2SBarry Smith 
9904a2ae208SSatish Balay #undef __FUNCT__
9914a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
992b07ff414SBarry Smith /*@
993d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
994d763cef2SBarry Smith     timestepper.
995d763cef2SBarry Smith 
996d763cef2SBarry Smith     Not Collective
997d763cef2SBarry Smith 
998d763cef2SBarry Smith     Input Parameter:
999d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1000d763cef2SBarry Smith 
1001d763cef2SBarry Smith     Output Parameter:
1002d763cef2SBarry Smith .   usrP - user context
1003d763cef2SBarry Smith 
1004d763cef2SBarry Smith     Level: intermediate
1005d763cef2SBarry Smith 
1006d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1007d763cef2SBarry Smith 
1008d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1009d763cef2SBarry Smith @*/
1010e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1011d763cef2SBarry Smith {
1012d763cef2SBarry Smith   PetscFunctionBegin;
10130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1014e71120c6SJed Brown   *(void**)usrP = ts->user;
1015d763cef2SBarry Smith   PetscFunctionReturn(0);
1016d763cef2SBarry Smith }
1017d763cef2SBarry Smith 
10184a2ae208SSatish Balay #undef __FUNCT__
10194a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1020d763cef2SBarry Smith /*@
1021b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1022d763cef2SBarry Smith 
1023d763cef2SBarry Smith    Not Collective
1024d763cef2SBarry Smith 
1025d763cef2SBarry Smith    Input Parameter:
1026d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1027d763cef2SBarry Smith 
1028d763cef2SBarry Smith    Output Parameter:
1029b8123daeSJed Brown .  iter - number of steps completed so far
1030d763cef2SBarry Smith 
1031d763cef2SBarry Smith    Level: intermediate
1032d763cef2SBarry Smith 
1033d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1034b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1035d763cef2SBarry Smith @*/
10367087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
1037d763cef2SBarry Smith {
1038d763cef2SBarry Smith   PetscFunctionBegin;
10390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10404482741eSBarry Smith   PetscValidIntPointer(iter,2);
1041d763cef2SBarry Smith   *iter = ts->steps;
1042d763cef2SBarry Smith   PetscFunctionReturn(0);
1043d763cef2SBarry Smith }
1044d763cef2SBarry Smith 
10454a2ae208SSatish Balay #undef __FUNCT__
10464a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1047d763cef2SBarry Smith /*@
1048d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1049d763cef2SBarry Smith    as well as the initial time.
1050d763cef2SBarry Smith 
10513f9fe445SBarry Smith    Logically Collective on TS
1052d763cef2SBarry Smith 
1053d763cef2SBarry Smith    Input Parameters:
1054d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1055d763cef2SBarry Smith .  initial_time - the initial time
1056d763cef2SBarry Smith -  time_step - the size of the timestep
1057d763cef2SBarry Smith 
1058d763cef2SBarry Smith    Level: intermediate
1059d763cef2SBarry Smith 
1060d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1061d763cef2SBarry Smith 
1062d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1063d763cef2SBarry Smith @*/
10647087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1065d763cef2SBarry Smith {
1066e144a568SJed Brown   PetscErrorCode ierr;
1067e144a568SJed Brown 
1068d763cef2SBarry Smith   PetscFunctionBegin;
10690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1070e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1071d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1072d763cef2SBarry Smith   PetscFunctionReturn(0);
1073d763cef2SBarry Smith }
1074d763cef2SBarry Smith 
10754a2ae208SSatish Balay #undef __FUNCT__
10764a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1077d763cef2SBarry Smith /*@
1078d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1079d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1080d763cef2SBarry Smith 
10813f9fe445SBarry Smith    Logically Collective on TS
1082d763cef2SBarry Smith 
1083d763cef2SBarry Smith    Input Parameters:
1084d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1085d763cef2SBarry Smith -  time_step - the size of the timestep
1086d763cef2SBarry Smith 
1087d763cef2SBarry Smith    Level: intermediate
1088d763cef2SBarry Smith 
1089d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1090d763cef2SBarry Smith 
1091d763cef2SBarry Smith .keywords: TS, set, timestep
1092d763cef2SBarry Smith @*/
10937087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1094d763cef2SBarry Smith {
1095d763cef2SBarry Smith   PetscFunctionBegin;
10960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1097c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1098d763cef2SBarry Smith   ts->time_step = time_step;
109931748224SBarry Smith   ts->time_step_orig = time_step;
1100d763cef2SBarry Smith   PetscFunctionReturn(0);
1101d763cef2SBarry Smith }
1102d763cef2SBarry Smith 
11034a2ae208SSatish Balay #undef __FUNCT__
1104a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1105a43b19c4SJed Brown /*@
1106a43b19c4SJed Brown    TSSetExactFinalTime - Determines whether to interpolate solution to the
1107a43b19c4SJed Brown       exact final time requested by the user or just returns it at the final time
1108a43b19c4SJed Brown       it computed.
1109a43b19c4SJed Brown 
1110a43b19c4SJed Brown   Logically Collective on TS
1111a43b19c4SJed Brown 
1112a43b19c4SJed Brown    Input Parameter:
1113a43b19c4SJed Brown +   ts - the time-step context
1114a43b19c4SJed Brown -   ft - PETSC_TRUE if interpolates, else PETSC_FALSE
1115a43b19c4SJed Brown 
1116a43b19c4SJed Brown    Level: beginner
1117a43b19c4SJed Brown 
1118a43b19c4SJed Brown .seealso: TSSetDuration()
1119a43b19c4SJed Brown @*/
1120a43b19c4SJed Brown PetscErrorCode  TSSetExactFinalTime(TS ts,PetscBool flg)
1121a43b19c4SJed Brown {
1122a43b19c4SJed Brown 
1123a43b19c4SJed Brown   PetscFunctionBegin;
1124a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1125d8cd7023SBarry Smith   PetscValidLogicalCollectiveBool(ts,flg,2);
1126a43b19c4SJed Brown   ts->exact_final_time = flg;
1127a43b19c4SJed Brown   PetscFunctionReturn(0);
1128a43b19c4SJed Brown }
1129a43b19c4SJed Brown 
1130a43b19c4SJed Brown #undef __FUNCT__
11314a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1132d763cef2SBarry Smith /*@
1133d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1134d763cef2SBarry Smith 
1135d763cef2SBarry Smith    Not Collective
1136d763cef2SBarry Smith 
1137d763cef2SBarry Smith    Input Parameter:
1138d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1139d763cef2SBarry Smith 
1140d763cef2SBarry Smith    Output Parameter:
1141d763cef2SBarry Smith .  dt - the current timestep size
1142d763cef2SBarry Smith 
1143d763cef2SBarry Smith    Level: intermediate
1144d763cef2SBarry Smith 
1145d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1146d763cef2SBarry Smith 
1147d763cef2SBarry Smith .keywords: TS, get, timestep
1148d763cef2SBarry Smith @*/
11497087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1150d763cef2SBarry Smith {
1151d763cef2SBarry Smith   PetscFunctionBegin;
11520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1153f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1154d763cef2SBarry Smith   *dt = ts->time_step;
1155d763cef2SBarry Smith   PetscFunctionReturn(0);
1156d763cef2SBarry Smith }
1157d763cef2SBarry Smith 
11584a2ae208SSatish Balay #undef __FUNCT__
11594a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1160d8e5e3e6SSatish Balay /*@
1161d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1162d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1163d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1164d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1165d763cef2SBarry Smith 
1166d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1167d763cef2SBarry Smith 
1168d763cef2SBarry Smith    Input Parameter:
1169d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1170d763cef2SBarry Smith 
1171d763cef2SBarry Smith    Output Parameter:
1172d763cef2SBarry Smith .  v - the vector containing the solution
1173d763cef2SBarry Smith 
1174d763cef2SBarry Smith    Level: intermediate
1175d763cef2SBarry Smith 
1176d763cef2SBarry Smith .seealso: TSGetTimeStep()
1177d763cef2SBarry Smith 
1178d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1179d763cef2SBarry Smith @*/
11807087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1181d763cef2SBarry Smith {
1182d763cef2SBarry Smith   PetscFunctionBegin;
11830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11844482741eSBarry Smith   PetscValidPointer(v,2);
11858737fe31SLisandro Dalcin   *v = ts->vec_sol;
1186d763cef2SBarry Smith   PetscFunctionReturn(0);
1187d763cef2SBarry Smith }
1188d763cef2SBarry Smith 
1189bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
11904a2ae208SSatish Balay #undef __FUNCT__
1191bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1192d8e5e3e6SSatish Balay /*@
1193bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1194d763cef2SBarry Smith 
1195bdad233fSMatthew Knepley   Not collective
1196d763cef2SBarry Smith 
1197bdad233fSMatthew Knepley   Input Parameters:
1198bdad233fSMatthew Knepley + ts   - The TS
1199bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1200d763cef2SBarry Smith .vb
1201d763cef2SBarry Smith          U_t = A U
1202d763cef2SBarry Smith          U_t = A(t) U
1203d763cef2SBarry Smith          U_t = F(t,U)
1204d763cef2SBarry Smith .ve
1205d763cef2SBarry Smith 
1206d763cef2SBarry Smith    Level: beginner
1207d763cef2SBarry Smith 
1208bdad233fSMatthew Knepley .keywords: TS, problem type
1209bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1210d763cef2SBarry Smith @*/
12117087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1212a7cc72afSBarry Smith {
12139e2a6581SJed Brown   PetscErrorCode ierr;
12149e2a6581SJed Brown 
1215d763cef2SBarry Smith   PetscFunctionBegin;
12160700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1217bdad233fSMatthew Knepley   ts->problem_type = type;
12189e2a6581SJed Brown   if (type == TS_LINEAR) {
12199e2a6581SJed Brown     SNES snes;
12209e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12219e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
12229e2a6581SJed Brown   }
1223d763cef2SBarry Smith   PetscFunctionReturn(0);
1224d763cef2SBarry Smith }
1225d763cef2SBarry Smith 
1226bdad233fSMatthew Knepley #undef __FUNCT__
1227bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1228bdad233fSMatthew Knepley /*@C
1229bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1230bdad233fSMatthew Knepley 
1231bdad233fSMatthew Knepley   Not collective
1232bdad233fSMatthew Knepley 
1233bdad233fSMatthew Knepley   Input Parameter:
1234bdad233fSMatthew Knepley . ts   - The TS
1235bdad233fSMatthew Knepley 
1236bdad233fSMatthew Knepley   Output Parameter:
1237bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1238bdad233fSMatthew Knepley .vb
1239089b2837SJed Brown          M U_t = A U
1240089b2837SJed Brown          M(t) U_t = A(t) U
1241bdad233fSMatthew Knepley          U_t = F(t,U)
1242bdad233fSMatthew Knepley .ve
1243bdad233fSMatthew Knepley 
1244bdad233fSMatthew Knepley    Level: beginner
1245bdad233fSMatthew Knepley 
1246bdad233fSMatthew Knepley .keywords: TS, problem type
1247bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1248bdad233fSMatthew Knepley @*/
12497087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1250a7cc72afSBarry Smith {
1251bdad233fSMatthew Knepley   PetscFunctionBegin;
12520700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
12534482741eSBarry Smith   PetscValidIntPointer(type,2);
1254bdad233fSMatthew Knepley   *type = ts->problem_type;
1255bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1256bdad233fSMatthew Knepley }
1257d763cef2SBarry Smith 
12584a2ae208SSatish Balay #undef __FUNCT__
12594a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1260d763cef2SBarry Smith /*@
1261d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1262d763cef2SBarry Smith    of a timestepper.
1263d763cef2SBarry Smith 
1264d763cef2SBarry Smith    Collective on TS
1265d763cef2SBarry Smith 
1266d763cef2SBarry Smith    Input Parameter:
1267d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1268d763cef2SBarry Smith 
1269d763cef2SBarry Smith    Notes:
1270d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1271d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1272d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1273d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1274d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1275d763cef2SBarry Smith 
1276d763cef2SBarry Smith    Level: advanced
1277d763cef2SBarry Smith 
1278d763cef2SBarry Smith .keywords: TS, timestep, setup
1279d763cef2SBarry Smith 
1280d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1281d763cef2SBarry Smith @*/
12827087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1283d763cef2SBarry Smith {
1284dfbe8321SBarry Smith   PetscErrorCode ierr;
12856c6b9e74SPeter Brune   DM             dm;
12866c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
12876c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
12886c6b9e74SPeter Brune   TSIJacobian    ijac;
12896c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1290d763cef2SBarry Smith 
1291d763cef2SBarry Smith   PetscFunctionBegin;
12920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1293277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1294277b19d0SLisandro Dalcin 
12957adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
12969596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1297d763cef2SBarry Smith   }
1298a43b19c4SJed Brown   if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE;
1299277b19d0SLisandro Dalcin 
1300277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1301277b19d0SLisandro Dalcin 
13021c3436cfSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
13031c3436cfSJed Brown 
1304277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1305000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1306277b19d0SLisandro Dalcin   }
1307277b19d0SLisandro Dalcin 
13086c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
13096c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
13106c6b9e74SPeter Brune    */
13116c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
13126c6b9e74SPeter Brune   ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr);
13136c6b9e74SPeter Brune   if (!func) {
13146c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
13156c6b9e74SPeter Brune   }
13166c6b9e74SPeter 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.
13176c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
13186c6b9e74SPeter Brune    */
13196c6b9e74SPeter Brune   ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr);
13206c6b9e74SPeter Brune   ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr);
13216c6b9e74SPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr);
13226c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
13236c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
13246c6b9e74SPeter Brune   }
1325277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1326277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1327277b19d0SLisandro Dalcin }
1328277b19d0SLisandro Dalcin 
1329277b19d0SLisandro Dalcin #undef __FUNCT__
1330277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1331277b19d0SLisandro Dalcin /*@
1332277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1333277b19d0SLisandro Dalcin 
1334277b19d0SLisandro Dalcin    Collective on TS
1335277b19d0SLisandro Dalcin 
1336277b19d0SLisandro Dalcin    Input Parameter:
1337277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1338277b19d0SLisandro Dalcin 
1339277b19d0SLisandro Dalcin    Level: beginner
1340277b19d0SLisandro Dalcin 
1341277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1342277b19d0SLisandro Dalcin 
1343277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1344277b19d0SLisandro Dalcin @*/
1345277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1346277b19d0SLisandro Dalcin {
1347277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1348277b19d0SLisandro Dalcin 
1349277b19d0SLisandro Dalcin   PetscFunctionBegin;
1350277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1351277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1352277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1353277b19d0SLisandro Dalcin   }
1354277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
13554e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
13564e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1357214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
13586bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1359e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1360e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
136138637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1362277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1363d763cef2SBarry Smith   PetscFunctionReturn(0);
1364d763cef2SBarry Smith }
1365d763cef2SBarry Smith 
13664a2ae208SSatish Balay #undef __FUNCT__
13674a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1368d8e5e3e6SSatish Balay /*@
1369d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1370d763cef2SBarry Smith    with TSCreate().
1371d763cef2SBarry Smith 
1372d763cef2SBarry Smith    Collective on TS
1373d763cef2SBarry Smith 
1374d763cef2SBarry Smith    Input Parameter:
1375d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1376d763cef2SBarry Smith 
1377d763cef2SBarry Smith    Level: beginner
1378d763cef2SBarry Smith 
1379d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1380d763cef2SBarry Smith 
1381d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1382d763cef2SBarry Smith @*/
13836bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1384d763cef2SBarry Smith {
13856849ba73SBarry Smith   PetscErrorCode ierr;
1386d763cef2SBarry Smith 
1387d763cef2SBarry Smith   PetscFunctionBegin;
13886bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
13896bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
13906bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1391d763cef2SBarry Smith 
13926bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1393277b19d0SLisandro Dalcin 
1394be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13956bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
13966bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
13976d4c513bSLisandro Dalcin 
139884df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
13996bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
14006bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
14016bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
14026d4c513bSLisandro Dalcin 
1403a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1404d763cef2SBarry Smith   PetscFunctionReturn(0);
1405d763cef2SBarry Smith }
1406d763cef2SBarry Smith 
14074a2ae208SSatish Balay #undef __FUNCT__
14084a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1409d8e5e3e6SSatish Balay /*@
1410d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1411d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1412d763cef2SBarry Smith 
1413d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1414d763cef2SBarry Smith 
1415d763cef2SBarry Smith    Input Parameter:
1416d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1417d763cef2SBarry Smith 
1418d763cef2SBarry Smith    Output Parameter:
1419d763cef2SBarry Smith .  snes - the nonlinear solver context
1420d763cef2SBarry Smith 
1421d763cef2SBarry Smith    Notes:
1422d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1423d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
142494b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1425d763cef2SBarry Smith 
1426d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1427d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1428d763cef2SBarry Smith 
1429d763cef2SBarry Smith    Level: beginner
1430d763cef2SBarry Smith 
1431d763cef2SBarry Smith .keywords: timestep, get, SNES
1432d763cef2SBarry Smith @*/
14337087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1434d763cef2SBarry Smith {
1435d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1436d372ba47SLisandro Dalcin 
1437d763cef2SBarry Smith   PetscFunctionBegin;
14380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14394482741eSBarry Smith   PetscValidPointer(snes,2);
1440d372ba47SLisandro Dalcin   if (!ts->snes) {
1441d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
14426c6b9e74SPeter Brune     ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1443d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1444d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1445496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
14469e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
14479e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
14489e2a6581SJed Brown     }
1449d372ba47SLisandro Dalcin   }
1450d763cef2SBarry Smith   *snes = ts->snes;
1451d763cef2SBarry Smith   PetscFunctionReturn(0);
1452d763cef2SBarry Smith }
1453d763cef2SBarry Smith 
14544a2ae208SSatish Balay #undef __FUNCT__
145594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1456d8e5e3e6SSatish Balay /*@
145794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1458d763cef2SBarry Smith    a TS (timestepper) context.
1459d763cef2SBarry Smith 
146094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1461d763cef2SBarry Smith 
1462d763cef2SBarry Smith    Input Parameter:
1463d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1464d763cef2SBarry Smith 
1465d763cef2SBarry Smith    Output Parameter:
146694b7f48cSBarry Smith .  ksp - the nonlinear solver context
1467d763cef2SBarry Smith 
1468d763cef2SBarry Smith    Notes:
146994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1470d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1471d763cef2SBarry Smith    KSP and PC contexts as well.
1472d763cef2SBarry Smith 
147394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
147494b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1475d763cef2SBarry Smith 
1476d763cef2SBarry Smith    Level: beginner
1477d763cef2SBarry Smith 
147894b7f48cSBarry Smith .keywords: timestep, get, KSP
1479d763cef2SBarry Smith @*/
14807087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1481d763cef2SBarry Smith {
1482d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1483089b2837SJed Brown   SNES           snes;
1484d372ba47SLisandro Dalcin 
1485d763cef2SBarry Smith   PetscFunctionBegin;
14860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14874482741eSBarry Smith   PetscValidPointer(ksp,2);
148817186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1489e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1490089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1491089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1492d763cef2SBarry Smith   PetscFunctionReturn(0);
1493d763cef2SBarry Smith }
1494d763cef2SBarry Smith 
1495d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1496d763cef2SBarry Smith 
14974a2ae208SSatish Balay #undef __FUNCT__
1498adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1499adb62b0dSMatthew Knepley /*@
1500adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1501adb62b0dSMatthew Knepley    maximum time for iteration.
1502adb62b0dSMatthew Knepley 
15033f9fe445SBarry Smith    Not Collective
1504adb62b0dSMatthew Knepley 
1505adb62b0dSMatthew Knepley    Input Parameters:
1506adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1507adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1508adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1509adb62b0dSMatthew Knepley 
1510adb62b0dSMatthew Knepley    Level: intermediate
1511adb62b0dSMatthew Knepley 
1512adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1513adb62b0dSMatthew Knepley @*/
15147087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1515adb62b0dSMatthew Knepley {
1516adb62b0dSMatthew Knepley   PetscFunctionBegin;
15170700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1518abc0a331SBarry Smith   if (maxsteps) {
15194482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1520adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1521adb62b0dSMatthew Knepley   }
1522abc0a331SBarry Smith   if (maxtime) {
15234482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1524adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1525adb62b0dSMatthew Knepley   }
1526adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1527adb62b0dSMatthew Knepley }
1528adb62b0dSMatthew Knepley 
1529adb62b0dSMatthew Knepley #undef __FUNCT__
15304a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1531d763cef2SBarry Smith /*@
1532d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1533d763cef2SBarry Smith    maximum time for iteration.
1534d763cef2SBarry Smith 
15353f9fe445SBarry Smith    Logically Collective on TS
1536d763cef2SBarry Smith 
1537d763cef2SBarry Smith    Input Parameters:
1538d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1539d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1540d763cef2SBarry Smith -  maxtime - final time to iterate to
1541d763cef2SBarry Smith 
1542d763cef2SBarry Smith    Options Database Keys:
1543d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
15443bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1545d763cef2SBarry Smith 
1546d763cef2SBarry Smith    Notes:
1547d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1548d763cef2SBarry Smith 
1549d763cef2SBarry Smith    Level: intermediate
1550d763cef2SBarry Smith 
1551d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1552a43b19c4SJed Brown 
1553a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1554d763cef2SBarry Smith @*/
15557087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1556d763cef2SBarry Smith {
1557d763cef2SBarry Smith   PetscFunctionBegin;
15580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1559c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1560c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
156139b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
156239b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1563d763cef2SBarry Smith   PetscFunctionReturn(0);
1564d763cef2SBarry Smith }
1565d763cef2SBarry Smith 
15664a2ae208SSatish Balay #undef __FUNCT__
15674a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1568d763cef2SBarry Smith /*@
1569d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1570d763cef2SBarry Smith    for use by the TS routines.
1571d763cef2SBarry Smith 
15723f9fe445SBarry Smith    Logically Collective on TS and Vec
1573d763cef2SBarry Smith 
1574d763cef2SBarry Smith    Input Parameters:
1575d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1576d763cef2SBarry Smith -  x - the solution vector
1577d763cef2SBarry Smith 
1578d763cef2SBarry Smith    Level: beginner
1579d763cef2SBarry Smith 
1580d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1581d763cef2SBarry Smith @*/
15827087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1583d763cef2SBarry Smith {
15848737fe31SLisandro Dalcin   PetscErrorCode ierr;
1585496e6a7aSJed Brown   DM             dm;
15868737fe31SLisandro Dalcin 
1587d763cef2SBarry Smith   PetscFunctionBegin;
15880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15890700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
15908737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
15916bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
15928737fe31SLisandro Dalcin   ts->vec_sol = x;
1593496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1594496e6a7aSJed Brown   ierr = DMShellSetGlobalVector(dm,x);CHKERRQ(ierr);
1595d763cef2SBarry Smith   PetscFunctionReturn(0);
1596d763cef2SBarry Smith }
1597d763cef2SBarry Smith 
1598e74ef692SMatthew Knepley #undef __FUNCT__
1599e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1600ac226902SBarry Smith /*@C
1601000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
16023f2090d5SJed Brown   called once at the beginning of each time step.
1603000e7ae3SMatthew Knepley 
16043f9fe445SBarry Smith   Logically Collective on TS
1605000e7ae3SMatthew Knepley 
1606000e7ae3SMatthew Knepley   Input Parameters:
1607000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1608000e7ae3SMatthew Knepley - func - The function
1609000e7ae3SMatthew Knepley 
1610000e7ae3SMatthew Knepley   Calling sequence of func:
1611000e7ae3SMatthew Knepley . func (TS ts);
1612000e7ae3SMatthew Knepley 
1613000e7ae3SMatthew Knepley   Level: intermediate
1614000e7ae3SMatthew Knepley 
1615b8123daeSJed Brown   Note:
1616b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1617b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1618b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1619b8123daeSJed Brown 
1620000e7ae3SMatthew Knepley .keywords: TS, timestep
1621b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1622000e7ae3SMatthew Knepley @*/
16237087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1624000e7ae3SMatthew Knepley {
1625000e7ae3SMatthew Knepley   PetscFunctionBegin;
16260700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1627000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1628000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1629000e7ae3SMatthew Knepley }
1630000e7ae3SMatthew Knepley 
1631e74ef692SMatthew Knepley #undef __FUNCT__
16323f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
163309ee8438SJed Brown /*@
16343f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
16353f2090d5SJed Brown 
16363f2090d5SJed Brown   Collective on TS
16373f2090d5SJed Brown 
16383f2090d5SJed Brown   Input Parameters:
16393f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
16403f2090d5SJed Brown 
16413f2090d5SJed Brown   Notes:
16423f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
16433f2090d5SJed Brown   so most users would not generally call this routine themselves.
16443f2090d5SJed Brown 
16453f2090d5SJed Brown   Level: developer
16463f2090d5SJed Brown 
16473f2090d5SJed Brown .keywords: TS, timestep
1648b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
16493f2090d5SJed Brown @*/
16507087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
16513f2090d5SJed Brown {
16523f2090d5SJed Brown   PetscErrorCode ierr;
16533f2090d5SJed Brown 
16543f2090d5SJed Brown   PetscFunctionBegin;
16550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
165672ac3e02SJed Brown   if (ts->ops->prestep) {
16573f2090d5SJed Brown     PetscStackPush("TS PreStep function");
16583f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
16593f2090d5SJed Brown     PetscStackPop;
1660312ce896SJed Brown   }
16613f2090d5SJed Brown   PetscFunctionReturn(0);
16623f2090d5SJed Brown }
16633f2090d5SJed Brown 
16643f2090d5SJed Brown #undef __FUNCT__
1665b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1666b8123daeSJed Brown /*@C
1667b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1668b8123daeSJed Brown   called once at the beginning of each stage.
1669b8123daeSJed Brown 
1670b8123daeSJed Brown   Logically Collective on TS
1671b8123daeSJed Brown 
1672b8123daeSJed Brown   Input Parameters:
1673b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1674b8123daeSJed Brown - func - The function
1675b8123daeSJed Brown 
1676b8123daeSJed Brown   Calling sequence of func:
1677b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1678b8123daeSJed Brown 
1679b8123daeSJed Brown   Level: intermediate
1680b8123daeSJed Brown 
1681b8123daeSJed Brown   Note:
1682b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
1683b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
1684b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
1685b8123daeSJed Brown 
1686b8123daeSJed Brown .keywords: TS, timestep
1687b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
1688b8123daeSJed Brown @*/
1689b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
1690b8123daeSJed Brown {
1691b8123daeSJed Brown   PetscFunctionBegin;
1692b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1693b8123daeSJed Brown   ts->ops->prestage = func;
1694b8123daeSJed Brown   PetscFunctionReturn(0);
1695b8123daeSJed Brown }
1696b8123daeSJed Brown 
1697b8123daeSJed Brown #undef __FUNCT__
1698b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
1699b8123daeSJed Brown /*@
1700b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
1701b8123daeSJed Brown 
1702b8123daeSJed Brown   Collective on TS
1703b8123daeSJed Brown 
1704b8123daeSJed Brown   Input Parameters:
1705b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
1706b8123daeSJed Brown 
1707b8123daeSJed Brown   Notes:
1708b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
1709b8123daeSJed Brown   most users would not generally call this routine themselves.
1710b8123daeSJed Brown 
1711b8123daeSJed Brown   Level: developer
1712b8123daeSJed Brown 
1713b8123daeSJed Brown .keywords: TS, timestep
1714b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
1715b8123daeSJed Brown @*/
1716b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
1717b8123daeSJed Brown {
1718b8123daeSJed Brown   PetscErrorCode ierr;
1719b8123daeSJed Brown 
1720b8123daeSJed Brown   PetscFunctionBegin;
1721b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1722b8123daeSJed Brown   if (ts->ops->prestage) {
1723b8123daeSJed Brown     PetscStackPush("TS PreStage function");
1724b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
1725b8123daeSJed Brown     PetscStackPop;
1726b8123daeSJed Brown   }
1727b8123daeSJed Brown   PetscFunctionReturn(0);
1728b8123daeSJed Brown }
1729b8123daeSJed Brown 
1730b8123daeSJed Brown #undef __FUNCT__
1731e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1732ac226902SBarry Smith /*@C
1733000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
17343f2090d5SJed Brown   called once at the end of each time step.
1735000e7ae3SMatthew Knepley 
17363f9fe445SBarry Smith   Logically Collective on TS
1737000e7ae3SMatthew Knepley 
1738000e7ae3SMatthew Knepley   Input Parameters:
1739000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1740000e7ae3SMatthew Knepley - func - The function
1741000e7ae3SMatthew Knepley 
1742000e7ae3SMatthew Knepley   Calling sequence of func:
1743b8123daeSJed Brown $ func (TS ts);
1744000e7ae3SMatthew Knepley 
1745000e7ae3SMatthew Knepley   Level: intermediate
1746000e7ae3SMatthew Knepley 
1747000e7ae3SMatthew Knepley .keywords: TS, timestep
1748b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
1749000e7ae3SMatthew Knepley @*/
17507087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1751000e7ae3SMatthew Knepley {
1752000e7ae3SMatthew Knepley   PetscFunctionBegin;
17530700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1754000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1755000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1756000e7ae3SMatthew Knepley }
1757000e7ae3SMatthew Knepley 
1758e74ef692SMatthew Knepley #undef __FUNCT__
17593f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
176009ee8438SJed Brown /*@
17613f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
17623f2090d5SJed Brown 
17633f2090d5SJed Brown   Collective on TS
17643f2090d5SJed Brown 
17653f2090d5SJed Brown   Input Parameters:
17663f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
17673f2090d5SJed Brown 
17683f2090d5SJed Brown   Notes:
17693f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
17703f2090d5SJed Brown   so most users would not generally call this routine themselves.
17713f2090d5SJed Brown 
17723f2090d5SJed Brown   Level: developer
17733f2090d5SJed Brown 
17743f2090d5SJed Brown .keywords: TS, timestep
17753f2090d5SJed Brown @*/
17767087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
17773f2090d5SJed Brown {
17783f2090d5SJed Brown   PetscErrorCode ierr;
17793f2090d5SJed Brown 
17803f2090d5SJed Brown   PetscFunctionBegin;
17810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
178272ac3e02SJed Brown   if (ts->ops->poststep) {
17833f2090d5SJed Brown     PetscStackPush("TS PostStep function");
17843f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
17853f2090d5SJed Brown     PetscStackPop;
178672ac3e02SJed Brown   }
17873f2090d5SJed Brown   PetscFunctionReturn(0);
17883f2090d5SJed Brown }
17893f2090d5SJed Brown 
1790d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1791d763cef2SBarry Smith 
17924a2ae208SSatish Balay #undef __FUNCT__
1793a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1794d763cef2SBarry Smith /*@C
1795a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1796d763cef2SBarry Smith    timestep to display the iteration's  progress.
1797d763cef2SBarry Smith 
17983f9fe445SBarry Smith    Logically Collective on TS
1799d763cef2SBarry Smith 
1800d763cef2SBarry Smith    Input Parameters:
1801d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1802e213d8f1SJed Brown .  monitor - monitoring routine
1803329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1804b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1805b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1806b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1807d763cef2SBarry Smith 
1808e213d8f1SJed Brown    Calling sequence of monitor:
1809e213d8f1SJed Brown $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1810d763cef2SBarry Smith 
1811d763cef2SBarry Smith +    ts - the TS context
1812d763cef2SBarry Smith .    steps - iteration number
18131f06c33eSBarry Smith .    time - current time
1814d763cef2SBarry Smith .    x - current iterate
1815d763cef2SBarry Smith -    mctx - [optional] monitoring context
1816d763cef2SBarry Smith 
1817d763cef2SBarry Smith    Notes:
1818d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1819d763cef2SBarry Smith    already has been loaded.
1820d763cef2SBarry Smith 
1821025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1822025f1a04SBarry Smith 
1823d763cef2SBarry Smith    Level: intermediate
1824d763cef2SBarry Smith 
1825d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1826d763cef2SBarry Smith 
1827a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1828d763cef2SBarry Smith @*/
1829c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1830d763cef2SBarry Smith {
1831d763cef2SBarry Smith   PetscFunctionBegin;
18320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
183317186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1834d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1835329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1836d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1837d763cef2SBarry Smith   PetscFunctionReturn(0);
1838d763cef2SBarry Smith }
1839d763cef2SBarry Smith 
18404a2ae208SSatish Balay #undef __FUNCT__
1841a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1842d763cef2SBarry Smith /*@C
1843a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1844d763cef2SBarry Smith 
18453f9fe445SBarry Smith    Logically Collective on TS
1846d763cef2SBarry Smith 
1847d763cef2SBarry Smith    Input Parameters:
1848d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1849d763cef2SBarry Smith 
1850d763cef2SBarry Smith    Notes:
1851d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1852d763cef2SBarry Smith 
1853d763cef2SBarry Smith    Level: intermediate
1854d763cef2SBarry Smith 
1855d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1856d763cef2SBarry Smith 
1857a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1858d763cef2SBarry Smith @*/
18597087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1860d763cef2SBarry Smith {
1861d952e501SBarry Smith   PetscErrorCode ierr;
1862d952e501SBarry Smith   PetscInt       i;
1863d952e501SBarry Smith 
1864d763cef2SBarry Smith   PetscFunctionBegin;
18650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1866d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1867d952e501SBarry Smith     if (ts->mdestroy[i]) {
18683c4aec1bSBarry Smith       ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1869d952e501SBarry Smith     }
1870d952e501SBarry Smith   }
1871d763cef2SBarry Smith   ts->numbermonitors = 0;
1872d763cef2SBarry Smith   PetscFunctionReturn(0);
1873d763cef2SBarry Smith }
1874d763cef2SBarry Smith 
18754a2ae208SSatish Balay #undef __FUNCT__
1876a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1877d8e5e3e6SSatish Balay /*@
1878a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
18795516499fSSatish Balay 
18805516499fSSatish Balay    Level: intermediate
188141251cbbSSatish Balay 
18825516499fSSatish Balay .keywords: TS, set, monitor
18835516499fSSatish Balay 
188441251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
188541251cbbSSatish Balay @*/
1886649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
1887d763cef2SBarry Smith {
1888dfbe8321SBarry Smith   PetscErrorCode ierr;
1889649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
1890d132466eSBarry Smith 
1891d763cef2SBarry Smith   PetscFunctionBegin;
1892649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1893971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
1894649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1895d763cef2SBarry Smith   PetscFunctionReturn(0);
1896d763cef2SBarry Smith }
1897d763cef2SBarry Smith 
18984a2ae208SSatish Balay #undef __FUNCT__
1899cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
1900cd652676SJed Brown /*@
1901cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
1902cd652676SJed Brown 
1903cd652676SJed Brown    Logically Collective on TS
1904cd652676SJed Brown 
1905cd652676SJed Brown    Input Argument:
1906cd652676SJed Brown .  ts - time stepping context
1907cd652676SJed Brown 
1908cd652676SJed Brown    Output Argument:
1909cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
1910cd652676SJed Brown 
1911cd652676SJed Brown    Level: intermediate
1912cd652676SJed Brown 
1913cd652676SJed Brown .keywords: TS, set
1914cd652676SJed Brown 
1915cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
1916cd652676SJed Brown @*/
1917cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
1918cd652676SJed Brown {
1919cd652676SJed Brown 
1920cd652676SJed Brown   PetscFunctionBegin;
1921cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1922cd652676SJed Brown   ts->retain_stages = flg;
1923cd652676SJed Brown   PetscFunctionReturn(0);
1924cd652676SJed Brown }
1925cd652676SJed Brown 
1926cd652676SJed Brown #undef __FUNCT__
1927cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
1928cd652676SJed Brown /*@
1929cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
1930cd652676SJed Brown 
1931cd652676SJed Brown    Collective on TS
1932cd652676SJed Brown 
1933cd652676SJed Brown    Input Argument:
1934cd652676SJed Brown +  ts - time stepping context
1935cd652676SJed Brown -  t - time to interpolate to
1936cd652676SJed Brown 
1937cd652676SJed Brown    Output Argument:
1938cd652676SJed Brown .  X - state at given time
1939cd652676SJed Brown 
1940cd652676SJed Brown    Notes:
1941cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
1942cd652676SJed Brown 
1943cd652676SJed Brown    Level: intermediate
1944cd652676SJed Brown 
1945cd652676SJed Brown    Developer Notes:
1946cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
1947cd652676SJed Brown 
1948cd652676SJed Brown .keywords: TS, set
1949cd652676SJed Brown 
1950cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
1951cd652676SJed Brown @*/
1952cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X)
1953cd652676SJed Brown {
1954cd652676SJed Brown   PetscErrorCode ierr;
1955cd652676SJed Brown 
1956cd652676SJed Brown   PetscFunctionBegin;
1957cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1958d8cd7023SBarry 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);
1959cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
1960cd652676SJed Brown   ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr);
1961cd652676SJed Brown   PetscFunctionReturn(0);
1962cd652676SJed Brown }
1963cd652676SJed Brown 
1964cd652676SJed Brown #undef __FUNCT__
19654a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1966d763cef2SBarry Smith /*@
19676d9e5789SSean Farley    TSStep - Steps one time step
1968d763cef2SBarry Smith 
1969d763cef2SBarry Smith    Collective on TS
1970d763cef2SBarry Smith 
1971d763cef2SBarry Smith    Input Parameter:
1972d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1973d763cef2SBarry Smith 
19746d9e5789SSean Farley    Level: intermediate
1975d763cef2SBarry Smith 
1976b8123daeSJed Brown    Notes:
1977b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
1978b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
1979b8123daeSJed Brown 
1980d763cef2SBarry Smith .keywords: TS, timestep, solve
1981d763cef2SBarry Smith 
1982b8123daeSJed Brown .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage()
1983d763cef2SBarry Smith @*/
1984193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
1985d763cef2SBarry Smith {
1986362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
1987dfbe8321SBarry Smith   PetscErrorCode ierr;
1988d763cef2SBarry Smith 
1989d763cef2SBarry Smith   PetscFunctionBegin;
19900700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1991d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
1992d405a339SMatthew Knepley 
1993362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
1994362cd11cSLisandro Dalcin 
1995362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
1996d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1997193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
1998d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1999362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2000362cd11cSLisandro Dalcin 
2001362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2002cef5090cSJed Brown     if (ts->errorifstepfailed) {
2003cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2004cef5090cSJed 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]);
2005cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2006cef5090cSJed Brown     }
2007362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2008362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2009362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2010362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2011362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2012362cd11cSLisandro Dalcin   }
2013362cd11cSLisandro Dalcin 
2014d763cef2SBarry Smith   PetscFunctionReturn(0);
2015d763cef2SBarry Smith }
2016d763cef2SBarry Smith 
20174a2ae208SSatish Balay #undef __FUNCT__
201805175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
201905175c85SJed Brown /*@
202005175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
202105175c85SJed Brown 
20221c3436cfSJed Brown    Collective on TS
202305175c85SJed Brown 
202405175c85SJed Brown    Input Arguments:
20251c3436cfSJed Brown +  ts - time stepping context
20261c3436cfSJed Brown .  order - desired order of accuracy
20271c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
202805175c85SJed Brown 
202905175c85SJed Brown    Output Arguments:
20301c3436cfSJed Brown .  X - state at the end of the current step
203105175c85SJed Brown 
203205175c85SJed Brown    Level: advanced
203305175c85SJed Brown 
2034108c343cSJed Brown    Notes:
2035108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2036108c343cSJed 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.
2037108c343cSJed Brown 
20381c3436cfSJed Brown .seealso: TSStep(), TSAdapt
203905175c85SJed Brown @*/
20401c3436cfSJed Brown PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec X,PetscBool *done)
204105175c85SJed Brown {
204205175c85SJed Brown   PetscErrorCode ierr;
204305175c85SJed Brown 
204405175c85SJed Brown   PetscFunctionBegin;
204505175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
204605175c85SJed Brown   PetscValidType(ts,1);
204705175c85SJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
20481c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
20491c3436cfSJed Brown   ierr = (*ts->ops->evaluatestep)(ts,order,X,done);CHKERRQ(ierr);
205005175c85SJed Brown   PetscFunctionReturn(0);
205105175c85SJed Brown }
205205175c85SJed Brown 
205305175c85SJed Brown #undef __FUNCT__
20546a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
20556a4d4014SLisandro Dalcin /*@
20566a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
20576a4d4014SLisandro Dalcin 
20586a4d4014SLisandro Dalcin    Collective on TS
20596a4d4014SLisandro Dalcin 
20606a4d4014SLisandro Dalcin    Input Parameter:
20616a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2062362cd11cSLisandro Dalcin -  x - the solution vector
20636a4d4014SLisandro Dalcin 
20645a3a76d0SJed Brown    Output Parameter:
20655a3a76d0SJed Brown .  ftime - time of the state vector x upon completion
20665a3a76d0SJed Brown 
20676a4d4014SLisandro Dalcin    Level: beginner
20686a4d4014SLisandro Dalcin 
20695a3a76d0SJed Brown    Notes:
20705a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
20715a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
20725a3a76d0SJed Brown    stepped over the final time.
20735a3a76d0SJed Brown 
20746a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
20756a4d4014SLisandro Dalcin 
20766a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
20776a4d4014SLisandro Dalcin @*/
20785a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime)
20796a4d4014SLisandro Dalcin {
20804d7d938eSLisandro Dalcin   PetscBool      flg;
20814d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
20824d7d938eSLisandro Dalcin   PetscViewer    viewer;
20836a4d4014SLisandro Dalcin   PetscErrorCode ierr;
2084f22f69f0SBarry Smith 
20856a4d4014SLisandro Dalcin   PetscFunctionBegin;
20860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2087193ac0bcSJed Brown   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
20885a3a76d0SJed Brown   if (ts->exact_final_time) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
20895a3a76d0SJed Brown     if (!ts->vec_sol || x == ts->vec_sol) {
20905a3a76d0SJed Brown       Vec y;
20915a3a76d0SJed Brown       ierr = VecDuplicate(x,&y);CHKERRQ(ierr);
20925a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
20935a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
20945a3a76d0SJed Brown     }
2095362cd11cSLisandro Dalcin     ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr);
20965a3a76d0SJed Brown   } else {
2097193ac0bcSJed Brown     ierr = TSSetSolution(ts,x);CHKERRQ(ierr);
20985a3a76d0SJed Brown   }
2099b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
21006a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2101193ac0bcSJed Brown   ts->steps = 0;
21025ef26d82SJed Brown   ts->ksp_its = 0;
21035ef26d82SJed Brown   ts->snes_its = 0;
2104c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2105c610991cSLisandro Dalcin   ts->reject = 0;
2106193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
2107193ac0bcSJed Brown 
2108193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2109193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
21105a3a76d0SJed Brown     ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
2111a5b82c69SSean Farley     if (ftime) *ftime = ts->ptime;
2112193ac0bcSJed Brown   } else {
21136a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2114362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2115362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2116362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2117362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2118362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2119e1a7a14fSJed Brown     while (!ts->reason) {
2120193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2121193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2122193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2123193ac0bcSJed Brown     }
2124362cd11cSLisandro Dalcin     if (ts->exact_final_time && ts->ptime > ts->max_time) {
2125a43b19c4SJed Brown       ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr);
21265a3a76d0SJed Brown       if (ftime) *ftime = ts->max_time;
21270574a7fbSJed Brown     } else {
21280574a7fbSJed Brown       ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
21290574a7fbSJed Brown       if (ftime) *ftime = ts->ptime;
21300574a7fbSJed Brown     }
2131193ac0bcSJed Brown   }
21324d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
21334d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
21344d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
21354d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
21364d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2137193ac0bcSJed Brown   }
21386a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
21396a4d4014SLisandro Dalcin }
21406a4d4014SLisandro Dalcin 
21416a4d4014SLisandro Dalcin #undef __FUNCT__
21424a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2143228d79bcSJed Brown /*@
2144228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2145228d79bcSJed Brown 
2146228d79bcSJed Brown    Collective on TS
2147228d79bcSJed Brown 
2148228d79bcSJed Brown    Input Parameters:
2149228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2150228d79bcSJed Brown .  step - step number that has just completed
2151228d79bcSJed Brown .  ptime - model time of the state
2152228d79bcSJed Brown -  x - state at the current model time
2153228d79bcSJed Brown 
2154228d79bcSJed Brown    Notes:
2155228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2156228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2157228d79bcSJed Brown 
2158228d79bcSJed Brown    Level: advanced
2159228d79bcSJed Brown 
2160228d79bcSJed Brown .keywords: TS, timestep
2161228d79bcSJed Brown @*/
2162a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
2163d763cef2SBarry Smith {
21646849ba73SBarry Smith   PetscErrorCode ierr;
2165a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2166d763cef2SBarry Smith 
2167d763cef2SBarry Smith   PetscFunctionBegin;
2168d763cef2SBarry Smith   for (i=0; i<n; i++) {
2169142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
2170d763cef2SBarry Smith   }
2171d763cef2SBarry Smith   PetscFunctionReturn(0);
2172d763cef2SBarry Smith }
2173d763cef2SBarry Smith 
2174d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
2175d763cef2SBarry Smith 
21764a2ae208SSatish Balay #undef __FUNCT__
2177a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
2178d763cef2SBarry Smith /*@C
2179a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
2180d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
2181d763cef2SBarry Smith 
2182d763cef2SBarry Smith    Collective on TS
2183d763cef2SBarry Smith 
2184d763cef2SBarry Smith    Input Parameters:
2185d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2186d763cef2SBarry Smith .  label - the title to put in the title bar
21877c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2188d763cef2SBarry Smith -  m, n - the screen width and height in pixels
2189d763cef2SBarry Smith 
2190d763cef2SBarry Smith    Output Parameter:
2191d763cef2SBarry Smith .  draw - the drawing context
2192d763cef2SBarry Smith 
2193d763cef2SBarry Smith    Options Database Key:
2194a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
2195d763cef2SBarry Smith 
2196d763cef2SBarry Smith    Notes:
2197a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
2198d763cef2SBarry Smith 
2199d763cef2SBarry Smith    Level: intermediate
2200d763cef2SBarry Smith 
22017c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2202d763cef2SBarry Smith 
2203a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
22047c922b88SBarry Smith 
2205d763cef2SBarry Smith @*/
2206a80ad3e0SBarry Smith PetscErrorCode  TSMonitorLGCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2207d763cef2SBarry Smith {
2208b0a32e0cSBarry Smith   PetscDraw      win;
2209dfbe8321SBarry Smith   PetscErrorCode ierr;
221070ddbef3SBarry Smith   PetscDrawAxis  axis;
2211d763cef2SBarry Smith 
2212d763cef2SBarry Smith   PetscFunctionBegin;
2213a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
2214b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
2215b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
2216b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
221770ddbef3SBarry Smith   ierr = PetscDrawLGGetAxis(*draw,&axis);CHKERRQ(ierr);
221870ddbef3SBarry Smith   ierr = PetscDrawAxisSetLabels(axis,"Integration Time","Iteration","Time");CHKERRQ(ierr);
221952e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
2220d763cef2SBarry Smith   PetscFunctionReturn(0);
2221d763cef2SBarry Smith }
2222d763cef2SBarry Smith 
22234a2ae208SSatish Balay #undef __FUNCT__
2224a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
2225a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2226d763cef2SBarry Smith {
2227b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
222887828ca2SBarry Smith   PetscReal      x,y = ptime;
2229dfbe8321SBarry Smith   PetscErrorCode ierr;
2230d763cef2SBarry Smith 
2231d763cef2SBarry Smith   PetscFunctionBegin;
22327c922b88SBarry Smith   if (!monctx) {
22337c922b88SBarry Smith     MPI_Comm      comm;
2234b0a32e0cSBarry Smith     PetscViewer   viewer;
223570ddbef3SBarry Smith     PetscDrawAxis axis;
22367c922b88SBarry Smith 
22377c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
2238b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
2239b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
224070ddbef3SBarry Smith     ierr   = PetscDrawLGGetAxis(lg,&axis);CHKERRQ(ierr);
224170ddbef3SBarry Smith     ierr   = PetscDrawAxisSetLabels(axis,"Integration Time","Iteration","Time");CHKERRQ(ierr);
22427c922b88SBarry Smith   }
22437c922b88SBarry Smith 
2244b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
224587828ca2SBarry Smith   x = (PetscReal)n;
2246b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2247d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
2248b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2249d763cef2SBarry Smith   }
2250d763cef2SBarry Smith   PetscFunctionReturn(0);
2251d763cef2SBarry Smith }
2252d763cef2SBarry Smith 
22534a2ae208SSatish Balay #undef __FUNCT__
2254a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
2255d763cef2SBarry Smith /*@C
2256a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
2257a6570f20SBarry Smith    with TSMonitorLGCreate().
2258d763cef2SBarry Smith 
2259b0a32e0cSBarry Smith    Collective on PetscDrawLG
2260d763cef2SBarry Smith 
2261d763cef2SBarry Smith    Input Parameter:
2262d763cef2SBarry Smith .  draw - the drawing context
2263d763cef2SBarry Smith 
2264d763cef2SBarry Smith    Level: intermediate
2265d763cef2SBarry Smith 
2266d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2267d763cef2SBarry Smith 
2268a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
2269d763cef2SBarry Smith @*/
22706bf464f9SBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG *drawlg)
2271d763cef2SBarry Smith {
2272b0a32e0cSBarry Smith   PetscDraw      draw;
2273dfbe8321SBarry Smith   PetscErrorCode ierr;
2274d763cef2SBarry Smith 
2275d763cef2SBarry Smith   PetscFunctionBegin;
22766bf464f9SBarry Smith   ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr);
22776bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
2278b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
2279d763cef2SBarry Smith   PetscFunctionReturn(0);
2280d763cef2SBarry Smith }
2281d763cef2SBarry Smith 
22824a2ae208SSatish Balay #undef __FUNCT__
22834a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2284d763cef2SBarry Smith /*@
2285b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2286d763cef2SBarry Smith 
2287d763cef2SBarry Smith    Not Collective
2288d763cef2SBarry Smith 
2289d763cef2SBarry Smith    Input Parameter:
2290d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2291d763cef2SBarry Smith 
2292d763cef2SBarry Smith    Output Parameter:
2293d763cef2SBarry Smith .  t  - the current time
2294d763cef2SBarry Smith 
2295d763cef2SBarry Smith    Level: beginner
2296d763cef2SBarry Smith 
2297b8123daeSJed Brown    Note:
2298b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2299b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2300b8123daeSJed Brown 
2301d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2302d763cef2SBarry Smith 
2303d763cef2SBarry Smith .keywords: TS, get, time
2304d763cef2SBarry Smith @*/
23057087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2306d763cef2SBarry Smith {
2307d763cef2SBarry Smith   PetscFunctionBegin;
23080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2309f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2310d763cef2SBarry Smith   *t = ts->ptime;
2311d763cef2SBarry Smith   PetscFunctionReturn(0);
2312d763cef2SBarry Smith }
2313d763cef2SBarry Smith 
23144a2ae208SSatish Balay #undef __FUNCT__
23156a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
23166a4d4014SLisandro Dalcin /*@
23176a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
23186a4d4014SLisandro Dalcin 
23193f9fe445SBarry Smith    Logically Collective on TS
23206a4d4014SLisandro Dalcin 
23216a4d4014SLisandro Dalcin    Input Parameters:
23226a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
23236a4d4014SLisandro Dalcin -  time - the time
23246a4d4014SLisandro Dalcin 
23256a4d4014SLisandro Dalcin    Level: intermediate
23266a4d4014SLisandro Dalcin 
23276a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
23286a4d4014SLisandro Dalcin 
23296a4d4014SLisandro Dalcin .keywords: TS, set, time
23306a4d4014SLisandro Dalcin @*/
23317087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
23326a4d4014SLisandro Dalcin {
23336a4d4014SLisandro Dalcin   PetscFunctionBegin;
23340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2335c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
23366a4d4014SLisandro Dalcin   ts->ptime = t;
23376a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
23386a4d4014SLisandro Dalcin }
23396a4d4014SLisandro Dalcin 
23406a4d4014SLisandro Dalcin #undef __FUNCT__
23414a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2342d763cef2SBarry Smith /*@C
2343d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2344d763cef2SBarry Smith    TS options in the database.
2345d763cef2SBarry Smith 
23463f9fe445SBarry Smith    Logically Collective on TS
2347d763cef2SBarry Smith 
2348d763cef2SBarry Smith    Input Parameter:
2349d763cef2SBarry Smith +  ts     - The TS context
2350d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2351d763cef2SBarry Smith 
2352d763cef2SBarry Smith    Notes:
2353d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2354d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2355d763cef2SBarry Smith    hyphen.
2356d763cef2SBarry Smith 
2357d763cef2SBarry Smith    Level: advanced
2358d763cef2SBarry Smith 
2359d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2360d763cef2SBarry Smith 
2361d763cef2SBarry Smith .seealso: TSSetFromOptions()
2362d763cef2SBarry Smith 
2363d763cef2SBarry Smith @*/
23647087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2365d763cef2SBarry Smith {
2366dfbe8321SBarry Smith   PetscErrorCode ierr;
2367089b2837SJed Brown   SNES           snes;
2368d763cef2SBarry Smith 
2369d763cef2SBarry Smith   PetscFunctionBegin;
23700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2371d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2372089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2373089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2374d763cef2SBarry Smith   PetscFunctionReturn(0);
2375d763cef2SBarry Smith }
2376d763cef2SBarry Smith 
2377d763cef2SBarry Smith 
23784a2ae208SSatish Balay #undef __FUNCT__
23794a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2380d763cef2SBarry Smith /*@C
2381d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2382d763cef2SBarry Smith    TS options in the database.
2383d763cef2SBarry Smith 
23843f9fe445SBarry Smith    Logically Collective on TS
2385d763cef2SBarry Smith 
2386d763cef2SBarry Smith    Input Parameter:
2387d763cef2SBarry Smith +  ts     - The TS context
2388d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2389d763cef2SBarry Smith 
2390d763cef2SBarry Smith    Notes:
2391d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2392d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2393d763cef2SBarry Smith    hyphen.
2394d763cef2SBarry Smith 
2395d763cef2SBarry Smith    Level: advanced
2396d763cef2SBarry Smith 
2397d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2398d763cef2SBarry Smith 
2399d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2400d763cef2SBarry Smith 
2401d763cef2SBarry Smith @*/
24027087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2403d763cef2SBarry Smith {
2404dfbe8321SBarry Smith   PetscErrorCode ierr;
2405089b2837SJed Brown   SNES           snes;
2406d763cef2SBarry Smith 
2407d763cef2SBarry Smith   PetscFunctionBegin;
24080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2409d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2410089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2411089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2412d763cef2SBarry Smith   PetscFunctionReturn(0);
2413d763cef2SBarry Smith }
2414d763cef2SBarry Smith 
24154a2ae208SSatish Balay #undef __FUNCT__
24164a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2417d763cef2SBarry Smith /*@C
2418d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2419d763cef2SBarry Smith    TS options in the database.
2420d763cef2SBarry Smith 
2421d763cef2SBarry Smith    Not Collective
2422d763cef2SBarry Smith 
2423d763cef2SBarry Smith    Input Parameter:
2424d763cef2SBarry Smith .  ts - The TS context
2425d763cef2SBarry Smith 
2426d763cef2SBarry Smith    Output Parameter:
2427d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2428d763cef2SBarry Smith 
2429d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2430d763cef2SBarry Smith    sufficient length to hold the prefix.
2431d763cef2SBarry Smith 
2432d763cef2SBarry Smith    Level: intermediate
2433d763cef2SBarry Smith 
2434d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2435d763cef2SBarry Smith 
2436d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2437d763cef2SBarry Smith @*/
24387087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2439d763cef2SBarry Smith {
2440dfbe8321SBarry Smith   PetscErrorCode ierr;
2441d763cef2SBarry Smith 
2442d763cef2SBarry Smith   PetscFunctionBegin;
24430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24444482741eSBarry Smith   PetscValidPointer(prefix,2);
2445d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2446d763cef2SBarry Smith   PetscFunctionReturn(0);
2447d763cef2SBarry Smith }
2448d763cef2SBarry Smith 
24494a2ae208SSatish Balay #undef __FUNCT__
24504a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2451d763cef2SBarry Smith /*@C
2452d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2453d763cef2SBarry Smith 
2454d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2455d763cef2SBarry Smith 
2456d763cef2SBarry Smith    Input Parameter:
2457d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2458d763cef2SBarry Smith 
2459d763cef2SBarry Smith    Output Parameters:
2460d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2461d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2462089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2463d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2464d763cef2SBarry Smith 
2465d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2466d763cef2SBarry Smith 
2467d763cef2SBarry Smith    Level: intermediate
2468d763cef2SBarry Smith 
246926d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2470d763cef2SBarry Smith 
2471d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2472d763cef2SBarry Smith @*/
2473089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2474d763cef2SBarry Smith {
2475089b2837SJed Brown   PetscErrorCode ierr;
2476089b2837SJed Brown   SNES           snes;
247724989b8cSPeter Brune   DM             dm;
2478089b2837SJed Brown 
2479d763cef2SBarry Smith   PetscFunctionBegin;
2480089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2481089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
248224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
248324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2484d763cef2SBarry Smith   PetscFunctionReturn(0);
2485d763cef2SBarry Smith }
2486d763cef2SBarry Smith 
24871713a123SBarry Smith #undef __FUNCT__
24882eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
24892eca1d9cSJed Brown /*@C
24902eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
24912eca1d9cSJed Brown 
24922eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
24932eca1d9cSJed Brown 
24942eca1d9cSJed Brown    Input Parameter:
24952eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
24962eca1d9cSJed Brown 
24972eca1d9cSJed Brown    Output Parameters:
24982eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
24992eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
25002eca1d9cSJed Brown .  f   - The function to compute the matrices
25012eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
25022eca1d9cSJed Brown 
25032eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
25042eca1d9cSJed Brown 
25052eca1d9cSJed Brown    Level: advanced
25062eca1d9cSJed Brown 
25072eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
25082eca1d9cSJed Brown 
25092eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
25102eca1d9cSJed Brown @*/
25117087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
25122eca1d9cSJed Brown {
2513089b2837SJed Brown   PetscErrorCode ierr;
2514089b2837SJed Brown   SNES           snes;
251524989b8cSPeter Brune   DM             dm;
25162eca1d9cSJed Brown   PetscFunctionBegin;
2517089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2518089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
251924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
252024989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
25212eca1d9cSJed Brown   PetscFunctionReturn(0);
25222eca1d9cSJed Brown }
25232eca1d9cSJed Brown 
25246083293cSBarry Smith typedef struct {
25256083293cSBarry Smith   PetscViewer viewer;
25266083293cSBarry Smith   Vec         initialsolution;
25276083293cSBarry Smith   PetscBool   showinitial;
25286083293cSBarry Smith } TSMonitorSolutionCtx;
25296083293cSBarry Smith 
25302eca1d9cSJed Brown #undef __FUNCT__
2531a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
25321713a123SBarry Smith /*@C
2533a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
25341713a123SBarry Smith    VecView() for the solution at each timestep
25351713a123SBarry Smith 
25361713a123SBarry Smith    Collective on TS
25371713a123SBarry Smith 
25381713a123SBarry Smith    Input Parameters:
25391713a123SBarry Smith +  ts - the TS context
25401713a123SBarry Smith .  step - current time-step
2541142b95e3SSatish Balay .  ptime - current time
25421713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
25431713a123SBarry Smith 
25441713a123SBarry Smith    Level: intermediate
25451713a123SBarry Smith 
25461713a123SBarry Smith .keywords: TS,  vector, monitor, view
25471713a123SBarry Smith 
2548a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
25491713a123SBarry Smith @*/
25507087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
25511713a123SBarry Smith {
2552dfbe8321SBarry Smith   PetscErrorCode       ierr;
25536083293cSBarry Smith   TSMonitorSolutionCtx *ictx = (TSMonitorSolutionCtx*)dummy;
25541713a123SBarry Smith 
25551713a123SBarry Smith   PetscFunctionBegin;
25566083293cSBarry Smith   if (!step && ictx->showinitial) {
25576083293cSBarry Smith     if (!ictx->initialsolution) {
25586083293cSBarry Smith       ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr);
25591713a123SBarry Smith     }
25606083293cSBarry Smith     ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr);
25616083293cSBarry Smith   }
25626083293cSBarry Smith   if (ictx->showinitial) {
25636083293cSBarry Smith     PetscReal pause;
25646083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
25656083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
25666083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
25676083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
25686083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
25696083293cSBarry Smith   }
25706083293cSBarry Smith   ierr = VecView(x,ictx->viewer);CHKERRQ(ierr);
25716083293cSBarry Smith   if (ictx->showinitial) {
25726083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
25736083293cSBarry Smith   }
25741713a123SBarry Smith   PetscFunctionReturn(0);
25751713a123SBarry Smith }
25761713a123SBarry Smith 
25771713a123SBarry Smith 
25786c699258SBarry Smith #undef __FUNCT__
25796083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionDestroy"
25806083293cSBarry Smith /*@C
25816083293cSBarry Smith    TSMonitorSolutionDestroy - Destroys the monitor context for TSMonitorSolution
25826083293cSBarry Smith 
25836083293cSBarry Smith    Collective on TS
25846083293cSBarry Smith 
25856083293cSBarry Smith    Input Parameters:
25866083293cSBarry Smith .    ctx - the monitor context
25876083293cSBarry Smith 
25886083293cSBarry Smith    Level: intermediate
25896083293cSBarry Smith 
25906083293cSBarry Smith .keywords: TS,  vector, monitor, view
25916083293cSBarry Smith 
25926083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
25936083293cSBarry Smith @*/
25946083293cSBarry Smith PetscErrorCode  TSMonitorSolutionDestroy(void **ctx)
25956083293cSBarry Smith {
25966083293cSBarry Smith   PetscErrorCode       ierr;
25976083293cSBarry Smith   TSMonitorSolutionCtx *ictx = *(TSMonitorSolutionCtx**)ctx;
25986083293cSBarry Smith 
25996083293cSBarry Smith   PetscFunctionBegin;
26006083293cSBarry Smith   ierr = PetscViewerDestroy(&ictx->viewer);CHKERRQ(ierr);
26016083293cSBarry Smith   ierr = VecDestroy(&ictx->initialsolution);CHKERRQ(ierr);
26026083293cSBarry Smith   ierr = PetscFree(ictx);CHKERRQ(ierr);
26036083293cSBarry Smith   PetscFunctionReturn(0);
26046083293cSBarry Smith }
26056083293cSBarry Smith 
26066083293cSBarry Smith #undef __FUNCT__
26076083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionCreate"
26086083293cSBarry Smith /*@C
26096083293cSBarry Smith    TSMonitorSolutionCreate - Creates the monitor context for TSMonitorSolution
26106083293cSBarry Smith 
26116083293cSBarry Smith    Collective on TS
26126083293cSBarry Smith 
26136083293cSBarry Smith    Input Parameter:
26146083293cSBarry Smith .    ts - time-step context
26156083293cSBarry Smith 
26166083293cSBarry Smith    Output Patameter:
26176083293cSBarry Smith .    ctx - the monitor context
26186083293cSBarry Smith 
26196083293cSBarry Smith    Level: intermediate
26206083293cSBarry Smith 
26216083293cSBarry Smith .keywords: TS,  vector, monitor, view
26226083293cSBarry Smith 
26236083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
26246083293cSBarry Smith @*/
26256083293cSBarry Smith PetscErrorCode  TSMonitorSolutionCreate(TS ts,PetscViewer viewer,void **ctx)
26266083293cSBarry Smith {
26276083293cSBarry Smith   PetscErrorCode       ierr;
26286083293cSBarry Smith   TSMonitorSolutionCtx *ictx;
26296083293cSBarry Smith 
26306083293cSBarry Smith   PetscFunctionBegin;
26316083293cSBarry Smith   ierr = PetscNew(TSMonitorSolutionCtx,&ictx);CHKERRQ(ierr);
26326083293cSBarry Smith   *ctx = (void*)ictx;
26336083293cSBarry Smith   if (!viewer) {
26346083293cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
26356083293cSBarry Smith   }
26366083293cSBarry Smith   ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr);
26376083293cSBarry Smith   ictx->viewer      = viewer;
26386083293cSBarry Smith   ictx->showinitial = PETSC_FALSE;
26396083293cSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix,"-ts_monitor_solution_initial",&ictx->showinitial,PETSC_NULL);CHKERRQ(ierr);
26406083293cSBarry Smith   PetscFunctionReturn(0);
26416083293cSBarry Smith }
26426083293cSBarry Smith 
26436083293cSBarry Smith #undef __FUNCT__
26446c699258SBarry Smith #define __FUNCT__ "TSSetDM"
26456c699258SBarry Smith /*@
26466c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
26476c699258SBarry Smith 
26483f9fe445SBarry Smith    Logically Collective on TS and DM
26496c699258SBarry Smith 
26506c699258SBarry Smith    Input Parameters:
26516c699258SBarry Smith +  ts - the preconditioner context
26526c699258SBarry Smith -  dm - the dm
26536c699258SBarry Smith 
26546c699258SBarry Smith    Level: intermediate
26556c699258SBarry Smith 
26566c699258SBarry Smith 
26576c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
26586c699258SBarry Smith @*/
26597087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
26606c699258SBarry Smith {
26616c699258SBarry Smith   PetscErrorCode ierr;
2662089b2837SJed Brown   SNES           snes;
266324989b8cSPeter Brune   TSDM           tsdm;
26646c699258SBarry Smith 
26656c699258SBarry Smith   PetscFunctionBegin;
26660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
266770663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
266824989b8cSPeter Brune   if (ts->dm) {               /* Move the TSDM context over to the new DM unless the new DM already has one */
266924989b8cSPeter Brune     PetscContainer oldcontainer,container;
267024989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr);
267124989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr);
267224989b8cSPeter Brune     if (oldcontainer && !container) {
267324989b8cSPeter Brune       ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr);
267424989b8cSPeter Brune       ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr);
267524989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
267624989b8cSPeter Brune         tsdm->originaldm = dm;
267724989b8cSPeter Brune       }
267824989b8cSPeter Brune     }
26796bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
268024989b8cSPeter Brune   }
26816c699258SBarry Smith   ts->dm = dm;
2682089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2683089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
26846c699258SBarry Smith   PetscFunctionReturn(0);
26856c699258SBarry Smith }
26866c699258SBarry Smith 
26876c699258SBarry Smith #undef __FUNCT__
26886c699258SBarry Smith #define __FUNCT__ "TSGetDM"
26896c699258SBarry Smith /*@
26906c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
26916c699258SBarry Smith 
26923f9fe445SBarry Smith    Not Collective
26936c699258SBarry Smith 
26946c699258SBarry Smith    Input Parameter:
26956c699258SBarry Smith . ts - the preconditioner context
26966c699258SBarry Smith 
26976c699258SBarry Smith    Output Parameter:
26986c699258SBarry Smith .  dm - the dm
26996c699258SBarry Smith 
27006c699258SBarry Smith    Level: intermediate
27016c699258SBarry Smith 
27026c699258SBarry Smith 
27036c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
27046c699258SBarry Smith @*/
27057087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
27066c699258SBarry Smith {
2707496e6a7aSJed Brown   PetscErrorCode ierr;
2708496e6a7aSJed Brown 
27096c699258SBarry Smith   PetscFunctionBegin;
27100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2711496e6a7aSJed Brown   if (!ts->dm) {
2712496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
2713496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
2714496e6a7aSJed Brown   }
27156c699258SBarry Smith   *dm = ts->dm;
27166c699258SBarry Smith   PetscFunctionReturn(0);
27176c699258SBarry Smith }
27181713a123SBarry Smith 
27190f5c6efeSJed Brown #undef __FUNCT__
27200f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
27210f5c6efeSJed Brown /*@
27220f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
27230f5c6efeSJed Brown 
27243f9fe445SBarry Smith    Logically Collective on SNES
27250f5c6efeSJed Brown 
27260f5c6efeSJed Brown    Input Parameter:
2727d42a1c89SJed Brown + snes - nonlinear solver
27280f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2729d42a1c89SJed Brown - ctx - user context, must be a TS
27300f5c6efeSJed Brown 
27310f5c6efeSJed Brown    Output Parameter:
27320f5c6efeSJed Brown . F - the nonlinear residual
27330f5c6efeSJed Brown 
27340f5c6efeSJed Brown    Notes:
27350f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
27360f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
27370f5c6efeSJed Brown 
27380f5c6efeSJed Brown    Level: advanced
27390f5c6efeSJed Brown 
27400f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
27410f5c6efeSJed Brown @*/
27427087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
27430f5c6efeSJed Brown {
27440f5c6efeSJed Brown   TS ts = (TS)ctx;
27450f5c6efeSJed Brown   PetscErrorCode ierr;
27460f5c6efeSJed Brown 
27470f5c6efeSJed Brown   PetscFunctionBegin;
27480f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27490f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
27500f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
27510f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
27520f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
27530f5c6efeSJed Brown   PetscFunctionReturn(0);
27540f5c6efeSJed Brown }
27550f5c6efeSJed Brown 
27560f5c6efeSJed Brown #undef __FUNCT__
27570f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
27580f5c6efeSJed Brown /*@
27590f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
27600f5c6efeSJed Brown 
27610f5c6efeSJed Brown    Collective on SNES
27620f5c6efeSJed Brown 
27630f5c6efeSJed Brown    Input Parameter:
27640f5c6efeSJed Brown + snes - nonlinear solver
27650f5c6efeSJed Brown . X - the current state at which to evaluate the residual
27660f5c6efeSJed Brown - ctx - user context, must be a TS
27670f5c6efeSJed Brown 
27680f5c6efeSJed Brown    Output Parameter:
27690f5c6efeSJed Brown + A - the Jacobian
27700f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
27710f5c6efeSJed Brown - flag - indicates any structure change in the matrix
27720f5c6efeSJed Brown 
27730f5c6efeSJed Brown    Notes:
27740f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
27750f5c6efeSJed Brown 
27760f5c6efeSJed Brown    Level: developer
27770f5c6efeSJed Brown 
27780f5c6efeSJed Brown .seealso: SNESSetJacobian()
27790f5c6efeSJed Brown @*/
27807087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
27810f5c6efeSJed Brown {
27820f5c6efeSJed Brown   TS ts = (TS)ctx;
27830f5c6efeSJed Brown   PetscErrorCode ierr;
27840f5c6efeSJed Brown 
27850f5c6efeSJed Brown   PetscFunctionBegin;
27860f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27870f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
27880f5c6efeSJed Brown   PetscValidPointer(A,3);
27890f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
27900f5c6efeSJed Brown   PetscValidPointer(B,4);
27910f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
27920f5c6efeSJed Brown   PetscValidPointer(flag,5);
27930f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
27940f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
27950f5c6efeSJed Brown   PetscFunctionReturn(0);
27960f5c6efeSJed Brown }
2797325fc9f4SBarry Smith 
27980e4ef248SJed Brown #undef __FUNCT__
27990e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
28000e4ef248SJed Brown /*@C
28010e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
28020e4ef248SJed Brown 
28030e4ef248SJed Brown    Collective on TS
28040e4ef248SJed Brown 
28050e4ef248SJed Brown    Input Arguments:
28060e4ef248SJed Brown +  ts - time stepping context
28070e4ef248SJed Brown .  t - time at which to evaluate
28080e4ef248SJed Brown .  X - state at which to evaluate
28090e4ef248SJed Brown -  ctx - context
28100e4ef248SJed Brown 
28110e4ef248SJed Brown    Output Arguments:
28120e4ef248SJed Brown .  F - right hand side
28130e4ef248SJed Brown 
28140e4ef248SJed Brown    Level: intermediate
28150e4ef248SJed Brown 
28160e4ef248SJed Brown    Notes:
28170e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
28180e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
28190e4ef248SJed Brown 
28200e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
28210e4ef248SJed Brown @*/
28220e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx)
28230e4ef248SJed Brown {
28240e4ef248SJed Brown   PetscErrorCode ierr;
28250e4ef248SJed Brown   Mat Arhs,Brhs;
28260e4ef248SJed Brown   MatStructure flg2;
28270e4ef248SJed Brown 
28280e4ef248SJed Brown   PetscFunctionBegin;
28290e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
28300e4ef248SJed Brown   ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
28310e4ef248SJed Brown   ierr = MatMult(Arhs,X,F);CHKERRQ(ierr);
28320e4ef248SJed Brown   PetscFunctionReturn(0);
28330e4ef248SJed Brown }
28340e4ef248SJed Brown 
28350e4ef248SJed Brown #undef __FUNCT__
28360e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
28370e4ef248SJed Brown /*@C
28380e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
28390e4ef248SJed Brown 
28400e4ef248SJed Brown    Collective on TS
28410e4ef248SJed Brown 
28420e4ef248SJed Brown    Input Arguments:
28430e4ef248SJed Brown +  ts - time stepping context
28440e4ef248SJed Brown .  t - time at which to evaluate
28450e4ef248SJed Brown .  X - state at which to evaluate
28460e4ef248SJed Brown -  ctx - context
28470e4ef248SJed Brown 
28480e4ef248SJed Brown    Output Arguments:
28490e4ef248SJed Brown +  A - pointer to operator
28500e4ef248SJed Brown .  B - pointer to preconditioning matrix
28510e4ef248SJed Brown -  flg - matrix structure flag
28520e4ef248SJed Brown 
28530e4ef248SJed Brown    Level: intermediate
28540e4ef248SJed Brown 
28550e4ef248SJed Brown    Notes:
28560e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
28570e4ef248SJed Brown 
28580e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
28590e4ef248SJed Brown @*/
28600e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx)
28610e4ef248SJed Brown {
28620e4ef248SJed Brown 
28630e4ef248SJed Brown   PetscFunctionBegin;
28640e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
28650e4ef248SJed Brown   PetscFunctionReturn(0);
28660e4ef248SJed Brown }
28670e4ef248SJed Brown 
28680026cea9SSean Farley #undef __FUNCT__
28690026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
28700026cea9SSean Farley /*@C
28710026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
28720026cea9SSean Farley 
28730026cea9SSean Farley    Collective on TS
28740026cea9SSean Farley 
28750026cea9SSean Farley    Input Arguments:
28760026cea9SSean Farley +  ts - time stepping context
28770026cea9SSean Farley .  t - time at which to evaluate
28780026cea9SSean Farley .  X - state at which to evaluate
28790026cea9SSean Farley .  Xdot - time derivative of state vector
28800026cea9SSean Farley -  ctx - context
28810026cea9SSean Farley 
28820026cea9SSean Farley    Output Arguments:
28830026cea9SSean Farley .  F - left hand side
28840026cea9SSean Farley 
28850026cea9SSean Farley    Level: intermediate
28860026cea9SSean Farley 
28870026cea9SSean Farley    Notes:
28880026cea9SSean Farley    The assumption here is that the left hand side is of the form A*Xdot (and not A*Xdot + B*X). For other cases, the
28890026cea9SSean Farley    user is required to write their own TSComputeIFunction.
28900026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
28910026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
28920026cea9SSean Farley 
28930026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
28940026cea9SSean Farley @*/
28950026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx)
28960026cea9SSean Farley {
28970026cea9SSean Farley   PetscErrorCode ierr;
28980026cea9SSean Farley   Mat A,B;
28990026cea9SSean Farley   MatStructure flg2;
29000026cea9SSean Farley 
29010026cea9SSean Farley   PetscFunctionBegin;
29020026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
29030026cea9SSean Farley   ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
29040026cea9SSean Farley   ierr = MatMult(A,Xdot,F);CHKERRQ(ierr);
29050026cea9SSean Farley   PetscFunctionReturn(0);
29060026cea9SSean Farley }
29070026cea9SSean Farley 
29080026cea9SSean Farley #undef __FUNCT__
29090026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
29100026cea9SSean Farley /*@C
291124e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
29120026cea9SSean Farley 
29130026cea9SSean Farley    Collective on TS
29140026cea9SSean Farley 
29150026cea9SSean Farley    Input Arguments:
29160026cea9SSean Farley +  ts - time stepping context
29170026cea9SSean Farley .  t - time at which to evaluate
29180026cea9SSean Farley .  X - state at which to evaluate
29190026cea9SSean Farley .  Xdot - time derivative of state vector
29200026cea9SSean Farley .  shift - shift to apply
29210026cea9SSean Farley -  ctx - context
29220026cea9SSean Farley 
29230026cea9SSean Farley    Output Arguments:
29240026cea9SSean Farley +  A - pointer to operator
29250026cea9SSean Farley .  B - pointer to preconditioning matrix
29260026cea9SSean Farley -  flg - matrix structure flag
29270026cea9SSean Farley 
29280026cea9SSean Farley    Level: intermediate
29290026cea9SSean Farley 
29300026cea9SSean Farley    Notes:
29310026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
29320026cea9SSean Farley 
29330026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
29340026cea9SSean Farley @*/
29350026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
29360026cea9SSean Farley {
29370026cea9SSean Farley 
29380026cea9SSean Farley   PetscFunctionBegin;
29390026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
29400026cea9SSean Farley   PetscFunctionReturn(0);
29410026cea9SSean Farley }
29420026cea9SSean Farley 
29434af1b03aSJed Brown 
29444af1b03aSJed Brown #undef __FUNCT__
29454af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
29464af1b03aSJed Brown /*@
29474af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
29484af1b03aSJed Brown 
29494af1b03aSJed Brown    Not Collective
29504af1b03aSJed Brown 
29514af1b03aSJed Brown    Input Parameter:
29524af1b03aSJed Brown .  ts - the TS context
29534af1b03aSJed Brown 
29544af1b03aSJed Brown    Output Parameter:
29554af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
29564af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
29574af1b03aSJed Brown 
29584af1b03aSJed Brown    Level: intermediate
29594af1b03aSJed Brown 
2960cd652676SJed Brown    Notes:
2961cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
29624af1b03aSJed Brown 
29634af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
29644af1b03aSJed Brown 
29654af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
29664af1b03aSJed Brown @*/
29674af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
29684af1b03aSJed Brown {
29694af1b03aSJed Brown   PetscFunctionBegin;
29704af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29714af1b03aSJed Brown   PetscValidPointer(reason,2);
29724af1b03aSJed Brown   *reason = ts->reason;
29734af1b03aSJed Brown   PetscFunctionReturn(0);
29744af1b03aSJed Brown }
29754af1b03aSJed Brown 
2976fb1732b5SBarry Smith #undef __FUNCT__
29775ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
29789f67acb7SJed Brown /*@
29795ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
29809f67acb7SJed Brown    used by the time integrator.
29819f67acb7SJed Brown 
29829f67acb7SJed Brown    Not Collective
29839f67acb7SJed Brown 
29849f67acb7SJed Brown    Input Parameter:
29859f67acb7SJed Brown .  ts - TS context
29869f67acb7SJed Brown 
29879f67acb7SJed Brown    Output Parameter:
29889f67acb7SJed Brown .  nits - number of nonlinear iterations
29899f67acb7SJed Brown 
29909f67acb7SJed Brown    Notes:
29919f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
29929f67acb7SJed Brown 
29939f67acb7SJed Brown    Level: intermediate
29949f67acb7SJed Brown 
29959f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
29969f67acb7SJed Brown 
29975ef26d82SJed Brown .seealso:  TSGetKSPIterations()
29989f67acb7SJed Brown @*/
29995ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
30009f67acb7SJed Brown {
30019f67acb7SJed Brown   PetscFunctionBegin;
30029f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30039f67acb7SJed Brown   PetscValidIntPointer(nits,2);
30045ef26d82SJed Brown   *nits = ts->snes_its;
30059f67acb7SJed Brown   PetscFunctionReturn(0);
30069f67acb7SJed Brown }
30079f67acb7SJed Brown 
30089f67acb7SJed Brown #undef __FUNCT__
30095ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
30109f67acb7SJed Brown /*@
30115ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
30129f67acb7SJed Brown    used by the time integrator.
30139f67acb7SJed Brown 
30149f67acb7SJed Brown    Not Collective
30159f67acb7SJed Brown 
30169f67acb7SJed Brown    Input Parameter:
30179f67acb7SJed Brown .  ts - TS context
30189f67acb7SJed Brown 
30199f67acb7SJed Brown    Output Parameter:
30209f67acb7SJed Brown .  lits - number of linear iterations
30219f67acb7SJed Brown 
30229f67acb7SJed Brown    Notes:
30239f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
30249f67acb7SJed Brown 
30259f67acb7SJed Brown    Level: intermediate
30269f67acb7SJed Brown 
30279f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
30289f67acb7SJed Brown 
30295ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
30309f67acb7SJed Brown @*/
30315ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
30329f67acb7SJed Brown {
30339f67acb7SJed Brown   PetscFunctionBegin;
30349f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30359f67acb7SJed Brown   PetscValidIntPointer(lits,2);
30365ef26d82SJed Brown   *lits = ts->ksp_its;
30379f67acb7SJed Brown   PetscFunctionReturn(0);
30389f67acb7SJed Brown }
30399f67acb7SJed Brown 
30409f67acb7SJed Brown #undef __FUNCT__
3041cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3042cef5090cSJed Brown /*@
3043cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3044cef5090cSJed Brown 
3045cef5090cSJed Brown    Not Collective
3046cef5090cSJed Brown 
3047cef5090cSJed Brown    Input Parameter:
3048cef5090cSJed Brown .  ts - TS context
3049cef5090cSJed Brown 
3050cef5090cSJed Brown    Output Parameter:
3051cef5090cSJed Brown .  rejects - number of steps rejected
3052cef5090cSJed Brown 
3053cef5090cSJed Brown    Notes:
3054cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3055cef5090cSJed Brown 
3056cef5090cSJed Brown    Level: intermediate
3057cef5090cSJed Brown 
3058cef5090cSJed Brown .keywords: TS, get, number
3059cef5090cSJed Brown 
30605ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3061cef5090cSJed Brown @*/
3062cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3063cef5090cSJed Brown {
3064cef5090cSJed Brown   PetscFunctionBegin;
3065cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3066cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3067cef5090cSJed Brown   *rejects = ts->reject;
3068cef5090cSJed Brown   PetscFunctionReturn(0);
3069cef5090cSJed Brown }
3070cef5090cSJed Brown 
3071cef5090cSJed Brown #undef __FUNCT__
3072cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3073cef5090cSJed Brown /*@
3074cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3075cef5090cSJed Brown 
3076cef5090cSJed Brown    Not Collective
3077cef5090cSJed Brown 
3078cef5090cSJed Brown    Input Parameter:
3079cef5090cSJed Brown .  ts - TS context
3080cef5090cSJed Brown 
3081cef5090cSJed Brown    Output Parameter:
3082cef5090cSJed Brown .  fails - number of failed nonlinear solves
3083cef5090cSJed Brown 
3084cef5090cSJed Brown    Notes:
3085cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3086cef5090cSJed Brown 
3087cef5090cSJed Brown    Level: intermediate
3088cef5090cSJed Brown 
3089cef5090cSJed Brown .keywords: TS, get, number
3090cef5090cSJed Brown 
30915ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3092cef5090cSJed Brown @*/
3093cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3094cef5090cSJed Brown {
3095cef5090cSJed Brown   PetscFunctionBegin;
3096cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3097cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3098cef5090cSJed Brown   *fails = ts->num_snes_failures;
3099cef5090cSJed Brown   PetscFunctionReturn(0);
3100cef5090cSJed Brown }
3101cef5090cSJed Brown 
3102cef5090cSJed Brown #undef __FUNCT__
3103cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3104cef5090cSJed Brown /*@
3105cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3106cef5090cSJed Brown 
3107cef5090cSJed Brown    Not Collective
3108cef5090cSJed Brown 
3109cef5090cSJed Brown    Input Parameter:
3110cef5090cSJed Brown +  ts - TS context
3111cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3112cef5090cSJed Brown 
3113cef5090cSJed Brown    Notes:
3114cef5090cSJed Brown    The counter is reset to zero for each step
3115cef5090cSJed Brown 
3116cef5090cSJed Brown    Options Database Key:
3117cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3118cef5090cSJed Brown 
3119cef5090cSJed Brown    Level: intermediate
3120cef5090cSJed Brown 
3121cef5090cSJed Brown .keywords: TS, set, maximum, number
3122cef5090cSJed Brown 
31235ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3124cef5090cSJed Brown @*/
3125cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3126cef5090cSJed Brown {
3127cef5090cSJed Brown   PetscFunctionBegin;
3128cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3129cef5090cSJed Brown   ts->max_reject = rejects;
3130cef5090cSJed Brown   PetscFunctionReturn(0);
3131cef5090cSJed Brown }
3132cef5090cSJed Brown 
3133cef5090cSJed Brown #undef __FUNCT__
3134cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3135cef5090cSJed Brown /*@
3136cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3137cef5090cSJed Brown 
3138cef5090cSJed Brown    Not Collective
3139cef5090cSJed Brown 
3140cef5090cSJed Brown    Input Parameter:
3141cef5090cSJed Brown +  ts - TS context
3142cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3143cef5090cSJed Brown 
3144cef5090cSJed Brown    Notes:
3145cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3146cef5090cSJed Brown 
3147cef5090cSJed Brown    Options Database Key:
3148cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3149cef5090cSJed Brown 
3150cef5090cSJed Brown    Level: intermediate
3151cef5090cSJed Brown 
3152cef5090cSJed Brown .keywords: TS, set, maximum, number
3153cef5090cSJed Brown 
31545ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3155cef5090cSJed Brown @*/
3156cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3157cef5090cSJed Brown {
3158cef5090cSJed Brown   PetscFunctionBegin;
3159cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3160cef5090cSJed Brown   ts->max_snes_failures = fails;
3161cef5090cSJed Brown   PetscFunctionReturn(0);
3162cef5090cSJed Brown }
3163cef5090cSJed Brown 
3164cef5090cSJed Brown #undef __FUNCT__
3165cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3166cef5090cSJed Brown /*@
3167cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3168cef5090cSJed Brown 
3169cef5090cSJed Brown    Not Collective
3170cef5090cSJed Brown 
3171cef5090cSJed Brown    Input Parameter:
3172cef5090cSJed Brown +  ts - TS context
3173cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3174cef5090cSJed Brown 
3175cef5090cSJed Brown    Options Database Key:
3176cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3177cef5090cSJed Brown 
3178cef5090cSJed Brown    Level: intermediate
3179cef5090cSJed Brown 
3180cef5090cSJed Brown .keywords: TS, set, error
3181cef5090cSJed Brown 
31825ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3183cef5090cSJed Brown @*/
3184cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3185cef5090cSJed Brown {
3186cef5090cSJed Brown   PetscFunctionBegin;
3187cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3188cef5090cSJed Brown   ts->errorifstepfailed = err;
3189cef5090cSJed Brown   PetscFunctionReturn(0);
3190cef5090cSJed Brown }
3191cef5090cSJed Brown 
3192cef5090cSJed Brown #undef __FUNCT__
3193fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3194fb1732b5SBarry Smith /*@C
3195fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3196fb1732b5SBarry Smith 
3197fb1732b5SBarry Smith    Collective on TS
3198fb1732b5SBarry Smith 
3199fb1732b5SBarry Smith    Input Parameters:
3200fb1732b5SBarry Smith +  ts - the TS context
3201fb1732b5SBarry Smith .  step - current time-step
3202fb1732b5SBarry Smith .  ptime - current time
3203ed81e22dSJed Brown .  x - current state
3204fb1732b5SBarry Smith -  viewer - binary viewer
3205fb1732b5SBarry Smith 
3206fb1732b5SBarry Smith    Level: intermediate
3207fb1732b5SBarry Smith 
3208fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3209fb1732b5SBarry Smith 
3210fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3211fb1732b5SBarry Smith @*/
3212ed81e22dSJed Brown PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec x,void *viewer)
3213fb1732b5SBarry Smith {
3214fb1732b5SBarry Smith   PetscErrorCode       ierr;
3215ed81e22dSJed Brown   PetscViewer          v = (PetscViewer)viewer;
3216fb1732b5SBarry Smith 
3217fb1732b5SBarry Smith   PetscFunctionBegin;
3218ed81e22dSJed Brown   ierr = VecView(x,v);CHKERRQ(ierr);
3219ed81e22dSJed Brown   PetscFunctionReturn(0);
3220ed81e22dSJed Brown }
3221ed81e22dSJed Brown 
3222ed81e22dSJed Brown #undef __FUNCT__
3223ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3224ed81e22dSJed Brown /*@C
3225ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3226ed81e22dSJed Brown 
3227ed81e22dSJed Brown    Collective on TS
3228ed81e22dSJed Brown 
3229ed81e22dSJed Brown    Input Parameters:
3230ed81e22dSJed Brown +  ts - the TS context
3231ed81e22dSJed Brown .  step - current time-step
3232ed81e22dSJed Brown .  ptime - current time
3233ed81e22dSJed Brown .  x - current state
3234ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3235ed81e22dSJed Brown 
3236ed81e22dSJed Brown    Level: intermediate
3237ed81e22dSJed Brown 
3238ed81e22dSJed Brown    Notes:
3239ed81e22dSJed 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.
3240ed81e22dSJed Brown    These are named according to the file name template.
3241ed81e22dSJed Brown 
3242ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3243ed81e22dSJed Brown 
3244ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3245ed81e22dSJed Brown 
3246ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3247ed81e22dSJed Brown @*/
3248ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec x,void *filenametemplate)
3249ed81e22dSJed Brown {
3250ed81e22dSJed Brown   PetscErrorCode ierr;
3251ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3252ed81e22dSJed Brown   PetscViewer    viewer;
3253ed81e22dSJed Brown 
3254ed81e22dSJed Brown   PetscFunctionBegin;
32558caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3256ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
3257fb1732b5SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
3258ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3259ed81e22dSJed Brown   PetscFunctionReturn(0);
3260ed81e22dSJed Brown }
3261ed81e22dSJed Brown 
3262ed81e22dSJed Brown #undef __FUNCT__
3263ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3264ed81e22dSJed Brown /*@C
3265ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3266ed81e22dSJed Brown 
3267ed81e22dSJed Brown    Collective on TS
3268ed81e22dSJed Brown 
3269ed81e22dSJed Brown    Input Parameters:
3270ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3271ed81e22dSJed Brown 
3272ed81e22dSJed Brown    Level: intermediate
3273ed81e22dSJed Brown 
3274ed81e22dSJed Brown    Note:
3275ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3276ed81e22dSJed Brown 
3277ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3278ed81e22dSJed Brown 
3279ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3280ed81e22dSJed Brown @*/
3281ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3282ed81e22dSJed Brown {
3283ed81e22dSJed Brown   PetscErrorCode ierr;
3284ed81e22dSJed Brown 
3285ed81e22dSJed Brown   PetscFunctionBegin;
3286ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3287fb1732b5SBarry Smith   PetscFunctionReturn(0);
3288fb1732b5SBarry Smith }
3289fb1732b5SBarry Smith 
329084df9cb4SJed Brown #undef __FUNCT__
329184df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt"
329284df9cb4SJed Brown /*@
329384df9cb4SJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
329484df9cb4SJed Brown 
3295ed81e22dSJed Brown    Collective on TS if controller has not been created yet
329684df9cb4SJed Brown 
329784df9cb4SJed Brown    Input Arguments:
3298ed81e22dSJed Brown .  ts - time stepping context
329984df9cb4SJed Brown 
330084df9cb4SJed Brown    Output Arguments:
3301ed81e22dSJed Brown .  adapt - adaptive controller
330284df9cb4SJed Brown 
330384df9cb4SJed Brown    Level: intermediate
330484df9cb4SJed Brown 
3305ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
330684df9cb4SJed Brown @*/
330784df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
330884df9cb4SJed Brown {
330984df9cb4SJed Brown   PetscErrorCode ierr;
331084df9cb4SJed Brown 
331184df9cb4SJed Brown   PetscFunctionBegin;
331284df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
331384df9cb4SJed Brown   PetscValidPointer(adapt,2);
331484df9cb4SJed Brown   if (!ts->adapt) {
331584df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
33161c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
33171c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
331884df9cb4SJed Brown   }
331984df9cb4SJed Brown   *adapt = ts->adapt;
332084df9cb4SJed Brown   PetscFunctionReturn(0);
332184df9cb4SJed Brown }
3322d6ebe24aSShri Abhyankar 
3323d6ebe24aSShri Abhyankar #undef __FUNCT__
33241c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
33251c3436cfSJed Brown /*@
33261c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
33271c3436cfSJed Brown 
33281c3436cfSJed Brown    Logically Collective
33291c3436cfSJed Brown 
33301c3436cfSJed Brown    Input Arguments:
33311c3436cfSJed Brown +  ts - time integration context
33321c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
33331c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
33341c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
33351c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
33361c3436cfSJed Brown 
33371c3436cfSJed Brown    Level: beginner
33381c3436cfSJed Brown 
3339c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
33401c3436cfSJed Brown @*/
33411c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
33421c3436cfSJed Brown {
33431c3436cfSJed Brown   PetscErrorCode ierr;
33441c3436cfSJed Brown 
33451c3436cfSJed Brown   PetscFunctionBegin;
3346c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
33471c3436cfSJed Brown   if (vatol) {
33481c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
33491c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
33501c3436cfSJed Brown     ts->vatol = vatol;
33511c3436cfSJed Brown   }
3352c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
33531c3436cfSJed Brown   if (vrtol) {
33541c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
33551c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
33561c3436cfSJed Brown     ts->vrtol = vrtol;
33571c3436cfSJed Brown   }
33581c3436cfSJed Brown   PetscFunctionReturn(0);
33591c3436cfSJed Brown }
33601c3436cfSJed Brown 
33611c3436cfSJed Brown #undef __FUNCT__
3362c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3363c5033834SJed Brown /*@
3364c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3365c5033834SJed Brown 
3366c5033834SJed Brown    Logically Collective
3367c5033834SJed Brown 
3368c5033834SJed Brown    Input Arguments:
3369c5033834SJed Brown .  ts - time integration context
3370c5033834SJed Brown 
3371c5033834SJed Brown    Output Arguments:
3372c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3373c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3374c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3375c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3376c5033834SJed Brown 
3377c5033834SJed Brown    Level: beginner
3378c5033834SJed Brown 
3379c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3380c5033834SJed Brown @*/
3381c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3382c5033834SJed Brown {
3383c5033834SJed Brown 
3384c5033834SJed Brown   PetscFunctionBegin;
3385c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3386c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3387c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3388c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3389c5033834SJed Brown   PetscFunctionReturn(0);
3390c5033834SJed Brown }
3391c5033834SJed Brown 
3392c5033834SJed Brown #undef __FUNCT__
33931c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
33941c3436cfSJed Brown /*@
33951c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
33961c3436cfSJed Brown 
33971c3436cfSJed Brown    Collective on TS
33981c3436cfSJed Brown 
33991c3436cfSJed Brown    Input Arguments:
34001c3436cfSJed Brown +  ts - time stepping context
34011c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
34021c3436cfSJed Brown 
34031c3436cfSJed Brown    Output Arguments:
34041c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
34051c3436cfSJed Brown 
34061c3436cfSJed Brown    Level: developer
34071c3436cfSJed Brown 
34081c3436cfSJed Brown .seealso: TSSetTolerances()
34091c3436cfSJed Brown @*/
34101c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
34111c3436cfSJed Brown {
34121c3436cfSJed Brown   PetscErrorCode ierr;
34131c3436cfSJed Brown   PetscInt i,n,N;
34141c3436cfSJed Brown   const PetscScalar *x,*y;
34151c3436cfSJed Brown   Vec X;
34161c3436cfSJed Brown   PetscReal sum,gsum;
34171c3436cfSJed Brown 
34181c3436cfSJed Brown   PetscFunctionBegin;
34191c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34201c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
34211c3436cfSJed Brown   PetscValidPointer(norm,3);
34221c3436cfSJed Brown   X = ts->vec_sol;
34231c3436cfSJed Brown   PetscCheckSameTypeAndComm(X,1,Y,2);
34241c3436cfSJed Brown   if (X == Y) SETERRQ(((PetscObject)X)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
34251c3436cfSJed Brown 
34261c3436cfSJed Brown   ierr = VecGetSize(X,&N);CHKERRQ(ierr);
34271c3436cfSJed Brown   ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr);
34281c3436cfSJed Brown   ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr);
34291c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
34301c3436cfSJed Brown   sum = 0.;
3431ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3432ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3433ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3434ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3435ceefc113SJed Brown     for (i=0; i<n; i++) {
3436ceefc113SJed Brown       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3437ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3438ceefc113SJed Brown     }
3439ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3440ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3441ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3442ceefc113SJed Brown     const PetscScalar *atol;
3443ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3444ceefc113SJed Brown     for (i=0; i<n; i++) {
3445ceefc113SJed Brown       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3446ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3447ceefc113SJed Brown     }
3448ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3449ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3450ceefc113SJed Brown     const PetscScalar *rtol;
3451ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3452ceefc113SJed Brown     for (i=0; i<n; i++) {
3453ceefc113SJed Brown       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3454ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3455ceefc113SJed Brown     }
3456ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3457ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
34581c3436cfSJed Brown     for (i=0; i<n; i++) {
34591c3436cfSJed Brown       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
34601c3436cfSJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
34611c3436cfSJed Brown     }
3462ceefc113SJed Brown   }
34631c3436cfSJed Brown   ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr);
34641c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
34651c3436cfSJed Brown 
34661c3436cfSJed Brown   ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
34671c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
34681c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
34691c3436cfSJed Brown   PetscFunctionReturn(0);
34701c3436cfSJed Brown }
34711c3436cfSJed Brown 
34721c3436cfSJed Brown #undef __FUNCT__
34738d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
34748d59e960SJed Brown /*@
34758d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
34768d59e960SJed Brown 
34778d59e960SJed Brown    Logically Collective on TS
34788d59e960SJed Brown 
34798d59e960SJed Brown    Input Arguments:
34808d59e960SJed Brown +  ts - time stepping context
34818d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
34828d59e960SJed Brown 
34838d59e960SJed Brown    Note:
34848d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
34858d59e960SJed Brown 
34868d59e960SJed Brown    Level: intermediate
34878d59e960SJed Brown 
34888d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
34898d59e960SJed Brown @*/
34908d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
34918d59e960SJed Brown {
34928d59e960SJed Brown 
34938d59e960SJed Brown   PetscFunctionBegin;
34948d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34958d59e960SJed Brown   ts->cfltime_local = cfltime;
34968d59e960SJed Brown   ts->cfltime = -1.;
34978d59e960SJed Brown   PetscFunctionReturn(0);
34988d59e960SJed Brown }
34998d59e960SJed Brown 
35008d59e960SJed Brown #undef __FUNCT__
35018d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
35028d59e960SJed Brown /*@
35038d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
35048d59e960SJed Brown 
35058d59e960SJed Brown    Collective on TS
35068d59e960SJed Brown 
35078d59e960SJed Brown    Input Arguments:
35088d59e960SJed Brown .  ts - time stepping context
35098d59e960SJed Brown 
35108d59e960SJed Brown    Output Arguments:
35118d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
35128d59e960SJed Brown 
35138d59e960SJed Brown    Level: advanced
35148d59e960SJed Brown 
35158d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
35168d59e960SJed Brown @*/
35178d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
35188d59e960SJed Brown {
35198d59e960SJed Brown   PetscErrorCode ierr;
35208d59e960SJed Brown 
35218d59e960SJed Brown   PetscFunctionBegin;
35228d59e960SJed Brown   if (ts->cfltime < 0) {
35238d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
35248d59e960SJed Brown   }
35258d59e960SJed Brown   *cfltime = ts->cfltime;
35268d59e960SJed Brown   PetscFunctionReturn(0);
35278d59e960SJed Brown }
35288d59e960SJed Brown 
35298d59e960SJed Brown #undef __FUNCT__
3530d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
3531d6ebe24aSShri Abhyankar /*@
3532d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
3533d6ebe24aSShri Abhyankar 
3534d6ebe24aSShri Abhyankar    Input Parameters:
3535d6ebe24aSShri Abhyankar .  ts   - the TS context.
3536d6ebe24aSShri Abhyankar .  xl   - lower bound.
3537d6ebe24aSShri Abhyankar .  xu   - upper bound.
3538d6ebe24aSShri Abhyankar 
3539d6ebe24aSShri Abhyankar    Notes:
3540d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
354133c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
3542d6ebe24aSShri Abhyankar 
35432bd2b0e6SSatish Balay    Level: advanced
35442bd2b0e6SSatish Balay 
3545d6ebe24aSShri Abhyankar @*/
3546d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
3547d6ebe24aSShri Abhyankar {
3548d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
3549d6ebe24aSShri Abhyankar   SNES           snes;
3550d6ebe24aSShri Abhyankar 
3551d6ebe24aSShri Abhyankar   PetscFunctionBegin;
3552d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3553d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
3554d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
3555d6ebe24aSShri Abhyankar }
3556d6ebe24aSShri Abhyankar 
3557325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3558c6db04a5SJed Brown #include <mex.h>
3559325fc9f4SBarry Smith 
3560325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
3561325fc9f4SBarry Smith 
3562325fc9f4SBarry Smith #undef __FUNCT__
3563325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
3564325fc9f4SBarry Smith /*
3565325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
3566325fc9f4SBarry Smith                          TSSetFunctionMatlab().
3567325fc9f4SBarry Smith 
3568325fc9f4SBarry Smith    Collective on TS
3569325fc9f4SBarry Smith 
3570325fc9f4SBarry Smith    Input Parameters:
3571325fc9f4SBarry Smith +  snes - the TS context
3572325fc9f4SBarry Smith -  x - input vector
3573325fc9f4SBarry Smith 
3574325fc9f4SBarry Smith    Output Parameter:
3575325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
3576325fc9f4SBarry Smith 
3577325fc9f4SBarry Smith    Notes:
3578325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
3579325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
3580325fc9f4SBarry Smith    themselves.
3581325fc9f4SBarry Smith 
3582325fc9f4SBarry Smith    Level: developer
3583325fc9f4SBarry Smith 
3584325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3585325fc9f4SBarry Smith 
3586325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3587325fc9f4SBarry Smith */
35887087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
3589325fc9f4SBarry Smith {
3590325fc9f4SBarry Smith   PetscErrorCode   ierr;
3591325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3592325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
3593325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
3594325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
3595325fc9f4SBarry Smith 
3596325fc9f4SBarry Smith   PetscFunctionBegin;
3597325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
3598325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3599325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
3600325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
3601325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
3602325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
3603325fc9f4SBarry Smith 
3604325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3605325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3606880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
3607325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
3608325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3609325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
3610325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3611325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3612325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
3613325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
3614325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
3615325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
3616325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3617325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3618325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3619325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3620325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3621325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3622325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3623325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3624325fc9f4SBarry Smith   PetscFunctionReturn(0);
3625325fc9f4SBarry Smith }
3626325fc9f4SBarry Smith 
3627325fc9f4SBarry Smith 
3628325fc9f4SBarry Smith #undef __FUNCT__
3629325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
3630325fc9f4SBarry Smith /*
3631325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
3632325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
3633e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
3634325fc9f4SBarry Smith 
3635325fc9f4SBarry Smith    Logically Collective on TS
3636325fc9f4SBarry Smith 
3637325fc9f4SBarry Smith    Input Parameters:
3638325fc9f4SBarry Smith +  ts - the TS context
3639325fc9f4SBarry Smith -  func - function evaluation routine
3640325fc9f4SBarry Smith 
3641325fc9f4SBarry Smith    Calling sequence of func:
3642325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
3643325fc9f4SBarry Smith 
3644325fc9f4SBarry Smith    Level: beginner
3645325fc9f4SBarry Smith 
3646325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3647325fc9f4SBarry Smith 
3648325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3649325fc9f4SBarry Smith */
3650cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
3651325fc9f4SBarry Smith {
3652325fc9f4SBarry Smith   PetscErrorCode  ierr;
3653325fc9f4SBarry Smith   TSMatlabContext *sctx;
3654325fc9f4SBarry Smith 
3655325fc9f4SBarry Smith   PetscFunctionBegin;
3656325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3657325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3658325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3659325fc9f4SBarry Smith   /*
3660325fc9f4SBarry Smith      This should work, but it doesn't
3661325fc9f4SBarry Smith   sctx->ctx = ctx;
3662325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3663325fc9f4SBarry Smith   */
3664325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3665cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
3666325fc9f4SBarry Smith   PetscFunctionReturn(0);
3667325fc9f4SBarry Smith }
3668325fc9f4SBarry Smith 
3669325fc9f4SBarry Smith #undef __FUNCT__
3670325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
3671325fc9f4SBarry Smith /*
3672325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
3673325fc9f4SBarry Smith                          TSSetJacobianMatlab().
3674325fc9f4SBarry Smith 
3675325fc9f4SBarry Smith    Collective on TS
3676325fc9f4SBarry Smith 
3677325fc9f4SBarry Smith    Input Parameters:
3678cdcf91faSSean Farley +  ts - the TS context
3679325fc9f4SBarry Smith .  x - input vector
3680325fc9f4SBarry Smith .  A, B - the matrices
3681325fc9f4SBarry Smith -  ctx - user context
3682325fc9f4SBarry Smith 
3683325fc9f4SBarry Smith    Output Parameter:
3684325fc9f4SBarry Smith .  flag - structure of the matrix
3685325fc9f4SBarry Smith 
3686325fc9f4SBarry Smith    Level: developer
3687325fc9f4SBarry Smith 
3688325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3689325fc9f4SBarry Smith 
3690325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3691325fc9f4SBarry Smith @*/
3692cdcf91faSSean Farley PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
3693325fc9f4SBarry Smith {
3694325fc9f4SBarry Smith   PetscErrorCode  ierr;
3695325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3696325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
3697325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
3698325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
3699325fc9f4SBarry Smith 
3700325fc9f4SBarry Smith   PetscFunctionBegin;
3701cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3702325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3703325fc9f4SBarry Smith 
3704325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
3705325fc9f4SBarry Smith 
3706cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3707325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3708325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
3709325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
3710325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
3711325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3712325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
3713325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3714325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3715325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
3716325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
3717325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
3718325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
3719325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
3720325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
3721325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3722325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
3723325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3724325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3725325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3726325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3727325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3728325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3729325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
3730325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
3731325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3732325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
3733325fc9f4SBarry Smith   PetscFunctionReturn(0);
3734325fc9f4SBarry Smith }
3735325fc9f4SBarry Smith 
3736325fc9f4SBarry Smith 
3737325fc9f4SBarry Smith #undef __FUNCT__
3738325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
3739325fc9f4SBarry Smith /*
3740325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
3741e3c5b3baSBarry 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
3742325fc9f4SBarry Smith 
3743325fc9f4SBarry Smith    Logically Collective on TS
3744325fc9f4SBarry Smith 
3745325fc9f4SBarry Smith    Input Parameters:
3746cdcf91faSSean Farley +  ts - the TS context
3747325fc9f4SBarry Smith .  A,B - Jacobian matrices
3748325fc9f4SBarry Smith .  func - function evaluation routine
3749325fc9f4SBarry Smith -  ctx - user context
3750325fc9f4SBarry Smith 
3751325fc9f4SBarry Smith    Calling sequence of func:
3752cdcf91faSSean Farley $    flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
3753325fc9f4SBarry Smith 
3754325fc9f4SBarry Smith 
3755325fc9f4SBarry Smith    Level: developer
3756325fc9f4SBarry Smith 
3757325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3758325fc9f4SBarry Smith 
3759325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3760325fc9f4SBarry Smith */
3761cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
3762325fc9f4SBarry Smith {
3763325fc9f4SBarry Smith   PetscErrorCode    ierr;
3764325fc9f4SBarry Smith   TSMatlabContext *sctx;
3765325fc9f4SBarry Smith 
3766325fc9f4SBarry Smith   PetscFunctionBegin;
3767325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3768325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3769325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3770325fc9f4SBarry Smith   /*
3771325fc9f4SBarry Smith      This should work, but it doesn't
3772325fc9f4SBarry Smith   sctx->ctx = ctx;
3773325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3774325fc9f4SBarry Smith   */
3775325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3776cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
3777325fc9f4SBarry Smith   PetscFunctionReturn(0);
3778325fc9f4SBarry Smith }
3779325fc9f4SBarry Smith 
3780b5b1a830SBarry Smith #undef __FUNCT__
3781b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
3782b5b1a830SBarry Smith /*
3783b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
3784b5b1a830SBarry Smith 
3785b5b1a830SBarry Smith    Collective on TS
3786b5b1a830SBarry Smith 
3787b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3788b5b1a830SBarry Smith @*/
3789cdcf91faSSean Farley PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx)
3790b5b1a830SBarry Smith {
3791b5b1a830SBarry Smith   PetscErrorCode  ierr;
3792b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3793a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
3794b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
3795b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
3796b5b1a830SBarry Smith 
3797b5b1a830SBarry Smith   PetscFunctionBegin;
3798cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3799b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
3800b5b1a830SBarry Smith 
3801cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3802b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3803b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3804b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
3805b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
3806b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
3807b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
3808b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
3809b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
3810b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3811b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
3812b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
3813b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
3814b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
3815b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
3816b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
3817b5b1a830SBarry Smith   PetscFunctionReturn(0);
3818b5b1a830SBarry Smith }
3819b5b1a830SBarry Smith 
3820b5b1a830SBarry Smith 
3821b5b1a830SBarry Smith #undef __FUNCT__
3822b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
3823b5b1a830SBarry Smith /*
3824b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
3825b5b1a830SBarry Smith 
3826b5b1a830SBarry Smith    Level: developer
3827b5b1a830SBarry Smith 
3828b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
3829b5b1a830SBarry Smith 
3830b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3831b5b1a830SBarry Smith */
3832cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
3833b5b1a830SBarry Smith {
3834b5b1a830SBarry Smith   PetscErrorCode    ierr;
3835b5b1a830SBarry Smith   TSMatlabContext *sctx;
3836b5b1a830SBarry Smith 
3837b5b1a830SBarry Smith   PetscFunctionBegin;
3838b5b1a830SBarry Smith   /* currently sctx is memory bleed */
3839b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3840b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3841b5b1a830SBarry Smith   /*
3842b5b1a830SBarry Smith      This should work, but it doesn't
3843b5b1a830SBarry Smith   sctx->ctx = ctx;
3844b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3845b5b1a830SBarry Smith   */
3846b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3847cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3848b5b1a830SBarry Smith   PetscFunctionReturn(0);
3849b5b1a830SBarry Smith }
3850325fc9f4SBarry Smith #endif
3851b3603a34SBarry Smith 
3852b3603a34SBarry Smith #undef __FUNCT__
3853b3603a34SBarry Smith #define __FUNCT__ "TSMonitorSolutionODE"
3854b3603a34SBarry Smith /*@C
3855b3603a34SBarry Smith    TSMonitorSolutionODE - Monitors progress of the TS solvers by plotting each component of the solution vector
3856b3603a34SBarry Smith        in a time based line graph
3857b3603a34SBarry Smith 
3858b3603a34SBarry Smith    Collective on TS
3859b3603a34SBarry Smith 
3860b3603a34SBarry Smith    Input Parameters:
3861b3603a34SBarry Smith +  ts - the TS context
3862b3603a34SBarry Smith .  step - current time-step
3863b3603a34SBarry Smith .  ptime - current time
3864b3603a34SBarry Smith -  lg - a line graph object
3865b3603a34SBarry Smith 
3866b3603a34SBarry Smith    Level: intermediate
3867b3603a34SBarry Smith 
3868b3603a34SBarry Smith     Notes: only for sequential solves
3869b3603a34SBarry Smith 
3870b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
3871b3603a34SBarry Smith 
3872b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3873b3603a34SBarry Smith @*/
3874b3603a34SBarry Smith PetscErrorCode  TSMonitorSolutionODE(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
3875b3603a34SBarry Smith {
3876b3603a34SBarry Smith   PetscErrorCode    ierr;
3877b3603a34SBarry Smith   PetscDrawLG       lg = (PetscDrawLG)dummy;
3878b3603a34SBarry Smith   const PetscScalar *yy;
3879b3603a34SBarry Smith 
3880b3603a34SBarry Smith   PetscFunctionBegin;
3881b3603a34SBarry Smith   if (!step) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
3882b3603a34SBarry Smith   ierr = VecGetArrayRead(x,&yy);CHKERRQ(ierr);
3883*aa39b21eSBarry Smith   ierr = PetscDrawLGAddCommonPoint(lg,ptime,yy);CHKERRQ(ierr);
3884b3603a34SBarry Smith   ierr = VecRestoreArrayRead(x,&yy);CHKERRQ(ierr);
3885b3603a34SBarry Smith   ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
3886b3603a34SBarry Smith   PetscFunctionReturn(0);
3887b3603a34SBarry Smith }
3888b3603a34SBarry Smith 
3889b3603a34SBarry Smith #undef __FUNCT__
3890b3603a34SBarry Smith #define __FUNCT__ "TSMonitorSolutionODEDestroy"
3891b3603a34SBarry Smith /*@C
3892b3603a34SBarry Smith    TSMonitorSolutionODEDestroy - Destroys the monitor context for TSMonitorSolutionODE()
3893b3603a34SBarry Smith 
3894b3603a34SBarry Smith    Collective on TS
3895b3603a34SBarry Smith 
3896b3603a34SBarry Smith    Input Parameters:
3897b3603a34SBarry Smith .    ctx - the monitor context
3898b3603a34SBarry Smith 
3899b3603a34SBarry Smith    Level: intermediate
3900b3603a34SBarry Smith 
3901b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
3902b3603a34SBarry Smith 
3903b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolutionODE()
3904b3603a34SBarry Smith @*/
3905b3603a34SBarry Smith PetscErrorCode  TSMonitorSolutionODEDestroy(PetscDrawLG *lg)
3906b3603a34SBarry Smith {
3907b3603a34SBarry Smith   PetscDraw      draw;
3908b3603a34SBarry Smith   PetscErrorCode ierr;
3909b3603a34SBarry Smith 
3910b3603a34SBarry Smith   PetscFunctionBegin;
3911b3603a34SBarry Smith   ierr = PetscDrawLGGetDraw(*lg,&draw);CHKERRQ(ierr);
3912b3603a34SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
3913b3603a34SBarry Smith   ierr = PetscDrawLGDestroy(lg);CHKERRQ(ierr);
3914b3603a34SBarry Smith   PetscFunctionReturn(0);
3915b3603a34SBarry Smith }
3916b3603a34SBarry Smith 
3917b3603a34SBarry Smith #undef __FUNCT__
3918b3603a34SBarry Smith #define __FUNCT__ "TSMonitorSolutionODECreate"
3919b3603a34SBarry Smith /*@C
3920b3603a34SBarry Smith    TSMonitorSolutionODECreate - Creates the monitor context for TSMonitorSolutionODE()
3921b3603a34SBarry Smith 
3922b3603a34SBarry Smith    Collective on TS
3923b3603a34SBarry Smith 
3924b3603a34SBarry Smith    Input Parameter:
3925b3603a34SBarry Smith +    comm - MPI communicator
3926b3603a34SBarry Smith .    N - number of components in the solution vector
3927b3603a34SBarry Smith -
3928b3603a34SBarry Smith 
3929b3603a34SBarry Smith    Output Patameter:
3930b3603a34SBarry Smith .    ctx - the monitor context
3931b3603a34SBarry Smith 
3932b3603a34SBarry Smith    Level: intermediate
3933b3603a34SBarry Smith 
3934b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
3935b3603a34SBarry Smith 
3936b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
3937b3603a34SBarry Smith @*/
3938b3603a34SBarry Smith PetscErrorCode  TSMonitorSolutionODECreate(MPI_Comm comm,PetscInt N,const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
3939b3603a34SBarry Smith {
3940b3603a34SBarry Smith   PetscDraw      win;
3941b3603a34SBarry Smith   PetscErrorCode ierr;
3942b3603a34SBarry Smith   PetscDrawAxis  axis;
3943b3603a34SBarry Smith 
3944b3603a34SBarry Smith   PetscFunctionBegin;
3945b3603a34SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
3946b3603a34SBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
3947b3603a34SBarry Smith   ierr = PetscDrawLGCreate(win,N,draw);CHKERRQ(ierr);
3948b3603a34SBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
3949b3603a34SBarry Smith   ierr = PetscDrawLGGetAxis(*draw,&axis);CHKERRQ(ierr);
3950b3603a34SBarry Smith   ierr = PetscDrawAxisSetLabels(axis,"Solution","Time","Solution");CHKERRQ(ierr);
3951b3603a34SBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
3952b3603a34SBarry Smith   PetscFunctionReturn(0);
3953b3603a34SBarry Smith }
3954