Actual source code: traj.c
petsc-3.7.4 2016-10-02
2: #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/
4: PetscFunctionList TSTrajectoryList = NULL;
5: PetscBool TSTrajectoryRegisterAllCalled = PETSC_FALSE;
6: PetscClassId TSTRAJECTORY_CLASSID;
7: PetscLogEvent TSTrajectory_Set, TSTrajectory_Get;
11: /*@C
12: TSTrajectoryRegister - Adds a way of storing trajectories to the TS package
14: Not Collective
16: Input Parameters:
17: + name - the name of a new user-defined creation routine
18: - create_func - the creation routine itself
20: Notes:
21: TSTrajectoryRegister() may be called multiple times to add several user-defined tses.
23: Level: advanced
25: .keywords: TS, trajectory, timestep, register
27: .seealso: TSTrajectoryRegisterAll()
28: @*/
29: PetscErrorCode TSTrajectoryRegister(const char sname[],PetscErrorCode (*function)(TSTrajectory,TS))
30: {
34: PetscFunctionListAdd(&TSTrajectoryList,sname,function);
35: return(0);
36: }
40: PetscErrorCode TSTrajectorySet(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal time,Vec X)
41: {
45: if (!tj) return(0);
46: PetscLogEventBegin(TSTrajectory_Set,tj,ts,0,0);
47: (*tj->ops->set)(tj,ts,stepnum,time,X);
48: PetscLogEventEnd(TSTrajectory_Set,tj,ts,0,0);
49: return(0);
50: }
54: PetscErrorCode TSTrajectoryGet(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal *time)
55: {
59: if (!tj) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TS solver did not save trajectory");
60: PetscLogEventBegin(TSTrajectory_Get,tj,ts,0,0);
61: (*tj->ops->get)(tj,ts,stepnum,time);
62: PetscLogEventEnd(TSTrajectory_Get,tj,ts,0,0);
63: return(0);
64: }
68: /*@C
69: TSTrajectoryView - Prints information about the trajectory object
71: Collective on TSTrajectory
73: Input Parameters:
74: + tj - the TSTrajectory context obtained from TSTrajectoryCreate()
75: - viewer - visualization context
77: Options Database Key:
78: . -ts_trajectory_view - calls TSTrajectoryView() at end of TSAdjointStep()
80: Notes:
81: The available visualization contexts include
82: + PETSC_VIEWER_STDOUT_SELF - standard output (default)
83: - PETSC_VIEWER_STDOUT_WORLD - synchronized standard
84: output where only the first processor opens
85: the file. All other processors send their
86: data to the first processor to print.
88: The user can open an alternative visualization context with
89: PetscViewerASCIIOpen() - output to a specified file.
91: Level: beginner
93: .keywords: TS, trajectory, timestep, view
95: .seealso: PetscViewerASCIIOpen()
96: @*/
97: PetscErrorCode TSTrajectoryView(TSTrajectory tj,PetscViewer viewer)
98: {
100: PetscBool iascii;
104: if (!viewer) {
105: PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)tj),&viewer);
106: }
110: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
111: if (iascii) {
112: PetscObjectPrintClassNamePrefixType((PetscObject)tj,viewer);
113: PetscViewerASCIIPrintf(viewer," total number of recomputations for adjoint calculation = %D\n",tj->recomps);
114: PetscViewerASCIIPrintf(viewer," disk checkpoint reads = %D\n",tj->diskreads);
115: PetscViewerASCIIPrintf(viewer," disk checkpoint writes = %D\n",tj->diskwrites);
116: if (tj->ops->view) {
117: PetscViewerASCIIPushTab(viewer);
118: (*tj->ops->view)(tj,viewer);
119: PetscViewerASCIIPopTab(viewer);
120: }
121: }
122: return(0);
123: }
125: #undef __FUNCT__
127: /*@C
128: TSTrajectoryCreate - This function creates an empty trajectory object used to store the time dependent solution of an ODE/DAE
130: Collective on MPI_Comm
132: Input Parameter:
133: . comm - the communicator
135: Output Parameter:
136: . tj - the trajectory object
138: Level: advanced
140: Notes: Usually one does not call this routine, it is called automatically when one calls TSSetSaveTrajectory().
142: .keywords: TS, trajectory, create
144: .seealso: TSTrajectorySetUp(), TSTrajectoryDestroy(), TSTrajectorySetType()
145: @*/
146: PetscErrorCode TSTrajectoryCreate(MPI_Comm comm,TSTrajectory *tj)
147: {
148: TSTrajectory t;
153: *tj = NULL;
154: TSInitializePackage();
156: PetscHeaderCreate(t,TSTRAJECTORY_CLASSID,"TSTrajectory","Time stepping","TS",comm,TSTrajectoryDestroy,TSTrajectoryView);
157: t->setupcalled = PETSC_FALSE;
158: *tj = t;
159: return(0);
160: }
164: /*@C
165: TSTrajectorySetType - Sets the storage method to be used as in a trajectory
167: Collective on TS
169: Input Parameters:
170: + tj - the TSTrajectory context
171: . ts - the TS context
172: - type - a known method
174: Options Database Command:
175: . -ts_trajectory_type <type> - Sets the method; use -help for a list of available methods (for instance, basic)
177: Level: intermediate
179: .keywords: TS, trajectory, timestep, set, type
181: .seealso: TS, TSTrajectoryCreate(), TSTrajectorySetFromOptions(), TSTrajectoryDestroy()
183: @*/
184: PetscErrorCode TSTrajectorySetType(TSTrajectory tj,TS ts,const TSTrajectoryType type)
185: {
186: PetscErrorCode (*r)(TSTrajectory,TS);
187: PetscBool match;
192: PetscObjectTypeCompare((PetscObject)tj,type,&match);
193: if (match) return(0);
195: PetscFunctionListFind(TSTrajectoryList,type,&r);
196: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown TSTrajectory type: %s",type);
197: if (tj->ops->destroy) {
198: (*(tj)->ops->destroy)(tj);
200: tj->ops->destroy = NULL;
201: }
202: PetscMemzero(tj->ops,sizeof(*tj->ops));
204: PetscObjectChangeTypeName((PetscObject)tj,type);
205: (*r)(tj,ts);
206: return(0);
207: }
209: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Basic(TSTrajectory,TS);
210: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Singlefile(TSTrajectory,TS);
211: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Memory(TSTrajectory,TS);
212: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Visualization(TSTrajectory,TS);
216: /*@C
217: TSTrajectoryRegisterAll - Registers all of the trajectory storage schecmes in the TS package.
219: Not Collective
221: Level: advanced
223: .keywords: TS, trajectory, register, all
225: .seealso: TSTrajectoryRegister()
226: @*/
227: PetscErrorCode TSTrajectoryRegisterAll(void)
228: {
232: if (TSTrajectoryRegisterAllCalled) return(0);
233: TSTrajectoryRegisterAllCalled = PETSC_TRUE;
235: TSTrajectoryRegister(TSTRAJECTORYBASIC,TSTrajectoryCreate_Basic);
236: TSTrajectoryRegister(TSTRAJECTORYSINGLEFILE,TSTrajectoryCreate_Singlefile);
237: TSTrajectoryRegister(TSTRAJECTORYMEMORY,TSTrajectoryCreate_Memory);
238: TSTrajectoryRegister(TSTRAJECTORYVISUALIZATION,TSTrajectoryCreate_Visualization);
239: return(0);
240: }
244: /*@
245: TSTrajectoryDestroy - Destroys a trajectory context
247: Collective on TSTrajectory
249: Input Parameter:
250: . tj - the TSTrajectory context obtained from TSTrajectoryCreate()
252: Level: advanced
254: .keywords: TS, trajectory, timestep, destroy
256: .seealso: TSTrajectoryCreate(), TSTrajectorySetUp()
257: @*/
258: PetscErrorCode TSTrajectoryDestroy(TSTrajectory *tj)
259: {
263: if (!*tj) return(0);
265: if (--((PetscObject)(*tj))->refct > 0) {*tj = 0; return(0);}
267: if ((*tj)->ops->destroy) {(*(*tj)->ops->destroy)((*tj));}
268: PetscViewerDestroy(&(*tj)->monitor);
269: PetscHeaderDestroy(tj);
270: return(0);
271: }
275: /*
276: TSTrajectorySetTypeFromOptions_Private - Sets the type of ts from user options.
278: Collective on TSTrajectory
280: Input Parameter:
281: + tj - the TSTrajectory context
282: - ts - the TS context
284: Options Database Keys:
285: . -ts_trajectory_type <type> - TSTRAJECTORYBASIC, TSTRAJECTORYMEMORY, TSTRAJECTORYSINGLEFILE, TSTRAJECTORYVISUALIZATION
287: Level: intermediate
289: .keywords: TS, trajectory, set, options, type
291: .seealso: TSTrajectorySetFromOptions(), TSTrajectorySetType()
292: */
293: static PetscErrorCode TSTrajectorySetTypeFromOptions_Private(PetscOptionItems *PetscOptionsObject,TSTrajectory tj,TS ts)
294: {
295: PetscBool opt;
296: const char *defaultType;
297: char typeName[256];
298: PetscBool flg;
302: if (((PetscObject)tj)->type_name) defaultType = ((PetscObject)tj)->type_name;
303: else defaultType = TSTRAJECTORYBASIC;
305: TSTrajectoryRegisterAll();
306: PetscOptionsFList("-ts_trajectory_type","TSTrajectory method"," TSTrajectorySetType",TSTrajectoryList,defaultType,typeName,256,&opt);
307: if (opt) {
308: PetscStrcmp(typeName,TSTRAJECTORYMEMORY,&flg);
309: TSTrajectorySetType(tj,ts,typeName);
310: } else {
311: TSTrajectorySetType(tj,ts,defaultType);
312: }
313: return(0);
314: }
318: /*@
319: TSTrajectorySetMonitor - Monitor the schedules generated by the checkpointing controller
321: Collective on TSTrajectory
323: Input Arguments:
324: + tj - the TSTrajectory context
325: - flg - PETSC_TRUE to active a monitor, PETSC_FALSE to disable
327: Options Database Keys:
328: . -ts_trajectory_monitor - print TSTrajectory information
330: Level: intermediate
332: .keywords: TS, trajectory, set, monitor
334: .seealso: TSTrajectoryCreate(), TSTrajectoryDestroy(), TSTrajectorySetUp()
335: @*/
336: PetscErrorCode TSTrajectorySetMonitor(TSTrajectory tj,PetscBool flg)
337: {
343: if (flg) {
344: if (!tj->monitor) {PetscViewerASCIIOpen(PetscObjectComm((PetscObject)tj),"stdout",&tj->monitor);}
345: } else {
346: PetscViewerDestroy(&tj->monitor);
347: }
348: return(0);
349: }
353: /*@
354: TSTrajectorySetFromOptions - Sets various TSTrajectory parameters from user options.
356: Collective on TSTrajectory
358: Input Parameter:
359: + tj - the TSTrajectory context obtained from TSTrajectoryCreate()
360: - ts - the TS context
362: Options Database Keys:
363: + -ts_trajectory_type <type> - TSTRAJECTORYBASIC, TSTRAJECTORYMEMORY, TSTRAJECTORYSINGLEFILE, TSTRAJECTORYVISUALIZATION
364: - -ts_trajectory_monitor - print TSTrajectory information
366: Level: advanced
368: Notes: This is not normally called directly by users
370: .keywords: TS, trajectory, timestep, set, options, database
372: .seealso: TSSetSaveTrajectory(), TSTrajectorySetUp()
373: @*/
374: PetscErrorCode TSTrajectorySetFromOptions(TSTrajectory tj,TS ts)
375: {
377: PetscBool set,flg;
382: PetscObjectOptionsBegin((PetscObject)tj);
383: TSTrajectorySetTypeFromOptions_Private(PetscOptionsObject,tj,ts);
384: PetscOptionsBool("-ts_trajectory_monitor","Print checkpointing schedules","TSTrajectorySetMonitor",tj->monitor ? PETSC_TRUE:PETSC_FALSE,&flg,&set);
385: if (set) {TSTrajectorySetMonitor(tj,flg);}
386: /* Handle specific TS options */
387: if (tj->ops->setfromoptions) {
388: (*tj->ops->setfromoptions)(PetscOptionsObject,tj);
389: }
390: PetscOptionsEnd();
391: return(0);
392: }
396: /*@
397: TSTrajectorySetUp - Sets up the internal data structures, e.g. stacks, for the later use
398: of a TS trajectory.
400: Collective on TS
402: Input Parameter:
403: + ts - the TS context obtained from TSCreate()
404: - tj - the TS trajectory context
406: Level: advanced
408: .keywords: TS, trajectory, setup
410: .seealso: TSSetSaveTrajectory(), TSTrajectoryCreate(), TSTrajectoryDestroy()
411: @*/
412: PetscErrorCode TSTrajectorySetUp(TSTrajectory tj,TS ts)
413: {
417: if (!tj) return(0);
420: if (tj->setupcalled) return(0);
422: if (!((PetscObject)tj)->type_name) {
423: TSTrajectorySetType(tj,ts,TSTRAJECTORYBASIC);
424: }
425: if (tj->ops->setup) {
426: (*tj->ops->setup)(tj,ts);
427: }
429: tj->setupcalled = PETSC_TRUE;
431: /* Set the counters to zero */
432: tj->recomps = 0;
433: tj->diskreads = 0;
434: tj->diskwrites = 0;
435: return(0);
436: }