xref: /petsc/src/ts/trajectory/interface/traj.c (revision e6e75211d226c622f451867f53ce5d558649ff4f)
1 
2 #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
3 
4 PetscFunctionList TSTrajectoryList              = NULL;
5 PetscBool         TSTrajectoryRegisterAllCalled = PETSC_FALSE;
6 PetscClassId      TSTRAJECTORY_CLASSID;
7 
8 #undef __FUNCT__
9 #define __FUNCT__ "TSTrajectoryRegister"
10 /*@C
11   TSTrajectoryRegister - Adds a way of storing trajectories to the TS package
12 
13   Not Collective
14 
15   Input Parameters:
16 + name        - The name of a new user-defined creation routine
17 - create_func - The creation routine itself
18 
19   Notes:
20   TSTrajectoryRegister() may be called multiple times to add several user-defined tses.
21 
22   Level: advanced
23 
24 .keywords: TS, register
25 
26 .seealso: TSTrajectoryRegisterAll(), TSTrajectoryRegisterDestroy()
27 @*/
28 PetscErrorCode  TSTrajectoryRegister(const char sname[], PetscErrorCode (*function)(TSTrajectory))
29 {
30   PetscErrorCode ierr;
31 
32   PetscFunctionBegin;
33   ierr = PetscFunctionListAdd(&TSTrajectoryList,sname,function);CHKERRQ(ierr);
34   PetscFunctionReturn(0);
35 }
36 
37 #undef __FUNCT__
38 #define __FUNCT__ "TSTrajectorySet"
39 PetscErrorCode TSTrajectorySet(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal time,Vec X)
40 {
41   PetscErrorCode ierr;
42 
43   PetscFunctionBegin;
44   if (!tj) PetscFunctionReturn(0);
45   ierr = (*tj->ops->set)(tj,ts,stepnum,time,X);CHKERRQ(ierr);
46   PetscFunctionReturn(0);
47 }
48 
49 #undef __FUNCT__
50 #define __FUNCT__ "TSTrajectoryGet"
51 PetscErrorCode TSTrajectoryGet(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal *time)
52 {
53   PetscErrorCode ierr;
54 
55   PetscFunctionBegin;
56   if (!tj) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TS solver did not save trajectory");
57   ierr = (*tj->ops->get)(tj,ts,stepnum,time);CHKERRQ(ierr);
58   PetscFunctionReturn(0);
59 }
60 
61 #undef __FUNCT__
62 #define __FUNCT__ "TSTrajectoryView"
63 /*@C
64     TSTrajectoryView - Prints information about the trajectory object
65 
66     Collective on TSTrajectory
67 
68     Input Parameters:
69 +   ts - the TSTrajectory context obtained from TSTrajectoryCreate()
70 -   viewer - visualization context
71 
72     Options Database Key:
73 .   -ts_view - calls TSView() at end of TSStep()
74 
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.
82 
83     The user can open an alternative visualization context with
84     PetscViewerASCIIOpen() - output to a specified file.
85 
86     Level: beginner
87 
88 .keywords: TS, timestep, view
89 
90 .seealso: PetscViewerASCIIOpen()
91 @*/
92 PetscErrorCode  TSTrajectoryView(TSTrajectory ts,PetscViewer viewer)
93 {
94   PetscErrorCode ierr;
95   PetscBool      iascii;
96 
97   PetscFunctionBegin;
98   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
99   if (!viewer) {
100     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
101   }
102   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
103   PetscCheckSameComm(ts,1,viewer,2);
104 
105   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
106   if (iascii) {
107     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
108     if (ts->ops->view) {
109       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
110     }
111   }
112   PetscFunctionReturn(0);
113 }
114 
115 #undef  __FUNCT__
116 #define __FUNCT__ "TSTrajectoryCreate"
117 /*@C
118   TSTrajectoryCreate - This function creates an empty trajectory object used to store the time dependent solution of an ODE/DAE
119 
120   Collective on MPI_Comm
121 
122   Input Parameter:
123 . comm - The communicator
124 
125   Output Parameter:
126 . tstra   - The trajectory object
127 
128   Level: advanced
129 
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.
132 
133 .keywords: TS, create
134 .seealso: TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType(), TSGetTrajectory()
135 @*/
136 PetscErrorCode  TSTrajectoryCreate(MPI_Comm comm, TSTrajectory *tstra)
137 {
138   TSTrajectory   t;
139   PetscErrorCode ierr;
140 
141   PetscFunctionBegin;
142   PetscValidPointer(tstra,1);
143   *tstra = NULL;
144   ierr = TSInitializePackage();CHKERRQ(ierr);
145 
146   ierr = PetscHeaderCreate(t, TSTRAJECTORY_CLASSID, "TSTrajectory", "Time stepping", "TS", comm, TSTrajectoryDestroy, TSTrajectoryView);CHKERRQ(ierr);
147   *tstra = t;
148   PetscFunctionReturn(0);
149 }
150 
151 #undef __FUNCT__
152 #define __FUNCT__ "TSTrajectorySetType"
153 /*@C
154   TSTrajectorySetType - Sets the storage method to be used as in a trajectory
155 
156   Collective on TS
157 
158   Input Parameters:
159 + ts   - The TS context
160 - type - A known method
161 
162   Options Database Command:
163 . -tstrajectory_type <type> - Sets the method; use -help for a list of available methods (for instance, basic)
164 
165    Level: intermediate
166 
167 .keywords: TS, set, type
168 
169 .seealso: TS, TSSolve(), TSCreate(), TSSetFromOptions(), TSDestroy(), TSType
170 
171 @*/
172 PetscErrorCode  TSTrajectorySetType(TSTrajectory ts,const TSTrajectoryType type)
173 {
174   PetscErrorCode (*r)(TSTrajectory);
175   PetscBool      match;
176   PetscErrorCode ierr;
177 
178   PetscFunctionBegin;
179   PetscValidHeaderSpecific(ts, TSTRAJECTORY_CLASSID,1);
180   ierr = PetscObjectTypeCompare((PetscObject) ts, type, &match);CHKERRQ(ierr);
181   if (match) PetscFunctionReturn(0);
182 
183   ierr = PetscFunctionListFind(TSTrajectoryList,type,&r);CHKERRQ(ierr);
184   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown TSTrajectory type: %s", type);
185   if (ts->ops->destroy) {
186     ierr = (*(ts)->ops->destroy)(ts);CHKERRQ(ierr);
187 
188     ts->ops->destroy = NULL;
189   }
190   ierr = PetscMemzero(ts->ops,sizeof(*ts->ops));CHKERRQ(ierr);
191 
192   ierr = PetscObjectChangeTypeName((PetscObject)ts, type);CHKERRQ(ierr);
193   ierr = (*r)(ts);CHKERRQ(ierr);
194   PetscFunctionReturn(0);
195 }
196 
197 PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Basic(TSTrajectory);
198 PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Singlefile(TSTrajectory);
199 
200 #undef __FUNCT__
201 #define __FUNCT__ "TSTrajectoryRegisterAll"
202 /*@C
203   TSTrajectoryRegisterAll - Registers all of the trajectory storage schecmes in the TS package.
204 
205   Not Collective
206 
207   Level: advanced
208 
209 .keywords: TS, timestepper, register, all
210 .seealso: TSCreate(), TSRegister(), TSRegisterDestroy()
211 @*/
212 PetscErrorCode  TSTrajectoryRegisterAll(void)
213 {
214   PetscErrorCode ierr;
215 
216   PetscFunctionBegin;
217   TSTrajectoryRegisterAllCalled = PETSC_TRUE;
218 
219   ierr = TSTrajectoryRegister(TSTRAJECTORYBASIC,TSTrajectoryCreate_Basic);CHKERRQ(ierr);
220   ierr = TSTrajectoryRegister(TSTRAJECTORYSINGLEFILE,TSTrajectoryCreate_Singlefile);CHKERRQ(ierr);
221   PetscFunctionReturn(0);
222 }
223 
224 #undef __FUNCT__
225 #define __FUNCT__ "TSTrajectoryDestroy"
226 /*@
227    TSTrajectoryDestroy - Destroys a trajectory context
228 
229    Collective on TSTrajectory
230 
231    Input Parameter:
232 .  ts - the TSTrajectory context obtained from TSTrajectoryCreate()
233 
234    Level: advanced
235 
236 .keywords: TS, timestepper, destroy
237 
238 .seealso: TSCreate(), TSSetUp(), TSSolve()
239 @*/
240 PetscErrorCode  TSTrajectoryDestroy(TSTrajectory *ts)
241 {
242   PetscErrorCode ierr;
243 
244   PetscFunctionBegin;
245   if (!*ts) PetscFunctionReturn(0);
246   PetscValidHeaderSpecific((*ts),TSTRAJECTORY_CLASSID,1);
247   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
248 
249   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
250   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
251   PetscFunctionReturn(0);
252 }
253 
254 #undef __FUNCT__
255 #define __FUNCT__ "TSTrajectorySetTypeFromOptions_Private"
256 /*
257   TSTrajectorySetTypeFromOptions_Private - Sets the type of ts from user options.
258 
259   Collective on TSTrajectory
260 
261   Input Parameter:
262 . ts - The ts
263 
264   Level: intermediate
265 
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];
274   PetscErrorCode ierr;
275 
276   PetscFunctionBegin;
277   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
278   else defaultType = TSTRAJECTORYBASIC;
279 
280   if (!TSRegisterAllCalled) {ierr = TSTrajectoryRegisterAll();CHKERRQ(ierr);}
281   ierr = PetscOptionsFList("-tstrajectory_type", "TSTrajectory method"," TSTrajectorySetType", TSTrajectoryList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
282   if (opt) {
283     ierr = TSTrajectorySetType(ts, typeName);CHKERRQ(ierr);
284   } else {
285     ierr = TSTrajectorySetType(ts, defaultType);CHKERRQ(ierr);
286   }
287   PetscFunctionReturn(0);
288 }
289 
290 #undef __FUNCT__
291 #define __FUNCT__ "TSTrajectorySetFromOptions"
292 /*@
293    TSTrajectorySetFromOptions - Sets various TSTrajectory parameters from user options.
294 
295    Collective on TSTrajectory
296 
297    Input Parameter:
298 .  ts - the TSTrajectory context obtained from TSTrajectoryCreate()
299 
300    Options Database Keys:
301 .  -tstrajectory_type <type> - TSTRAJECTORYBASIC
302 
303    Level: advanced
304 
305    Notes: This is not normally called directly by users, instead it is called by TSSetFromOptions() after a call to
306    TSSetSaveTrajectory()
307 
308 .keywords: TS, timestep, set, options, database
309 
310 .seealso: TSGetType(), TSSetSaveTrajectory(), TSGetTrajectory()
311 @*/
312 PetscErrorCode  TSTrajectorySetFromOptions(TSTrajectory ts)
313 {
314   PetscErrorCode ierr;
315 
316   PetscFunctionBegin;
317   PetscValidHeaderSpecific(ts, TSTRAJECTORY_CLASSID,1);
318   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
319   ierr = TSTrajectorySetTypeFromOptions_Private(PetscOptionsObject,ts);CHKERRQ(ierr);
320   ierr = PetscOptionsEnd();CHKERRQ(ierr);
321   PetscFunctionReturn(0);
322 }
323