Actual source code: traj.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  2:  #include <petsc/private/tsimpl.h>

  4: PetscFunctionList TSTrajectoryList              = NULL;
  5: PetscBool         TSTrajectoryRegisterAllCalled = PETSC_FALSE;
  6: PetscClassId      TSTRAJECTORY_CLASSID;
  7: PetscLogEvent     TSTrajectory_Set, TSTrajectory_Get;

  9: /*@C
 10:   TSTrajectoryRegister - Adds a way of storing trajectories to the TS package

 12:   Not Collective

 14:   Input Parameters:
 15: + name        - the name of a new user-defined creation routine
 16: - create_func - the creation routine itself

 18:   Notes:
 19:   TSTrajectoryRegister() may be called multiple times to add several user-defined tses.

 21:   Level: developer

 23: .keywords: TS, trajectory, timestep, register

 25: .seealso: TSTrajectoryRegisterAll()
 26: @*/
 27: PetscErrorCode TSTrajectoryRegister(const char sname[],PetscErrorCode (*function)(TSTrajectory,TS))
 28: {

 32:   PetscFunctionListAdd(&TSTrajectoryList,sname,function);
 33:   return(0);
 34: }

 36: PetscErrorCode TSTrajectorySet(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal time,Vec X)
 37: {

 41:   if (!tj) return(0);
 42:   PetscLogEventBegin(TSTrajectory_Set,tj,ts,0,0);
 43:   (*tj->ops->set)(tj,ts,stepnum,time,X);
 44:   PetscLogEventEnd(TSTrajectory_Set,tj,ts,0,0);
 45:   return(0);
 46: }

 48: PetscErrorCode TSTrajectoryGet(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal *time)
 49: {

 53:   if (!tj) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TS solver did not save trajectory");
 54:   PetscLogEventBegin(TSTrajectory_Get,tj,ts,0,0);
 55:   (*tj->ops->get)(tj,ts,stepnum,time);
 56:   PetscLogEventEnd(TSTrajectory_Get,tj,ts,0,0);
 57:   return(0);
 58: }

 60: /*@C
 61:     TSTrajectoryView - Prints information about the trajectory object

 63:     Collective on TSTrajectory

 65:     Input Parameters:
 66: +   tj - the TSTrajectory context obtained from TSTrajectoryCreate()
 67: -   viewer - visualization context

 69:     Options Database Key:
 70: .   -ts_trajectory_view - calls TSTrajectoryView() at end of TSAdjointStep()

 72:     Notes:
 73:     The available visualization contexts include
 74: +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
 75: -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
 76:          output where only the first processor opens
 77:          the file.  All other processors send their
 78:          data to the first processor to print.

 80:     The user can open an alternative visualization context with
 81:     PetscViewerASCIIOpen() - output to a specified file.

 83:     Level: developer

 85: .keywords: TS, trajectory, timestep, view

 87: .seealso: PetscViewerASCIIOpen()
 88: @*/
 89: PetscErrorCode  TSTrajectoryView(TSTrajectory tj,PetscViewer viewer)
 90: {
 92:   PetscBool      iascii;

 96:   if (!viewer) {
 97:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)tj),&viewer);
 98:   }

102:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
103:   if (iascii) {
104:     PetscObjectPrintClassNamePrefixType((PetscObject)tj,viewer);
105:     PetscViewerASCIIPrintf(viewer,"  total number of recomputations for adjoint calculation = %D\n",tj->recomps);
106:     PetscViewerASCIIPrintf(viewer,"  disk checkpoint reads = %D\n",tj->diskreads);
107:     PetscViewerASCIIPrintf(viewer,"  disk checkpoint writes = %D\n",tj->diskwrites);
108:     if (tj->ops->view) {
109:       PetscViewerASCIIPushTab(viewer);
110:       (*tj->ops->view)(tj,viewer);
111:       PetscViewerASCIIPopTab(viewer);
112:     }
113:   }
114:   return(0);
115: }

