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