1 #include <../src/sys/classes/viewer/impls/vtk/vtkvimpl.h> /*I "petscviewer.h" I*/ 2 3 /*MC 4 PetscViewerVTKWriteFunction - functional form used to provide a writer to the `PETSCVIEWERVTK` 5 6 Synopsis: 7 #include <petscviewer.h> 8 PetscViewerVTKWriteFunction(PetscObject object,PetscViewer viewer) 9 10 Input Parameters: 11 + object - the PETSc object to be written 12 - viewer - viewer it is to be written to 13 14 Level: developer 15 16 .seealso: `PetscViewerVTKAddField()` 17 M*/ 18 19 /*@C 20 PetscViewerVTKAddField - Add a field to the viewer 21 22 Collective 23 24 Input Parameters: 25 + viewer - `PETSCVIEWERVTK` 26 . dm - `DM` on which `Vec` lives 27 . PetscViewerVTKWriteFunction - function to write this `Vec` 28 . fieldnum - which field of the DM to write (`PETSC_DEFAULT` if the whle vector should be written) 29 . fieldtype - Either `PETSC_VTK_POINT_FIELD` or `PETSC_VTK_CELL_FIELD` 30 . checkdm - whether to check for identical dm arguments as fields are added 31 - vec - `Vec` from which to write 32 33 Note: 34 This routine keeps exclusive ownership of the `Vec`. The caller should not use or destroy the `Vec` after calling it. 35 36 Level: developer 37 38 .seealso: `PETSCVIEWERVTK`, `PetscViewerVTKOpen()`, `DMDAVTKWriteAll()`, `PetscViewerVTKWriteFunction`, `PetscViewerVTKGetDM()` 39 @*/ 40 PetscErrorCode PetscViewerVTKAddField(PetscViewer viewer, PetscObject dm, PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject, PetscViewer), PetscInt fieldnum, PetscViewerVTKFieldType fieldtype, PetscBool checkdm, PetscObject vec) { 41 PetscFunctionBegin; 42 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 43 PetscValidHeader(dm, 2); 44 PetscValidHeader(vec, 7); 45 PetscUseMethod(viewer, "PetscViewerVTKAddField_C", (PetscViewer, PetscObject, PetscErrorCode(*)(PetscObject, PetscViewer), PetscInt, PetscViewerVTKFieldType, PetscBool, PetscObject), (viewer, dm, PetscViewerVTKWriteFunction, fieldnum, fieldtype, checkdm, vec)); 46 PetscFunctionReturn(0); 47 } 48 49 /*@C 50 PetscViewerVTKGetDM - get the `DM` associated with the `PETSCVIEWERVTK` viewer 51 52 Collective 53 54 Input Parameters: 55 + viewer - `PETSCVIEWERVTK` viewer 56 - dm - `DM` associated with the viewer (as PetscObject) 57 58 Level: developer 59 60 .seealso: `PETSCVIEWERVTK`, `PetscViewerVTKOpen()`, `DMDAVTKWriteAll()`, `PetscViewerVTKWriteFunction`, `PetscViewerVTKAddField()` 61 @*/ 62 PetscErrorCode PetscViewerVTKGetDM(PetscViewer viewer, PetscObject *dm) { 63 PetscFunctionBegin; 64 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 65 PetscUseMethod(viewer, "PetscViewerVTKGetDM_C", (PetscViewer, PetscObject *), (viewer, dm)); 66 PetscFunctionReturn(0); 67 } 68 69 static PetscErrorCode PetscViewerDestroy_VTK(PetscViewer viewer) { 70 PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data; 71 72 PetscFunctionBegin; 73 PetscCall(PetscFree(vtk->filename)); 74 PetscCall(PetscFree(vtk)); 75 PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL)); 76 PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL)); 77 PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL)); 78 PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL)); 79 PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerVTKAddField_C", NULL)); 80 PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerVTKGetDM_C", NULL)); 81 PetscFunctionReturn(0); 82 } 83 84 static PetscErrorCode PetscViewerFlush_VTK(PetscViewer viewer) { 85 PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data; 86 PetscViewerVTKObjectLink link, next; 87 88 PetscFunctionBegin; 89 PetscCheck(!vtk->link || !(!vtk->dm || !vtk->write), PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "No fields or no grid"); 90 if (vtk->write) PetscCall((*vtk->write)(vtk->dm, viewer)); 91 for (link = vtk->link; link; link = next) { 92 next = link->next; 93 PetscCall(PetscObjectDestroy(&link->vec)); 94 PetscCall(PetscFree(link)); 95 } 96 PetscCall(PetscObjectDestroy(&vtk->dm)); 97 vtk->write = NULL; 98 vtk->link = NULL; 99 PetscFunctionReturn(0); 100 } 101 102 PetscErrorCode PetscViewerFileSetName_VTK(PetscViewer viewer, const char name[]) { 103 PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data; 104 PetscBool isvtk, isvts, isvtu, isvtr; 105 size_t len; 106 107 PetscFunctionBegin; 108 PetscCall(PetscViewerFlush(viewer)); 109 PetscCall(PetscFree(vtk->filename)); 110 PetscCall(PetscStrlen(name, &len)); 111 if (!len) { 112 isvtk = PETSC_TRUE; 113 } else { 114 PetscCall(PetscStrcasecmp(name + len - 4, ".vtk", &isvtk)); 115 PetscCall(PetscStrcasecmp(name + len - 4, ".vts", &isvts)); 116 PetscCall(PetscStrcasecmp(name + len - 4, ".vtu", &isvtu)); 117 PetscCall(PetscStrcasecmp(name + len - 4, ".vtr", &isvtr)); 118 } 119 if (isvtk) { 120 if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_ASCII_VTK_DEPRECATED; 121 PetscCheck(viewer->format == PETSC_VIEWER_ASCII_VTK_DEPRECATED, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_INCOMP, "Cannot use file '%s' with format %s, should have '.vtk' extension", name, PetscViewerFormats[viewer->format]); 122 } else if (isvts) { 123 if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTS; 124 PetscCheck(viewer->format == PETSC_VIEWER_VTK_VTS, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_INCOMP, "Cannot use file '%s' with format %s, should have '.vts' extension", name, PetscViewerFormats[viewer->format]); 125 } else if (isvtu) { 126 if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTU; 127 PetscCheck(viewer->format == PETSC_VIEWER_VTK_VTU, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_INCOMP, "Cannot use file '%s' with format %s, should have '.vtu' extension", name, PetscViewerFormats[viewer->format]); 128 } else if (isvtr) { 129 if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTR; 130 PetscCheck(viewer->format == PETSC_VIEWER_VTK_VTR, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_INCOMP, "Cannot use file '%s' with format %s, should have '.vtr' extension", name, PetscViewerFormats[viewer->format]); 131 } else SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_UNKNOWN_TYPE, "File '%s' has unrecognized extension", name); 132 PetscCall(PetscStrallocpy(len ? name : "stdout", &vtk->filename)); 133 PetscFunctionReturn(0); 134 } 135 136 PetscErrorCode PetscViewerFileGetName_VTK(PetscViewer viewer, const char **name) { 137 PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data; 138 PetscFunctionBegin; 139 *name = vtk->filename; 140 PetscFunctionReturn(0); 141 } 142 143 PetscErrorCode PetscViewerFileSetMode_VTK(PetscViewer viewer, PetscFileMode type) { 144 PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data; 145 146 PetscFunctionBegin; 147 vtk->btype = type; 148 PetscFunctionReturn(0); 149 } 150 151 PetscErrorCode PetscViewerFileGetMode_VTK(PetscViewer viewer, PetscFileMode *type) { 152 PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data; 153 154 PetscFunctionBegin; 155 *type = vtk->btype; 156 PetscFunctionReturn(0); 157 } 158 159 PetscErrorCode PetscViewerVTKAddField_VTK(PetscViewer viewer, PetscObject dm, PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject, PetscViewer), PetscInt fieldnum, PetscViewerVTKFieldType fieldtype, PetscBool checkdm, PetscObject vec) { 160 PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data; 161 PetscViewerVTKObjectLink link, tail = vtk->link; 162 163 PetscFunctionBegin; 164 if (vtk->dm) { 165 PetscCheck(!checkdm || dm == vtk->dm, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_INCOMP, "Refusing to write a field from more than one grid to the same VTK file. Set checkdm = PETSC_FALSE to skip this check."); 166 } else { 167 PetscCall(PetscObjectReference(dm)); 168 vtk->dm = dm; 169 } 170 vtk->write = PetscViewerVTKWriteFunction; 171 PetscCall(PetscNew(&link)); 172 link->ft = fieldtype; 173 link->vec = vec; 174 link->field = fieldnum; 175 link->next = NULL; 176 /* Append to list */ 177 if (tail) { 178 while (tail->next) tail = tail->next; 179 tail->next = link; 180 } else vtk->link = link; 181 PetscFunctionReturn(0); 182 } 183 184 PetscErrorCode PetscViewerVTKGetDM_VTK(PetscViewer viewer, PetscObject *dm) { 185 PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data; 186 187 PetscFunctionBegin; 188 *dm = vtk->dm; 189 PetscFunctionReturn(0); 190 } 191 192 /*MC 193 PETSCVIEWERVTK - A viewer that writes to a VTK file 194 195 Level: beginner 196 197 .seealso: `PetscViewerVTKOpen()`, `PetscViewerHDF5Open()`, `PetscViewerStringSPrintf()`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSCVIEWERSOCKET`, 198 `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`, `PETSCVIEWERSTRING`, 199 `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`, 200 `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()` 201 M*/ 202 203 PETSC_EXTERN PetscErrorCode PetscViewerCreate_VTK(PetscViewer v) { 204 PetscViewer_VTK *vtk; 205 206 PetscFunctionBegin; 207 PetscCall(PetscNew(&vtk)); 208 209 v->data = (void *)vtk; 210 v->ops->destroy = PetscViewerDestroy_VTK; 211 v->ops->flush = PetscViewerFlush_VTK; 212 vtk->btype = FILE_MODE_UNDEFINED; 213 vtk->filename = NULL; 214 215 PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_VTK)); 216 PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_VTK)); 217 PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_VTK)); 218 PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_VTK)); 219 PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerVTKAddField_C", PetscViewerVTKAddField_VTK)); 220 PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerVTKGetDM_C", PetscViewerVTKGetDM_VTK)); 221 PetscFunctionReturn(0); 222 } 223 224 /*@C 225 PetscViewerVTKOpen - Opens a `PETSCVIEWERVTK` viewer file. 226 227 Collective 228 229 Input Parameters: 230 + comm - MPI communicator 231 . name - name of file 232 - type - type of file 233 $ `FILE_MODE_WRITE` - create new file for binary output 234 $ `FILE_MODE_READ` - open existing file for binary input (not currently supported) 235 $ `FILE_MODE_APPEND` - open existing file for binary output (not currently supported) 236 237 Output Parameter: 238 . vtk - `PetscViewer` for VTK input/output to use with the specified file 239 240 Level: beginner 241 242 .seealso: `PETSCVIEWERVTK`, `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, 243 `VecView()`, `MatView()`, `VecLoad()`, `MatLoad()`, 244 `PetscFileMode`, `PetscViewer` 245 @*/ 246 PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *vtk) { 247 PetscFunctionBegin; 248 PetscCall(PetscViewerCreate(comm, vtk)); 249 PetscCall(PetscViewerSetType(*vtk, PETSCVIEWERVTK)); 250 PetscCall(PetscViewerFileSetMode(*vtk, type)); 251 PetscCall(PetscViewerFileSetName(*vtk, name)); 252 PetscFunctionReturn(0); 253 } 254 255 /*@C 256 PetscViewerVTKFWrite - write binary data preceded by 32-bit int length (in bytes), does not do byte swapping. 257 258 Logically collective on viewer 259 260 Input Parameters: 261 + viewer - logically collective viewer, data written from rank 0 262 . fp - file pointer valid on rank 0 263 . data - data pointer valid on rank 0 264 . n - number of data items 265 - dtype - data type 266 267 Level: developer 268 269 Note: 270 If `PetscScalar` is `__float128` then the binary files are written in double precision 271 272 .seealso: `PETSCVIEWERVTK`, `DMDAVTKWriteAll()`, `DMPlexVTKWriteAll()`, `PetscViewerPushFormat()`, `PetscViewerVTKOpen()`, `PetscBinaryWrite()` 273 @*/ 274 PetscErrorCode PetscViewerVTKFWrite(PetscViewer viewer, FILE *fp, const void *data, PetscInt n, MPI_Datatype dtype) { 275 PetscMPIInt rank; 276 MPI_Datatype vdtype = dtype; 277 #if defined(PETSC_USE_REAL___FLOAT128) 278 double *tmp; 279 PetscInt i; 280 PetscReal *ttmp = (PetscReal *)data; 281 #endif 282 283 PetscFunctionBegin; 284 PetscCheck(n >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_OUTOFRANGE, "Trying to write a negative amount of data %" PetscInt_FMT, n); 285 if (!n) PetscFunctionReturn(0); 286 PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank)); 287 if (rank == 0) { 288 size_t count; 289 PetscMPIInt dsize; 290 PetscVTKInt bytes; 291 292 #if defined(PETSC_USE_REAL___FLOAT128) 293 if (dtype == MPIU___FLOAT128) { 294 PetscCall(PetscMalloc1(n, &tmp)); 295 for (i = 0; i < n; i++) tmp[i] = ttmp[i]; 296 data = (void *)tmp; 297 vdtype = MPI_DOUBLE; 298 } 299 #endif 300 PetscCallMPI(MPI_Type_size(vdtype, &dsize)); 301 bytes = PetscVTKIntCast(dsize * n); 302 303 count = fwrite(&bytes, sizeof(int), 1, fp); 304 PetscCheck(count == 1, PETSC_COMM_SELF, PETSC_ERR_FILE_WRITE, "Error writing byte count"); 305 count = fwrite(data, dsize, (size_t)n, fp); 306 PetscCheck((PetscInt)count == n, PETSC_COMM_SELF, PETSC_ERR_FILE_WRITE, "Wrote %" PetscInt_FMT "/%" PetscInt_FMT " array members of size %d", (PetscInt)count, n, dsize); 307 #if defined(PETSC_USE_REAL___FLOAT128) 308 if (dtype == MPIU___FLOAT128) PetscCall(PetscFree(tmp)); 309 #endif 310 } 311 PetscFunctionReturn(0); 312 } 313