Actual source code: vtkv.c

  1: #include <../src/sys/classes/viewer/impls/vtk/vtkvimpl.h>

  3: /*@
  4:   PetscViewerVTKAddField - Add a field to the viewer

  6:   Collective

  8:   Input Parameters:
  9: + viewer    - `PETSCVIEWERVTK`
 10: . dm        - `DM` on which `Vec` lives
 11: . write     - function to write this `Vec`, see `PetscViewerVTKWriteFn`
 12: . fieldnum  - which field of the `DM` to write (`PETSC_DEFAULT` if the whole vector should be written)
 13: . fieldtype - Either `PETSC_VTK_POINT_FIELD` or `PETSC_VTK_CELL_FIELD`
 14: . checkdm   - whether to check for identical dm arguments as fields are added
 15: - vec       - `Vec` from which to write

 17:   Level: developer

 19:   Note:
 20:   This routine keeps exclusive ownership of the `Vec`. The caller should not use or destroy the `Vec` after calling it.

 22: .seealso: [](sec_viewers), `PETSCVIEWERVTK`, `PetscViewerVTKOpen()`, `DMDAVTKWriteAll()`, `PetscViewerVTKWriteFn`, `PetscViewerVTKGetDM()`
 23: @*/
 24: PetscErrorCode PetscViewerVTKAddField(PetscViewer viewer, PetscObject dm, PetscViewerVTKWriteFn *write, PetscInt fieldnum, PetscViewerVTKFieldType fieldtype, PetscBool checkdm, PetscObject vec)
 25: {
 26:   PetscFunctionBegin;
 30:   PetscUseMethod(viewer, "PetscViewerVTKAddField_C", (PetscViewer, PetscObject, PetscViewerVTKWriteFn *, PetscInt, PetscViewerVTKFieldType, PetscBool, PetscObject), (viewer, dm, write, fieldnum, fieldtype, checkdm, vec));
 31:   PetscFunctionReturn(PETSC_SUCCESS);
 32: }

 34: /*@
 35:   PetscViewerVTKGetDM - get the `DM` associated with the `PETSCVIEWERVTK` viewer

 37:   Collective

 39:   Input Parameters:
 40: + viewer - `PETSCVIEWERVTK` viewer
 41: - dm     - `DM` associated with the viewer (as a `PetscObject`)

 43:   Level: developer

 45: .seealso: [](sec_viewers), `PETSCVIEWERVTK`, `PetscViewerVTKOpen()`, `DMDAVTKWriteAll()`, `PetscViewerVTKWriteFn`, `PetscViewerVTKAddField()`
 46: @*/
 47: PetscErrorCode PetscViewerVTKGetDM(PetscViewer viewer, PetscObject *dm)
 48: {
 49:   PetscFunctionBegin;
 51:   PetscUseMethod(viewer, "PetscViewerVTKGetDM_C", (PetscViewer, PetscObject *), (viewer, dm));
 52:   PetscFunctionReturn(PETSC_SUCCESS);
 53: }

 55: static PetscErrorCode PetscViewerDestroy_VTK(PetscViewer viewer)
 56: {
 57:   PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data;

 59:   PetscFunctionBegin;
 60:   PetscCall(PetscFree(vtk->filename));
 61:   PetscCall(PetscFree(vtk));
 62:   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL));
 63:   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL));
 64:   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL));
 65:   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL));
 66:   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerVTKAddField_C", NULL));
 67:   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerVTKGetDM_C", NULL));
 68:   PetscFunctionReturn(PETSC_SUCCESS);
 69: }

 71: static PetscErrorCode PetscViewerFlush_VTK(PetscViewer viewer)
 72: {
 73:   PetscViewer_VTK         *vtk = (PetscViewer_VTK *)viewer->data;
 74:   PetscViewerVTKObjectLink link, next;

 76:   PetscFunctionBegin;
 77:   PetscCheck(!vtk->link || !(!vtk->dm || !vtk->write), PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "No fields or no grid");
 78:   if (vtk->write) PetscCall((*vtk->write)(vtk->dm, viewer));
 79:   for (link = vtk->link; link; link = next) {
 80:     next = link->next;
 81:     PetscCall(PetscObjectDestroy(&link->vec));
 82:     PetscCall(PetscFree(link));
 83:   }
 84:   PetscCall(PetscObjectDestroy(&vtk->dm));
 85:   vtk->write = NULL;
 86:   vtk->link  = NULL;
 87:   PetscFunctionReturn(PETSC_SUCCESS);
 88: }

 90: static PetscErrorCode PetscViewerFileSetName_VTK(PetscViewer viewer, const char name[])
 91: {
 92:   PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data;
 93:   PetscBool        isvtk, isvts, isvtu, isvtr;
 94:   size_t           len;

 96:   PetscFunctionBegin;
 97:   PetscCall(PetscViewerFlush(viewer));
 98:   PetscCall(PetscFree(vtk->filename));
 99:   PetscCall(PetscStrlen(name, &len));
100:   if (!len) {
101:     isvtk = PETSC_TRUE;
102:   } else {
103:     PetscCall(PetscStrcasecmp(name + len - 4, ".vtk", &isvtk));
104:     PetscCall(PetscStrcasecmp(name + len - 4, ".vts", &isvts));
105:     PetscCall(PetscStrcasecmp(name + len - 4, ".vtu", &isvtu));
106:     PetscCall(PetscStrcasecmp(name + len - 4, ".vtr", &isvtr));
107:   }
108:   if (isvtk) {
109:     PetscCheck(viewer->format == PETSC_VIEWER_DEFAULT, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_INCOMP, "Cannot use file '%s' with format %s, should have '.vtk' extension", name, PetscViewerFormats[viewer->format]);
110:   } else if (isvts) {
111:     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTS;
112:     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]);
113:   } else if (isvtu) {
114:     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTU;
115:     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]);
116:   } else if (isvtr) {
117:     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTR;
118:     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]);
119:   } else SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_UNKNOWN_TYPE, "File '%s' has unrecognized extension", name);
120:   PetscCall(PetscStrallocpy(len ? name : "stdout", &vtk->filename));
121:   PetscFunctionReturn(PETSC_SUCCESS);
122: }

