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