Actual source code: cgnsv.c
1: #include <petsc/private/viewercgnsimpl.h>
2: #include <petsc/private/dmpleximpl.h>
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>
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;
13: PetscErrorCode PetscViewerCGNSRegisterLogEvents_Internal()
14: {
15: static PetscBool is_initialized = PETSC_FALSE;
17: PetscFunctionBegin;
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: }
29: static PetscErrorCode PetscViewerSetFromOptions_CGNS(PetscViewer v, PetscOptionItems PetscOptionsObject)
30: {
31: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)v->data;
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: }
40: static PetscErrorCode PetscViewerView_CGNS(PetscViewer v, PetscViewer viewer)
41: {
42: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)v->data;
44: PetscFunctionBegin;
45: if (cgv->filename) PetscCall(PetscViewerASCIIPrintf(viewer, "Filename: %s\n", cgv->filename));
46: PetscFunctionReturn(PETSC_SUCCESS);
47: }
49: static PetscErrorCode PetscViewerFileClose_CGNS(PetscViewer viewer)
50: {
51: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
53: PetscFunctionBegin;
54: if (cgv->file_num && cgv->base && cgv->num_descriptors > 0) {
55: int cgns_ier;
57: cgns_ier = cg_goto(cgv->file_num, cgv->base, NULL);
58: if (cgns_ier == CG_OK) {
59: for (PetscInt i = 0; i < cgv->num_descriptors; i++) {
60: PetscCallCGNSWrite(cg_descriptor_write(cgv->descriptor_names[i], cgv->descriptor_values[i]), viewer, 0);
61: }
62: // Don't throw error if base isn't written to file
63: } else if (cgns_ier != CG_NODE_NOT_FOUND) PetscCallCGNS(cgns_ier);
64: }
66: if (cgv->output_times) {
67: PetscCount size, width = 32, *steps;
68: char *solnames;
69: PetscReal *times;
70: cgsize_t num_times;
71: PetscCall(PetscSegBufferGetSize(cgv->output_times, &size));
72: PetscCall(PetscSegBufferExtractInPlace(cgv->output_times, ×));
73: PetscCall(PetscSegBufferExtractInPlace(cgv->output_steps, &steps));
74: num_times = size;
75: PetscCallCGNSWrite(cg_biter_write(cgv->file_num, cgv->base, "TimeIterValues", (int)num_times), viewer, 0);
76: PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL));
77: PetscCallCGNSWrite(cg_array_write("TimeValues", CGNS_ENUMV(RealDouble), 1, &num_times, times), viewer, 0);
78: { // Cast output_steps to long for writing into file
79: int *steps_int;
80: PetscCall(PetscMalloc1(size, &steps_int));
81: for (PetscCount i = 0; i < size; i++) PetscCall(PetscCIntCast(steps[i], &steps_int[i]));
82: PetscCallCGNSWrite(cg_array_write("IterationValues", CGNS_ENUMV(Integer), 1, &num_times, steps_int), viewer, 0);
83: PetscCall(PetscFree(steps_int));
84: }
85: PetscCall(PetscSegBufferDestroy(&cgv->output_times));
86: PetscCallCGNSWrite(cg_ziter_write(cgv->file_num, cgv->base, cgv->zone, "ZoneIterativeData"), viewer, 0);
87: PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL));
88: PetscCall(PetscMalloc(size * width + 1, &solnames));
89: for (PetscCount i = 0; i < size; i++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "FlowSolution%-20zu", (size_t)steps[i]));
90: PetscCall(PetscSegBufferDestroy(&cgv->output_steps));
91: cgsize_t shape[2] = {(cgsize_t)width, (cgsize_t)size};
92: PetscCallCGNSWrite(cg_array_write("FlowSolutionPointers", CGNS_ENUMV(Character), 2, shape, solnames), viewer, 0);
93: // The VTK reader looks for names like FlowSolution*Pointers.
94: for (PetscCount i = 0; i < size; i++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "%-32s", "CellInfo"));
95: PetscCallCGNSWrite(cg_array_write("FlowSolutionCellInfoPointers", CGNS_ENUMV(Character), 2, shape, solnames), viewer, 0);
96: PetscCall(PetscFree(solnames));
98: PetscCallCGNSWrite(cg_simulation_type_write(cgv->file_num, cgv->base, CGNS_ENUMV(TimeAccurate)), viewer, 0);
99: }
100: PetscCall(PetscFree(cgv->filename));
101: #if defined(PETSC_HDF5_HAVE_PARALLEL)
102: if (cgv->file_num) PetscCallCGNSClose(cgp_close(cgv->file_num), viewer, 0);
103: #else
104: if (cgv->file_num) PetscCallCGNSClose(cg_close(cgv->file_num), viewer, 0);
105: #endif
106: cgv->file_num = 0;
107: PetscCall(PetscFree(cgv->node_l2g));
108: for (PetscInt i = 0; i < cgv->num_nodal_fields; i++) PetscCall(PetscFree(cgv->nodal_fields[i]));
109: cgv->num_nodal_fields = 0;
110: PetscCall(PetscFree(cgv->nodal_fields));
111: PetscFunctionReturn(PETSC_SUCCESS);
112: }
114: PetscErrorCode PetscViewerCGNSFileOpen_Internal(PetscViewer viewer, PetscInt sequence_number)
115: {
116: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
117: int cg_file_mode = -1;
119: PetscFunctionBegin;
120: PetscCheck((cgv->filename == NULL) ^ (sequence_number < 0), PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_INCOMP, "Expect either a template filename or non-negative sequence number");
121: if (!cgv->filename) {
122: char filename_numbered[PETSC_MAX_PATH_LEN];
123: // Cast sequence_number so %d can be used also when PetscInt is 64-bit. We could upgrade the format string if users
124: // run more than 2B time steps.
125: PetscCall(PetscSNPrintf(filename_numbered, sizeof filename_numbered, cgv->filename_template, (int)sequence_number));
126: PetscCall(PetscStrallocpy(filename_numbered, &cgv->filename));
127: }
128: switch (cgv->btype) {
129: case FILE_MODE_READ:
130: cg_file_mode = CG_MODE_READ;
131: break;
132: case FILE_MODE_WRITE:
133: cg_file_mode = CG_MODE_WRITE;
134: break;
135: case FILE_MODE_UNDEFINED:
136: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
137: default:
138: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[cgv->btype]);
139: }
140: #if defined(PETSC_HDF5_HAVE_PARALLEL)
141: PetscCallCGNS(cgp_mpi_comm(PetscObjectComm((PetscObject)viewer)));
142: PetscCallCGNSOpen(cgp_open(cgv->filename, cg_file_mode, &cgv->file_num), viewer, 0);
143: #else
144: PetscCallCGNSOpen(cg_open(filename, cg_file_mode, &cgv->file_num), viewer, 0);
145: #endif
146: PetscFunctionReturn(PETSC_SUCCESS);
147: }
149: PetscErrorCode PetscViewerCGNSCheckBatch_Internal(PetscViewer viewer)
150: {
151: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
152: PetscCount num_steps;
154: PetscFunctionBegin;
155: if (!cgv->filename_template) PetscFunctionReturn(PETSC_SUCCESS); // Batches are closed when viewer is destroyed
156: PetscCall(PetscSegBufferGetSize(cgv->output_times, &num_steps));
157: if (num_steps >= (PetscCount)cgv->batch_size) PetscCall(PetscViewerFileClose_CGNS(viewer));
158: PetscFunctionReturn(PETSC_SUCCESS);
159: }
161: static PetscErrorCode PetscViewerDestroy_CGNS(PetscViewer viewer)
162: {
163: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
165: PetscFunctionBegin;
166: PetscCall(PetscViewerFileClose_CGNS(viewer));
167: PetscCall(PetscFree(cgv->solution_name));
168: PetscCall(PetscFree(cgv->filename_template));
169: for (PetscInt i = 0; i < cgv->num_descriptors; i++) {
170: PetscCall(PetscFree(cgv->descriptor_names[i]));
171: PetscCall(PetscFree(cgv->descriptor_values[i]));
172: }
173: PetscCall(PetscFree(cgv->descriptor_names));
174: PetscCall(PetscFree(cgv->descriptor_values));
175: PetscCall(PetscFree(cgv));
176: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL));
177: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL));
178: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL));
179: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL));
180: PetscFunctionReturn(PETSC_SUCCESS);
181: }
183: static PetscErrorCode PetscViewerFileSetMode_CGNS(PetscViewer viewer, PetscFileMode type)
184: {
185: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
187: PetscFunctionBegin;
188: cgv->btype = type;
189: PetscFunctionReturn(PETSC_SUCCESS);
190: }
192: static PetscErrorCode PetscViewerFileGetMode_CGNS(PetscViewer viewer, PetscFileMode *type)
193: {
194: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
196: PetscFunctionBegin;
197: *type = cgv->btype;
198: PetscFunctionReturn(PETSC_SUCCESS);
199: }
201: static PetscErrorCode PetscViewerFileSetName_CGNS(PetscViewer viewer, const char *filename)
202: {
203: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
204: char *has_pattern;
206: PetscFunctionBegin;
207: #if defined(PETSC_HDF5_HAVE_PARALLEL)
208: if (cgv->file_num) PetscCallCGNSClose(cgp_close(cgv->file_num), viewer, 0);
209: #else
210: if (cgv->file_num) PetscCallCGNSClose(cg_close(cgv->file_num), viewer, 0);
211: #endif
212: PetscCall(PetscFree(cgv->filename));
213: PetscCall(PetscFree(cgv->filename_template));
214: PetscCall(PetscStrchr(filename, '%', &has_pattern));
215: if (has_pattern) {
216: PetscCall(PetscStrallocpy(filename, &cgv->filename_template));
217: // Templated file names open lazily, once we know DMGetOutputSequenceNumber()
218: } else {
219: PetscCall(PetscStrallocpy(filename, &cgv->filename));
220: PetscCall(PetscViewerCGNSFileOpen_Internal(viewer, -1));
221: }
222: PetscFunctionReturn(PETSC_SUCCESS);
223: }
225: static PetscErrorCode PetscViewerFileGetName_CGNS(PetscViewer viewer, const char **filename)
226: {
227: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
229: PetscFunctionBegin;
230: *filename = cgv->filename;
231: PetscFunctionReturn(PETSC_SUCCESS);
232: }
234: /*MC
235: PETSCVIEWERCGNS - A viewer for CGNS files
237: Level: beginner
239: Options Database Key:
240: . -viewer_cgns_batch_size SIZE - set max number of output sequence times to write per batch
242: Note:
243: If the filename contains an integer format character, the CGNS viewer will created a batched output sequence. For
244: example, one could use `-ts_monitor_solution cgns:flow-%d.cgns`. This is desirable if one wants to limit file sizes or
245: if the job might crash/be killed by a resource manager before exiting cleanly.
247: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `VecView()`, `DMView()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `TSSetFromOptions()`
248: M*/
249: PETSC_EXTERN PetscErrorCode PetscViewerCreate_CGNS(PetscViewer v)
250: {
251: PetscViewer_CGNS *cgv;
253: PetscFunctionBegin;
254: PetscCall(PetscViewerCGNSRegisterLogEvents_Internal());
255: PetscCall(PetscNew(&cgv));
257: v->data = cgv;
258: v->ops->destroy = PetscViewerDestroy_CGNS;
259: v->ops->setfromoptions = PetscViewerSetFromOptions_CGNS;
260: v->ops->view = PetscViewerView_CGNS;
261: cgv->btype = FILE_MODE_UNDEFINED;
262: cgv->filename = NULL;
263: cgv->batch_size = 20;
264: cgv->solution_index = -1; // Default to use the "last" solution
265: cgv->base = 1;
266: cgv->zone = 1;
267: cgv->previous_output_step = -1; // No previous output step
269: PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_CGNS));
270: PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_CGNS));
271: PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_CGNS));
272: PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_CGNS));
273: PetscFunctionReturn(PETSC_SUCCESS);
274: }
276: // Find DataArray_t node under the current node (determined by `cg_goto` and friends) that matches `name`
277: // Return the index of that array and (optionally) other data about the array
278: 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)
279: {
280: int narrays; // number of arrays under the current node
281: char array_name[CGIO_MAX_NAME_LENGTH + 1];
282: CGNS_ENUMT(DataType_t) data_type_local;
283: int _dim;
284: cgsize_t _size[12];
285: PetscBool matches_name = PETSC_FALSE;
287: PetscFunctionBeginUser;
288: PetscCallCGNSRead(cg_narrays(&narrays), 0, 0);
289: for (int i = 1; i <= narrays; i++) {
290: PetscCallCGNSRead(cg_array_info(i, array_name, &data_type_local, &_dim, _size), 0, 0);
291: PetscCall(PetscStrcmp(name, array_name, &matches_name));
292: if (matches_name) {
293: *A_index = i;
294: if (data_type) *data_type = data_type_local;
295: if (dim) *dim = _dim;
296: if (size) PetscArraycpy(size, _size, _dim);
297: if (found) *found = PETSC_TRUE;
298: PetscFunctionReturn(PETSC_SUCCESS);
299: }
300: }
301: if (found) *found = PETSC_FALSE;
302: PetscFunctionReturn(PETSC_SUCCESS);
303: }
305: /*@
306: PetscViewerCGNSOpen - Opens a file for CGNS input/output.
308: Collective
310: Input Parameters:
311: + comm - MPI communicator
312: . name - name of file
313: - type - type of file
314: .vb
315: FILE_MODE_WRITE - create new file for binary output
316: FILE_MODE_READ - open existing file for binary input
317: FILE_MODE_APPEND - open existing file for binary output
318: .ve
320: Output Parameter:
321: . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
323: Level: beginner
325: .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
326: `DMLoad()`, `PetscFileMode`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
327: @*/
328: PetscErrorCode PetscViewerCGNSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *viewer)
329: {
330: PetscFunctionBegin;
331: PetscAssertPointer(name, 2);
332: PetscAssertPointer(viewer, 4);
333: PetscCall(PetscViewerCreate(comm, viewer));
334: PetscCall(PetscViewerSetType(*viewer, PETSCVIEWERCGNS));
335: PetscCall(PetscViewerFileSetMode(*viewer, type));
336: PetscCall(PetscViewerFileSetName(*viewer, name));
337: PetscCall(PetscViewerSetFromOptions(*viewer));
338: PetscFunctionReturn(PETSC_SUCCESS);
339: }
341: /*@
342: PetscViewerCGNSSetSolutionIndex - Set index of solution
344: Not Collective
346: Input Parameters:
347: + viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
348: - solution_id - Index of the solution id, or `-1` for the last solution on the file
350: Level: intermediate
352: Notes:
353: By default, `solution_id` is set to `-1` to mean the last solution available in the file.
354: If the file contains a FlowSolutionPointers node, then that array is indexed to determine which FlowSolution_t node to read from.
355: Otherwise, `solution_id` indexes the total available FlowSolution_t nodes in the file.
357: This solution index is used by `VecLoad()` to determine which solution to load from the file
359: .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()`
360: @*/
361: PetscErrorCode PetscViewerCGNSSetSolutionIndex(PetscViewer viewer, PetscInt solution_id)
362: {
363: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
365: PetscFunctionBegin;
368: 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);
369: cgv->solution_index = solution_id;
370: cgv->solution_file_index = 0; // Reset sol_index when solution_id changes (0 is invalid)
371: PetscFunctionReturn(PETSC_SUCCESS);
372: }
374: /*@
375: PetscViewerCGNSGetSolutionIndex - Get index of solution
377: Not Collective
379: Input Parameter:
380: . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
382: Output Parameter:
383: . solution_id - Index of the solution id
385: Level: intermediate
387: Notes:
388: By default, solution_id is set to `-1` to mean the last solution available in the file
390: .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()`
391: @*/
392: PetscErrorCode PetscViewerCGNSGetSolutionIndex(PetscViewer viewer, PetscInt *solution_id)
393: {
394: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
396: PetscFunctionBegin;
398: PetscAssertPointer(solution_id, 2);
399: *solution_id = cgv->solution_index;
400: PetscFunctionReturn(PETSC_SUCCESS);
401: }
403: // Gets the index for the solution in this CGNS file
404: PetscErrorCode PetscViewerCGNSGetSolutionFileIndex_Internal(PetscViewer viewer, int *sol_index)
405: {
406: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
407: MPI_Comm comm = PetscObjectComm((PetscObject)viewer);
408: int nsols, cgns_ier;
409: char buffer[CGIO_MAX_NAME_LENGTH + 1];
410: CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway
412: PetscFunctionBegin;
413: if (cgv->solution_file_index > 0) {
414: *sol_index = cgv->solution_file_index;
415: PetscFunctionReturn(PETSC_SUCCESS);
416: }
418: PetscCallCGNSRead(cg_nsols(cgv->file_num, cgv->base, cgv->zone, &nsols), viewer, 0);
419: cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, "FlowSolutionPointers", 0, NULL);
420: if (cgns_ier == CG_NODE_NOT_FOUND) {
421: // If FlowSolutionPointers does not exist, then just index off of nsols (which can include non-solution data)
422: 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);
424: cgv->solution_file_index = cgv->solution_index == -1 ? nsols : cgv->solution_index;
425: } else {
426: // If FlowSolutionPointers exists, then solution_id should index that array of FlowSolutions
427: char *pointer_id_name;
428: PetscBool matches_name = PETSC_FALSE;
429: int sol_id;
431: PetscCheck(cgns_ier == CG_OK, PETSC_COMM_SELF, PETSC_ERR_LIB, "CGNS error %d %s", cgns_ier, cg_get_error());
432: cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL);
434: { // Get FlowSolutionPointer name corresponding to solution_id
435: cgsize_t size[12];
436: int dim, A_index;
437: char *pointer_names, *pointer_id_name_ref;
438: PetscBool found_array;
440: PetscCall(CGNS_Find_Array(comm, "FlowSolutionPointers", &A_index, NULL, &dim, size, &found_array));
441: PetscCheck(found_array, comm, PETSC_ERR_SUP, "Cannot find FlowSolutionPointers array under current CGNS node");
442: 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]);
443: 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
444: PetscCallCGNSRead(cg_array_read_as(1, CGNS_ENUMV(Character), pointer_names), viewer, 0);
445: cgv->solution_file_pointer_index = cgv->solution_index == -1 ? (int)size[1] : cgv->solution_index;
446: pointer_id_name_ref = &pointer_names[size[0] * (cgv->solution_file_pointer_index - 1)];
447: { // Set last non-whitespace character of the pointer name to \0 (CGNS pads with spaces)
448: cgsize_t str_idx;
449: for (str_idx = size[0] - 1; str_idx > 0; str_idx--) {
450: if (!isspace((unsigned char)pointer_id_name_ref[str_idx])) break;
451: }
452: pointer_id_name_ref[str_idx + 1] = '\0';
453: }
454: PetscCall(PetscStrallocpy(pointer_id_name_ref, &pointer_id_name));
455: PetscCall(PetscFree(pointer_names));
456: }
458: // Find FlowSolution_t node that matches pointer_id_name
459: for (sol_id = 1; sol_id <= nsols; sol_id++) {
460: PetscCallCGNSRead(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc), viewer, 0);
461: PetscCall(PetscStrcmp(pointer_id_name, buffer, &matches_name));
462: if (matches_name) break;
463: }
464: PetscCheck(matches_name, comm, PETSC_ERR_LIB, "Cannot find FlowSolution_t node %s in file", pointer_id_name);
465: cgv->solution_file_index = sol_id;
466: PetscCall(PetscFree(pointer_id_name));
467: }
469: *sol_index = cgv->solution_file_index;
470: PetscFunctionReturn(PETSC_SUCCESS);
471: }
473: /*@
474: PetscViewerCGNSGetSolutionTime - Gets the solution time for the FlowSolution of the viewer
476: Collective
478: Input Parameter:
479: . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
481: Output Parameters:
482: + time - Solution time of the FlowSolution_t node
483: - set - Whether the time data is in the file
485: Level: intermediate
487: Notes:
488: Reads data from a DataArray named `TimeValues` under a `BaseIterativeData_t` node
490: .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSGetSolutionIteration()`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionName()`
491: @*/
492: PetscErrorCode PetscViewerCGNSGetSolutionTime(PetscViewer viewer, PetscReal *time, PetscBool *set)
493: {
494: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
495: int cgns_ier, A_index = 0, sol_id;
496: PetscReal *times;
497: cgsize_t size[12];
499: PetscFunctionBegin;
501: PetscAssertPointer(time, 2);
502: PetscAssertPointer(set, 3);
503: cgns_ier = cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL);
504: if (cgns_ier == CG_NODE_NOT_FOUND) {
505: *set = PETSC_FALSE;
506: PetscFunctionReturn(PETSC_SUCCESS);
507: } else PetscCallCGNS(cgns_ier);
508: PetscCall(CGNS_Find_Array(PetscObjectComm((PetscObject)viewer), "TimeValues", &A_index, NULL, NULL, size, set));
509: if (!*set) PetscFunctionReturn(PETSC_SUCCESS);
510: PetscCall(PetscMalloc1(size[0], ×));
511: PetscCallCGNSRead(cg_array_read_as(A_index, CGNS_ENUMV(RealDouble), times), viewer, 0);
512: PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); // Call to set file pointer index
513: *time = times[cgv->solution_file_pointer_index - 1];
514: PetscCall(PetscFree(times));
515: PetscFunctionReturn(PETSC_SUCCESS);
516: }
518: /*@
519: PetscViewerCGNSGetSolutionIteration - Gets the solution iteration for the FlowSolution of the viewer
521: Collective
523: Input Parameter:
524: . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
526: Output Parameters:
527: + iteration - Solution iteration of the FlowSolution_t node
528: - set - Whether the time data is in the file
530: Level: intermediate
532: Notes:
533: Reads data from a DataArray named `IterationValues` under a `BaseIterativeData_t` node
535: .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSGetSolutionTime()`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionName()`
536: @*/
537: PetscErrorCode PetscViewerCGNSGetSolutionIteration(PetscViewer viewer, PetscInt *iteration, PetscBool *set)
538: {
539: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
540: int cgns_ier, A_index = 0, sol_id;
541: int *steps;
542: cgsize_t size[12];
544: PetscFunctionBegin;
546: PetscAssertPointer(iteration, 2);
547: PetscAssertPointer(set, 3);
548: cgns_ier = cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL);
549: if (cgns_ier == CG_NODE_NOT_FOUND) {
550: *set = PETSC_FALSE;
551: PetscFunctionReturn(PETSC_SUCCESS);
552: } else PetscCallCGNS(cgns_ier);
553: PetscCall(CGNS_Find_Array(PetscObjectComm((PetscObject)viewer), "IterationValues", &A_index, NULL, NULL, size, set));
554: if (!*set) PetscFunctionReturn(PETSC_SUCCESS);
555: PetscCall(PetscMalloc1(size[0], &steps));
556: PetscCallCGNSRead(cg_array_read_as(A_index, CGNS_ENUMV(Integer), steps), viewer, 0);
557: PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); // Call to set file pointer index
558: *iteration = (PetscInt)steps[cgv->solution_file_pointer_index - 1];
559: PetscCall(PetscFree(steps));
560: PetscFunctionReturn(PETSC_SUCCESS);
561: }
563: /*@
564: PetscViewerCGNSGetSolutionName - Gets name of FlowSolution of the viewer
566: Collective
568: Input Parameter:
569: . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
571: Output Parameter:
572: . name - Name of the FlowSolution_t node corresponding to the solution index
574: Level: intermediate
576: Notes:
577: Currently assumes there is only one Zone in the CGNS file
579: .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionTime()`
580: @*/
581: PetscErrorCode PetscViewerCGNSGetSolutionName(PetscViewer viewer, const char *name[])
582: {
583: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
584: int sol_id;
585: char buffer[CGIO_MAX_NAME_LENGTH + 1];
586: CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway
588: PetscFunctionBegin;
590: PetscAssertPointer(name, 2);
591: PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id));
593: PetscCallCGNSRead(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc), viewer, 0);
594: PetscCall(PetscFree(cgv->solution_name));
595: PetscCall(PetscStrallocpy(buffer, &cgv->solution_name));
596: *name = cgv->solution_name;
597: PetscFunctionReturn(PETSC_SUCCESS);
598: }
600: /*@
601: PetscViewerCGNSGetDescriptors - Get all descriptors set at the base level of the CGNS viewer
603: Logically collective
605: Input Parameter:
606: . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
608: Output Parameters:
609: + num_descriptors - Number of descriptors set on the file
610: . names - Pointer to store array of descriptor names
611: - values - Pointer to store array of descriptor values
613: Level: intermediate
615: Note:
616: Memory must be freed via calling `PetscViewerCGNSRestoreDescriptors()`
618: .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSRestoreDescriptors()`, `PetscViewerCGNSSetDescriptor()`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionTime()`
619: @*/
620: PetscErrorCode PetscViewerCGNSGetDescriptors(PetscViewer viewer, PetscInt *num_descriptors, char ***names, char ***values)
621: {
622: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
623: int ndesc, cgns_ier;
625: PetscFunctionBegin;
627: PetscAssertPointer(num_descriptors, 2);
628: PetscAssertPointer(names, 3);
629: PetscAssertPointer(values, 4);
631: cgns_ier = cg_goto(cgv->file_num, cgv->base, NULL);
632: if (cgns_ier == CG_NODE_NOT_FOUND) {
633: *num_descriptors = 0;
634: *names = NULL;
635: *values = NULL;
636: PetscFunctionReturn(PETSC_SUCCESS);
637: } else PetscCallCGNS(cgns_ier);
639: PetscCallCGNSRead(cg_ndescriptors(&ndesc), viewer, 0);
640: *num_descriptors = ndesc;
642: PetscCall(PetscCalloc2(ndesc, values, ndesc, names));
644: for (PetscInt i = 1; i <= ndesc; i++) {
645: char namebuf[PETSC_MAX_OPTION_NAME] = {0};
646: char *desc;
648: PetscCallCGNSRead(cg_descriptor_read(i, namebuf, &desc), viewer, 0);
649: if (desc != NULL) {
650: PetscCall(PetscStrallocpy(desc, &(*values)[i - 1]));
651: PetscCallCGNS(cg_free(desc));
652: }
653: PetscCall(PetscStrallocpy(namebuf, &(*names)[i - 1]));
654: }
655: PetscFunctionReturn(PETSC_SUCCESS);
656: }
658: /*@
659: PetscViewerCGNSRestoreDescriptors - Free memory allocated by `PetscViewerCGNSGetDescriptors()`
661: Logically collective
663: Input Parameter:
664: . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
666: Output Parameters:
667: + num_descriptors - Number of descriptors set on the file
668: . names - Pointer to store array of descriptor names
669: - values - Pointer to store array of descriptor values
671: Level: intermediate
673: .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSGetDescriptors()`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionTime()`
674: @*/
675: PetscErrorCode PetscViewerCGNSRestoreDescriptors(PetscViewer viewer, PetscInt *num_descriptors, char ***names, char ***values)
676: {
677: PetscFunctionBegin;
679: PetscAssertPointer(num_descriptors, 2);
680: PetscAssertPointer(names, 3);
681: PetscAssertPointer(values, 4);
682: for (PetscInt i = 0; i < *num_descriptors; i++) {
683: PetscCall(PetscFree((*values)[i]));
684: PetscCall(PetscFree((*names)[i]));
685: }
686: PetscCall(PetscFree2(*values, *names));
687: *num_descriptors = 0;
688: PetscFunctionReturn(PETSC_SUCCESS);
689: }
691: /*@
692: PetscViewerCGNSSetDescriptor - Set a descriptor at the base level of the CGNS viewer
694: Logically collective
696: Input Parameters:
697: + viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file
698: . name - Name of descriptor, must be null terminated with length less than `PETSC_MAX_OPTION_NAME`
699: - value - Value of descriptor, must be null terminated
701: Level: intermediate
703: .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSGetDescriptors()`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionTime()`
704: @*/
705: PetscErrorCode PetscViewerCGNSSetDescriptor(PetscViewer viewer, const char name[], const char value[])
706: {
707: PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
708: PetscSizeT name_len;
709: PetscInt name_idx;
710: PetscBool found;
712: PetscFunctionBegin;
714: PetscAssertPointer(name, 2);
715: PetscAssertPointer(value, 3);
717: if (cgv->descriptor_capacity == 0) {
718: cgv->descriptor_capacity = 2;
719: PetscCall(PetscCalloc1(cgv->descriptor_capacity, &cgv->descriptor_names));
720: PetscCall(PetscCalloc1(cgv->descriptor_capacity, &cgv->descriptor_values));
721: } else if (cgv->num_descriptors == cgv->descriptor_capacity) {
722: cgv->descriptor_capacity *= 2;
723: PetscCall(PetscRealloc(cgv->descriptor_capacity * sizeof(char *), &cgv->descriptor_names));
724: PetscCall(PetscRealloc(cgv->descriptor_capacity * sizeof(char *), &cgv->descriptor_values));
725: }
727: PetscCall(PetscStrlen(name, &name_len));
728: PetscCheck(name_len < PETSC_MAX_OPTION_NAME, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_OUTOFRANGE, "Descriptor name must be shorter than %" PetscInt_FMT, (PetscInt)PETSC_MAX_OPTION_NAME);
730: PetscCall(PetscEListFind(cgv->num_descriptors, (const char *const *)cgv->descriptor_names, name, &name_idx, &found));
731: if (found) {
732: PetscCall(PetscFree(cgv->descriptor_values[name_idx]));
733: PetscCall(PetscStrallocpy(value, &cgv->descriptor_values[name_idx]));
734: } else {
735: PetscCall(PetscStrallocpy(name, &cgv->descriptor_names[cgv->num_descriptors]));
736: PetscCall(PetscStrallocpy(value, &cgv->descriptor_values[cgv->num_descriptors]));
737: cgv->num_descriptors++;
738: }
739: PetscFunctionReturn(PETSC_SUCCESS);
740: }