124: static PetscErrorCode PetscViewerFileGetName_VTK(PetscViewer viewer, const char **name)
125: {
126:   PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data;

128:   PetscFunctionBegin;
129:   *name = vtk->filename;
130:   PetscFunctionReturn(PETSC_SUCCESS);
131: }

133: static PetscErrorCode PetscViewerFileSetMode_VTK(PetscViewer viewer, PetscFileMode type)
134: {
135:   PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data;

137:   PetscFunctionBegin;
138:   vtk->btype = type;
139:   PetscFunctionReturn(PETSC_SUCCESS);
140: }

142: static PetscErrorCode PetscViewerFileGetMode_VTK(PetscViewer viewer, PetscFileMode *type)
143: {
144:   PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data;

146:   PetscFunctionBegin;
147:   *type = vtk->btype;
148:   PetscFunctionReturn(PETSC_SUCCESS);
149: }

151: static PetscErrorCode PetscViewerVTKAddField_VTK(PetscViewer viewer, PetscObject dm, PetscViewerVTKWriteFn *write, PetscInt fieldnum, PetscViewerVTKFieldType fieldtype, PetscBool checkdm, PetscObject vec)
152: {
153:   PetscViewer_VTK         *vtk = (PetscViewer_VTK *)viewer->data;
154:   PetscViewerVTKObjectLink link, tail = vtk->link;

156:   PetscFunctionBegin;
157:   if (vtk->dm) {
158:     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.");
159:   } else {
160:     PetscCall(PetscObjectReference(dm));
161:     vtk->dm = dm;
162:   }
163:   vtk->write = write;
164:   PetscCall(PetscNew(&link));
165:   link->ft    = fieldtype;
166:   link->vec   = vec;
167:   link->field = fieldnum;
168:   link->next  = NULL;
169:   /* Append to list */
170:   if (tail) {
171:     while (tail->next) tail = tail->next;
172:     tail->next = link;
173:   } else vtk->link = link;
174:   PetscFunctionReturn(PETSC_SUCCESS);
175: }

177: static PetscErrorCode PetscViewerVTKGetDM_VTK(PetscViewer viewer, PetscObject *dm)
178: {
179:   PetscViewer_VTK *vtk = (PetscViewer_VTK *)viewer->data;

181:   PetscFunctionBegin;
182:   *dm = vtk->dm;
183:   PetscFunctionReturn(PETSC_SUCCESS);
184: }

186: /*MC
187:    PETSCVIEWERVTK - A viewer that writes to a VTK file

189:   Level: beginner

191: .seealso: [](sec_viewers), `PetscViewerVTKOpen()`, `PetscViewerHDF5Open()`, `PetscViewerStringSPrintf()`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSCVIEWERSOCKET`,
192:           `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`, `PETSCVIEWERSTRING`,
193:           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
194:           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
195: M*/

