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