15f34f2dcSJed Brown #include <petsc/private/viewercgnsimpl.h> /*I "petscviewer.h" I*/ 2472b9844SJames Wright #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 35f34f2dcSJed Brown #if defined(PETSC_HDF5_HAVE_PARALLEL) 45f34f2dcSJed Brown #include <pcgnslib.h> 55f34f2dcSJed Brown #else 65f34f2dcSJed Brown #include <cgnslib.h> 75f34f2dcSJed Brown #endif 8472b9844SJames Wright #include <cgns_io.h> 9472b9844SJames Wright #include <ctype.h> 105f34f2dcSJed Brown 115582b114SJames Wright 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; 125582b114SJames Wright 135582b114SJames Wright PetscErrorCode PetscViewerCGNSRegisterLogEvents_Internal() 145582b114SJames Wright { 155582b114SJames Wright static PetscBool is_initialized = PETSC_FALSE; 165582b114SJames Wright 175582b114SJames Wright PetscFunctionBeginUser; 185582b114SJames Wright if (is_initialized) PetscFunctionReturn(PETSC_SUCCESS); 195582b114SJames Wright PetscCall(PetscLogEventRegister("CGNSOpen", PETSC_VIEWER_CLASSID, &PETSC_VIEWER_CGNS_Open)); 205582b114SJames Wright PetscCall(PetscLogEventRegister("CGNSClose", PETSC_VIEWER_CLASSID, &PETSC_VIEWER_CGNS_Close)); 215582b114SJames Wright PetscCall(PetscLogEventRegister("CGNSReadMeta", PETSC_VIEWER_CLASSID, &PETSC_VIEWER_CGNS_ReadMeta)); 225582b114SJames Wright PetscCall(PetscLogEventRegister("CGNSReadData", PETSC_VIEWER_CLASSID, &PETSC_VIEWER_CGNS_ReadData)); 235582b114SJames Wright PetscCall(PetscLogEventRegister("CGNSWriteMeta", PETSC_VIEWER_CLASSID, &PETSC_VIEWER_CGNS_WriteMeta)); 245582b114SJames Wright PetscCall(PetscLogEventRegister("CGNSWriteData", PETSC_VIEWER_CLASSID, &PETSC_VIEWER_CGNS_WriteData)); 255582b114SJames Wright is_initialized = PETSC_TRUE; 265582b114SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 275582b114SJames Wright } 285582b114SJames Wright 29ce78bad3SBarry Smith static PetscErrorCode PetscViewerSetFromOptions_CGNS(PetscViewer v, PetscOptionItems PetscOptionsObject) 30d71ae5a4SJacob Faibussowitsch { 3125760affSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)v->data; 324d86920dSPierre Jolivet 335f34f2dcSJed Brown PetscFunctionBegin; 345f34f2dcSJed Brown PetscOptionsHeadBegin(PetscOptionsObject, "CGNS Viewer Options"); 35da0802e2SStefano Zampini 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)); 365f34f2dcSJed Brown PetscOptionsHeadEnd(); 373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 385f34f2dcSJed Brown } 395f34f2dcSJed Brown 40d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerView_CGNS(PetscViewer v, PetscViewer viewer) 41d71ae5a4SJacob Faibussowitsch { 425f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)v->data; 435f34f2dcSJed Brown 445f34f2dcSJed Brown PetscFunctionBegin; 4548a46eb9SPierre Jolivet if (cgv->filename) PetscCall(PetscViewerASCIIPrintf(viewer, "Filename: %s\n", cgv->filename)); 463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 475f34f2dcSJed Brown } 485f34f2dcSJed Brown 49d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileClose_CGNS(PetscViewer viewer) 50d71ae5a4SJacob Faibussowitsch { 515f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 525f34f2dcSJed Brown 535f34f2dcSJed Brown PetscFunctionBegin; 545f34f2dcSJed Brown if (cgv->output_times) { 556497c311SBarry Smith PetscCount size, width = 32, *steps; 565f34f2dcSJed Brown char *solnames; 575f34f2dcSJed Brown PetscReal *times; 585f34f2dcSJed Brown cgsize_t num_times; 595f34f2dcSJed Brown PetscCall(PetscSegBufferGetSize(cgv->output_times, &size)); 605f34f2dcSJed Brown PetscCall(PetscSegBufferExtractInPlace(cgv->output_times, ×)); 6173adc74eSJames Wright PetscCall(PetscSegBufferExtractInPlace(cgv->output_steps, &steps)); 625f34f2dcSJed Brown num_times = size; 634c155f28SJames Wright PetscCallCGNSWrite(cg_biter_write(cgv->file_num, cgv->base, "TimeIterValues", num_times), viewer, 0); 645f34f2dcSJed Brown PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL)); 654c155f28SJames Wright PetscCallCGNSWrite(cg_array_write("TimeValues", CGNS_ENUMV(RealDouble), 1, &num_times, times), viewer, 0); 6673adc74eSJames Wright { // Cast output_steps to long for writing into file 6773adc74eSJames Wright int *steps_int; 6873adc74eSJames Wright PetscCall(PetscMalloc1(size, &steps_int)); 6973adc74eSJames Wright for (PetscCount i = 0; i < size; i++) PetscCall(PetscCIntCast(steps[i], &steps_int[i])); 7073adc74eSJames Wright PetscCallCGNSWrite(cg_array_write("IterationValues", CGNS_ENUMV(Integer), 1, &num_times, steps_int), viewer, 0); 7173adc74eSJames Wright PetscCall(PetscFree(steps_int)); 7273adc74eSJames Wright } 735f34f2dcSJed Brown PetscCall(PetscSegBufferDestroy(&cgv->output_times)); 744c155f28SJames Wright PetscCallCGNSWrite(cg_ziter_write(cgv->file_num, cgv->base, cgv->zone, "ZoneIterativeData"), viewer, 0); 755f34f2dcSJed Brown PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL)); 765f34f2dcSJed Brown PetscCall(PetscMalloc(size * width + 1, &solnames)); 776497c311SBarry Smith for (PetscCount i = 0; i < size; i++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "FlowSolution%-20zu", (size_t)steps[i])); 789812b6beSJed Brown PetscCall(PetscSegBufferDestroy(&cgv->output_steps)); 795f34f2dcSJed Brown cgsize_t shape[2] = {(cgsize_t)width, (cgsize_t)size}; 804c155f28SJames Wright PetscCallCGNSWrite(cg_array_write("FlowSolutionPointers", CGNS_ENUMV(Character), 2, shape, solnames), viewer, 0); 815f34f2dcSJed Brown // The VTK reader looks for names like FlowSolution*Pointers. 826497c311SBarry Smith for (PetscCount i = 0; i < size; i++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "%-32s", "CellInfo")); 834c155f28SJames Wright PetscCallCGNSWrite(cg_array_write("FlowSolutionCellInfoPointers", CGNS_ENUMV(Character), 2, shape, solnames), viewer, 0); 845f34f2dcSJed Brown PetscCall(PetscFree(solnames)); 855f34f2dcSJed Brown 864c155f28SJames Wright PetscCallCGNSWrite(cg_simulation_type_write(cgv->file_num, cgv->base, CGNS_ENUMV(TimeAccurate)), viewer, 0); 875f34f2dcSJed Brown } 885f34f2dcSJed Brown PetscCall(PetscFree(cgv->filename)); 895f34f2dcSJed Brown #if defined(PETSC_HDF5_HAVE_PARALLEL) 904c155f28SJames Wright if (cgv->file_num) PetscCallCGNSClose(cgp_close(cgv->file_num), viewer, 0); 915f34f2dcSJed Brown #else 924c155f28SJames Wright if (cgv->file_num) PetscCallCGNSClose(cg_close(cgv->file_num), viewer, 0); 935f34f2dcSJed Brown #endif 945f34f2dcSJed Brown cgv->file_num = 0; 9525760affSJed Brown PetscCall(PetscFree(cgv->node_l2g)); 9625760affSJed Brown PetscCall(PetscFree(cgv->nodal_field)); 973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9825760affSJed Brown } 9925760affSJed Brown 10025760affSJed Brown PetscErrorCode PetscViewerCGNSFileOpen_Internal(PetscViewer viewer, PetscInt sequence_number) 10125760affSJed Brown { 10225760affSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 103ec76947aSJames Wright int cg_file_mode = -1; 1044d86920dSPierre Jolivet 10525760affSJed Brown PetscFunctionBegin; 10625760affSJed Brown PetscCheck((cgv->filename == NULL) ^ (sequence_number < 0), PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_INCOMP, "Expect either a template filename or non-negative sequence number"); 10725760affSJed Brown if (!cgv->filename) { 10825760affSJed Brown char filename_numbered[PETSC_MAX_PATH_LEN]; 10925760affSJed Brown // Cast sequence_number so %d can be used also when PetscInt is 64-bit. We could upgrade the format string if users 11025760affSJed Brown // run more than 2B time steps. 11125760affSJed Brown PetscCall(PetscSNPrintf(filename_numbered, sizeof filename_numbered, cgv->filename_template, (int)sequence_number)); 11225760affSJed Brown PetscCall(PetscStrallocpy(filename_numbered, &cgv->filename)); 11325760affSJed Brown } 11425760affSJed Brown switch (cgv->btype) { 11525760affSJed Brown case FILE_MODE_READ: 116ec76947aSJames Wright cg_file_mode = CG_MODE_READ; 11725760affSJed Brown break; 11825760affSJed Brown case FILE_MODE_WRITE: 119ec76947aSJames Wright cg_file_mode = CG_MODE_WRITE; 12025760affSJed Brown break; 12125760affSJed Brown case FILE_MODE_UNDEFINED: 12225760affSJed Brown SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()"); 12325760affSJed Brown default: 12425760affSJed Brown SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[cgv->btype]); 12525760affSJed Brown } 126ec76947aSJames Wright #if defined(PETSC_HDF5_HAVE_PARALLEL) 127ec76947aSJames Wright PetscCallCGNS(cgp_mpi_comm(PetscObjectComm((PetscObject)viewer))); 1284c155f28SJames Wright PetscCallCGNSOpen(cgp_open(cgv->filename, cg_file_mode, &cgv->file_num), viewer, 0); 129ec76947aSJames Wright #else 1304c155f28SJames Wright PetscCallCGNSOpen(cg_open(filename, cg_file_mode, &cgv->file_num), viewer, 0); 131ec76947aSJames Wright #endif 1323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 13325760affSJed Brown } 13425760affSJed Brown 13525760affSJed Brown PetscErrorCode PetscViewerCGNSCheckBatch_Internal(PetscViewer viewer) 13625760affSJed Brown { 13725760affSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 1386497c311SBarry Smith PetscCount num_steps; 13925760affSJed Brown 14025760affSJed Brown PetscFunctionBegin; 1413ba16761SJacob Faibussowitsch if (!cgv->filename_template) PetscFunctionReturn(PETSC_SUCCESS); // Batches are closed when viewer is destroyed 14225760affSJed Brown PetscCall(PetscSegBufferGetSize(cgv->output_times, &num_steps)); 1436497c311SBarry Smith if (num_steps >= (PetscCount)cgv->batch_size) PetscCall(PetscViewerFileClose_CGNS(viewer)); 1443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1455f34f2dcSJed Brown } 1465f34f2dcSJed Brown 147d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerDestroy_CGNS(PetscViewer viewer) 148d71ae5a4SJacob Faibussowitsch { 1495f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 1505f34f2dcSJed Brown 1515f34f2dcSJed Brown PetscFunctionBegin; 1525f34f2dcSJed Brown PetscCall(PetscViewerFileClose_CGNS(viewer)); 153472b9844SJames Wright PetscCall(PetscFree(cgv->solution_name)); 154*8e562f8dSJames Wright PetscCall(PetscFree(cgv->filename_template)); 1555f34f2dcSJed Brown PetscCall(PetscFree(cgv)); 1565f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL)); 1575f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL)); 1585f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL)); 1595f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL)); 1603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1615f34f2dcSJed Brown } 1625f34f2dcSJed Brown 163d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileSetMode_CGNS(PetscViewer viewer, PetscFileMode type) 164d71ae5a4SJacob Faibussowitsch { 1655f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 1665f34f2dcSJed Brown 1675f34f2dcSJed Brown PetscFunctionBegin; 1685f34f2dcSJed Brown cgv->btype = type; 1693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1705f34f2dcSJed Brown } 1715f34f2dcSJed Brown 172d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileGetMode_CGNS(PetscViewer viewer, PetscFileMode *type) 173d71ae5a4SJacob Faibussowitsch { 1745f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 1755f34f2dcSJed Brown 1765f34f2dcSJed Brown PetscFunctionBegin; 1775f34f2dcSJed Brown *type = cgv->btype; 1783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1795f34f2dcSJed Brown } 1805f34f2dcSJed Brown 181d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileSetName_CGNS(PetscViewer viewer, const char *filename) 182d71ae5a4SJacob Faibussowitsch { 1835f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 18425760affSJed Brown char *has_pattern; 1855f34f2dcSJed Brown 1865f34f2dcSJed Brown PetscFunctionBegin; 1875582b114SJames Wright #if defined(PETSC_HDF5_HAVE_PARALLEL) 1884c155f28SJames Wright if (cgv->file_num) PetscCallCGNSClose(cgp_close(cgv->file_num), viewer, 0); 1895582b114SJames Wright #else 1904c155f28SJames Wright if (cgv->file_num) PetscCallCGNSClose(cg_close(cgv->file_num), viewer, 0); 1915582b114SJames Wright #endif 1925f34f2dcSJed Brown PetscCall(PetscFree(cgv->filename)); 19325760affSJed Brown PetscCall(PetscFree(cgv->filename_template)); 19425760affSJed Brown PetscCall(PetscStrchr(filename, '%', &has_pattern)); 19525760affSJed Brown if (has_pattern) { 19625760affSJed Brown PetscCall(PetscStrallocpy(filename, &cgv->filename_template)); 19725760affSJed Brown // Templated file names open lazily, once we know DMGetOutputSequenceNumber() 19825760affSJed Brown } else { 1995f34f2dcSJed Brown PetscCall(PetscStrallocpy(filename, &cgv->filename)); 20025760affSJed Brown PetscCall(PetscViewerCGNSFileOpen_Internal(viewer, -1)); 2015f34f2dcSJed Brown } 2023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2035f34f2dcSJed Brown } 2045f34f2dcSJed Brown 205d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileGetName_CGNS(PetscViewer viewer, const char **filename) 206d71ae5a4SJacob Faibussowitsch { 2075f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 2085f34f2dcSJed Brown 2095f34f2dcSJed Brown PetscFunctionBegin; 2105f34f2dcSJed Brown *filename = cgv->filename; 2113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2125f34f2dcSJed Brown } 2135f34f2dcSJed Brown 2145f34f2dcSJed Brown /*MC 2155f34f2dcSJed Brown PETSCVIEWERCGNS - A viewer for CGNS files 2165f34f2dcSJed Brown 2175f34f2dcSJed Brown Level: beginner 218811af0c4SBarry Smith 2193f423023SBarry Smith Options Database Key: 2203f423023SBarry Smith . -viewer_cgns_batch_size SIZE - set max number of output sequence times to write per batch 22125760affSJed Brown 2223f423023SBarry Smith Note: 22325760affSJed Brown If the filename contains an integer format character, the CGNS viewer will created a batched output sequence. For 22425760affSJed Brown example, one could use `-ts_monitor_solution cgns:flow-%d.cgns`. This is desirable if one wants to limit file sizes or 22525760affSJed Brown if the job might crash/be killed by a resource manager before exiting cleanly. 22625760affSJed Brown 22725760affSJed Brown .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `VecView()`, `DMView()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `TSSetFromOptions()` 2285f34f2dcSJed Brown M*/ 22914640894SJames Wright PetscErrorCode PetscViewerCreate_CGNS(PetscViewer v) 230d71ae5a4SJacob Faibussowitsch { 2315f34f2dcSJed Brown PetscViewer_CGNS *cgv; 2325f34f2dcSJed Brown 2335f34f2dcSJed Brown PetscFunctionBegin; 2345582b114SJames Wright PetscCall(PetscViewerCGNSRegisterLogEvents_Internal()); 2354dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&cgv)); 2365f34f2dcSJed Brown 2375f34f2dcSJed Brown v->data = cgv; 2385f34f2dcSJed Brown v->ops->destroy = PetscViewerDestroy_CGNS; 2395f34f2dcSJed Brown v->ops->setfromoptions = PetscViewerSetFromOptions_CGNS; 2405f34f2dcSJed Brown v->ops->view = PetscViewerView_CGNS; 2415f34f2dcSJed Brown cgv->btype = FILE_MODE_UNDEFINED; 2425f34f2dcSJed Brown cgv->filename = NULL; 24325760affSJed Brown cgv->batch_size = 20; 244472b9844SJames Wright cgv->solution_index = -1; // Default to use the "last" solution 245472b9844SJames Wright cgv->base = 1; 246472b9844SJames Wright cgv->zone = 1; 2475f34f2dcSJed Brown 2485f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_CGNS)); 2495f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_CGNS)); 2505f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_CGNS)); 2515f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_CGNS)); 2523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2535f34f2dcSJed Brown } 254472b9844SJames Wright 255472b9844SJames Wright // Find DataArray_t node under the current node (determined by `cg_goto` and friends) that matches `name` 256472b9844SJames Wright // Return the index of that array and (optionally) other data about the array 2570919f86aSJames Wright 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) 258472b9844SJames Wright { 259472b9844SJames Wright int narrays; // number of arrays under the current node 260472b9844SJames Wright char array_name[CGIO_MAX_NAME_LENGTH + 1]; 261472b9844SJames Wright CGNS_ENUMT(DataType_t) data_type_local; 262472b9844SJames Wright int _dim; 263472b9844SJames Wright cgsize_t _size[12]; 264472b9844SJames Wright PetscBool matches_name = PETSC_FALSE; 265472b9844SJames Wright 266472b9844SJames Wright PetscFunctionBeginUser; 2674c155f28SJames Wright PetscCallCGNSRead(cg_narrays(&narrays), 0, 0); 268472b9844SJames Wright for (int i = 1; i <= narrays; i++) { 2694c155f28SJames Wright PetscCallCGNSRead(cg_array_info(i, array_name, &data_type_local, &_dim, _size), 0, 0); 270472b9844SJames Wright PetscCall(PetscStrcmp(name, array_name, &matches_name)); 271472b9844SJames Wright if (matches_name) { 272472b9844SJames Wright *A_index = i; 273472b9844SJames Wright if (data_type) *data_type = data_type_local; 274472b9844SJames Wright if (dim) *dim = _dim; 275472b9844SJames Wright if (size) PetscArraycpy(size, _size, _dim); 2760919f86aSJames Wright if (found) *found = PETSC_TRUE; 277472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 278472b9844SJames Wright } 279472b9844SJames Wright } 2800919f86aSJames Wright if (found) *found = PETSC_FALSE; 2810919f86aSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 282472b9844SJames Wright } 283472b9844SJames Wright 284472b9844SJames Wright /*@ 285472b9844SJames Wright PetscViewerCGNSOpen - Opens a file for CGNS input/output. 286472b9844SJames Wright 287472b9844SJames Wright Collective 288472b9844SJames Wright 289472b9844SJames Wright Input Parameters: 290472b9844SJames Wright + comm - MPI communicator 291472b9844SJames Wright . name - name of file 292472b9844SJames Wright - type - type of file 293472b9844SJames Wright .vb 294472b9844SJames Wright FILE_MODE_WRITE - create new file for binary output 295472b9844SJames Wright FILE_MODE_READ - open existing file for binary input 296472b9844SJames Wright FILE_MODE_APPEND - open existing file for binary output 297472b9844SJames Wright .ve 298472b9844SJames Wright 299472b9844SJames Wright Output Parameter: 300472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 301472b9844SJames Wright 302472b9844SJames Wright Level: beginner 303472b9844SJames Wright 304472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, 305472b9844SJames Wright `DMLoad()`, `PetscFileMode`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()` 306472b9844SJames Wright @*/ 307472b9844SJames Wright PetscErrorCode PetscViewerCGNSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *viewer) 308472b9844SJames Wright { 309472b9844SJames Wright PetscFunctionBegin; 31014640894SJames Wright PetscAssertPointer(name, 2); 31114640894SJames Wright PetscAssertPointer(viewer, 4); 312472b9844SJames Wright PetscCall(PetscViewerCreate(comm, viewer)); 313472b9844SJames Wright PetscCall(PetscViewerSetType(*viewer, PETSCVIEWERCGNS)); 314472b9844SJames Wright PetscCall(PetscViewerFileSetMode(*viewer, type)); 315472b9844SJames Wright PetscCall(PetscViewerFileSetName(*viewer, name)); 316472b9844SJames Wright PetscCall(PetscViewerSetFromOptions(*viewer)); 317472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 318472b9844SJames Wright } 319472b9844SJames Wright 320472b9844SJames Wright /*@ 321472b9844SJames Wright PetscViewerCGNSSetSolutionIndex - Set index of solution 322472b9844SJames Wright 323472b9844SJames Wright Not Collective 324472b9844SJames Wright 325472b9844SJames Wright Input Parameters: 326472b9844SJames Wright + viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 327472b9844SJames Wright - solution_id - Index of the solution id, or `-1` for the last solution on the file 328472b9844SJames Wright 329472b9844SJames Wright Level: intermediate 330472b9844SJames Wright 331472b9844SJames Wright Notes: 332472b9844SJames Wright By default, `solution_id` is set to `-1` to mean the last solution available in the file. 333472b9844SJames Wright If the file contains a FlowSolutionPointers node, then that array is indexed to determine which FlowSolution_t node to read from. 334472b9844SJames Wright Otherwise, `solution_id` indexes the total available FlowSolution_t nodes in the file. 335472b9844SJames Wright 336472b9844SJames Wright This solution index is used by `VecLoad()` to determine which solution to load from the file 337472b9844SJames Wright 338472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()` 339472b9844SJames Wright 340472b9844SJames Wright @*/ 341472b9844SJames Wright PetscErrorCode PetscViewerCGNSSetSolutionIndex(PetscViewer viewer, PetscInt solution_id) 342472b9844SJames Wright { 343472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 344472b9844SJames Wright 345472b9844SJames Wright PetscFunctionBeginUser; 34614640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 34714640894SJames Wright PetscValidLogicalCollectiveInt(viewer, solution_id, 2); 348472b9844SJames Wright 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); 349472b9844SJames Wright cgv->solution_index = solution_id; 350472b9844SJames Wright cgv->solution_file_index = 0; // Reset sol_index when solution_id changes (0 is invalid) 351472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 352472b9844SJames Wright } 353472b9844SJames Wright 354472b9844SJames Wright /*@ 355472b9844SJames Wright PetscViewerCGNSGetSolutionIndex - Get index of solution 356472b9844SJames Wright 357472b9844SJames Wright Not Collective 358472b9844SJames Wright 359472b9844SJames Wright Input Parameter: 360472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 361472b9844SJames Wright 362472b9844SJames Wright Output Parameter: 363472b9844SJames Wright . solution_id - Index of the solution id 364472b9844SJames Wright 365472b9844SJames Wright Level: intermediate 366472b9844SJames Wright 367472b9844SJames Wright Notes: 368472b9844SJames Wright By default, solution_id is set to `-1` to mean the last solution available in the file 369472b9844SJames Wright 370472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()` 371472b9844SJames Wright 372472b9844SJames Wright @*/ 373472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionIndex(PetscViewer viewer, PetscInt *solution_id) 374472b9844SJames Wright { 375472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 376472b9844SJames Wright 377472b9844SJames Wright PetscFunctionBeginUser; 37814640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 37914640894SJames Wright PetscAssertPointer(solution_id, 2); 380472b9844SJames Wright *solution_id = cgv->solution_index; 381472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 382472b9844SJames Wright } 383472b9844SJames Wright 384472b9844SJames Wright // Gets the index for the solution in this CGNS file 385472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionFileIndex_Internal(PetscViewer viewer, int *sol_index) 386472b9844SJames Wright { 387472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 388472b9844SJames Wright MPI_Comm comm = PetscObjectComm((PetscObject)viewer); 389472b9844SJames Wright int nsols, cgns_ier; 390472b9844SJames Wright char buffer[CGIO_MAX_NAME_LENGTH + 1]; 391472b9844SJames Wright CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway 392472b9844SJames Wright 393472b9844SJames Wright PetscFunctionBeginUser; 394472b9844SJames Wright if (cgv->solution_file_index > 0) { 395472b9844SJames Wright *sol_index = cgv->solution_file_index; 396472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 397472b9844SJames Wright } 398472b9844SJames Wright 3994c155f28SJames Wright PetscCallCGNSRead(cg_nsols(cgv->file_num, cgv->base, cgv->zone, &nsols), viewer, 0); 400472b9844SJames Wright cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, "FlowSolutionPointers", 0, NULL); 401472b9844SJames Wright if (cgns_ier == CG_NODE_NOT_FOUND) { 402472b9844SJames Wright // If FlowSolutionPointers does not exist, then just index off of nsols (which can include non-solution data) 403472b9844SJames Wright 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); 404472b9844SJames Wright 405472b9844SJames Wright cgv->solution_file_index = cgv->solution_index == -1 ? nsols : cgv->solution_index; 406472b9844SJames Wright } else { 407472b9844SJames Wright // If FlowSolutionPointers exists, then solution_id should index that array of FlowSolutions 408472b9844SJames Wright char *pointer_id_name; 409472b9844SJames Wright PetscBool matches_name = PETSC_FALSE; 410472b9844SJames Wright int sol_id; 411472b9844SJames Wright 412472b9844SJames Wright PetscCheck(cgns_ier == CG_OK, PETSC_COMM_SELF, PETSC_ERR_LIB, "CGNS error %d %s", cgns_ier, cg_get_error()); 413472b9844SJames Wright cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL); 414472b9844SJames Wright 415472b9844SJames Wright { // Get FlowSolutionPointer name corresponding to solution_id 416472b9844SJames Wright cgsize_t size[12]; 417b63c620bSJames Wright int dim, A_index; 418472b9844SJames Wright char *pointer_names, *pointer_id_name_ref; 4190919f86aSJames Wright PetscBool found_array; 420472b9844SJames Wright 4210919f86aSJames Wright PetscCall(CGNS_Find_Array(comm, "FlowSolutionPointers", &A_index, NULL, &dim, size, &found_array)); 4220919f86aSJames Wright PetscCheck(found_array, comm, PETSC_ERR_SUP, "Cannot find FlowSolutionPointers array under current CGNS node"); 423472b9844SJames Wright 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]); 424472b9844SJames Wright 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 4254c155f28SJames Wright PetscCallCGNSRead(cg_array_read_as(1, CGNS_ENUMV(Character), pointer_names), viewer, 0); 426b63c620bSJames Wright cgv->solution_file_pointer_index = cgv->solution_index == -1 ? size[1] : cgv->solution_index; 427b63c620bSJames Wright pointer_id_name_ref = &pointer_names[size[0] * (cgv->solution_file_pointer_index - 1)]; 428472b9844SJames Wright { // Set last non-whitespace character of the pointer name to \0 (CGNS pads with spaces) 429472b9844SJames Wright int str_idx; 430472b9844SJames Wright for (str_idx = size[0] - 1; str_idx > 0; str_idx--) { 431472b9844SJames Wright if (!isspace((unsigned char)pointer_id_name_ref[str_idx])) break; 432472b9844SJames Wright } 433472b9844SJames Wright pointer_id_name_ref[str_idx + 1] = '\0'; 434472b9844SJames Wright } 435472b9844SJames Wright PetscCall(PetscStrallocpy(pointer_id_name_ref, &pointer_id_name)); 436472b9844SJames Wright PetscCall(PetscFree(pointer_names)); 437472b9844SJames Wright } 438472b9844SJames Wright 439472b9844SJames Wright // Find FlowSolution_t node that matches pointer_id_name 440472b9844SJames Wright for (sol_id = 1; sol_id <= nsols; sol_id++) { 4414c155f28SJames Wright PetscCallCGNSRead(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc), viewer, 0); 442472b9844SJames Wright PetscCall(PetscStrcmp(pointer_id_name, buffer, &matches_name)); 443472b9844SJames Wright if (matches_name) break; 444472b9844SJames Wright } 445472b9844SJames Wright PetscCheck(matches_name, comm, PETSC_ERR_LIB, "Cannot find FlowSolution_t node %s in file", pointer_id_name); 446472b9844SJames Wright cgv->solution_file_index = sol_id; 447472b9844SJames Wright PetscCall(PetscFree(pointer_id_name)); 448472b9844SJames Wright } 449472b9844SJames Wright 450472b9844SJames Wright *sol_index = cgv->solution_file_index; 451472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 452472b9844SJames Wright } 453472b9844SJames Wright 454472b9844SJames Wright /*@ 455472b9844SJames Wright PetscViewerCGNSGetSolutionTime - Gets the solution time for the FlowSolution of the viewer 456472b9844SJames Wright 457472b9844SJames Wright Collective 458472b9844SJames Wright 459472b9844SJames Wright Input Parameter: 460472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 461472b9844SJames Wright 4627dcfde4dSJose E. Roman Output Parameters: 463472b9844SJames Wright + time - Solution time of the FlowSolution_t node 464472b9844SJames Wright - set - Whether the time data is in the file 465472b9844SJames Wright 466472b9844SJames Wright Level: intermediate 467472b9844SJames Wright 468472b9844SJames Wright Notes: 469472b9844SJames Wright Reads data from a DataArray named `TimeValues` under a `BaseIterativeData_t` node 470472b9844SJames Wright 471bd70a9a6SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSGetSolutionIteration()`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionName()` 472472b9844SJames Wright @*/ 473472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionTime(PetscViewer viewer, PetscReal *time, PetscBool *set) 474472b9844SJames Wright { 475472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 476b63c620bSJames Wright int cgns_ier, A_index = 0, sol_id; 477472b9844SJames Wright PetscReal *times; 478472b9844SJames Wright cgsize_t size[12]; 479472b9844SJames Wright 480472b9844SJames Wright PetscFunctionBeginUser; 48114640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 48214640894SJames Wright PetscAssertPointer(time, 2); 48314640894SJames Wright PetscAssertPointer(set, 3); 484472b9844SJames Wright cgns_ier = cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL); 485472b9844SJames Wright if (cgns_ier == CG_NODE_NOT_FOUND) { 486472b9844SJames Wright *set = PETSC_FALSE; 487472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 488472b9844SJames Wright } else PetscCallCGNS(cgns_ier); 4890919f86aSJames Wright PetscCall(CGNS_Find_Array(PetscObjectComm((PetscObject)viewer), "TimeValues", &A_index, NULL, NULL, size, set)); 4900919f86aSJames Wright if (!set) PetscFunctionReturn(PETSC_SUCCESS); 491472b9844SJames Wright PetscCall(PetscMalloc1(size[0], ×)); 4924c155f28SJames Wright PetscCallCGNSRead(cg_array_read_as(A_index, CGNS_ENUMV(RealDouble), times), viewer, 0); 493b63c620bSJames Wright PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); // Call to set file pointer index 494b63c620bSJames Wright *time = times[cgv->solution_file_pointer_index - 1]; 495472b9844SJames Wright PetscCall(PetscFree(times)); 496472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 497472b9844SJames Wright } 498472b9844SJames Wright 499472b9844SJames Wright /*@ 500bd70a9a6SJames Wright PetscViewerCGNSGetSolutionIteration - Gets the solution iteration for the FlowSolution of the viewer 501bd70a9a6SJames Wright 502bd70a9a6SJames Wright Collective 503bd70a9a6SJames Wright 504bd70a9a6SJames Wright Input Parameter: 505bd70a9a6SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 506bd70a9a6SJames Wright 507bd70a9a6SJames Wright Output Parameters: 508bd70a9a6SJames Wright + iteration - Solution iteration of the FlowSolution_t node 509bd70a9a6SJames Wright - set - Whether the time data is in the file 510bd70a9a6SJames Wright 511bd70a9a6SJames Wright Level: intermediate 512bd70a9a6SJames Wright 513bd70a9a6SJames Wright Notes: 514bd70a9a6SJames Wright Reads data from a DataArray named `IterationValues` under a `BaseIterativeData_t` node 515bd70a9a6SJames Wright 516bd70a9a6SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSGetSolutionTime()`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionName()` 517bd70a9a6SJames Wright @*/ 518bd70a9a6SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionIteration(PetscViewer viewer, PetscInt *iteration, PetscBool *set) 519bd70a9a6SJames Wright { 520bd70a9a6SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 521bd70a9a6SJames Wright int cgns_ier, A_index = 0, sol_id; 522bd70a9a6SJames Wright int *steps; 523bd70a9a6SJames Wright cgsize_t size[12]; 524bd70a9a6SJames Wright 525bd70a9a6SJames Wright PetscFunctionBeginUser; 526bd70a9a6SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 527bd70a9a6SJames Wright PetscAssertPointer(iteration, 2); 528bd70a9a6SJames Wright PetscAssertPointer(set, 3); 529bd70a9a6SJames Wright cgns_ier = cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL); 530bd70a9a6SJames Wright if (cgns_ier == CG_NODE_NOT_FOUND) { 531bd70a9a6SJames Wright *set = PETSC_FALSE; 532bd70a9a6SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 533bd70a9a6SJames Wright } else PetscCallCGNS(cgns_ier); 534bd70a9a6SJames Wright PetscCall(CGNS_Find_Array(PetscObjectComm((PetscObject)viewer), "IterationValues", &A_index, NULL, NULL, size, set)); 535bd70a9a6SJames Wright if (!set) PetscFunctionReturn(PETSC_SUCCESS); 536bd70a9a6SJames Wright PetscCall(PetscMalloc1(size[0], &steps)); 537bd70a9a6SJames Wright PetscCallCGNSRead(cg_array_read_as(A_index, CGNS_ENUMV(Integer), steps), viewer, 0); 538bd70a9a6SJames Wright PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); // Call to set file pointer index 539bd70a9a6SJames Wright *iteration = (PetscInt)steps[cgv->solution_file_pointer_index - 1]; 540bd70a9a6SJames Wright PetscCall(PetscFree(steps)); 541bd70a9a6SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 542bd70a9a6SJames Wright } 543bd70a9a6SJames Wright 544bd70a9a6SJames Wright /*@ 545472b9844SJames Wright PetscViewerCGNSGetSolutionName - Gets name of FlowSolution of the viewer 546472b9844SJames Wright 547472b9844SJames Wright Collective 548472b9844SJames Wright 549472b9844SJames Wright Input Parameter: 550472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 551472b9844SJames Wright 552472b9844SJames Wright Output Parameter: 553472b9844SJames Wright . name - Name of the FlowSolution_t node corresponding to the solution index 554472b9844SJames Wright 555472b9844SJames Wright Level: intermediate 556472b9844SJames Wright 557472b9844SJames Wright Notes: 558472b9844SJames Wright Currently assumes there is only one Zone in the CGNS file 559472b9844SJames Wright 560472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionTime()` 561472b9844SJames Wright @*/ 562472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionName(PetscViewer viewer, const char *name[]) 563472b9844SJames Wright { 564472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 565472b9844SJames Wright int sol_id; 566472b9844SJames Wright char buffer[CGIO_MAX_NAME_LENGTH + 1]; 567472b9844SJames Wright CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway 568472b9844SJames Wright 569472b9844SJames Wright PetscFunctionBeginUser; 57014640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 57114640894SJames Wright PetscAssertPointer(name, 2); 572472b9844SJames Wright PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); 573472b9844SJames Wright 5744c155f28SJames Wright PetscCallCGNSRead(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc), viewer, 0); 575472b9844SJames Wright if (cgv->solution_name) PetscCall(PetscFree(cgv->solution_name)); 576472b9844SJames Wright PetscCall(PetscStrallocpy(buffer, &cgv->solution_name)); 577472b9844SJames Wright *name = cgv->solution_name; 578472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 579472b9844SJames Wright } 580