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