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 29d71ae5a4SJacob Faibussowitsch 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, ×)); 615f34f2dcSJed Brown num_times = size; 62*4c155f28SJames Wright PetscCallCGNSWrite(cg_biter_write(cgv->file_num, cgv->base, "TimeIterValues", num_times), viewer, 0); 635f34f2dcSJed Brown PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL)); 64*4c155f28SJames Wright PetscCallCGNSWrite(cg_array_write("TimeValues", CGNS_ENUMV(RealDouble), 1, &num_times, times), viewer, 0); 655f34f2dcSJed Brown PetscCall(PetscSegBufferDestroy(&cgv->output_times)); 66*4c155f28SJames Wright PetscCallCGNSWrite(cg_ziter_write(cgv->file_num, cgv->base, cgv->zone, "ZoneIterativeData"), viewer, 0); 675f34f2dcSJed Brown PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL)); 685f34f2dcSJed Brown PetscCall(PetscMalloc(size * width + 1, &solnames)); 699812b6beSJed Brown PetscCall(PetscSegBufferExtractInPlace(cgv->output_steps, &steps)); 706497c311SBarry Smith for (PetscCount i = 0; i < size; i++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "FlowSolution%-20zu", (size_t)steps[i])); 719812b6beSJed Brown PetscCall(PetscSegBufferDestroy(&cgv->output_steps)); 725f34f2dcSJed Brown cgsize_t shape[2] = {(cgsize_t)width, (cgsize_t)size}; 73*4c155f28SJames Wright PetscCallCGNSWrite(cg_array_write("FlowSolutionPointers", CGNS_ENUMV(Character), 2, shape, solnames), viewer, 0); 745f34f2dcSJed Brown // The VTK reader looks for names like FlowSolution*Pointers. 756497c311SBarry Smith for (PetscCount i = 0; i < size; i++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "%-32s", "CellInfo")); 76*4c155f28SJames Wright PetscCallCGNSWrite(cg_array_write("FlowSolutionCellInfoPointers", CGNS_ENUMV(Character), 2, shape, solnames), viewer, 0); 775f34f2dcSJed Brown PetscCall(PetscFree(solnames)); 785f34f2dcSJed Brown 79*4c155f28SJames Wright PetscCallCGNSWrite(cg_simulation_type_write(cgv->file_num, cgv->base, CGNS_ENUMV(TimeAccurate)), viewer, 0); 805f34f2dcSJed Brown } 815f34f2dcSJed Brown PetscCall(PetscFree(cgv->filename)); 825f34f2dcSJed Brown #if defined(PETSC_HDF5_HAVE_PARALLEL) 83*4c155f28SJames Wright if (cgv->file_num) PetscCallCGNSClose(cgp_close(cgv->file_num), viewer, 0); 845f34f2dcSJed Brown #else 85*4c155f28SJames Wright if (cgv->file_num) PetscCallCGNSClose(cg_close(cgv->file_num), viewer, 0); 865f34f2dcSJed Brown #endif 875f34f2dcSJed Brown cgv->file_num = 0; 8825760affSJed Brown PetscCall(PetscFree(cgv->node_l2g)); 8925760affSJed Brown PetscCall(PetscFree(cgv->nodal_field)); 903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9125760affSJed Brown } 9225760affSJed Brown 9325760affSJed Brown PetscErrorCode PetscViewerCGNSFileOpen_Internal(PetscViewer viewer, PetscInt sequence_number) 9425760affSJed Brown { 9525760affSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 96ec76947aSJames Wright int cg_file_mode = -1; 974d86920dSPierre Jolivet 9825760affSJed Brown PetscFunctionBegin; 9925760affSJed 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"); 10025760affSJed Brown if (!cgv->filename) { 10125760affSJed Brown char filename_numbered[PETSC_MAX_PATH_LEN]; 10225760affSJed Brown // Cast sequence_number so %d can be used also when PetscInt is 64-bit. We could upgrade the format string if users 10325760affSJed Brown // run more than 2B time steps. 10425760affSJed Brown PetscCall(PetscSNPrintf(filename_numbered, sizeof filename_numbered, cgv->filename_template, (int)sequence_number)); 10525760affSJed Brown PetscCall(PetscStrallocpy(filename_numbered, &cgv->filename)); 10625760affSJed Brown } 10725760affSJed Brown switch (cgv->btype) { 10825760affSJed Brown case FILE_MODE_READ: 109ec76947aSJames Wright cg_file_mode = CG_MODE_READ; 11025760affSJed Brown break; 11125760affSJed Brown case FILE_MODE_WRITE: 112ec76947aSJames Wright cg_file_mode = CG_MODE_WRITE; 11325760affSJed Brown break; 11425760affSJed Brown case FILE_MODE_UNDEFINED: 11525760affSJed Brown SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()"); 11625760affSJed Brown default: 11725760affSJed Brown SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[cgv->btype]); 11825760affSJed Brown } 119ec76947aSJames Wright #if defined(PETSC_HDF5_HAVE_PARALLEL) 120ec76947aSJames Wright PetscCallCGNS(cgp_mpi_comm(PetscObjectComm((PetscObject)viewer))); 121*4c155f28SJames Wright PetscCallCGNSOpen(cgp_open(cgv->filename, cg_file_mode, &cgv->file_num), viewer, 0); 122ec76947aSJames Wright #else 123*4c155f28SJames Wright PetscCallCGNSOpen(cg_open(filename, cg_file_mode, &cgv->file_num), viewer, 0); 124ec76947aSJames Wright #endif 1253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 12625760affSJed Brown } 12725760affSJed Brown 12825760affSJed Brown PetscErrorCode PetscViewerCGNSCheckBatch_Internal(PetscViewer viewer) 12925760affSJed Brown { 13025760affSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 1316497c311SBarry Smith PetscCount num_steps; 13225760affSJed Brown 13325760affSJed Brown PetscFunctionBegin; 1343ba16761SJacob Faibussowitsch if (!cgv->filename_template) PetscFunctionReturn(PETSC_SUCCESS); // Batches are closed when viewer is destroyed 13525760affSJed Brown PetscCall(PetscSegBufferGetSize(cgv->output_times, &num_steps)); 1366497c311SBarry Smith if (num_steps >= (PetscCount)cgv->batch_size) PetscCall(PetscViewerFileClose_CGNS(viewer)); 1373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1385f34f2dcSJed Brown } 1395f34f2dcSJed Brown 140d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerDestroy_CGNS(PetscViewer viewer) 141d71ae5a4SJacob Faibussowitsch { 1425f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 1435f34f2dcSJed Brown 1445f34f2dcSJed Brown PetscFunctionBegin; 1455f34f2dcSJed Brown PetscCall(PetscViewerFileClose_CGNS(viewer)); 146472b9844SJames Wright PetscCall(PetscFree(cgv->solution_name)); 1475f34f2dcSJed Brown PetscCall(PetscFree(cgv)); 1485f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL)); 1495f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL)); 1505f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL)); 1515f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL)); 1523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1535f34f2dcSJed Brown } 1545f34f2dcSJed Brown 155d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileSetMode_CGNS(PetscViewer viewer, PetscFileMode type) 156d71ae5a4SJacob Faibussowitsch { 1575f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 1585f34f2dcSJed Brown 1595f34f2dcSJed Brown PetscFunctionBegin; 1605f34f2dcSJed Brown cgv->btype = type; 1613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1625f34f2dcSJed Brown } 1635f34f2dcSJed Brown 164d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileGetMode_CGNS(PetscViewer viewer, PetscFileMode *type) 165d71ae5a4SJacob Faibussowitsch { 1665f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 1675f34f2dcSJed Brown 1685f34f2dcSJed Brown PetscFunctionBegin; 1695f34f2dcSJed Brown *type = cgv->btype; 1703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1715f34f2dcSJed Brown } 1725f34f2dcSJed Brown 173d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileSetName_CGNS(PetscViewer viewer, const char *filename) 174d71ae5a4SJacob Faibussowitsch { 1755f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 17625760affSJed Brown char *has_pattern; 1775f34f2dcSJed Brown 1785f34f2dcSJed Brown PetscFunctionBegin; 1795582b114SJames Wright #if defined(PETSC_HDF5_HAVE_PARALLEL) 180*4c155f28SJames Wright if (cgv->file_num) PetscCallCGNSClose(cgp_close(cgv->file_num), viewer, 0); 1815582b114SJames Wright #else 182*4c155f28SJames Wright if (cgv->file_num) PetscCallCGNSClose(cg_close(cgv->file_num), viewer, 0); 1835582b114SJames Wright #endif 1845f34f2dcSJed Brown PetscCall(PetscFree(cgv->filename)); 18525760affSJed Brown PetscCall(PetscFree(cgv->filename_template)); 18625760affSJed Brown PetscCall(PetscStrchr(filename, '%', &has_pattern)); 18725760affSJed Brown if (has_pattern) { 18825760affSJed Brown PetscCall(PetscStrallocpy(filename, &cgv->filename_template)); 18925760affSJed Brown // Templated file names open lazily, once we know DMGetOutputSequenceNumber() 19025760affSJed Brown } else { 1915f34f2dcSJed Brown PetscCall(PetscStrallocpy(filename, &cgv->filename)); 19225760affSJed Brown PetscCall(PetscViewerCGNSFileOpen_Internal(viewer, -1)); 1935f34f2dcSJed Brown } 1943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1955f34f2dcSJed Brown } 1965f34f2dcSJed Brown 197d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileGetName_CGNS(PetscViewer viewer, const char **filename) 198d71ae5a4SJacob Faibussowitsch { 1995f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 2005f34f2dcSJed Brown 2015f34f2dcSJed Brown PetscFunctionBegin; 2025f34f2dcSJed Brown *filename = cgv->filename; 2033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2045f34f2dcSJed Brown } 2055f34f2dcSJed Brown 2065f34f2dcSJed Brown /*MC 2075f34f2dcSJed Brown PETSCVIEWERCGNS - A viewer for CGNS files 2085f34f2dcSJed Brown 2095f34f2dcSJed Brown Level: beginner 210811af0c4SBarry Smith 2113f423023SBarry Smith Options Database Key: 2123f423023SBarry Smith . -viewer_cgns_batch_size SIZE - set max number of output sequence times to write per batch 21325760affSJed Brown 2143f423023SBarry Smith Note: 21525760affSJed Brown If the filename contains an integer format character, the CGNS viewer will created a batched output sequence. For 21625760affSJed Brown example, one could use `-ts_monitor_solution cgns:flow-%d.cgns`. This is desirable if one wants to limit file sizes or 21725760affSJed Brown if the job might crash/be killed by a resource manager before exiting cleanly. 21825760affSJed Brown 21925760affSJed Brown .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `VecView()`, `DMView()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `TSSetFromOptions()` 2205f34f2dcSJed Brown M*/ 22114640894SJames Wright PetscErrorCode PetscViewerCreate_CGNS(PetscViewer v) 222d71ae5a4SJacob Faibussowitsch { 2235f34f2dcSJed Brown PetscViewer_CGNS *cgv; 2245f34f2dcSJed Brown 2255f34f2dcSJed Brown PetscFunctionBegin; 2265582b114SJames Wright PetscCall(PetscViewerCGNSRegisterLogEvents_Internal()); 2274dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&cgv)); 2285f34f2dcSJed Brown 2295f34f2dcSJed Brown v->data = cgv; 2305f34f2dcSJed Brown v->ops->destroy = PetscViewerDestroy_CGNS; 2315f34f2dcSJed Brown v->ops->setfromoptions = PetscViewerSetFromOptions_CGNS; 2325f34f2dcSJed Brown v->ops->view = PetscViewerView_CGNS; 2335f34f2dcSJed Brown cgv->btype = FILE_MODE_UNDEFINED; 2345f34f2dcSJed Brown cgv->filename = NULL; 23525760affSJed Brown cgv->batch_size = 20; 236472b9844SJames Wright cgv->solution_index = -1; // Default to use the "last" solution 237472b9844SJames Wright cgv->base = 1; 238472b9844SJames Wright cgv->zone = 1; 2395f34f2dcSJed Brown 2405f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_CGNS)); 2415f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_CGNS)); 2425f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_CGNS)); 2435f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_CGNS)); 2443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2455f34f2dcSJed Brown } 246472b9844SJames Wright 247472b9844SJames Wright // Find DataArray_t node under the current node (determined by `cg_goto` and friends) that matches `name` 248472b9844SJames Wright // Return the index of that array and (optionally) other data about the array 249472b9844SJames 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[]) 250472b9844SJames Wright { 251472b9844SJames Wright int narrays; // number of arrays under the current node 252472b9844SJames Wright char array_name[CGIO_MAX_NAME_LENGTH + 1]; 253472b9844SJames Wright CGNS_ENUMT(DataType_t) data_type_local; 254472b9844SJames Wright int _dim; 255472b9844SJames Wright cgsize_t _size[12]; 256472b9844SJames Wright PetscBool matches_name = PETSC_FALSE; 257472b9844SJames Wright 258472b9844SJames Wright PetscFunctionBeginUser; 259*4c155f28SJames Wright PetscCallCGNSRead(cg_narrays(&narrays), 0, 0); 260472b9844SJames Wright for (int i = 1; i <= narrays; i++) { 261*4c155f28SJames Wright PetscCallCGNSRead(cg_array_info(i, array_name, &data_type_local, &_dim, _size), 0, 0); 262472b9844SJames Wright PetscCall(PetscStrcmp(name, array_name, &matches_name)); 263472b9844SJames Wright if (matches_name) { 264472b9844SJames Wright *A_index = i; 265472b9844SJames Wright if (data_type) *data_type = data_type_local; 266472b9844SJames Wright if (dim) *dim = _dim; 267472b9844SJames Wright if (size) PetscArraycpy(size, _size, _dim); 268472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 269472b9844SJames Wright } 270472b9844SJames Wright } 271472b9844SJames Wright SETERRQ(comm, PETSC_ERR_SUP, "Cannot find %s array under current CGNS node", name); 272472b9844SJames Wright } 273472b9844SJames Wright 274472b9844SJames Wright /*@ 275472b9844SJames Wright PetscViewerCGNSOpen - Opens a file for CGNS input/output. 276472b9844SJames Wright 277472b9844SJames Wright Collective 278472b9844SJames Wright 279472b9844SJames Wright Input Parameters: 280472b9844SJames Wright + comm - MPI communicator 281472b9844SJames Wright . name - name of file 282472b9844SJames Wright - type - type of file 283472b9844SJames Wright .vb 284472b9844SJames Wright FILE_MODE_WRITE - create new file for binary output 285472b9844SJames Wright FILE_MODE_READ - open existing file for binary input 286472b9844SJames Wright FILE_MODE_APPEND - open existing file for binary output 287472b9844SJames Wright .ve 288472b9844SJames Wright 289472b9844SJames Wright Output Parameter: 290472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 291472b9844SJames Wright 292472b9844SJames Wright Level: beginner 293472b9844SJames Wright 294472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, 295472b9844SJames Wright `DMLoad()`, `PetscFileMode`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()` 296472b9844SJames Wright @*/ 297472b9844SJames Wright PetscErrorCode PetscViewerCGNSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *viewer) 298472b9844SJames Wright { 299472b9844SJames Wright PetscFunctionBegin; 30014640894SJames Wright PetscAssertPointer(name, 2); 30114640894SJames Wright PetscAssertPointer(viewer, 4); 302472b9844SJames Wright PetscCall(PetscViewerCreate(comm, viewer)); 303472b9844SJames Wright PetscCall(PetscViewerSetType(*viewer, PETSCVIEWERCGNS)); 304472b9844SJames Wright PetscCall(PetscViewerFileSetMode(*viewer, type)); 305472b9844SJames Wright PetscCall(PetscViewerFileSetName(*viewer, name)); 306472b9844SJames Wright PetscCall(PetscViewerSetFromOptions(*viewer)); 307472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 308472b9844SJames Wright } 309472b9844SJames Wright 310472b9844SJames Wright /*@ 311472b9844SJames Wright PetscViewerCGNSSetSolutionIndex - Set index of solution 312472b9844SJames Wright 313472b9844SJames Wright Not Collective 314472b9844SJames Wright 315472b9844SJames Wright Input Parameters: 316472b9844SJames Wright + viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 317472b9844SJames Wright - solution_id - Index of the solution id, or `-1` for the last solution on the file 318472b9844SJames Wright 319472b9844SJames Wright Level: intermediate 320472b9844SJames Wright 321472b9844SJames Wright Notes: 322472b9844SJames Wright By default, `solution_id` is set to `-1` to mean the last solution available in the file. 323472b9844SJames Wright If the file contains a FlowSolutionPointers node, then that array is indexed to determine which FlowSolution_t node to read from. 324472b9844SJames Wright Otherwise, `solution_id` indexes the total available FlowSolution_t nodes in the file. 325472b9844SJames Wright 326472b9844SJames Wright This solution index is used by `VecLoad()` to determine which solution to load from the file 327472b9844SJames Wright 328472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()` 329472b9844SJames Wright 330472b9844SJames Wright @*/ 331472b9844SJames Wright PetscErrorCode PetscViewerCGNSSetSolutionIndex(PetscViewer viewer, PetscInt solution_id) 332472b9844SJames Wright { 333472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 334472b9844SJames Wright 335472b9844SJames Wright PetscFunctionBeginUser; 33614640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 33714640894SJames Wright PetscValidLogicalCollectiveInt(viewer, solution_id, 2); 338472b9844SJames 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); 339472b9844SJames Wright cgv->solution_index = solution_id; 340472b9844SJames Wright cgv->solution_file_index = 0; // Reset sol_index when solution_id changes (0 is invalid) 341472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 342472b9844SJames Wright } 343472b9844SJames Wright 344472b9844SJames Wright /*@ 345472b9844SJames Wright PetscViewerCGNSGetSolutionIndex - Get index of solution 346472b9844SJames Wright 347472b9844SJames Wright Not Collective 348472b9844SJames Wright 349472b9844SJames Wright Input Parameter: 350472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 351472b9844SJames Wright 352472b9844SJames Wright Output Parameter: 353472b9844SJames Wright . solution_id - Index of the solution id 354472b9844SJames Wright 355472b9844SJames Wright Level: intermediate 356472b9844SJames Wright 357472b9844SJames Wright Notes: 358472b9844SJames Wright By default, solution_id is set to `-1` to mean the last solution available in the file 359472b9844SJames Wright 360472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()` 361472b9844SJames Wright 362472b9844SJames Wright @*/ 363472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionIndex(PetscViewer viewer, PetscInt *solution_id) 364472b9844SJames Wright { 365472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 366472b9844SJames Wright 367472b9844SJames Wright PetscFunctionBeginUser; 36814640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 36914640894SJames Wright PetscAssertPointer(solution_id, 2); 370472b9844SJames Wright *solution_id = cgv->solution_index; 371472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 372472b9844SJames Wright } 373472b9844SJames Wright 374472b9844SJames Wright // Gets the index for the solution in this CGNS file 375472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionFileIndex_Internal(PetscViewer viewer, int *sol_index) 376472b9844SJames Wright { 377472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 378472b9844SJames Wright MPI_Comm comm = PetscObjectComm((PetscObject)viewer); 379472b9844SJames Wright int nsols, cgns_ier; 380472b9844SJames Wright char buffer[CGIO_MAX_NAME_LENGTH + 1]; 381472b9844SJames Wright CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway 382472b9844SJames Wright 383472b9844SJames Wright PetscFunctionBeginUser; 384472b9844SJames Wright if (cgv->solution_file_index > 0) { 385472b9844SJames Wright *sol_index = cgv->solution_file_index; 386472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 387472b9844SJames Wright } 388472b9844SJames Wright 389*4c155f28SJames Wright PetscCallCGNSRead(cg_nsols(cgv->file_num, cgv->base, cgv->zone, &nsols), viewer, 0); 390472b9844SJames Wright cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, "FlowSolutionPointers", 0, NULL); 391472b9844SJames Wright if (cgns_ier == CG_NODE_NOT_FOUND) { 392472b9844SJames Wright // If FlowSolutionPointers does not exist, then just index off of nsols (which can include non-solution data) 393472b9844SJames 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); 394472b9844SJames Wright 395472b9844SJames Wright cgv->solution_file_index = cgv->solution_index == -1 ? nsols : cgv->solution_index; 396472b9844SJames Wright } else { 397472b9844SJames Wright // If FlowSolutionPointers exists, then solution_id should index that array of FlowSolutions 398472b9844SJames Wright char *pointer_id_name; 399472b9844SJames Wright PetscBool matches_name = PETSC_FALSE; 400472b9844SJames Wright int sol_id; 401472b9844SJames Wright 402472b9844SJames Wright PetscCheck(cgns_ier == CG_OK, PETSC_COMM_SELF, PETSC_ERR_LIB, "CGNS error %d %s", cgns_ier, cg_get_error()); 403472b9844SJames Wright cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL); 404472b9844SJames Wright 405472b9844SJames Wright { // Get FlowSolutionPointer name corresponding to solution_id 406472b9844SJames Wright cgsize_t size[12]; 407472b9844SJames Wright int dim, A_index, pointer_id; 408472b9844SJames Wright char *pointer_names, *pointer_id_name_ref; 409472b9844SJames Wright 410472b9844SJames Wright PetscCall(CGNS_Find_Array(comm, "FlowSolutionPointers", &A_index, NULL, &dim, size)); 411472b9844SJames 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]); 412472b9844SJames 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 413*4c155f28SJames Wright PetscCallCGNSRead(cg_array_read_as(1, CGNS_ENUMV(Character), pointer_names), viewer, 0); 414472b9844SJames Wright pointer_id = cgv->solution_index == -1 ? size[1] : cgv->solution_index; 415472b9844SJames Wright pointer_id_name_ref = &pointer_names[size[0] * (pointer_id - 1)]; 416472b9844SJames Wright { // Set last non-whitespace character of the pointer name to \0 (CGNS pads with spaces) 417472b9844SJames Wright int str_idx; 418472b9844SJames Wright for (str_idx = size[0] - 1; str_idx > 0; str_idx--) { 419472b9844SJames Wright if (!isspace((unsigned char)pointer_id_name_ref[str_idx])) break; 420472b9844SJames Wright } 421472b9844SJames Wright pointer_id_name_ref[str_idx + 1] = '\0'; 422472b9844SJames Wright } 423472b9844SJames Wright PetscCall(PetscStrallocpy(pointer_id_name_ref, &pointer_id_name)); 424472b9844SJames Wright PetscCall(PetscFree(pointer_names)); 425472b9844SJames Wright } 426472b9844SJames Wright 427472b9844SJames Wright // Find FlowSolution_t node that matches pointer_id_name 428472b9844SJames Wright for (sol_id = 1; sol_id <= nsols; sol_id++) { 429*4c155f28SJames Wright PetscCallCGNSRead(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc), viewer, 0); 430472b9844SJames Wright PetscCall(PetscStrcmp(pointer_id_name, buffer, &matches_name)); 431472b9844SJames Wright if (matches_name) break; 432472b9844SJames Wright } 433472b9844SJames Wright PetscCheck(matches_name, comm, PETSC_ERR_LIB, "Cannot find FlowSolution_t node %s in file", pointer_id_name); 434472b9844SJames Wright cgv->solution_file_index = sol_id; 435472b9844SJames Wright PetscCall(PetscFree(pointer_id_name)); 436472b9844SJames Wright } 437472b9844SJames Wright 438472b9844SJames Wright *sol_index = cgv->solution_file_index; 439472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 440472b9844SJames Wright } 441472b9844SJames Wright 442472b9844SJames Wright /*@ 443472b9844SJames Wright PetscViewerCGNSGetSolutionTime - Gets the solution time for the FlowSolution of the viewer 444472b9844SJames Wright 445472b9844SJames Wright Collective 446472b9844SJames Wright 447472b9844SJames Wright Input Parameter: 448472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 449472b9844SJames Wright 4507dcfde4dSJose E. Roman Output Parameters: 451472b9844SJames Wright + time - Solution time of the FlowSolution_t node 452472b9844SJames Wright - set - Whether the time data is in the file 453472b9844SJames Wright 454472b9844SJames Wright Level: intermediate 455472b9844SJames Wright 456472b9844SJames Wright Notes: 457472b9844SJames Wright Reads data from a DataArray named `TimeValues` under a `BaseIterativeData_t` node 458472b9844SJames Wright 459472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionName()` 460472b9844SJames Wright @*/ 461472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionTime(PetscViewer viewer, PetscReal *time, PetscBool *set) 462472b9844SJames Wright { 463472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 464472b9844SJames Wright int cgns_ier, A_index = 0; 465472b9844SJames Wright PetscReal *times; 466472b9844SJames Wright cgsize_t size[12]; 467472b9844SJames Wright 468472b9844SJames Wright PetscFunctionBeginUser; 46914640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 47014640894SJames Wright PetscAssertPointer(time, 2); 47114640894SJames Wright PetscAssertPointer(set, 3); 472472b9844SJames Wright cgns_ier = cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL); 473472b9844SJames Wright if (cgns_ier == CG_NODE_NOT_FOUND) { 474472b9844SJames Wright *set = PETSC_FALSE; 475472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 476472b9844SJames Wright } else PetscCallCGNS(cgns_ier); 477472b9844SJames Wright PetscCall(CGNS_Find_Array(PetscObjectComm((PetscObject)viewer), "TimeValues", &A_index, NULL, NULL, size)); 478472b9844SJames Wright PetscCall(PetscMalloc1(size[0], ×)); 479*4c155f28SJames Wright PetscCallCGNSRead(cg_array_read_as(A_index, CGNS_ENUMV(RealDouble), times), viewer, 0); 480472b9844SJames Wright *time = times[cgv->solution_index - 1]; 481472b9844SJames Wright *set = PETSC_TRUE; 482472b9844SJames Wright PetscCall(PetscFree(times)); 483472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 484472b9844SJames Wright } 485472b9844SJames Wright 486472b9844SJames Wright /*@ 487472b9844SJames Wright PetscViewerCGNSGetSolutionName - Gets name of FlowSolution of the viewer 488472b9844SJames Wright 489472b9844SJames Wright Collective 490472b9844SJames Wright 491472b9844SJames Wright Input Parameter: 492472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 493472b9844SJames Wright 494472b9844SJames Wright Output Parameter: 495472b9844SJames Wright . name - Name of the FlowSolution_t node corresponding to the solution index 496472b9844SJames Wright 497472b9844SJames Wright Level: intermediate 498472b9844SJames Wright 499472b9844SJames Wright Notes: 500472b9844SJames Wright Currently assumes there is only one Zone in the CGNS file 501472b9844SJames Wright 502472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionTime()` 503472b9844SJames Wright @*/ 504472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionName(PetscViewer viewer, const char *name[]) 505472b9844SJames Wright { 506472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 507472b9844SJames Wright int sol_id; 508472b9844SJames Wright char buffer[CGIO_MAX_NAME_LENGTH + 1]; 509472b9844SJames Wright CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway 510472b9844SJames Wright 511472b9844SJames Wright PetscFunctionBeginUser; 51214640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 51314640894SJames Wright PetscAssertPointer(name, 2); 514472b9844SJames Wright PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); 515472b9844SJames Wright 516*4c155f28SJames Wright PetscCallCGNSRead(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc), viewer, 0); 517472b9844SJames Wright if (cgv->solution_name) PetscCall(PetscFree(cgv->solution_name)); 518472b9844SJames Wright PetscCall(PetscStrallocpy(buffer, &cgv->solution_name)); 519472b9844SJames Wright *name = cgv->solution_name; 520472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 521472b9844SJames Wright } 522