xref: /petsc/src/sys/classes/viewer/impls/cgns/cgnsv.c (revision bbd20b7e897435610760efc4b644bee35cb0ddb2)
1 #include <petsc/private/viewercgnsimpl.h> /*I "petscviewer.h" I*/
2 #include <petsc/private/dmpleximpl.h>     /*I   "petscdmplex.h"   I*/
3 #if defined(PETSC_HDF5_HAVE_PARALLEL)
4   #include <pcgnslib.h>
5 #else
6   #include <cgnslib.h>
7 #endif
8 #include <cgns_io.h>
9 #include <ctype.h>
10 
11 static PetscErrorCode PetscViewerSetFromOptions_CGNS(PetscViewer v, PetscOptionItems *PetscOptionsObject)
12 {
13   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)v->data;
14 
15   PetscFunctionBegin;
16   PetscOptionsHeadBegin(PetscOptionsObject, "CGNS Viewer Options");
17   PetscCall(PetscOptionsInt("-viewer_cgns_batch_size", "Max number of steps to store in single file when using a template cgns:name-\%d.cgns", "", cgv->batch_size, &cgv->batch_size, NULL));
18   PetscOptionsHeadEnd();
19   PetscFunctionReturn(PETSC_SUCCESS);
20 }
21 
22 static PetscErrorCode PetscViewerView_CGNS(PetscViewer v, PetscViewer viewer)
23 {
24   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)v->data;
25 
26   PetscFunctionBegin;
27   if (cgv->filename) PetscCall(PetscViewerASCIIPrintf(viewer, "Filename: %s\n", cgv->filename));
28   PetscFunctionReturn(PETSC_SUCCESS);
29 }
30 
31 static PetscErrorCode PetscViewerFileClose_CGNS(PetscViewer viewer)
32 {
33   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
34 
35   PetscFunctionBegin;
36   if (cgv->output_times) {
37     PetscCount size, width = 32, *steps;
38     char      *solnames;
39     PetscReal *times;
40     cgsize_t   num_times;
41     PetscCall(PetscSegBufferGetSize(cgv->output_times, &size));
42     PetscCall(PetscSegBufferExtractInPlace(cgv->output_times, &times));
43     num_times = size;
44     PetscCallCGNS(cg_biter_write(cgv->file_num, cgv->base, "TimeIterValues", num_times));
45     PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL));
46     PetscCallCGNS(cg_array_write("TimeValues", CGNS_ENUMV(RealDouble), 1, &num_times, times));
47     PetscCall(PetscSegBufferDestroy(&cgv->output_times));
48     PetscCallCGNS(cg_ziter_write(cgv->file_num, cgv->base, cgv->zone, "ZoneIterativeData"));
49     PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL));
50     PetscCall(PetscMalloc(size * width + 1, &solnames));
51     PetscCall(PetscSegBufferExtractInPlace(cgv->output_steps, &steps));
52     for (PetscCount i = 0; i < size; i++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "FlowSolution%-20zu", (size_t)steps[i]));
53     PetscCall(PetscSegBufferDestroy(&cgv->output_steps));
54     cgsize_t shape[2] = {(cgsize_t)width, (cgsize_t)size};
55     PetscCallCGNS(cg_array_write("FlowSolutionPointers", CGNS_ENUMV(Character), 2, shape, solnames));
56     // The VTK reader looks for names like FlowSolution*Pointers.
57     for (PetscCount i = 0; i < size; i++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "%-32s", "CellInfo"));
58     PetscCallCGNS(cg_array_write("FlowSolutionCellInfoPointers", CGNS_ENUMV(Character), 2, shape, solnames));
59     PetscCall(PetscFree(solnames));
60 
61     PetscCallCGNS(cg_simulation_type_write(cgv->file_num, cgv->base, CGNS_ENUMV(TimeAccurate)));
62   }
63   PetscCall(PetscFree(cgv->filename));
64 #if defined(PETSC_HDF5_HAVE_PARALLEL)
65   if (cgv->file_num) PetscCallCGNS(cgp_close(cgv->file_num));
66 #else
67   if (cgv->file_num) PetscCallCGNS(cg_close(cgv->file_num));
68 #endif
69   cgv->file_num = 0;
70   PetscCall(PetscFree(cgv->node_l2g));
71   PetscCall(PetscFree(cgv->nodal_field));
72   PetscFunctionReturn(PETSC_SUCCESS);
73 }
74 
75 PetscErrorCode PetscViewerCGNSFileOpen_Internal(PetscViewer viewer, PetscInt sequence_number)
76 {
77   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
78 
79   PetscFunctionBegin;
80   PetscCheck((cgv->filename == NULL) ^ (sequence_number < 0), PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_INCOMP, "Expect either a template filename or non-negative sequence number");
81   if (!cgv->filename) {
82     char filename_numbered[PETSC_MAX_PATH_LEN];
83     // Cast sequence_number so %d can be used also when PetscInt is 64-bit. We could upgrade the format string if users
84     // run more than 2B time steps.
85     PetscCall(PetscSNPrintf(filename_numbered, sizeof filename_numbered, cgv->filename_template, (int)sequence_number));
86     PetscCall(PetscStrallocpy(filename_numbered, &cgv->filename));
87   }
88   switch (cgv->btype) {
89   case FILE_MODE_READ:
90 #if defined(PETSC_HDF5_HAVE_PARALLEL)
91     PetscCallCGNS(cgp_mpi_comm(PetscObjectComm((PetscObject)viewer)));
92     PetscCallCGNS(cgp_open(cgv->filename, CG_MODE_READ, &cgv->file_num));
93 #else
94     PetscCallCGNS(cg_open(filename, CG_MODE_READ, &cgv->file_num));
95 #endif
96     break;
97   case FILE_MODE_WRITE:
98 #if defined(PETSC_HDF5_HAVE_PARALLEL)
99     PetscCallCGNS(cgp_mpi_comm(PetscObjectComm((PetscObject)viewer)));
100     PetscCallCGNS(cgp_open(cgv->filename, CG_MODE_WRITE, &cgv->file_num));
101 #else
102     PetscCallCGNS(cg_open(filename, CG_MODE_WRITE, &cgv->file_num));
103 #endif
104     break;
105   case FILE_MODE_UNDEFINED:
106     SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
107   default:
108     SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[cgv->btype]);
109   }
110   PetscFunctionReturn(PETSC_SUCCESS);
111 }
112 
113 PetscErrorCode PetscViewerCGNSCheckBatch_Internal(PetscViewer viewer)
114 {
115   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
116   PetscCount        num_steps;
117 
118   PetscFunctionBegin;
119   if (!cgv->filename_template) PetscFunctionReturn(PETSC_SUCCESS); // Batches are closed when viewer is destroyed
120   PetscCall(PetscSegBufferGetSize(cgv->output_times, &num_steps));
121   if (num_steps >= (PetscCount)cgv->batch_size) PetscCall(PetscViewerFileClose_CGNS(viewer));
122   PetscFunctionReturn(PETSC_SUCCESS);
123 }
124 
125 static PetscErrorCode PetscViewerDestroy_CGNS(PetscViewer viewer)
126 {
127   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
128 
129   PetscFunctionBegin;
130   PetscCall(PetscViewerFileClose_CGNS(viewer));
131   PetscCall(PetscFree(cgv->solution_name));
132   PetscCall(PetscFree(cgv));
133   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL));
134   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL));
135   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL));
136   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL));
137   PetscFunctionReturn(PETSC_SUCCESS);
138 }
139 
140 static PetscErrorCode PetscViewerFileSetMode_CGNS(PetscViewer viewer, PetscFileMode type)
141 {
142   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
143 
144   PetscFunctionBegin;
145   cgv->btype = type;
146   PetscFunctionReturn(PETSC_SUCCESS);
147 }
148 
149 static PetscErrorCode PetscViewerFileGetMode_CGNS(PetscViewer viewer, PetscFileMode *type)
150 {
151   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
152 
153   PetscFunctionBegin;
154   *type = cgv->btype;
155   PetscFunctionReturn(PETSC_SUCCESS);
156 }
157 
158 static PetscErrorCode PetscViewerFileSetName_CGNS(PetscViewer viewer, const char *filename)
159 {
160   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
161   char             *has_pattern;
162 
163   PetscFunctionBegin;
164   if (cgv->file_num) PetscCallCGNS(cg_close(cgv->file_num));
165   PetscCall(PetscFree(cgv->filename));
166   PetscCall(PetscFree(cgv->filename_template));
167   PetscCall(PetscStrchr(filename, '%', &has_pattern));
168   if (has_pattern) {
169     PetscCall(PetscStrallocpy(filename, &cgv->filename_template));
170     // Templated file names open lazily, once we know DMGetOutputSequenceNumber()
171   } else {
172     PetscCall(PetscStrallocpy(filename, &cgv->filename));
173     PetscCall(PetscViewerCGNSFileOpen_Internal(viewer, -1));
174   }
175   PetscFunctionReturn(PETSC_SUCCESS);
176 }
177 
178 static PetscErrorCode PetscViewerFileGetName_CGNS(PetscViewer viewer, const char **filename)
179 {
180   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
181 
182   PetscFunctionBegin;
183   *filename = cgv->filename;
184   PetscFunctionReturn(PETSC_SUCCESS);
185 }
186 
187 /*MC
188    PETSCVIEWERCGNS - A viewer for CGNS files
189 
190   Level: beginner
191 
192   Options Database Key:
193 . -viewer_cgns_batch_size SIZE - set max number of output sequence times to write per batch
194 
195   Note:
196   If the filename contains an integer format character, the CGNS viewer will created a batched output sequence. For
197   example, one could use `-ts_monitor_solution cgns:flow-%d.cgns`. This is desirable if one wants to limit file sizes or
198   if the job might crash/be killed by a resource manager before exiting cleanly.
199 
200 .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `VecView()`, `DMView()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `TSSetFromOptions()`
201 M*/
202 PetscErrorCode PetscViewerCreate_CGNS(PetscViewer v)
203 {
204   PetscViewer_CGNS *cgv;
205 
206   PetscFunctionBegin;
207   PetscCall(PetscNew(&cgv));
208 
209   v->data                = cgv;
210   v->ops->destroy        = PetscViewerDestroy_CGNS;
211   v->ops->setfromoptions = PetscViewerSetFromOptions_CGNS;
212   v->ops->view           = PetscViewerView_CGNS;
213   cgv->btype             = FILE_MODE_UNDEFINED;
214   cgv->filename          = NULL;
215   cgv->batch_size        = 20;
216   cgv->solution_index    = -1; // Default to use the "last" solution
217   cgv->base              = 1;
218   cgv->zone              = 1;
219 
220   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_CGNS));
221   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_CGNS));
222   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_CGNS));
223   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_CGNS));
224   PetscFunctionReturn(PETSC_SUCCESS);
225 }
226 
227 // Find DataArray_t node under the current node (determined by `cg_goto` and friends) that matches `name`
228 // Return the index of that array and (optionally) other data about the array
229 static inline PetscErrorCode CGNS_Find_Array(MPI_Comm comm, const char name[], int *A_index, CGNS_ENUMT(DataType_t) * data_type, int *dim, cgsize_t size[])
230 {
231   int  narrays; // number of arrays under the current node
232   char array_name[CGIO_MAX_NAME_LENGTH + 1];
233   CGNS_ENUMT(DataType_t) data_type_local;
234   int       _dim;
235   cgsize_t  _size[12];
236   PetscBool matches_name = PETSC_FALSE;
237 
238   PetscFunctionBeginUser;
239   PetscCallCGNS(cg_narrays(&narrays));
240   for (int i = 1; i <= narrays; i++) {
241     PetscCallCGNS(cg_array_info(i, array_name, &data_type_local, &_dim, _size));
242     PetscCall(PetscStrcmp(name, array_name, &matches_name));
243     if (matches_name) {
244       *A_index = i;
245       if (data_type) *data_type = data_type_local;
246       if (dim) *dim = _dim;
247       if (size) PetscArraycpy(size, _size, _dim);
248       PetscFunctionReturn(PETSC_SUCCESS);
249     }
250   }
251   SETERRQ(comm, PETSC_ERR_SUP, "Cannot find %s array under current CGNS node", name);
252 }
253 
254 /*@
255   PetscViewerCGNSOpen - Opens a file for CGNS input/output.
256 
257   Collective
258 
259   Input Parameters:
260 + comm - MPI communicator
261 . name - name of file
262 - type - type of file
263 .vb
264     FILE_MODE_WRITE - create new file for binary output
265     FILE_MODE_READ - open existing file for binary input
266     FILE_MODE_APPEND - open existing file for binary output
267 .ve
268 
269   Output Parameter:
270 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
271 
272   Level: beginner
273 
274 .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
275           `DMLoad()`, `PetscFileMode`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
276 @*/
277 PetscErrorCode PetscViewerCGNSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *viewer)
278 {
279   PetscFunctionBegin;
280   PetscAssertPointer(name, 2);
281   PetscAssertPointer(viewer, 4);
282   PetscCall(PetscViewerCreate(comm, viewer));
283   PetscCall(PetscViewerSetType(*viewer, PETSCVIEWERCGNS));
284   PetscCall(PetscViewerFileSetMode(*viewer, type));
285   PetscCall(PetscViewerFileSetName(*viewer, name));
286   PetscCall(PetscViewerSetFromOptions(*viewer));
287   PetscFunctionReturn(PETSC_SUCCESS);
288 }
289 
290 /*@
291   PetscViewerCGNSSetSolutionIndex - Set index of solution
292 
293   Not Collective
294 
295   Input Parameters:
296 + viewer      - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
297 - solution_id - Index of the solution id, or `-1` for the last solution on the file
298 
299   Level: intermediate
300 
301   Notes:
302   By default, `solution_id` is set to `-1` to mean the last solution available in the file.
303   If the file contains a FlowSolutionPointers node, then that array is indexed to determine which FlowSolution_t node to read from.
304   Otherwise, `solution_id` indexes the total available FlowSolution_t nodes in the file.
305 
306   This solution index is used by `VecLoad()` to determine which solution to load from the file
307 
308 .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()`
309 
310 @*/
311 PetscErrorCode PetscViewerCGNSSetSolutionIndex(PetscViewer viewer, PetscInt solution_id)
312 {
313   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
314 
315   PetscFunctionBeginUser;
316   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
317   PetscValidLogicalCollectiveInt(viewer, solution_id, 2);
318   PetscCheck((solution_id != 0) && (solution_id > -2), PetscObjectComm((PetscObject)viewer), PETSC_ERR_USER_INPUT, "Solution index must be either -1 or greater than 0, not %" PetscInt_FMT, solution_id);
319   cgv->solution_index      = solution_id;
320   cgv->solution_file_index = 0; // Reset sol_index when solution_id changes (0 is invalid)
321   PetscFunctionReturn(PETSC_SUCCESS);
322 }
323 
324 /*@
325   PetscViewerCGNSGetSolutionIndex - Get index of solution
326 
327   Not Collective
328 
329   Input Parameter:
330 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
331 
332   Output Parameter:
333 . solution_id - Index of the solution id
334 
335   Level: intermediate
336 
337   Notes:
338   By default, solution_id is set to `-1` to mean the last solution available in the file
339 
340 .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()`
341 
342 @*/
343 PetscErrorCode PetscViewerCGNSGetSolutionIndex(PetscViewer viewer, PetscInt *solution_id)
344 {
345   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
346 
347   PetscFunctionBeginUser;
348   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
349   PetscAssertPointer(solution_id, 2);
350   *solution_id = cgv->solution_index;
351   PetscFunctionReturn(PETSC_SUCCESS);
352 }
353 
354 // Gets the index for the solution in this CGNS file
355 PetscErrorCode PetscViewerCGNSGetSolutionFileIndex_Internal(PetscViewer viewer, int *sol_index)
356 {
357   PetscViewer_CGNS *cgv  = (PetscViewer_CGNS *)viewer->data;
358   MPI_Comm          comm = PetscObjectComm((PetscObject)viewer);
359   int               nsols, cgns_ier;
360   char              buffer[CGIO_MAX_NAME_LENGTH + 1];
361   CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway
362 
363   PetscFunctionBeginUser;
364   if (cgv->solution_file_index > 0) {
365     *sol_index = cgv->solution_file_index;
366     PetscFunctionReturn(PETSC_SUCCESS);
367   }
368 
369   PetscCallCGNS(cg_nsols(cgv->file_num, cgv->base, cgv->zone, &nsols));
370   cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, "FlowSolutionPointers", 0, NULL);
371   if (cgns_ier == CG_NODE_NOT_FOUND) {
372     // If FlowSolutionPointers does not exist, then just index off of nsols (which can include non-solution data)
373     PetscCheck(cgv->solution_index == -1 || cgv->solution_index <= nsols, comm, PETSC_ERR_ARG_OUTOFRANGE, "CGNS Solution index (%" PetscInt_FMT ") not in [1, %d]", cgv->solution_index, nsols);
374 
375     cgv->solution_file_index = cgv->solution_index == -1 ? nsols : cgv->solution_index;
376   } else {
377     // If FlowSolutionPointers exists, then solution_id should index that array of FlowSolutions
378     char     *pointer_id_name;
379     PetscBool matches_name = PETSC_FALSE;
380     int       sol_id;
381 
382     PetscCheck(cgns_ier == CG_OK, PETSC_COMM_SELF, PETSC_ERR_LIB, "CGNS error %d %s", cgns_ier, cg_get_error());
383     cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL);
384 
385     { // Get FlowSolutionPointer name corresponding to solution_id
386       cgsize_t size[12];
387       int      dim, A_index, pointer_id;
388       char    *pointer_names, *pointer_id_name_ref;
389 
390       PetscCall(CGNS_Find_Array(comm, "FlowSolutionPointers", &A_index, NULL, &dim, size));
391       PetscCheck(cgv->solution_index == -1 || cgv->solution_index <= size[1], comm, PETSC_ERR_ARG_OUTOFRANGE, "CGNS Solution index (%" PetscInt_FMT ") not in range of FlowSolutionPointers [1, %" PRIdCGSIZE "]", cgv->solution_index, size[1]);
392       PetscCall(PetscCalloc1(size[0] * size[1] + 1, &pointer_names)); // Need the +1 for (possibly) setting \0 for the last pointer name if it's full
393       PetscCallCGNS(cg_array_read_as(1, CGNS_ENUMV(Character), pointer_names));
394       pointer_id          = cgv->solution_index == -1 ? size[1] : cgv->solution_index;
395       pointer_id_name_ref = &pointer_names[size[0] * (pointer_id - 1)];
396       { // Set last non-whitespace character of the pointer name to \0 (CGNS pads with spaces)
397         int str_idx;
398         for (str_idx = size[0] - 1; str_idx > 0; str_idx--) {
399           if (!isspace((unsigned char)pointer_id_name_ref[str_idx])) break;
400         }
401         pointer_id_name_ref[str_idx + 1] = '\0';
402       }
403       PetscCall(PetscStrallocpy(pointer_id_name_ref, &pointer_id_name));
404       PetscCall(PetscFree(pointer_names));
405     }
406 
407     // Find FlowSolution_t node that matches pointer_id_name
408     for (sol_id = 1; sol_id <= nsols; sol_id++) {
409       PetscCallCGNS(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc));
410       PetscCall(PetscStrcmp(pointer_id_name, buffer, &matches_name));
411       if (matches_name) break;
412     }
413     PetscCheck(matches_name, comm, PETSC_ERR_LIB, "Cannot find FlowSolution_t node %s in file", pointer_id_name);
414     cgv->solution_file_index = sol_id;
415     PetscCall(PetscFree(pointer_id_name));
416   }
417 
418   *sol_index = cgv->solution_file_index;
419   PetscFunctionReturn(PETSC_SUCCESS);
420 }
421 
422 /*@
423   PetscViewerCGNSGetSolutionTime - Gets the solution time for the FlowSolution of the viewer
424 
425   Collective
426 
427   Input Parameter:
428 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
429 
430   Output Parameters:
431 + time - Solution time of the FlowSolution_t node
432 - set  - Whether the time data is in the file
433 
434   Level: intermediate
435 
436   Notes:
437   Reads data from a DataArray named `TimeValues` under a `BaseIterativeData_t` node
438 
439 .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionName()`
440 @*/
441 PetscErrorCode PetscViewerCGNSGetSolutionTime(PetscViewer viewer, PetscReal *time, PetscBool *set)
442 {
443   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
444   int               cgns_ier, A_index = 0;
445   PetscReal        *times;
446   cgsize_t          size[12];
447 
448   PetscFunctionBeginUser;
449   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
450   PetscAssertPointer(time, 2);
451   PetscAssertPointer(set, 3);
452   cgns_ier = cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL);
453   if (cgns_ier == CG_NODE_NOT_FOUND) {
454     *set = PETSC_FALSE;
455     PetscFunctionReturn(PETSC_SUCCESS);
456   } else PetscCallCGNS(cgns_ier);
457   PetscCall(CGNS_Find_Array(PetscObjectComm((PetscObject)viewer), "TimeValues", &A_index, NULL, NULL, size));
458   PetscCall(PetscMalloc1(size[0], &times));
459   PetscCallCGNS(cg_array_read_as(A_index, CGNS_ENUMV(RealDouble), times));
460   *time = times[cgv->solution_index - 1];
461   *set  = PETSC_TRUE;
462   PetscCall(PetscFree(times));
463   PetscFunctionReturn(PETSC_SUCCESS);
464 }
465 
466 /*@
467   PetscViewerCGNSGetSolutionName - Gets name of FlowSolution of the viewer
468 
469   Collective
470 
471   Input Parameter:
472 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
473 
474   Output Parameter:
475 . name - Name of the FlowSolution_t node corresponding to the solution index
476 
477   Level: intermediate
478 
479   Notes:
480   Currently assumes there is only one Zone in the CGNS file
481 
482 .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionTime()`
483 @*/
484 PetscErrorCode PetscViewerCGNSGetSolutionName(PetscViewer viewer, const char *name[])
485 {
486   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
487   int               sol_id;
488   char              buffer[CGIO_MAX_NAME_LENGTH + 1];
489   CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway
490 
491   PetscFunctionBeginUser;
492   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
493   PetscAssertPointer(name, 2);
494   PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id));
495 
496   PetscCallCGNS(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc));
497   if (cgv->solution_name) PetscCall(PetscFree(cgv->solution_name));
498   PetscCall(PetscStrallocpy(buffer, &cgv->solution_name));
499   *name = cgv->solution_name;
500   PetscFunctionReturn(PETSC_SUCCESS);
501 }
502