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, ×)); 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)); 1545f34f2dcSJed Brown PetscCall(PetscFree(cgv)); 1555f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL)); 1565f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL)); 1575f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL)); 1585f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL)); 1593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1605f34f2dcSJed Brown } 1615f34f2dcSJed Brown 162d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileSetMode_CGNS(PetscViewer viewer, PetscFileMode type) 163d71ae5a4SJacob Faibussowitsch { 1645f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 1655f34f2dcSJed Brown 1665f34f2dcSJed Brown PetscFunctionBegin; 1675f34f2dcSJed Brown cgv->btype = type; 1683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1695f34f2dcSJed Brown } 1705f34f2dcSJed Brown 171d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileGetMode_CGNS(PetscViewer viewer, PetscFileMode *type) 172d71ae5a4SJacob Faibussowitsch { 1735f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 1745f34f2dcSJed Brown 1755f34f2dcSJed Brown PetscFunctionBegin; 1765f34f2dcSJed Brown *type = cgv->btype; 1773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1785f34f2dcSJed Brown } 1795f34f2dcSJed Brown 180d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileSetName_CGNS(PetscViewer viewer, const char *filename) 181d71ae5a4SJacob Faibussowitsch { 1825f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 18325760affSJed Brown char *has_pattern; 1845f34f2dcSJed Brown 1855f34f2dcSJed Brown PetscFunctionBegin; 1865582b114SJames Wright #if defined(PETSC_HDF5_HAVE_PARALLEL) 1874c155f28SJames Wright if (cgv->file_num) PetscCallCGNSClose(cgp_close(cgv->file_num), viewer, 0); 1885582b114SJames Wright #else 1894c155f28SJames Wright if (cgv->file_num) PetscCallCGNSClose(cg_close(cgv->file_num), viewer, 0); 1905582b114SJames Wright #endif 1915f34f2dcSJed Brown PetscCall(PetscFree(cgv->filename)); 19225760affSJed Brown PetscCall(PetscFree(cgv->filename_template)); 19325760affSJed Brown PetscCall(PetscStrchr(filename, '%', &has_pattern)); 19425760affSJed Brown if (has_pattern) { 19525760affSJed Brown PetscCall(PetscStrallocpy(filename, &cgv->filename_template)); 19625760affSJed Brown // Templated file names open lazily, once we know DMGetOutputSequenceNumber() 19725760affSJed Brown } else { 1985f34f2dcSJed Brown PetscCall(PetscStrallocpy(filename, &cgv->filename)); 19925760affSJed Brown PetscCall(PetscViewerCGNSFileOpen_Internal(viewer, -1)); 2005f34f2dcSJed Brown } 2013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2025f34f2dcSJed Brown } 2035f34f2dcSJed Brown 204d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileGetName_CGNS(PetscViewer viewer, const char **filename) 205d71ae5a4SJacob Faibussowitsch { 2065f34f2dcSJed Brown PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 2075f34f2dcSJed Brown 2085f34f2dcSJed Brown PetscFunctionBegin; 2095f34f2dcSJed Brown *filename = cgv->filename; 2103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2115f34f2dcSJed Brown } 2125f34f2dcSJed Brown 2135f34f2dcSJed Brown /*MC 2145f34f2dcSJed Brown PETSCVIEWERCGNS - A viewer for CGNS files 2155f34f2dcSJed Brown 2165f34f2dcSJed Brown Level: beginner 217811af0c4SBarry Smith 2183f423023SBarry Smith Options Database Key: 2193f423023SBarry Smith . -viewer_cgns_batch_size SIZE - set max number of output sequence times to write per batch 22025760affSJed Brown 2213f423023SBarry Smith Note: 22225760affSJed Brown If the filename contains an integer format character, the CGNS viewer will created a batched output sequence. For 22325760affSJed Brown example, one could use `-ts_monitor_solution cgns:flow-%d.cgns`. This is desirable if one wants to limit file sizes or 22425760affSJed Brown if the job might crash/be killed by a resource manager before exiting cleanly. 22525760affSJed Brown 22625760affSJed Brown .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `VecView()`, `DMView()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `TSSetFromOptions()` 2275f34f2dcSJed Brown M*/ 22814640894SJames Wright PetscErrorCode PetscViewerCreate_CGNS(PetscViewer v) 229d71ae5a4SJacob Faibussowitsch { 2305f34f2dcSJed Brown PetscViewer_CGNS *cgv; 2315f34f2dcSJed Brown 2325f34f2dcSJed Brown PetscFunctionBegin; 2335582b114SJames Wright PetscCall(PetscViewerCGNSRegisterLogEvents_Internal()); 2344dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&cgv)); 2355f34f2dcSJed Brown 2365f34f2dcSJed Brown v->data = cgv; 2375f34f2dcSJed Brown v->ops->destroy = PetscViewerDestroy_CGNS; 2385f34f2dcSJed Brown v->ops->setfromoptions = PetscViewerSetFromOptions_CGNS; 2395f34f2dcSJed Brown v->ops->view = PetscViewerView_CGNS; 2405f34f2dcSJed Brown cgv->btype = FILE_MODE_UNDEFINED; 2415f34f2dcSJed Brown cgv->filename = NULL; 24225760affSJed Brown cgv->batch_size = 20; 243472b9844SJames Wright cgv->solution_index = -1; // Default to use the "last" solution 244472b9844SJames Wright cgv->base = 1; 245472b9844SJames Wright cgv->zone = 1; 2465f34f2dcSJed Brown 2475f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_CGNS)); 2485f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_CGNS)); 2495f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_CGNS)); 2505f34f2dcSJed Brown PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_CGNS)); 2513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2525f34f2dcSJed Brown } 253472b9844SJames Wright 254472b9844SJames Wright // Find DataArray_t node under the current node (determined by `cg_goto` and friends) that matches `name` 255472b9844SJames Wright // Return the index of that array and (optionally) other data about the array 2560919f86aSJames 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) 257472b9844SJames Wright { 258472b9844SJames Wright int narrays; // number of arrays under the current node 259472b9844SJames Wright char array_name[CGIO_MAX_NAME_LENGTH + 1]; 260472b9844SJames Wright CGNS_ENUMT(DataType_t) data_type_local; 261472b9844SJames Wright int _dim; 262472b9844SJames Wright cgsize_t _size[12]; 263472b9844SJames Wright PetscBool matches_name = PETSC_FALSE; 264472b9844SJames Wright 265472b9844SJames Wright PetscFunctionBeginUser; 2664c155f28SJames Wright PetscCallCGNSRead(cg_narrays(&narrays), 0, 0); 267472b9844SJames Wright for (int i = 1; i <= narrays; i++) { 2684c155f28SJames Wright PetscCallCGNSRead(cg_array_info(i, array_name, &data_type_local, &_dim, _size), 0, 0); 269472b9844SJames Wright PetscCall(PetscStrcmp(name, array_name, &matches_name)); 270472b9844SJames Wright if (matches_name) { 271472b9844SJames Wright *A_index = i; 272472b9844SJames Wright if (data_type) *data_type = data_type_local; 273472b9844SJames Wright if (dim) *dim = _dim; 274472b9844SJames Wright if (size) PetscArraycpy(size, _size, _dim); 2750919f86aSJames Wright if (found) *found = PETSC_TRUE; 276472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 277472b9844SJames Wright } 278472b9844SJames Wright } 2790919f86aSJames Wright if (found) *found = PETSC_FALSE; 2800919f86aSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 281472b9844SJames Wright } 282472b9844SJames Wright 283472b9844SJames Wright /*@ 284472b9844SJames Wright PetscViewerCGNSOpen - Opens a file for CGNS input/output. 285472b9844SJames Wright 286472b9844SJames Wright Collective 287472b9844SJames Wright 288472b9844SJames Wright Input Parameters: 289472b9844SJames Wright + comm - MPI communicator 290472b9844SJames Wright . name - name of file 291472b9844SJames Wright - type - type of file 292472b9844SJames Wright .vb 293472b9844SJames Wright FILE_MODE_WRITE - create new file for binary output 294472b9844SJames Wright FILE_MODE_READ - open existing file for binary input 295472b9844SJames Wright FILE_MODE_APPEND - open existing file for binary output 296472b9844SJames Wright .ve 297472b9844SJames Wright 298472b9844SJames Wright Output Parameter: 299472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 300472b9844SJames Wright 301472b9844SJames Wright Level: beginner 302472b9844SJames Wright 303472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, 304472b9844SJames Wright `DMLoad()`, `PetscFileMode`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()` 305472b9844SJames Wright @*/ 306472b9844SJames Wright PetscErrorCode PetscViewerCGNSOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *viewer) 307472b9844SJames Wright { 308472b9844SJames Wright PetscFunctionBegin; 30914640894SJames Wright PetscAssertPointer(name, 2); 31014640894SJames Wright PetscAssertPointer(viewer, 4); 311472b9844SJames Wright PetscCall(PetscViewerCreate(comm, viewer)); 312472b9844SJames Wright PetscCall(PetscViewerSetType(*viewer, PETSCVIEWERCGNS)); 313472b9844SJames Wright PetscCall(PetscViewerFileSetMode(*viewer, type)); 314472b9844SJames Wright PetscCall(PetscViewerFileSetName(*viewer, name)); 315472b9844SJames Wright PetscCall(PetscViewerSetFromOptions(*viewer)); 316472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 317472b9844SJames Wright } 318472b9844SJames Wright 319472b9844SJames Wright /*@ 320472b9844SJames Wright PetscViewerCGNSSetSolutionIndex - Set index of solution 321472b9844SJames Wright 322472b9844SJames Wright Not Collective 323472b9844SJames Wright 324472b9844SJames Wright Input Parameters: 325472b9844SJames Wright + viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 326472b9844SJames Wright - solution_id - Index of the solution id, or `-1` for the last solution on the file 327472b9844SJames Wright 328472b9844SJames Wright Level: intermediate 329472b9844SJames Wright 330472b9844SJames Wright Notes: 331472b9844SJames Wright By default, `solution_id` is set to `-1` to mean the last solution available in the file. 332472b9844SJames Wright If the file contains a FlowSolutionPointers node, then that array is indexed to determine which FlowSolution_t node to read from. 333472b9844SJames Wright Otherwise, `solution_id` indexes the total available FlowSolution_t nodes in the file. 334472b9844SJames Wright 335472b9844SJames Wright This solution index is used by `VecLoad()` to determine which solution to load from the file 336472b9844SJames Wright 337472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()` 338472b9844SJames Wright 339472b9844SJames Wright @*/ 340472b9844SJames Wright PetscErrorCode PetscViewerCGNSSetSolutionIndex(PetscViewer viewer, PetscInt solution_id) 341472b9844SJames Wright { 342472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 343472b9844SJames Wright 344472b9844SJames Wright PetscFunctionBeginUser; 34514640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 34614640894SJames Wright PetscValidLogicalCollectiveInt(viewer, solution_id, 2); 347472b9844SJames 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); 348472b9844SJames Wright cgv->solution_index = solution_id; 349472b9844SJames Wright cgv->solution_file_index = 0; // Reset sol_index when solution_id changes (0 is invalid) 350472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 351472b9844SJames Wright } 352472b9844SJames Wright 353472b9844SJames Wright /*@ 354472b9844SJames Wright PetscViewerCGNSGetSolutionIndex - Get index of solution 355472b9844SJames Wright 356472b9844SJames Wright Not Collective 357472b9844SJames Wright 358472b9844SJames Wright Input Parameter: 359472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 360472b9844SJames Wright 361472b9844SJames Wright Output Parameter: 362472b9844SJames Wright . solution_id - Index of the solution id 363472b9844SJames Wright 364472b9844SJames Wright Level: intermediate 365472b9844SJames Wright 366472b9844SJames Wright Notes: 367472b9844SJames Wright By default, solution_id is set to `-1` to mean the last solution available in the file 368472b9844SJames Wright 369472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionInfo()` 370472b9844SJames Wright 371472b9844SJames Wright @*/ 372472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionIndex(PetscViewer viewer, PetscInt *solution_id) 373472b9844SJames Wright { 374472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 375472b9844SJames Wright 376472b9844SJames Wright PetscFunctionBeginUser; 37714640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 37814640894SJames Wright PetscAssertPointer(solution_id, 2); 379472b9844SJames Wright *solution_id = cgv->solution_index; 380472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 381472b9844SJames Wright } 382472b9844SJames Wright 383472b9844SJames Wright // Gets the index for the solution in this CGNS file 384472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionFileIndex_Internal(PetscViewer viewer, int *sol_index) 385472b9844SJames Wright { 386472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 387472b9844SJames Wright MPI_Comm comm = PetscObjectComm((PetscObject)viewer); 388472b9844SJames Wright int nsols, cgns_ier; 389472b9844SJames Wright char buffer[CGIO_MAX_NAME_LENGTH + 1]; 390472b9844SJames Wright CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway 391472b9844SJames Wright 392472b9844SJames Wright PetscFunctionBeginUser; 393472b9844SJames Wright if (cgv->solution_file_index > 0) { 394472b9844SJames Wright *sol_index = cgv->solution_file_index; 395472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 396472b9844SJames Wright } 397472b9844SJames Wright 3984c155f28SJames Wright PetscCallCGNSRead(cg_nsols(cgv->file_num, cgv->base, cgv->zone, &nsols), viewer, 0); 399472b9844SJames Wright cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, "FlowSolutionPointers", 0, NULL); 400472b9844SJames Wright if (cgns_ier == CG_NODE_NOT_FOUND) { 401472b9844SJames Wright // If FlowSolutionPointers does not exist, then just index off of nsols (which can include non-solution data) 402472b9844SJames 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); 403472b9844SJames Wright 404472b9844SJames Wright cgv->solution_file_index = cgv->solution_index == -1 ? nsols : cgv->solution_index; 405472b9844SJames Wright } else { 406472b9844SJames Wright // If FlowSolutionPointers exists, then solution_id should index that array of FlowSolutions 407472b9844SJames Wright char *pointer_id_name; 408472b9844SJames Wright PetscBool matches_name = PETSC_FALSE; 409472b9844SJames Wright int sol_id; 410472b9844SJames Wright 411472b9844SJames Wright PetscCheck(cgns_ier == CG_OK, PETSC_COMM_SELF, PETSC_ERR_LIB, "CGNS error %d %s", cgns_ier, cg_get_error()); 412472b9844SJames Wright cgns_ier = cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL); 413472b9844SJames Wright 414472b9844SJames Wright { // Get FlowSolutionPointer name corresponding to solution_id 415472b9844SJames Wright cgsize_t size[12]; 416b63c620bSJames Wright int dim, A_index; 417472b9844SJames Wright char *pointer_names, *pointer_id_name_ref; 4180919f86aSJames Wright PetscBool found_array; 419472b9844SJames Wright 4200919f86aSJames Wright PetscCall(CGNS_Find_Array(comm, "FlowSolutionPointers", &A_index, NULL, &dim, size, &found_array)); 4210919f86aSJames Wright PetscCheck(found_array, comm, PETSC_ERR_SUP, "Cannot find FlowSolutionPointers array under current CGNS node"); 422472b9844SJames 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]); 423472b9844SJames 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 4244c155f28SJames Wright PetscCallCGNSRead(cg_array_read_as(1, CGNS_ENUMV(Character), pointer_names), viewer, 0); 425b63c620bSJames Wright cgv->solution_file_pointer_index = cgv->solution_index == -1 ? size[1] : cgv->solution_index; 426b63c620bSJames Wright pointer_id_name_ref = &pointer_names[size[0] * (cgv->solution_file_pointer_index - 1)]; 427472b9844SJames Wright { // Set last non-whitespace character of the pointer name to \0 (CGNS pads with spaces) 428472b9844SJames Wright int str_idx; 429472b9844SJames Wright for (str_idx = size[0] - 1; str_idx > 0; str_idx--) { 430472b9844SJames Wright if (!isspace((unsigned char)pointer_id_name_ref[str_idx])) break; 431472b9844SJames Wright } 432472b9844SJames Wright pointer_id_name_ref[str_idx + 1] = '\0'; 433472b9844SJames Wright } 434472b9844SJames Wright PetscCall(PetscStrallocpy(pointer_id_name_ref, &pointer_id_name)); 435472b9844SJames Wright PetscCall(PetscFree(pointer_names)); 436472b9844SJames Wright } 437472b9844SJames Wright 438472b9844SJames Wright // Find FlowSolution_t node that matches pointer_id_name 439472b9844SJames Wright for (sol_id = 1; sol_id <= nsols; sol_id++) { 4404c155f28SJames Wright PetscCallCGNSRead(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc), viewer, 0); 441472b9844SJames Wright PetscCall(PetscStrcmp(pointer_id_name, buffer, &matches_name)); 442472b9844SJames Wright if (matches_name) break; 443472b9844SJames Wright } 444472b9844SJames Wright PetscCheck(matches_name, comm, PETSC_ERR_LIB, "Cannot find FlowSolution_t node %s in file", pointer_id_name); 445472b9844SJames Wright cgv->solution_file_index = sol_id; 446472b9844SJames Wright PetscCall(PetscFree(pointer_id_name)); 447472b9844SJames Wright } 448472b9844SJames Wright 449472b9844SJames Wright *sol_index = cgv->solution_file_index; 450472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 451472b9844SJames Wright } 452472b9844SJames Wright 453472b9844SJames Wright /*@ 454472b9844SJames Wright PetscViewerCGNSGetSolutionTime - Gets the solution time for the FlowSolution of the viewer 455472b9844SJames Wright 456472b9844SJames Wright Collective 457472b9844SJames Wright 458472b9844SJames Wright Input Parameter: 459472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 460472b9844SJames Wright 4617dcfde4dSJose E. Roman Output Parameters: 462472b9844SJames Wright + time - Solution time of the FlowSolution_t node 463472b9844SJames Wright - set - Whether the time data is in the file 464472b9844SJames Wright 465472b9844SJames Wright Level: intermediate 466472b9844SJames Wright 467472b9844SJames Wright Notes: 468472b9844SJames Wright Reads data from a DataArray named `TimeValues` under a `BaseIterativeData_t` node 469472b9844SJames Wright 470*bd70a9a6SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSGetSolutionIteration()`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionName()` 471472b9844SJames Wright @*/ 472472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionTime(PetscViewer viewer, PetscReal *time, PetscBool *set) 473472b9844SJames Wright { 474472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 475b63c620bSJames Wright int cgns_ier, A_index = 0, sol_id; 476472b9844SJames Wright PetscReal *times; 477472b9844SJames Wright cgsize_t size[12]; 478472b9844SJames Wright 479472b9844SJames Wright PetscFunctionBeginUser; 48014640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 48114640894SJames Wright PetscAssertPointer(time, 2); 48214640894SJames Wright PetscAssertPointer(set, 3); 483472b9844SJames Wright cgns_ier = cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL); 484472b9844SJames Wright if (cgns_ier == CG_NODE_NOT_FOUND) { 485472b9844SJames Wright *set = PETSC_FALSE; 486472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 487472b9844SJames Wright } else PetscCallCGNS(cgns_ier); 4880919f86aSJames Wright PetscCall(CGNS_Find_Array(PetscObjectComm((PetscObject)viewer), "TimeValues", &A_index, NULL, NULL, size, set)); 4890919f86aSJames Wright if (!set) PetscFunctionReturn(PETSC_SUCCESS); 490472b9844SJames Wright PetscCall(PetscMalloc1(size[0], ×)); 4914c155f28SJames Wright PetscCallCGNSRead(cg_array_read_as(A_index, CGNS_ENUMV(RealDouble), times), viewer, 0); 492b63c620bSJames Wright PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); // Call to set file pointer index 493b63c620bSJames Wright *time = times[cgv->solution_file_pointer_index - 1]; 494472b9844SJames Wright PetscCall(PetscFree(times)); 495472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 496472b9844SJames Wright } 497472b9844SJames Wright 498472b9844SJames Wright /*@ 499*bd70a9a6SJames Wright PetscViewerCGNSGetSolutionIteration - Gets the solution iteration for the FlowSolution of the viewer 500*bd70a9a6SJames Wright 501*bd70a9a6SJames Wright Collective 502*bd70a9a6SJames Wright 503*bd70a9a6SJames Wright Input Parameter: 504*bd70a9a6SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 505*bd70a9a6SJames Wright 506*bd70a9a6SJames Wright Output Parameters: 507*bd70a9a6SJames Wright + iteration - Solution iteration of the FlowSolution_t node 508*bd70a9a6SJames Wright - set - Whether the time data is in the file 509*bd70a9a6SJames Wright 510*bd70a9a6SJames Wright Level: intermediate 511*bd70a9a6SJames Wright 512*bd70a9a6SJames Wright Notes: 513*bd70a9a6SJames Wright Reads data from a DataArray named `IterationValues` under a `BaseIterativeData_t` node 514*bd70a9a6SJames Wright 515*bd70a9a6SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSGetSolutionTime()`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionName()` 516*bd70a9a6SJames Wright @*/ 517*bd70a9a6SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionIteration(PetscViewer viewer, PetscInt *iteration, PetscBool *set) 518*bd70a9a6SJames Wright { 519*bd70a9a6SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 520*bd70a9a6SJames Wright int cgns_ier, A_index = 0, sol_id; 521*bd70a9a6SJames Wright int *steps; 522*bd70a9a6SJames Wright cgsize_t size[12]; 523*bd70a9a6SJames Wright 524*bd70a9a6SJames Wright PetscFunctionBeginUser; 525*bd70a9a6SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 526*bd70a9a6SJames Wright PetscAssertPointer(iteration, 2); 527*bd70a9a6SJames Wright PetscAssertPointer(set, 3); 528*bd70a9a6SJames Wright cgns_ier = cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL); 529*bd70a9a6SJames Wright if (cgns_ier == CG_NODE_NOT_FOUND) { 530*bd70a9a6SJames Wright *set = PETSC_FALSE; 531*bd70a9a6SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 532*bd70a9a6SJames Wright } else PetscCallCGNS(cgns_ier); 533*bd70a9a6SJames Wright PetscCall(CGNS_Find_Array(PetscObjectComm((PetscObject)viewer), "IterationValues", &A_index, NULL, NULL, size, set)); 534*bd70a9a6SJames Wright if (!set) PetscFunctionReturn(PETSC_SUCCESS); 535*bd70a9a6SJames Wright PetscCall(PetscMalloc1(size[0], &steps)); 536*bd70a9a6SJames Wright PetscCallCGNSRead(cg_array_read_as(A_index, CGNS_ENUMV(Integer), steps), viewer, 0); 537*bd70a9a6SJames Wright PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); // Call to set file pointer index 538*bd70a9a6SJames Wright *iteration = (PetscInt)steps[cgv->solution_file_pointer_index - 1]; 539*bd70a9a6SJames Wright PetscCall(PetscFree(steps)); 540*bd70a9a6SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 541*bd70a9a6SJames Wright } 542*bd70a9a6SJames Wright 543*bd70a9a6SJames Wright /*@ 544472b9844SJames Wright PetscViewerCGNSGetSolutionName - Gets name of FlowSolution of the viewer 545472b9844SJames Wright 546472b9844SJames Wright Collective 547472b9844SJames Wright 548472b9844SJames Wright Input Parameter: 549472b9844SJames Wright . viewer - `PETSCVIEWERCGNS` `PetscViewer` for CGNS input/output to use with the specified file 550472b9844SJames Wright 551472b9844SJames Wright Output Parameter: 552472b9844SJames Wright . name - Name of the FlowSolution_t node corresponding to the solution index 553472b9844SJames Wright 554472b9844SJames Wright Level: intermediate 555472b9844SJames Wright 556472b9844SJames Wright Notes: 557472b9844SJames Wright Currently assumes there is only one Zone in the CGNS file 558472b9844SJames Wright 559472b9844SJames Wright .seealso: `PETSCVIEWERCGNS`, `PetscViewer`, `PetscViewerCGNSSetSolutionIndex()`, `PetscViewerCGNSGetSolutionIndex()`, `PetscViewerCGNSGetSolutionTime()` 560472b9844SJames Wright @*/ 561472b9844SJames Wright PetscErrorCode PetscViewerCGNSGetSolutionName(PetscViewer viewer, const char *name[]) 562472b9844SJames Wright { 563472b9844SJames Wright PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 564472b9844SJames Wright int sol_id; 565472b9844SJames Wright char buffer[CGIO_MAX_NAME_LENGTH + 1]; 566472b9844SJames Wright CGNS_ENUMT(GridLocation_t) gridloc; // Throwaway 567472b9844SJames Wright 568472b9844SJames Wright PetscFunctionBeginUser; 56914640894SJames Wright PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 57014640894SJames Wright PetscAssertPointer(name, 2); 571472b9844SJames Wright PetscCall(PetscViewerCGNSGetSolutionFileIndex_Internal(viewer, &sol_id)); 572472b9844SJames Wright 5734c155f28SJames Wright PetscCallCGNSRead(cg_sol_info(cgv->file_num, cgv->base, cgv->zone, sol_id, buffer, &gridloc), viewer, 0); 574472b9844SJames Wright if (cgv->solution_name) PetscCall(PetscFree(cgv->solution_name)); 575472b9844SJames Wright PetscCall(PetscStrallocpy(buffer, &cgv->solution_name)); 576472b9844SJames Wright *name = cgv->solution_name; 577472b9844SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 578472b9844SJames Wright } 579