xref: /petsc/src/sys/classes/viewer/impls/cgns/cgnsv.c (revision 5d8720fa41fb4169420198de95a3fb9ffc339d07)
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 
203 PETSC_EXTERN PetscErrorCode PetscViewerCreate_CGNS(PetscViewer v)
204 {
205   PetscViewer_CGNS *cgv;
206 
207   PetscFunctionBegin;
208   PetscCall(PetscNew(&cgv));
209 
210   v->data                = cgv;
211   v->ops->destroy        = PetscViewerDestroy_CGNS;
212   v->ops->setfromoptions = PetscViewerSetFromOptions_CGNS;
213   v->ops->view           = PetscViewerView_CGNS;
214   cgv->btype             = FILE_MODE_UNDEFINED;
215   cgv->filename          = NULL;
216   cgv->batch_size        = 20;
217   cgv->solution_index    = -1; // Default to use the "last" solution
218   cgv->base              = 1;
219   cgv->zone              = 1;
220 
221   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_CGNS));
222   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_CGNS));
223   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_CGNS));
224   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_CGNS));
225   PetscFunctionReturn(PETSC_SUCCESS);
226 }
227 
228 // Find DataArray_t node under the current node (determined by `cg_goto` and friends) that matches `name`
229 // Return the index of that array and (optionally) other data about the array
230 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[])
231 {
232   int  narrays; // number of arrays under the current node
233   char array_name[CGIO_MAX_NAME_LENGTH + 1];
234   CGNS_ENUMT(DataType_t) data_type_local;
235   int       _dim;
236   cgsize_t  _size[12];
237   PetscBool matches_name = PETSC_FALSE;
238 
239   PetscFunctionBeginUser;
240   PetscCallCGNS(cg_narrays(&narrays));
241   for (int i = 1; i <= narrays; i++) {
242     PetscCallCGNS(cg_array_info(i, array_name, &data_type_local, &_dim, _size));
243     PetscCall(PetscStrcmp(name, array_name, &matches_name));
244     if (matches_name) {
245       *A_index = i;
246       if (data_type) *data_type = data_type_local;
247       if (dim) *dim = _dim;
248       if (size) PetscArraycpy(size, _size, _dim);
249       PetscFunctionReturn(PETSC_SUCCESS);
250     }
251   }
252   SETERRQ(comm, PETSC_ERR_SUP, "Cannot find %s array under current CGNS node", name);
253 }
254 
255 /*@
256   PetscViewerCGNSOpen - Opens a file for CGNS input/output.
257 
258   Collective
259 
260   Input Parameters:
261 + comm - MPI communicator
262 . name - name of file
263 - type - type of file
264 .vb
265     FILE_MODE_WRITE - create new file for binary output
266     FILE_MODE_READ - open existing file for binary input
267     FILE_MODE_APPEND - open existing file for binary output
268 .ve
269 
270   Output Parameter:
271 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
272 
273   Level: beginner
274 
275 .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
276           `DMLoad()`, `PetscFileMode`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
277 @*/
278 PetscErrorCode PetscViewerCGNSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *viewer)
279 {
280   PetscFunctionBegin;
281   PetscCall(PetscViewerCreate(comm, viewer));
282   PetscCall(PetscViewerSetType(*viewer, PETSCVIEWERCGNS));
283   PetscCall(PetscViewerFileSetMode(*viewer, type));
284   PetscCall(PetscViewerFileSetName(*viewer, name));
285   PetscCall(PetscViewerSetFromOptions(*viewer));
286   PetscFunctionReturn(PETSC_SUCCESS);
287 }
288 
289 /*@
290   PetscViewerCGNSSetSolutionIndex - Set index of solution
291 
292   Not Collective
293 
294   Input Parameters:
295 + viewer      - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
296 - solution_id - Index of the solution id, or `-1` for the last solution on the file
297 
298   Level: intermediate
299 
300   Notes:
301   By default, `solution_id` is set to `-1` to mean the last solution available in the file.
302   If the file contains a FlowSolutionPointers node, then that array is indexed to determine which FlowSolution_t node to read from.
303   Otherwise, `solution_id` indexes the total available FlowSolution_t nodes in the file.
304 
305   This solution index is used by `VecLoad()` to determine which solution to load from the file
306 
307 .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()`
308 
309 @*/
310 PetscErrorCode PetscViewerCGNSSetSolutionIndex(PetscViewer viewer, PetscInt solution_id)
311 {
312   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
313 
314   PetscFunctionBeginUser;
315   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);
316   cgv->solution_index      = solution_id;
317   cgv->solution_file_index = 0; // Reset sol_index when solution_id changes (0 is invalid)
318   PetscFunctionReturn(PETSC_SUCCESS);
319 }
320 
321 /*@
322   PetscViewerCGNSGetSolutionIndex - Get index of solution
323 
324   Not Collective
325 
326   Input Parameter:
327 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
328 
329   Output Parameter:
330 . solution_id - Index of the solution id
331 
332   Level: intermediate
333 
334   Notes:
335   By default, solution_id is set to `-1` to mean the last solution available in the file
336 
337 .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()`
338 
339 @*/
340 PetscErrorCode PetscViewerCGNSGetSolutionIndex(PetscViewer viewer, PetscInt *solution_id)
341 {
342   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
343 
344   PetscFunctionBeginUser;
345   *solution_id = cgv->solution_index;
346   PetscFunctionReturn(PETSC_SUCCESS);
347 }
348 
349 // Gets the index for the solution in this CGNS file
350 PetscErrorCode PetscViewerCGNSGetSolutionFileIndex_Internal(PetscViewer viewer, int *sol_index)
351 {
352   PetscViewer_CGNS *cgv  = (PetscViewer_CGNS *)viewer->data;
353   MPI_Comm          comm = PetscObjectComm((PetscObject)viewer);
354   int               nsols, cgns_ier;
355   char              buffer[CGIO_MAX_NAME_LENGTH + 1];
356   CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway
357 
358   PetscFunctionBeginUser;
359   if (cgv->solution_file_index > 0) {
360     *sol_index = cgv->solution_file_index;
361     PetscFunctionReturn(PETSC_SUCCESS);
362   }
363 
364   PetscCallCGNS(cg_nsols(cgv->file_num, cgv->base, cgv->zone, &nsols));
365   cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, "FlowSolutionPointers", 0, NULL);
366   if (cgns_ier == CG_NODE_NOT_FOUND) {
367     // If FlowSolutionPointers does not exist, then just index off of nsols (which can include non-solution data)
368     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);
369 
370     cgv->solution_file_index = cgv->solution_index == -1 ? nsols : cgv->solution_index;
371   } else {
372     // If FlowSolutionPointers exists, then solution_id should index that array of FlowSolutions
373     char     *pointer_id_name;
374     PetscBool matches_name = PETSC_FALSE;
375     int       sol_id;
376 
377     PetscCheck(cgns_ier == CG_OK, PETSC_COMM_SELF, PETSC_ERR_LIB, "CGNS error %d %s", cgns_ier, cg_get_error());
378     cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL);
379 
380     { // Get FlowSolutionPointer name corresponding to solution_id
381       cgsize_t size[12];
382       int      dim, A_index;
383       char    *pointer_names, *pointer_id_name_ref;
384 
385       PetscCall(CGNS_Find_Array(comm, "FlowSolutionPointers", &A_index, NULL, &dim, size));
386       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]);
387       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
388       PetscCallCGNS(cg_array_read_as(1, CGNS_ENUMV(Character), pointer_names));
389       cgv->solution_file_pointer_index = cgv->solution_index == -1 ? size[1] : cgv->solution_index;
390       pointer_id_name_ref              = &pointer_names[size[0] * (cgv->solution_file_pointer_index - 1)];
391       { // Set last non-whitespace character of the pointer name to \0 (CGNS pads with spaces)
392         int str_idx;
393         for (str_idx = size[0] - 1; str_idx > 0; str_idx--) {
394           if (!isspace((unsigned char)pointer_id_name_ref[str_idx])) break;
395         }
396         pointer_id_name_ref[str_idx + 1] = '\0';
397       }
398       PetscCall(PetscStrallocpy(pointer_id_name_ref, &pointer_id_name));
399       PetscCall(PetscFree(pointer_names));
400     }
401 
402     // Find FlowSolution_t node that matches pointer_id_name
403     for (sol_id = 1; sol_id <= nsols; sol_id++) {
404       PetscCallCGNS(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc));
405       PetscCall(PetscStrcmp(pointer_id_name, buffer, &matches_name));
406       if (matches_name) break;
407     }
408     PetscCheck(matches_name, comm, PETSC_ERR_LIB, "Cannot find FlowSolution_t node %s in file", pointer_id_name);
409     cgv->solution_file_index = sol_id;
410     PetscCall(PetscFree(pointer_id_name));
411   }
412 
413   *sol_index = cgv->solution_file_index;
414   PetscFunctionReturn(PETSC_SUCCESS);
415 }
416 
417 /*@
418   PetscViewerCGNSGetSolutionTime - Gets the solution time for the FlowSolution of the viewer
419 
420   Collective
421 
422   Input Parameter:
423 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
424 
425   Output Parameters:
426 + time - Solution time of the FlowSolution_t node
427 - set  - Whether the time data is in the file
428 
429   Level: intermediate
430 
431   Notes:
432   Reads data from a DataArray named `TimeValues` under a `BaseIterativeData_t` node
433 
434 .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionName()`
435 @*/
436 PetscErrorCode PetscViewerCGNSGetSolutionTime(PetscViewer viewer, PetscReal *time, PetscBool *set)
437 {
438   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
439   int               cgns_ier, A_index = 0, sol_id;
440   PetscReal        *times;
441   cgsize_t          size[12];
442 
443   PetscFunctionBeginUser;
444   cgns_ier = cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL);
445   if (cgns_ier == CG_NODE_NOT_FOUND) {
446     *set = PETSC_FALSE;
447     PetscFunctionReturn(PETSC_SUCCESS);
448   } else PetscCallCGNS(cgns_ier);
449   PetscCall(CGNS_Find_Array(PetscObjectComm((PetscObject)viewer), "TimeValues", &A_index, NULL, NULL, size));
450   PetscCall(PetscMalloc1(size[0], &times));
451   PetscCallCGNS(cg_array_read_as(A_index, CGNS_ENUMV(RealDouble), times));
452   PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); // Call to set file pointer index
453   *time = times[cgv->solution_file_pointer_index - 1];
454   *set  = PETSC_TRUE;
455   PetscCall(PetscFree(times));
456   PetscFunctionReturn(PETSC_SUCCESS);
457 }
458 
459 /*@
460   PetscViewerCGNSGetSolutionName - Gets name of FlowSolution of the viewer
461 
462   Collective
463 
464   Input Parameter:
465 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
466 
467   Output Parameter:
468 . name - Name of the FlowSolution_t node corresponding to the solution index
469 
470   Level: intermediate
471 
472   Notes:
473   Currently assumes there is only one Zone in the CGNS file
474 
475 .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionTime()`
476 @*/
477 PetscErrorCode PetscViewerCGNSGetSolutionName(PetscViewer viewer, const char *name[])
478 {
479   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
480   int               sol_id;
481   char              buffer[CGIO_MAX_NAME_LENGTH + 1];
482   CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway
483 
484   PetscFunctionBeginUser;
485   PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id));
486 
487   PetscCallCGNS(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc));
488   if (cgv->solution_name) PetscCall(PetscFree(cgv->solution_name));
489   PetscCall(PetscStrallocpy(buffer, &cgv->solution_name));
490   *name = cgv->solution_name;
491   PetscFunctionReturn(PETSC_SUCCESS);
492 }
493