117: /*@C
118:    TSTrajectorySetVariableNames - Sets the name of each component in the solution vector so that it may be saved with the trajectory

120:    Collective on TSTrajectory

122:    Input Parameters:
123: +  tr - the trajectory context
124: -  names - the names of the components, final string must be NULL

126:    Level: intermediate

128: .keywords: TS, TSTrajectory, vector, monitor, view

130: .seealso: TSTrajectory, TSGetTrajectory()
131: @*/
132: PetscErrorCode  TSTrajectorySetVariableNames(TSTrajectory ctx,const char * const *names)
133: {
134:   PetscErrorCode    ierr;

137:   PetscStrArrayDestroy(&ctx->names);
138:   PetscStrArrayallocpy(names,&ctx->names);
139:   return(0);
140: }

142: /*@C
143:    TSTrjactorySetTransform - Solution vector will be transformed by provided function before being saved to disk

145:    Collective on TSLGCtx

147:    Input Parameters:
148: +  tj - the TSTrajectory context
149: .  transform - the transform function
150: .  destroy - function to destroy the optional context
151: -  ctx - optional context used by transform function

153:    Level: intermediate

155: .keywords: TSTrajectory,  vector, monitor, view

157: .seealso:  TSTrajectorySetVariableNames(), TSTrajectory, TSMonitorLGSetTransform()
158: @*/
159: PetscErrorCode  TSTrajectorySetTransform(TSTrajectory tj,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
160: {
162:   tj->transform        = transform;
163:   tj->transformdestroy = destroy;
164:   tj->transformctx     = tctx;
165:   return(0);
166: }


169: /*@C
170:   TSTrajectoryCreate - This function creates an empty trajectory object used to store the time dependent solution of an ODE/DAE

172:   Collective on MPI_Comm

174:   Input Parameter:
175: . comm - the communicator

177:   Output Parameter:
178: . tj   - the trajectory object

180:   Level: developer

182:   Notes: Usually one does not call this routine, it is called automatically when one calls TSSetSaveTrajectory().

184: .keywords: TS, trajectory, create

186: .seealso: TSTrajectorySetUp(), TSTrajectoryDestroy(), TSTrajectorySetType(), TSTrajectorySetVariableNames(), TSGetTrajectory()
187: @*/
188: PetscErrorCode  TSTrajectoryCreate(MPI_Comm comm,TSTrajectory *tj)
189: {
190:   TSTrajectory   t;

195:   *tj = NULL;
196:   TSInitializePackage();

198:   PetscHeaderCreate(t,TSTRAJECTORY_CLASSID,"TSTrajectory","Time stepping","TS",comm,TSTrajectoryDestroy,TSTrajectoryView);
199:   t->setupcalled = PETSC_FALSE;
200:   *tj = t;
201:   return(0);
202: }

204: /*@C
205:   TSTrajectorySetType - Sets the storage method to be used as in a trajectory

207:   Collective on TS

209:   Input Parameters:
210: + tj   - the TSTrajectory context
211: . ts   - the TS context
212: - type - a known method

214:   Options Database Command:
215: . -ts_trajectory_type <type> - Sets the method; use -help for a list of available methods (for instance, basic)

217:    Level: developer

219: .keywords: TS, trajectory, timestep, set, type

221: .seealso: TS, TSTrajectoryCreate(), TSTrajectorySetFromOptions(), TSTrajectoryDestroy()

223: @*/
224: PetscErrorCode  TSTrajectorySetType(TSTrajectory tj,TS ts,const TSTrajectoryType type)
225: {
226:   PetscErrorCode (*r)(TSTrajectory,TS);
227:   PetscBool      match;

232:   PetscObjectTypeCompare((PetscObject)tj,type,&match);
233:   if (match) return(0);

235:   PetscFunctionListFind(TSTrajectoryList,type,&r);
236:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown TSTrajectory type: %s",type);
237:   if (tj->ops->destroy) {
238:     (*(tj)->ops->destroy)(tj);

240:     tj->ops->destroy = NULL;
241:   }
242:   PetscMemzero(tj->ops,sizeof(*tj->ops));

244:   PetscObjectChangeTypeName((PetscObject)tj,type);
245:   (*r)(tj,ts);
246:   return(0);
247: }

249: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Basic(TSTrajectory,TS);
250: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Singlefile(TSTrajectory,TS);
251: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Memory(TSTrajectory,TS);
252: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Visualization(TSTrajectory,TS);

254: /*@C
255:   TSTrajectoryRegisterAll - Registers all of the trajectory storage schecmes in the TS package.

257:   Not Collective

259:   Level: developer

261: .keywords: TS, trajectory, register, all

263: .seealso: TSTrajectoryRegister()
264: @*/
265: PetscErrorCode  TSTrajectoryRegisterAll(void)
266: {

270:   if (TSTrajectoryRegisterAllCalled) return(0);
271:   TSTrajectoryRegisterAllCalled = PETSC_TRUE;

273:   TSTrajectoryRegister(TSTRAJECTORYBASIC,TSTrajectoryCreate_Basic);
274:   TSTrajectoryRegister(TSTRAJECTORYSINGLEFILE,TSTrajectoryCreate_Singlefile);
275:   TSTrajectoryRegister(TSTRAJECTORYMEMORY,TSTrajectoryCreate_Memory);
276:   TSTrajectoryRegister(TSTRAJECTORYVISUALIZATION,TSTrajectoryCreate_Visualization);
277:   return(0);
278: }

280: /*@
281:    TSTrajectoryDestroy - Destroys a trajectory context

283:    Collective on TSTrajectory

285:    Input Parameter:
286: .  tj - the TSTrajectory context obtained from TSTrajectoryCreate()

288:    Level: developer

290: .keywords: TS, trajectory, timestep, destroy

292: .seealso: TSTrajectoryCreate(), TSTrajectorySetUp()
293: @*/
294: PetscErrorCode  TSTrajectoryDestroy(TSTrajectory *tj)
295: {

299:   if (!*tj) return(0);
301:   if (--((PetscObject)(*tj))->refct > 0) {*tj = 0; return(0);}

303:   if ((*tj)->transformdestroy) {(*(*tj)->transformdestroy)((*tj)->transformctx);}
304:   if ((*tj)->ops->destroy) {(*(*tj)->ops->destroy)((*tj));}
305:   PetscViewerDestroy(&(*tj)->monitor);
306:   PetscStrArrayDestroy(&(*tj)->names);
307:   PetscHeaderDestroy(tj);
308:   return(0);
309: }

311: /*
312:   TSTrajectorySetTypeFromOptions_Private - Sets the type of ts from user options.

314:   Collective on TSTrajectory

316:   Input Parameter:
317: + tj - the TSTrajectory context
318: - ts - the TS context

320:   Options Database Keys:
321: . -ts_trajectory_type <type> - TSTRAJECTORYBASIC, TSTRAJECTORYMEMORY, TSTRAJECTORYSINGLEFILE, TSTRAJECTORYVISUALIZATION

323:   Level: developer

325: .keywords: TS, trajectory, set, options, type

327: .seealso: TSTrajectorySetFromOptions(), TSTrajectorySetType()
328: */
329: static PetscErrorCode TSTrajectorySetTypeFromOptions_Private(PetscOptionItems *PetscOptionsObject,TSTrajectory tj,TS ts)
330: {
331:   PetscBool      opt;
332:   const char     *defaultType;
333:   char           typeName[256];
334:   PetscBool      flg;

338:   if (((PetscObject)tj)->type_name) defaultType = ((PetscObject)tj)->type_name;
339:   else defaultType = TSTRAJECTORYBASIC;

341:   TSTrajectoryRegisterAll();
342:   PetscOptionsFList("-ts_trajectory_type","TSTrajectory method"," TSTrajectorySetType",TSTrajectoryList,defaultType,typeName,256,&opt);
343:   if (opt) {
344:     PetscStrcmp(typeName,TSTRAJECTORYMEMORY,&flg);
345:     TSTrajectorySetType(tj,ts,typeName);
346:   } else {
347:     TSTrajectorySetType(tj,ts,defaultType);
348:   }
349:   return(0);
350: }

352: /*@
353:    TSTrajectorySetMonitor - Monitor the schedules generated by the checkpointing controller

355:    Collective on TSTrajectory

357:    Input Arguments:
358: +  tj - the TSTrajectory context
359: -  flg - PETSC_TRUE to active a monitor, PETSC_FALSE to disable

361:    Options Database Keys:
362: .  -ts_trajectory_monitor - print TSTrajectory information

364:    Level: developer

366: .keywords: TS, trajectory, set, monitor

368: .seealso: TSTrajectoryCreate(), TSTrajectoryDestroy(), TSTrajectorySetUp()
369: @*/
370: PetscErrorCode TSTrajectorySetMonitor(TSTrajectory tj,PetscBool flg)
371: {

377:   if (flg) {
378:     if (!tj->monitor) {PetscViewerASCIIOpen(PetscObjectComm((PetscObject)tj),"stdout",&tj->monitor);}
379:   } else {
380:     PetscViewerDestroy(&tj->monitor);
381:   }
382:   return(0);
383: }

385: /*@
386:    TSTrajectorySetFromOptions - Sets various TSTrajectory parameters from user options.

388:    Collective on TSTrajectory

390:    Input Parameter:
391: +  tj - the TSTrajectory context obtained from TSTrajectoryCreate()
392: -  ts - the TS context

394:    Options Database Keys:
395: +  -ts_trajectory_type <type> - TSTRAJECTORYBASIC, TSTRAJECTORYMEMORY, TSTRAJECTORYSINGLEFILE, TSTRAJECTORYVISUALIZATION
396: -  -ts_trajectory_monitor - print TSTrajectory information

398:    Level: developer

400:    Notes: This is not normally called directly by users

402: .keywords: TS, trajectory, timestep, set, options, database

404: .seealso: TSSetSaveTrajectory(), TSTrajectorySetUp()
405: @*/
406: PetscErrorCode  TSTrajectorySetFromOptions(TSTrajectory tj,TS ts)
407: {
409:   PetscBool      set,flg;

414:   PetscObjectOptionsBegin((PetscObject)tj);
415:   TSTrajectorySetTypeFromOptions_Private(PetscOptionsObject,tj,ts);
416:   PetscOptionsBool("-ts_trajectory_monitor","Print checkpointing schedules","TSTrajectorySetMonitor",tj->monitor ? PETSC_TRUE:PETSC_FALSE,&flg,&set);
417:   if (set) {TSTrajectorySetMonitor(tj,flg);}
418:   /* Handle specific TS options */
419:   if (tj->ops->setfromoptions) {
420:     (*tj->ops->setfromoptions)(PetscOptionsObject,tj);
421:   }
422:   PetscOptionsEnd();
423:   return(0);
424: }

426: /*@
427:    TSTrajectorySetUp - Sets up the internal data structures, e.g. stacks, for the later use
428:    of a TS trajectory.

430:    Collective on TS

432:    Input Parameter:
433: +  ts - the TS context obtained from TSCreate()
434: -  tj - the TS trajectory context

436:    Level: developer

438: .keywords: TS, trajectory, setup

440: .seealso: TSSetSaveTrajectory(), TSTrajectoryCreate(), TSTrajectoryDestroy()
441: @*/
442: PetscErrorCode  TSTrajectorySetUp(TSTrajectory tj,TS ts)
443: {

447:   if (!tj) return(0);
450:   if (tj->setupcalled) return(0);

452:   if (!((PetscObject)tj)->type_name) {
453:     TSTrajectorySetType(tj,ts,TSTRAJECTORYBASIC);
454:   }
455:   if (tj->ops->setup) {
456:     (*tj->ops->setup)(tj,ts);
457:   }

459:   tj->setupcalled = PETSC_TRUE;

461:   /* Set the counters to zero */
462:   tj->recomps    = 0;
463:   tj->diskreads  = 0;
464:   tj->diskwrites = 0;
465:   return(0);
466: }