xref: /petsc/src/sys/classes/viewer/impls/cgns/cgnsv.c (revision 58ad77e8b9ee6fdbdfef97ebcff79a2d98620aab)
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->filename_template));
155   PetscCall(PetscFree(cgv));
156   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL));
157   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL));
158   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL));
159   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL));
160   PetscFunctionReturn(PETSC_SUCCESS);
161 }
162 
163 static PetscErrorCode PetscViewerFileSetMode_CGNS(PetscViewer viewer, PetscFileMode type)
164 {
165   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
166 
167   PetscFunctionBegin;
168   cgv->btype = type;
169   PetscFunctionReturn(PETSC_SUCCESS);
170 }
171 
172 static PetscErrorCode PetscViewerFileGetMode_CGNS(PetscViewer viewer, PetscFileMode *type)
173 {
174   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
175 
176   PetscFunctionBegin;
177   *type = cgv->btype;
178   PetscFunctionReturn(PETSC_SUCCESS);
179 }
180 
181 static PetscErrorCode PetscViewerFileSetName_CGNS(PetscViewer viewer, const char *filename)
182 {
183   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
184   char             *has_pattern;
185 
186   PetscFunctionBegin;
187 #if defined(PETSC_HDF5_HAVE_PARALLEL)
188   if (cgv->file_num) PetscCallCGNSClose(cgp_close(cgv->file_num), viewer, 0);
189 #else
190   if (cgv->file_num) PetscCallCGNSClose(cg_close(cgv->file_num), viewer, 0);
191 #endif
192   PetscCall(PetscFree(cgv->filename));
193   PetscCall(PetscFree(cgv->filename_template));
194   PetscCall(PetscStrchr(filename, '%', &has_pattern));
195   if (has_pattern) {
196     PetscCall(PetscStrallocpy(filename, &cgv->filename_template));
197     // Templated file names open lazily, once we know DMGetOutputSequenceNumber()
198   } else {
199     PetscCall(PetscStrallocpy(filename, &cgv->filename));
200     PetscCall(PetscViewerCGNSFileOpen_Internal(viewer, -1));
201   }
202   PetscFunctionReturn(PETSC_SUCCESS);
203 }
204 
205 static PetscErrorCode PetscViewerFileGetName_CGNS(PetscViewer viewer, const char **filename)
206 {
207   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
208 
209   PetscFunctionBegin;
210   *filename = cgv->filename;
211   PetscFunctionReturn(PETSC_SUCCESS);
212 }
213 
214 /*MC
215    PETSCVIEWERCGNS - A viewer for CGNS files
216 
217   Level: beginner
218 
219   Options Database Key:
220 . -viewer_cgns_batch_size SIZE - set max number of output sequence times to write per batch
221 
222   Note:
223   If the filename contains an integer format character, the CGNS viewer will created a batched output sequence. For
224   example, one could use `-ts_monitor_solution cgns:flow-%d.cgns`. This is desirable if one wants to limit file sizes or
225   if the job might crash/be killed by a resource manager before exiting cleanly.
226 
227 .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `VecView()`, `DMView()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `TSSetFromOptions()`
228 M*/
229 PetscErrorCode PetscViewerCreate_CGNS(PetscViewer v)
230 {
231   PetscViewer_CGNS *cgv;
232 
233   PetscFunctionBegin;
234   PetscCall(PetscViewerCGNSRegisterLogEvents_Internal());
235   PetscCall(PetscNew(&cgv));
236 
237   v->data                = cgv;
238   v->ops->destroy        = PetscViewerDestroy_CGNS;
239   v->ops->setfromoptions = PetscViewerSetFromOptions_CGNS;
240   v->ops->view           = PetscViewerView_CGNS;
241   cgv->btype             = FILE_MODE_UNDEFINED;
242   cgv->filename          = NULL;
243   cgv->batch_size        = 20;
244   cgv->solution_index    = -1; // Default to use the "last" solution
245   cgv->base              = 1;
246   cgv->zone              = 1;
247 
248   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_CGNS));
249   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_CGNS));
250   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_CGNS));
251   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_CGNS));
252   PetscFunctionReturn(PETSC_SUCCESS);
253 }
254 
255 // Find DataArray_t node under the current node (determined by `cg_goto` and friends) that matches `name`
256 // Return the index of that array and (optionally) other data about the array
257 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[], PetscBool *found)
258 {
259   int  narrays; // number of arrays under the current node
260   char array_name[CGIO_MAX_NAME_LENGTH + 1];
261   CGNS_ENUMT(DataType_t) data_type_local;
262   int       _dim;
263   cgsize_t  _size[12];
264   PetscBool matches_name = PETSC_FALSE;
265 
266   PetscFunctionBeginUser;
267   PetscCallCGNSRead(cg_narrays(&narrays), 0, 0);
268   for (int i = 1; i <= narrays; i++) {
269     PetscCallCGNSRead(cg_array_info(i, array_name, &data_type_local, &_dim, _size), 0, 0);
270     PetscCall(PetscStrcmp(name, array_name, &matches_name));
271     if (matches_name) {
272       *A_index = i;
273       if (data_type) *data_type = data_type_local;
274       if (dim) *dim = _dim;
275       if (size) PetscArraycpy(size, _size, _dim);
276       if (found) *found = PETSC_TRUE;
277       PetscFunctionReturn(PETSC_SUCCESS);
278     }
279   }
280   if (found) *found = PETSC_FALSE;
281   PetscFunctionReturn(PETSC_SUCCESS);
282 }
283 
284 /*@
285   PetscViewerCGNSOpen - Opens a file for CGNS input/output.
286 
287   Collective
288 
289   Input Parameters:
290 + comm - MPI communicator
291 . name - name of file
292 - type - type of file
293 .vb
294     FILE_MODE_WRITE - create new file for binary output
295     FILE_MODE_READ - open existing file for binary input
296     FILE_MODE_APPEND - open existing file for binary output
297 .ve
298 
299   Output Parameter:
300 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
301 
302   Level: beginner
303 
304 .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
305           `DMLoad()`, `PetscFileMode`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
306 @*/
307 PetscErrorCode PetscViewerCGNSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *viewer)
308 {
309   PetscFunctionBegin;
310   PetscAssertPointer(name, 2);
311   PetscAssertPointer(viewer, 4);
312   PetscCall(PetscViewerCreate(comm, viewer));
313   PetscCall(PetscViewerSetType(*viewer, PETSCVIEWERCGNS));
314   PetscCall(PetscViewerFileSetMode(*viewer, type));
315   PetscCall(PetscViewerFileSetName(*viewer, name));
316   PetscCall(PetscViewerSetFromOptions(*viewer));
317   PetscFunctionReturn(PETSC_SUCCESS);
318 }
319 
320 /*@
321   PetscViewerCGNSSetSolutionIndex - Set index of solution
322 
323   Not Collective
324 
325   Input Parameters:
326 + viewer      - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
327 - solution_id - Index of the solution id, or `-1` for the last solution on the file
328 
329   Level: intermediate
330 
331   Notes:
332   By default, `solution_id` is set to `-1` to mean the last solution available in the file.
333   If the file contains a FlowSolutionPointers node, then that array is indexed to determine which FlowSolution_t node to read from.
334   Otherwise, `solution_id` indexes the total available FlowSolution_t nodes in the file.
335 
336   This solution index is used by `VecLoad()` to determine which solution to load from the file
337 
338 .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()`
339 
340 @*/
341 PetscErrorCode PetscViewerCGNSSetSolutionIndex(PetscViewer viewer, PetscInt solution_id)
342 {
343   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
344 
345   PetscFunctionBeginUser;
346   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
347   PetscValidLogicalCollectiveInt(viewer, solution_id, 2);
348   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);
349   cgv->solution_index      = solution_id;
350   cgv->solution_file_index = 0; // Reset sol_index when solution_id changes (0 is invalid)
351   PetscFunctionReturn(PETSC_SUCCESS);
352 }
353 
354 /*@
355   PetscViewerCGNSGetSolutionIndex - Get index of solution
356 
357   Not Collective
358 
359   Input Parameter:
360 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
361 
362   Output Parameter:
363 . solution_id - Index of the solution id
364 
365   Level: intermediate
366 
367   Notes:
368   By default, solution_id is set to `-1` to mean the last solution available in the file
369 
370 .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()`
371 
372 @*/
373 PetscErrorCode PetscViewerCGNSGetSolutionIndex(PetscViewer viewer, PetscInt *solution_id)
374 {
375   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
376 
377   PetscFunctionBeginUser;
378   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
379   PetscAssertPointer(solution_id, 2);
380   *solution_id = cgv->solution_index;
381   PetscFunctionReturn(PETSC_SUCCESS);
382 }
383 
384 // Gets the index for the solution in this CGNS file
385 PetscErrorCode PetscViewerCGNSGetSolutionFileIndex_Internal(PetscViewer viewer, int *sol_index)
386 {
387   PetscViewer_CGNS *cgv  = (PetscViewer_CGNS *)viewer->data;
388   MPI_Comm          comm = PetscObjectComm((PetscObject)viewer);
389   int               nsols, cgns_ier;
390   char              buffer[CGIO_MAX_NAME_LENGTH + 1];
391   CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway
392 
393   PetscFunctionBeginUser;
394   if (cgv->solution_file_index > 0) {
395     *sol_index = cgv->solution_file_index;
396     PetscFunctionReturn(PETSC_SUCCESS);
397   }
398 
399   PetscCallCGNSRead(cg_nsols(cgv->file_num, cgv->base, cgv->zone, &nsols), viewer, 0);
400   cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, "FlowSolutionPointers", 0, NULL);
401   if (cgns_ier == CG_NODE_NOT_FOUND) {
402     // If FlowSolutionPointers does not exist, then just index off of nsols (which can include non-solution data)
403     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);
404 
405     cgv->solution_file_index = cgv->solution_index == -1 ? nsols : cgv->solution_index;
406   } else {
407     // If FlowSolutionPointers exists, then solution_id should index that array of FlowSolutions
408     char     *pointer_id_name;
409     PetscBool matches_name = PETSC_FALSE;
410     int       sol_id;
411 
412     PetscCheck(cgns_ier == CG_OK, PETSC_COMM_SELF, PETSC_ERR_LIB, "CGNS error %d %s", cgns_ier, cg_get_error());
413     cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL);
414 
415     { // Get FlowSolutionPointer name corresponding to solution_id
416       cgsize_t  size[12];
417       int       dim, A_index;
418       char     *pointer_names, *pointer_id_name_ref;
419       PetscBool found_array;
420 
421       PetscCall(CGNS_Find_Array(comm, "FlowSolutionPointers", &A_index, NULL, &dim, size, &found_array));
422       PetscCheck(found_array, comm, PETSC_ERR_SUP, "Cannot find FlowSolutionPointers array under current CGNS node");
423       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]);
424       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
425       PetscCallCGNSRead(cg_array_read_as(1, CGNS_ENUMV(Character), pointer_names), viewer, 0);
426       cgv->solution_file_pointer_index = cgv->solution_index == -1 ? size[1] : cgv->solution_index;
427       pointer_id_name_ref              = &pointer_names[size[0] * (cgv->solution_file_pointer_index - 1)];
428       { // Set last non-whitespace character of the pointer name to \0 (CGNS pads with spaces)
429         int str_idx;
430         for (str_idx = size[0] - 1; str_idx > 0; str_idx--) {
431           if (!isspace((unsigned char)pointer_id_name_ref[str_idx])) break;
432         }
433         pointer_id_name_ref[str_idx + 1] = '\0';
434       }
435       PetscCall(PetscStrallocpy(pointer_id_name_ref, &pointer_id_name));
436       PetscCall(PetscFree(pointer_names));
437     }
438 
439     // Find FlowSolution_t node that matches pointer_id_name
440     for (sol_id = 1; sol_id <= nsols; sol_id++) {
441       PetscCallCGNSRead(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc), viewer, 0);
442       PetscCall(PetscStrcmp(pointer_id_name, buffer, &matches_name));
443       if (matches_name) break;
444     }
445     PetscCheck(matches_name, comm, PETSC_ERR_LIB, "Cannot find FlowSolution_t node %s in file", pointer_id_name);
446     cgv->solution_file_index = sol_id;
447     PetscCall(PetscFree(pointer_id_name));
448   }
449 
450   *sol_index = cgv->solution_file_index;
451   PetscFunctionReturn(PETSC_SUCCESS);
452 }
453 
454 /*@
455   PetscViewerCGNSGetSolutionTime - Gets the solution time for the FlowSolution of the viewer
456 
457   Collective
458 
459   Input Parameter:
460 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
461 
462   Output Parameters:
463 + time - Solution time of the FlowSolution_t node
464 - set  - Whether the time data is in the file
465 
466   Level: intermediate
467 
468   Notes:
469   Reads data from a DataArray named `TimeValues` under a `BaseIterativeData_t` node
470 
471 .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSGetSolutionIteration()`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionName()`
472 @*/
473 PetscErrorCode PetscViewerCGNSGetSolutionTime(PetscViewer viewer, PetscReal *time, PetscBool *set)
474 {
475   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
476   int               cgns_ier, A_index = 0, sol_id;
477   PetscReal        *times;
478   cgsize_t          size[12];
479 
480   PetscFunctionBeginUser;
481   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
482   PetscAssertPointer(time, 2);
483   PetscAssertPointer(set, 3);
484   cgns_ier = cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL);
485   if (cgns_ier == CG_NODE_NOT_FOUND) {
486     *set = PETSC_FALSE;
487     PetscFunctionReturn(PETSC_SUCCESS);
488   } else PetscCallCGNS(cgns_ier);
489   PetscCall(CGNS_Find_Array(PetscObjectComm((PetscObject)viewer), "TimeValues", &A_index, NULL, NULL, size, set));
490   if (!*set) PetscFunctionReturn(PETSC_SUCCESS);
491   PetscCall(PetscMalloc1(size[0], &times));
492   PetscCallCGNSRead(cg_array_read_as(A_index, CGNS_ENUMV(RealDouble), times), viewer, 0);
493   PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); // Call to set file pointer index
494   *time = times[cgv->solution_file_pointer_index - 1];
495   PetscCall(PetscFree(times));
496   PetscFunctionReturn(PETSC_SUCCESS);
497 }
498 
499 /*@
500   PetscViewerCGNSGetSolutionIteration - Gets the solution iteration for the FlowSolution of the viewer
501 
502   Collective
503 
504   Input Parameter:
505 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
506 
507   Output Parameters:
508 + iteration - Solution iteration of the FlowSolution_t node
509 - set       - Whether the time data is in the file
510 
511   Level: intermediate
512 
513   Notes:
514   Reads data from a DataArray named `IterationValues` under a `BaseIterativeData_t` node
515 
516 .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSGetSolutionTime()`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionName()`
517 @*/
518 PetscErrorCode PetscViewerCGNSGetSolutionIteration(PetscViewer viewer, PetscInt *iteration, PetscBool *set)
519 {
520   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
521   int               cgns_ier, A_index = 0, sol_id;
522   int              *steps;
523   cgsize_t          size[12];
524 
525   PetscFunctionBeginUser;
526   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
527   PetscAssertPointer(iteration, 2);
528   PetscAssertPointer(set, 3);
529   cgns_ier = cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL);
530   if (cgns_ier == CG_NODE_NOT_FOUND) {
531     *set = PETSC_FALSE;
532     PetscFunctionReturn(PETSC_SUCCESS);
533   } else PetscCallCGNS(cgns_ier);
534   PetscCall(CGNS_Find_Array(PetscObjectComm((PetscObject)viewer), "IterationValues", &A_index, NULL, NULL, size, set));
535   if (!*set) PetscFunctionReturn(PETSC_SUCCESS);
536   PetscCall(PetscMalloc1(size[0], &steps));
537   PetscCallCGNSRead(cg_array_read_as(A_index, CGNS_ENUMV(Integer), steps), viewer, 0);
538   PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); // Call to set file pointer index
539   *iteration = (PetscInt)steps[cgv->solution_file_pointer_index - 1];
540   PetscCall(PetscFree(steps));
541   PetscFunctionReturn(PETSC_SUCCESS);
542 }
543 
544 /*@
545   PetscViewerCGNSGetSolutionName - Gets name of FlowSolution of the viewer
546 
547   Collective
548 
549   Input Parameter:
550 . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
551 
552   Output Parameter:
553 . name - Name of the FlowSolution_t node corresponding to the solution index
554 
555   Level: intermediate
556 
557   Notes:
558   Currently assumes there is only one Zone in the CGNS file
559 
560 .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionTime()`
561 @*/
562 PetscErrorCode PetscViewerCGNSGetSolutionName(PetscViewer viewer, const char *name[])
563 {
564   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
565   int               sol_id;
566   char              buffer[CGIO_MAX_NAME_LENGTH + 1];
567   CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway
568 
569   PetscFunctionBeginUser;
570   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
571   PetscAssertPointer(name, 2);
572   PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id));
573 
574   PetscCallCGNSRead(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc), viewer, 0);
575   if (cgv->solution_name) PetscCall(PetscFree(cgv->solution_name));
576   PetscCall(PetscStrallocpy(buffer, &cgv->solution_name));
577   *name = cgv->solution_name;
578   PetscFunctionReturn(PETSC_SUCCESS);
579 }
580