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