xref: /petsc/src/sys/classes/viewer/impls/vtk/vtkv.c (revision a16fd2c93c02146fccd68469496ac02ca99b9ebe)
1 #include <../src/sys/classes/viewer/impls/vtk/vtkvimpl.h> /*I "petscviewer.h" I*/
2 
3 /*MC
4     PetscViewerVTKWriteFunction - functional form used to provide 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 - VTK viewer
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 adding it.
35 
36    Level: developer
37 
38 .seealso: `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 viewer
51 
52    Collective
53 
54    Input Parameters:
55 + viewer - VTK viewer
56 - dm - DM associated with the viewer (as PetscObject)
57 
58    Level: developer
59 
60 .seealso: `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 .seealso: `PetscViewerVTKOpen()`, `PetscViewerHDF5Open()`, `PetscViewerStringSPrintf()`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSCVIEWERSOCKET`,
196           `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`, `PETSCVIEWERSTRING`,
197           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
198           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
199 
200   Level: beginner
201 M*/
202 
203 PETSC_EXTERN PetscErrorCode PetscViewerCreate_VTK(PetscViewer v) {
204   PetscViewer_VTK *vtk;
205 
206   PetscFunctionBegin;
207   PetscCall(PetscNewLog(v, &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 file for VTK output.
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    Note:
243    This PetscViewer should be destroyed with PetscViewerDestroy().
244 
245 .seealso: `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
246           `VecView()`, `MatView()`, `VecLoad()`, `MatLoad()`,
247           `PetscFileMode`, `PetscViewer`
248 @*/
249 PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *vtk) {
250   PetscFunctionBegin;
251   PetscCall(PetscViewerCreate(comm, vtk));
252   PetscCall(PetscViewerSetType(*vtk, PETSCVIEWERVTK));
253   PetscCall(PetscViewerFileSetMode(*vtk, type));
254   PetscCall(PetscViewerFileSetName(*vtk, name));
255   PetscFunctionReturn(0);
256 }
257 
258 /*@C
259    PetscViewerVTKFWrite - write binary data preceded by 32-bit int length (in bytes), does not do byte swapping.
260 
261    Logically collective on PetscViewer
262 
263    Input Parameters:
264 +  viewer - logically collective viewer, data written from rank 0
265 .  fp - file pointer valid on rank 0
266 .  data - data pointer valid on rank 0
267 .  n - number of data items
268 -  dtype - data type
269 
270    Level: developer
271 
272    Notes:
273     If PetscScalar is __float128 then the binary files are written in double precision
274 
275 .seealso: `DMDAVTKWriteAll()`, `DMPlexVTKWriteAll()`, `PetscViewerPushFormat()`, `PetscViewerVTKOpen()`, `PetscBinaryWrite()`
276 @*/
277 PetscErrorCode PetscViewerVTKFWrite(PetscViewer viewer, FILE *fp, const void *data, PetscInt n, MPI_Datatype dtype) {
278   PetscMPIInt  rank;
279   MPI_Datatype vdtype = dtype;
280 #if defined(PETSC_USE_REAL___FLOAT128)
281   double    *tmp;
282   PetscInt   i;
283   PetscReal *ttmp = (PetscReal *)data;
284 #endif
285 
286   PetscFunctionBegin;
287   PetscCheck(n >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_OUTOFRANGE, "Trying to write a negative amount of data %" PetscInt_FMT, n);
288   if (!n) PetscFunctionReturn(0);
289   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
290   if (rank == 0) {
291     size_t      count;
292     PetscMPIInt dsize;
293     PetscVTKInt bytes;
294 
295 #if defined(PETSC_USE_REAL___FLOAT128)
296     if (dtype == MPIU___FLOAT128) {
297       PetscCall(PetscMalloc1(n, &tmp));
298       for (i = 0; i < n; i++) tmp[i] = ttmp[i];
299       data   = (void *)tmp;
300       vdtype = MPI_DOUBLE;
301     }
302 #endif
303     PetscCallMPI(MPI_Type_size(vdtype, &dsize));
304     bytes = PetscVTKIntCast(dsize * n);
305 
306     count = fwrite(&bytes, sizeof(int), 1, fp);
307     PetscCheck(count == 1, PETSC_COMM_SELF, PETSC_ERR_FILE_WRITE, "Error writing byte count");
308     count = fwrite(data, dsize, (size_t)n, fp);
309     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);
310 #if defined(PETSC_USE_REAL___FLOAT128)
311     if (dtype == MPIU___FLOAT128) PetscCall(PetscFree(tmp));
312 #endif
313   }
314   PetscFunctionReturn(0);
315 }
316