Actual source code: traj.c

petsc-3.6.4 2016-04-12
Report Typos and Errors
  2: #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/

  4: PetscFunctionList TSTrajectoryList              = NULL;
  5: PetscBool         TSTrajectoryRegisterAllCalled = PETSC_FALSE;
  6: PetscClassId      TSTRAJECTORY_CLASSID;

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

 13:   Not Collective

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

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

 22:   Level: advanced

 24: .keywords: TS, register

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

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

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

 44:   if (!tj) return(0);
 45:   (*tj->ops->set)(tj,ts,stepnum,time,X);
 46:   return(0);
 47: }

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

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

 63: /*@C
 64:     TSTrajectoryView - Prints information about the trajectory object

 66:     Collective on TSTrajectory

 68:     Input Parameters:
 69: +   ts - the TSTrajectory context obtained from TSTrajectoryCreate()
 70: -   viewer - visualization context

 72:     Options Database Key:
 73: .   -ts_view - calls TSView() at end of TSStep()

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

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

 86:     Level: beginner

 88: .keywords: TS, timestep, view

 90: .seealso: PetscViewerASCIIOpen()
 91: @*/
 92: PetscErrorCode  TSTrajectoryView(TSTrajectory ts,PetscViewer viewer)
 93: {
 95:   PetscBool      iascii;

 99:   if (!viewer) {
100:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);
101:   }

105:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
106:   if (iascii) {
107:     PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);
108:     if (ts->ops->view) {
109:       (*ts->ops->view)(ts,viewer);
110:     }
111:   }
112:   return(0);
113: }

115: #undef  __FUNCT__
117: /*@C
118:   TSTrajectoryCreate - This function creates an empty trajectory object used to store the time dependent solution of an ODE/DAE

120:   Collective on MPI_Comm

122:   Input Parameter:
123: . comm - The communicator

125:   Output Parameter:
126: . tstra   - The trajectory object

128:   Level: advanced

130:   Notes: Usually one does not call this routine, it is called automatically when one calls TSSetSaveTrajectory(). One can call
131:    TSGetTrajectory() to access the created trajectory.

133: .keywords: TS, create
134: .seealso: TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType(), TSGetTrajectory()
135: @*/
136: PetscErrorCode  TSTrajectoryCreate(MPI_Comm comm, TSTrajectory *tstra)
137: {
138:   TSTrajectory   t;

143:   *tstra = NULL;
144:   TSInitializePackage();

146:   PetscHeaderCreate(t, TSTRAJECTORY_CLASSID, "TSTrajectory", "Time stepping", "TS", comm, TSTrajectoryDestroy, TSTrajectoryView);
147:   *tstra = t;
148:   return(0);
149: }

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

156:   Collective on TS

158:   Input Parameters:
159: + ts   - The TS context
160: - type - A known method

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

165:    Level: intermediate

167: .keywords: TS, set, type

169: .seealso: TS, TSSolve(), TSCreate(), TSSetFromOptions(), TSDestroy(), TSType

171: @*/
172: PetscErrorCode  TSTrajectorySetType(TSTrajectory ts,const TSTrajectoryType type)
173: {
174:   PetscErrorCode (*r)(TSTrajectory);
175:   PetscBool      match;

180:   PetscObjectTypeCompare((PetscObject) ts, type, &match);
181:   if (match) return(0);

183:   PetscFunctionListFind(TSTrajectoryList,type,&r);
184:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown TSTrajectory type: %s", type);
185:   if (ts->ops->destroy) {
186:     (*(ts)->ops->destroy)(ts);

188:     ts->ops->destroy = NULL;
189:   }
190:   PetscMemzero(ts->ops,sizeof(*ts->ops));

192:   PetscObjectChangeTypeName((PetscObject)ts, type);
193:   (*r)(ts);
194:   return(0);
195: }

197: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Basic(TSTrajectory);
198: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Singlefile(TSTrajectory);

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

205:   Not Collective

207:   Level: advanced

209: .keywords: TS, timestepper, register, all
210: .seealso: TSCreate(), TSRegister(), TSRegisterDestroy()
211: @*/
212: PetscErrorCode  TSTrajectoryRegisterAll(void)
213: {

217:   TSTrajectoryRegisterAllCalled = PETSC_TRUE;

219:   TSTrajectoryRegister(TSTRAJECTORYBASIC,TSTrajectoryCreate_Basic);
220:   TSTrajectoryRegister(TSTRAJECTORYSINGLEFILE,TSTrajectoryCreate_Singlefile);
221:   return(0);
222: }

226: /*@
227:    TSTrajectoryDestroy - Destroys a trajectory context

229:    Collective on TSTrajectory

231:    Input Parameter:
232: .  ts - the TSTrajectory context obtained from TSTrajectoryCreate()

234:    Level: advanced

236: .keywords: TS, timestepper, destroy

238: .seealso: TSCreate(), TSSetUp(), TSSolve()
239: @*/
240: PetscErrorCode  TSTrajectoryDestroy(TSTrajectory *ts)
241: {

245:   if (!*ts) return(0);
247:   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; return(0);}

249:   if ((*ts)->ops->destroy) {(*(*ts)->ops->destroy)((*ts));}
250:   PetscHeaderDestroy(ts);
251:   return(0);
252: }

256: /*
257:   TSTrajectorySetTypeFromOptions_Private - Sets the type of ts from user options.

259:   Collective on TSTrajectory

261:   Input Parameter:
262: . ts - The ts

264:   Level: intermediate

266: .keywords: TS, set, options, database, type
267: .seealso: TSSetFromOptions(), TSSetType()
268: */
269: static PetscErrorCode TSTrajectorySetTypeFromOptions_Private(PetscOptions *PetscOptionsObject,TSTrajectory ts)
270: {
271:   PetscBool      opt;
272:   const char     *defaultType;
273:   char           typeName[256];

277:   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
278:   else defaultType = TSTRAJECTORYBASIC;

280:   if (!TSRegisterAllCalled) {TSTrajectoryRegisterAll();}
281:   PetscOptionsFList("-tstrajectory_type", "TSTrajectory method"," TSTrajectorySetType", TSTrajectoryList, defaultType, typeName, 256, &opt);
282:   if (opt) {
283:     TSTrajectorySetType(ts, typeName);
284:   } else {
285:     TSTrajectorySetType(ts, defaultType);
286:   }
287:   return(0);
288: }

292: /*@
293:    TSTrajectorySetFromOptions - Sets various TSTrajectory parameters from user options.

295:    Collective on TSTrajectory

297:    Input Parameter:
298: .  ts - the TSTrajectory context obtained from TSTrajectoryCreate()

300:    Options Database Keys:
301: .  -tstrajectory_type <type> - TSTRAJECTORYBASIC

303:    Level: advanced

305:    Notes: This is not normally called directly by users, instead it is called by TSSetFromOptions() after a call to 
306:    TSSetSaveTrajectory()

308: .keywords: TS, timestep, set, options, database

310: .seealso: TSGetType(), TSSetSaveTrajectory(), TSGetTrajectory()
311: @*/
312: PetscErrorCode  TSTrajectorySetFromOptions(TSTrajectory ts)
313: {

318:   PetscObjectOptionsBegin((PetscObject)ts);
319:   TSTrajectorySetTypeFromOptions_Private(PetscOptionsObject,ts);
320:   PetscOptionsEnd();
321:   return(0);
322: }