197: PETSC_EXTERN PetscErrorCode PetscViewerCreate_VTK(PetscViewer v)
198: {
199:   PetscViewer_VTK *vtk;

201:   PetscFunctionBegin;
202:   PetscCall(PetscNew(&vtk));

204:   v->data         = (void *)vtk;
205:   v->ops->destroy = PetscViewerDestroy_VTK;
206:   v->ops->flush   = PetscViewerFlush_VTK;
207:   vtk->btype      = FILE_MODE_UNDEFINED;
208:   vtk->filename   = NULL;

210:   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_VTK));
211:   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_VTK));
212:   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_VTK));
213:   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_VTK));
214:   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerVTKAddField_C", PetscViewerVTKAddField_VTK));
215:   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerVTKGetDM_C", PetscViewerVTKGetDM_VTK));
216:   PetscFunctionReturn(PETSC_SUCCESS);
217: }

219: /*@
220:   PetscViewerVTKOpen - Opens a `PETSCVIEWERVTK` viewer file.

222:   Collective

224:   Input Parameters:
225: + comm - MPI communicator
226: . name - name of file
227: - type - type of file
228: .vb
229:   FILE_MODE_WRITE - create new file for binary output
230:   FILE_MODE_READ - open existing file for binary input (not currently supported)
231:   FILE_MODE_APPEND - open existing file for binary output (not currently supported)
232: .ve

234:   Output Parameter:
235: . vtk - `PetscViewer` for VTK input/output to use with the specified file

237:   Level: beginner

239: .seealso: [](sec_viewers), `PETSCVIEWERVTK`, `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
240:           `VecView()`, `MatView()`, `VecLoad()`, `MatLoad()`,
241:           `PetscFileMode`, `PetscViewer`
242: @*/
243: PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *vtk)
244: {
245:   PetscFunctionBegin;
246:   PetscCall(PetscViewerCreate(comm, vtk));
247:   PetscCall(PetscViewerSetType(*vtk, PETSCVIEWERVTK));
248:   PetscCall(PetscViewerFileSetMode(*vtk, type));
249:   PetscCall(PetscViewerFileSetName(*vtk, name));
250:   PetscFunctionReturn(PETSC_SUCCESS);
251: }

253: /*@
254:   PetscViewerVTKFWrite - write binary data preceded by 32-bit int length (in bytes), does not do byte swapping.

256:   Logically Collective

258:   Input Parameters:
259: + viewer - logically collective viewer, data written from rank 0
260: . fp     - file pointer valid on rank 0
261: . data   - data pointer valid on rank 0
262: . n      - number of data items
263: - dtype  - data type

265:   Level: developer

267:   Note:
268:   If `PetscScalar` is `__float128` then the binary files are written in double precision

270: .seealso: [](sec_viewers), `PETSCVIEWERVTK`, `DMDAVTKWriteAll()`, `DMPlexVTKWriteAll()`, `PetscViewerPushFormat()`, `PetscViewerVTKOpen()`, `PetscBinaryWrite()`
271: @*/
272: PetscErrorCode PetscViewerVTKFWrite(PetscViewer viewer, FILE *fp, const void *data, PetscCount n, MPI_Datatype dtype)
273: {
274:   PetscMPIInt  rank;
275:   MPI_Datatype vdtype = dtype;
276: #if PetscDefined(USE_REAL___FLOAT128)
277:   double *tmp;
278: #endif

280:   PetscFunctionBegin;
281:   PetscCheck(n >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_OUTOFRANGE, "Trying to write a negative amount of data %" PetscCount_FMT, n);
282:   if (!n) PetscFunctionReturn(PETSC_SUCCESS);
283:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
284:   if (rank == 0) {
285:     size_t      count;
286:     PetscMPIInt dsize;
287:     PetscInt64  bytes;

289: #if PetscDefined(USE_REAL___FLOAT128)
290:     if (dtype == MPIU___FLOAT128) {
291:       PetscReal *ttmp = (PetscReal *)data;

293:       PetscCall(PetscMalloc1(n, &tmp));
294:       for (PetscCount i = 0; i < n; i++) tmp[i] = ttmp[i];
295:       data   = (void *)tmp;
296:       vdtype = MPI_DOUBLE;
297:     }
298: #endif
299:     PetscCallMPI(MPI_Type_size(vdtype, &dsize));
300:     bytes = (PetscInt64)dsize * n;

302:     count = fwrite(&bytes, sizeof(bytes), 1, fp);
303:     PetscCheck(count == 1, PETSC_COMM_SELF, PETSC_ERR_FILE_WRITE, "Error writing byte count");
304:     count = fwrite(data, dsize, (size_t)n, fp);
305:     PetscCheck((PetscCount)count == n, PETSC_COMM_SELF, PETSC_ERR_FILE_WRITE, "Wrote %" PetscCount_FMT "/%" PetscCount_FMT " array members of size %d", (PetscCount)count, n, dsize);
306: #if PetscDefined(USE_REAL___FLOAT128)
307:     if (dtype == MPIU___FLOAT128) PetscCall(PetscFree(tmp));
308: #endif
309:   }
310:   PetscFunctionReturn(PETSC_SUCCESS);
311: }