1 #include <petsc/private/viewercgnsimpl.h> /*I "petscviewer.h" I*/ 2 #if defined(PETSC_HDF5_HAVE_PARALLEL) 3 #include <pcgnslib.h> 4 #else 5 #include <cgnslib.h> 6 #endif 7 8 static PetscErrorCode PetscViewerSetFromOptions_CGNS(PetscViewer v, PetscOptionItems *PetscOptionsObject) 9 { 10 PetscFunctionBegin; 11 PetscOptionsHeadBegin(PetscOptionsObject, "CGNS Viewer Options"); 12 PetscOptionsHeadEnd(); 13 PetscFunctionReturn(0); 14 } 15 16 static PetscErrorCode PetscViewerView_CGNS(PetscViewer v, PetscViewer viewer) 17 { 18 PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)v->data; 19 20 PetscFunctionBegin; 21 if (cgv->filename) PetscCall(PetscViewerASCIIPrintf(viewer, "Filename: %s\n", cgv->filename)); 22 PetscFunctionReturn(0); 23 } 24 25 static PetscErrorCode PetscViewerFileClose_CGNS(PetscViewer viewer) 26 { 27 PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 28 29 PetscFunctionBegin; 30 if (cgv->output_times) { 31 size_t size, width = 32, *steps; 32 char *solnames; 33 PetscReal *times; 34 cgsize_t num_times; 35 PetscCall(PetscSegBufferGetSize(cgv->output_times, &size)); 36 PetscCall(PetscSegBufferExtractInPlace(cgv->output_times, ×)); 37 num_times = size; 38 PetscCallCGNS(cg_biter_write(cgv->file_num, cgv->base, "TimeIterValues", num_times)); 39 PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL)); 40 PetscCallCGNS(cg_array_write("TimeValues", CGNS_ENUMV(RealDouble), 1, &num_times, times)); 41 PetscCall(PetscSegBufferDestroy(&cgv->output_times)); 42 PetscCallCGNS(cg_ziter_write(cgv->file_num, cgv->base, cgv->zone, "ZoneIterativeData")); 43 PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL)); 44 PetscCall(PetscMalloc(size * width + 1, &solnames)); 45 PetscCall(PetscSegBufferExtractInPlace(cgv->output_steps, &steps)); 46 for (size_t i = 0; i < size; i++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "FlowSolution%-20zu", steps[i])); 47 PetscCall(PetscSegBufferDestroy(&cgv->output_steps)); 48 cgsize_t shape[2] = {(cgsize_t)width, (cgsize_t)size}; 49 PetscCallCGNS(cg_array_write("FlowSolutionPointers", CGNS_ENUMV(Character), 2, shape, solnames)); 50 // The VTK reader looks for names like FlowSolution*Pointers. 51 for (size_t i = 0; i < size; i++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "%-32s", "CellInfo")); 52 PetscCallCGNS(cg_array_write("FlowSolutionCellInfoPointers", CGNS_ENUMV(Character), 2, shape, solnames)); 53 PetscCall(PetscFree(solnames)); 54 55 PetscCallCGNS(cg_simulation_type_write(cgv->file_num, cgv->base, CGNS_ENUMV(TimeAccurate))); 56 } 57 PetscCall(PetscFree(cgv->filename)); 58 #if defined(PETSC_HDF5_HAVE_PARALLEL) 59 PetscCallCGNS(cgp_close(cgv->file_num)); 60 #else 61 if (cgv->file_num) PetscCallCGNS(cg_close(cgv->file_num)); 62 #endif 63 cgv->file_num = 0; 64 PetscFunctionReturn(0); 65 } 66 67 static PetscErrorCode PetscViewerDestroy_CGNS(PetscViewer viewer) 68 { 69 PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 70 71 PetscFunctionBegin; 72 PetscCall(PetscViewerFileClose_CGNS(viewer)); 73 PetscCall(PetscFree(cgv->node_l2g)); 74 PetscCall(PetscFree(cgv->nodal_field)); 75 PetscCall(PetscFree(cgv)); 76 PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL)); 77 PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL)); 78 PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL)); 79 PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL)); 80 PetscFunctionReturn(0); 81 } 82 83 static PetscErrorCode PetscViewerFileSetMode_CGNS(PetscViewer viewer, PetscFileMode type) 84 { 85 PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 86 87 PetscFunctionBegin; 88 cgv->btype = type; 89 PetscFunctionReturn(0); 90 } 91 92 static PetscErrorCode PetscViewerFileGetMode_CGNS(PetscViewer viewer, PetscFileMode *type) 93 { 94 PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 95 96 PetscFunctionBegin; 97 *type = cgv->btype; 98 PetscFunctionReturn(0); 99 } 100 101 static PetscErrorCode PetscViewerFileSetName_CGNS(PetscViewer viewer, const char *filename) 102 { 103 PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 104 105 PetscFunctionBegin; 106 if (cgv->file_num) PetscCallCGNS(cg_close(cgv->file_num)); 107 PetscCall(PetscFree(cgv->filename)); 108 PetscCall(PetscStrallocpy(filename, &cgv->filename)); 109 110 switch (cgv->btype) { 111 case FILE_MODE_READ: 112 SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "FILE_MODE_READ not yet implemented"); 113 break; 114 case FILE_MODE_WRITE: 115 #if defined(PETSC_HDF5_HAVE_PARALLEL) 116 PetscCallCGNS(cgp_mpi_comm(PetscObjectComm((PetscObject)viewer))); 117 PetscCallCGNS(cgp_open(filename, CG_MODE_WRITE, &cgv->file_num)); 118 #else 119 PetscCallCGNS(cg_open(filename, CG_MODE_WRITE, &cgv->file_num)); 120 #endif 121 break; 122 case FILE_MODE_UNDEFINED: 123 SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()"); 124 default: 125 SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[cgv->btype]); 126 } 127 PetscFunctionReturn(0); 128 } 129 130 static PetscErrorCode PetscViewerFileGetName_CGNS(PetscViewer viewer, const char **filename) 131 { 132 PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data; 133 134 PetscFunctionBegin; 135 *filename = cgv->filename; 136 PetscFunctionReturn(0); 137 } 138 139 /*MC 140 PETSCVIEWERCGNS - A viewer for CGNS files 141 142 Level: beginner 143 144 .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `VecView()`, `DMView()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()` 145 M*/ 146 147 PETSC_EXTERN PetscErrorCode PetscViewerCreate_CGNS(PetscViewer v) 148 { 149 PetscViewer_CGNS *cgv; 150 151 PetscFunctionBegin; 152 PetscCall(PetscNew(&cgv)); 153 154 v->data = cgv; 155 v->ops->destroy = PetscViewerDestroy_CGNS; 156 v->ops->setfromoptions = PetscViewerSetFromOptions_CGNS; 157 v->ops->view = PetscViewerView_CGNS; 158 cgv->btype = FILE_MODE_UNDEFINED; 159 cgv->filename = NULL; 160 161 PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_CGNS)); 162 PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_CGNS)); 163 PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_CGNS)); 164 PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_CGNS)); 165 PetscFunctionReturn(0); 166 } 167