xref: /petsc/src/sys/classes/viewer/impls/vtk/vtkv.c (revision 01d7c5c3c619710e186693f6229a50b3702e7256)
1aaa7dc30SBarry Smith #include <../src/sys/classes/viewer/impls/vtk/vtkvimpl.h> /*I "petscviewer.h" I*/
25c6c1daeSBarry Smith 
318e2ec27SBarry Smith /*MC
418e2ec27SBarry Smith     PetscViewerVTKWriteFunction - functional form used to provide writer to the PetscViewerVTK
518e2ec27SBarry Smith 
618e2ec27SBarry Smith      Synopsis:
7aaa7dc30SBarry Smith      #include <petscviewer.h>
818e2ec27SBarry Smith      PetscViewerVTKWriteFunction(PetscObject object,PetscViewer viewer)
918e2ec27SBarry Smith 
1018e2ec27SBarry Smith      Input Parameters:
1118e2ec27SBarry Smith +      object - the PETSc object to be written
1218e2ec27SBarry Smith -      viewer - viewer it is to be written to
1318e2ec27SBarry Smith 
14878cb397SSatish Balay    Level: developer
15878cb397SSatish Balay 
1618e2ec27SBarry Smith .seealso:   PetscViewerVTKAddField()
1718e2ec27SBarry Smith M*/
1818e2ec27SBarry Smith 
195c6c1daeSBarry Smith #undef __FUNCT__
205c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVTKAddField"
215c6c1daeSBarry Smith /*@C
225c6c1daeSBarry Smith    PetscViewerVTKAddField - Add a field to the viewer
235c6c1daeSBarry Smith 
245c6c1daeSBarry Smith    Collective
255c6c1daeSBarry Smith 
265c6c1daeSBarry Smith    Input Arguments:
275c6c1daeSBarry Smith + viewer - VTK viewer
285c6c1daeSBarry Smith . dm - DM on which Vec lives
2918e2ec27SBarry Smith . PetscViewerVTKWriteFunction - function to write this Vec
305c6c1daeSBarry Smith . fieldtype - Either PETSC_VTK_POINT_FIELD or PETSC_VTK_CELL_FIELD
315c6c1daeSBarry Smith - vec - Vec to write
325c6c1daeSBarry Smith 
335c6c1daeSBarry Smith    Level: developer
345c6c1daeSBarry Smith 
355c6c1daeSBarry Smith    Note:
365c6c1daeSBarry Smith    This routine keeps exclusive ownership of the Vec. The caller should not use or destroy the Vec after adding it.
375c6c1daeSBarry Smith 
3818e2ec27SBarry Smith .seealso: PetscViewerVTKOpen(), DMDAVTKWriteAll(), PetscViewerVTKWriteFunction
395c6c1daeSBarry Smith @*/
4018e2ec27SBarry Smith PetscErrorCode PetscViewerVTKAddField(PetscViewer viewer,PetscObject dm,PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject,PetscViewer),PetscViewerVTKFieldType fieldtype,PetscObject vec)
415c6c1daeSBarry Smith {
425c6c1daeSBarry Smith   PetscErrorCode ierr;
435c6c1daeSBarry Smith 
445c6c1daeSBarry Smith   PetscFunctionBegin;
455c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
465c6c1daeSBarry Smith   PetscValidHeader(dm,2);
475c6c1daeSBarry Smith   PetscValidHeader(vec,4);
4818e2ec27SBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerVTKAddField_C",(PetscViewer,PetscObject,PetscErrorCode (*)(PetscObject,PetscViewer),PetscViewerVTKFieldType,PetscObject),(viewer,dm,PetscViewerVTKWriteFunction,fieldtype,vec));CHKERRQ(ierr);
495c6c1daeSBarry Smith   PetscFunctionReturn(0);
505c6c1daeSBarry Smith }
515c6c1daeSBarry Smith 
525c6c1daeSBarry Smith #undef __FUNCT__
535c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_VTK"
545c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_VTK(PetscViewer viewer)
555c6c1daeSBarry Smith {
565c6c1daeSBarry Smith   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
575c6c1daeSBarry Smith   PetscErrorCode  ierr;
585c6c1daeSBarry Smith 
595c6c1daeSBarry Smith   PetscFunctionBegin;
605c6c1daeSBarry Smith   ierr = PetscFree(vtk->filename);CHKERRQ(ierr);
615c6c1daeSBarry Smith   ierr = PetscFree(vtk);CHKERRQ(ierr);
62bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL);CHKERRQ(ierr);
63bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL);CHKERRQ(ierr);
64bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerVTKAddField_C",NULL);CHKERRQ(ierr);
655c6c1daeSBarry Smith   PetscFunctionReturn(0);
665c6c1daeSBarry Smith }
675c6c1daeSBarry Smith 
685c6c1daeSBarry Smith #undef __FUNCT__
695c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFlush_VTK"
705c6c1daeSBarry Smith static PetscErrorCode PetscViewerFlush_VTK(PetscViewer viewer)
715c6c1daeSBarry Smith {
725c6c1daeSBarry Smith   PetscViewer_VTK          *vtk = (PetscViewer_VTK*)viewer->data;
735c6c1daeSBarry Smith   PetscErrorCode           ierr;
745c6c1daeSBarry Smith   PetscViewerVTKObjectLink link,next;
755c6c1daeSBarry Smith 
765c6c1daeSBarry Smith   PetscFunctionBegin;
77ce94432eSBarry Smith   if (vtk->link && (!vtk->dm || !vtk->write)) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"No fields or no grid");
7818e2ec27SBarry Smith   if (vtk->write) {ierr = (*vtk->write)(vtk->dm,viewer);CHKERRQ(ierr);}
795c6c1daeSBarry Smith   for (link=vtk->link; link; link=next) {
805c6c1daeSBarry Smith     next = link->next;
815c6c1daeSBarry Smith     ierr = PetscObjectDestroy(&link->vec);CHKERRQ(ierr);
825c6c1daeSBarry Smith     ierr = PetscFree(link);CHKERRQ(ierr);
835c6c1daeSBarry Smith   }
845c6c1daeSBarry Smith   ierr       = PetscObjectDestroy(&vtk->dm);CHKERRQ(ierr);
850298fd71SBarry Smith   vtk->write = NULL;
865c6c1daeSBarry Smith   PetscFunctionReturn(0);
875c6c1daeSBarry Smith }
885c6c1daeSBarry Smith 
895c6c1daeSBarry Smith #undef __FUNCT__
905c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName_VTK"
915c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName_VTK(PetscViewer viewer,const char name[])
925c6c1daeSBarry Smith {
935c6c1daeSBarry Smith   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
945c6c1daeSBarry Smith   PetscErrorCode  ierr;
95a13bc4e3SShao-Ching Huang   PetscBool       isvtk,isvts,isvtu,isvtr;
965c6c1daeSBarry Smith   size_t          len;
975c6c1daeSBarry Smith 
985c6c1daeSBarry Smith   PetscFunctionBegin;
995c6c1daeSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1005c6c1daeSBarry Smith   ierr = PetscFree(vtk->filename);CHKERRQ(ierr);
1015c6c1daeSBarry Smith   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
1025c6c1daeSBarry Smith   ierr = PetscStrcasecmp(name+len-4,".vtk",&isvtk);CHKERRQ(ierr);
1035c6c1daeSBarry Smith   ierr = PetscStrcasecmp(name+len-4,".vts",&isvts);CHKERRQ(ierr);
1045c6c1daeSBarry Smith   ierr = PetscStrcasecmp(name+len-4,".vtu",&isvtu);CHKERRQ(ierr);
105a13bc4e3SShao-Ching Huang   ierr = PetscStrcasecmp(name+len-4,".vtr",&isvtr);CHKERRQ(ierr);
1065c6c1daeSBarry Smith   if (isvtk) {
1076a9046bcSBarry Smith     if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);}
108ce94432eSBarry Smith     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]);
1095c6c1daeSBarry Smith   } else if (isvts) {
1106a9046bcSBarry Smith     if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_VTK_VTS);CHKERRQ(ierr);}
111ce94432eSBarry Smith     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]);
1125c6c1daeSBarry Smith   } else if (isvtu) {
1136a9046bcSBarry Smith     if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_VTK_VTU);CHKERRQ(ierr);}
1143da621ecSMatthew G. Knepley     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]);
115a13bc4e3SShao-Ching Huang   } else if (isvtr) {
1166a9046bcSBarry Smith     if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_VTK_VTR);CHKERRQ(ierr);}
117a13bc4e3SShao-Ching Huang     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]);
118ce94432eSBarry Smith   } else SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_UNKNOWN_TYPE,"File '%s' has unrecognized extension",name);
1195c6c1daeSBarry Smith   ierr = PetscStrallocpy(name,&vtk->filename);CHKERRQ(ierr);
1205c6c1daeSBarry Smith   PetscFunctionReturn(0);
1215c6c1daeSBarry Smith }
1225c6c1daeSBarry Smith 
1235c6c1daeSBarry Smith #undef __FUNCT__
1245c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode_VTK"
1255c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetMode_VTK(PetscViewer viewer,PetscFileMode type)
1265c6c1daeSBarry Smith {
1275c6c1daeSBarry Smith   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
1285c6c1daeSBarry Smith 
1295c6c1daeSBarry Smith   PetscFunctionBegin;
1305c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1315c6c1daeSBarry Smith   vtk->btype = type;
1325c6c1daeSBarry Smith   PetscFunctionReturn(0);
1335c6c1daeSBarry Smith }
1345c6c1daeSBarry Smith 
1355c6c1daeSBarry Smith #undef __FUNCT__
1365c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVTKAddField_VTK"
13718e2ec27SBarry Smith PetscErrorCode  PetscViewerVTKAddField_VTK(PetscViewer viewer,PetscObject dm,PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject,PetscViewer),PetscViewerVTKFieldType fieldtype,PetscObject vec)
1385c6c1daeSBarry Smith {
1395c6c1daeSBarry Smith   PetscViewer_VTK          *vtk = (PetscViewer_VTK*)viewer->data;
1405c6c1daeSBarry Smith   PetscViewerVTKObjectLink link, tail = vtk->link;
1415c6c1daeSBarry Smith   PetscErrorCode           ierr;
1425c6c1daeSBarry Smith 
1435c6c1daeSBarry Smith   PetscFunctionBegin;
1445c6c1daeSBarry Smith   if (vtk->dm) {
145ce94432eSBarry Smith     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*01d7c5c3SPatrick Sanan   } else {
147*01d7c5c3SPatrick Sanan     ierr = PetscObjectReference(dm);CHKERRQ(ierr);
1485c6c1daeSBarry Smith     vtk->dm = dm;
149*01d7c5c3SPatrick Sanan   }
15018e2ec27SBarry Smith   vtk->write = PetscViewerVTKWriteFunction;
1515c6c1daeSBarry Smith   ierr       = PetscMalloc(sizeof(struct _n_PetscViewerVTKObjectLink),&link);CHKERRQ(ierr);
1525c6c1daeSBarry Smith   link->ft   = fieldtype;
1535c6c1daeSBarry Smith   link->vec  = vec;
1540298fd71SBarry Smith   link->next = NULL;
1555c6c1daeSBarry Smith   /* Append to list */
1565c6c1daeSBarry Smith   if (tail) {
1575c6c1daeSBarry Smith     while (tail->next) tail = tail->next;
1585c6c1daeSBarry Smith     tail->next = link;
159a297a907SKarl Rupp   } else vtk->link = link;
1605c6c1daeSBarry Smith   PetscFunctionReturn(0);
1615c6c1daeSBarry Smith }
1625c6c1daeSBarry Smith 
1635c6c1daeSBarry Smith #undef __FUNCT__
1645c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_VTK"
1658cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_VTK(PetscViewer v)
1665c6c1daeSBarry Smith {
1675c6c1daeSBarry Smith   PetscViewer_VTK *vtk;
1685c6c1daeSBarry Smith   PetscErrorCode  ierr;
1695c6c1daeSBarry Smith 
1705c6c1daeSBarry Smith   PetscFunctionBegin;
171b00a9115SJed Brown   ierr = PetscNewLog(v,&vtk);CHKERRQ(ierr);
1725c6c1daeSBarry Smith 
1735c6c1daeSBarry Smith   v->data         = (void*)vtk;
1745c6c1daeSBarry Smith   v->ops->destroy = PetscViewerDestroy_VTK;
1755c6c1daeSBarry Smith   v->ops->flush   = PetscViewerFlush_VTK;
1765c6c1daeSBarry Smith   vtk->btype      = (PetscFileMode) -1;
1775c6c1daeSBarry Smith   vtk->filename   = 0;
1785c6c1daeSBarry Smith 
179bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_VTK);CHKERRQ(ierr);
180bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_VTK);CHKERRQ(ierr);
181bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerVTKAddField_C",PetscViewerVTKAddField_VTK);CHKERRQ(ierr);
1825c6c1daeSBarry Smith   PetscFunctionReturn(0);
1835c6c1daeSBarry Smith }
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith #undef __FUNCT__
1865c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVTKOpen"
1875c6c1daeSBarry Smith /*@C
1885c6c1daeSBarry Smith    PetscViewerVTKOpen - Opens a file for VTK output.
1895c6c1daeSBarry Smith 
1905c6c1daeSBarry Smith    Collective on MPI_Comm
1915c6c1daeSBarry Smith 
1925c6c1daeSBarry Smith    Input Parameters:
1935c6c1daeSBarry Smith +  comm - MPI communicator
1945c6c1daeSBarry Smith .  name - name of file
1955c6c1daeSBarry Smith -  type - type of file
1965c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
1975c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input (not currently supported)
1985c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output (not currently supported)
1995c6c1daeSBarry Smith 
2005c6c1daeSBarry Smith    Output Parameter:
2015c6c1daeSBarry Smith .  vtk - PetscViewer for VTK input/output to use with the specified file
2025c6c1daeSBarry Smith 
2035c6c1daeSBarry Smith    Level: beginner
2045c6c1daeSBarry Smith 
2055c6c1daeSBarry Smith    Note:
2065c6c1daeSBarry Smith    This PetscViewer should be destroyed with PetscViewerDestroy().
2075c6c1daeSBarry Smith 
2085c6c1daeSBarry Smith    Concepts: VTK files
2095c6c1daeSBarry Smith    Concepts: PetscViewer^creating
2105c6c1daeSBarry Smith 
2116a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
2125c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(),
2135c6c1daeSBarry Smith           PetscFileMode, PetscViewer
2145c6c1daeSBarry Smith @*/
2155c6c1daeSBarry Smith PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *vtk)
2165c6c1daeSBarry Smith {
2175c6c1daeSBarry Smith   PetscErrorCode ierr;
2185c6c1daeSBarry Smith 
2195c6c1daeSBarry Smith   PetscFunctionBegin;
2205c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,vtk);CHKERRQ(ierr);
2215c6c1daeSBarry Smith   ierr = PetscViewerSetType(*vtk,PETSCVIEWERVTK);CHKERRQ(ierr);
2225c6c1daeSBarry Smith   ierr = PetscViewerFileSetMode(*vtk,type);CHKERRQ(ierr);
2235c6c1daeSBarry Smith   ierr = PetscViewerFileSetName(*vtk,name);CHKERRQ(ierr);
2245c6c1daeSBarry Smith   PetscFunctionReturn(0);
2255c6c1daeSBarry Smith }
2265c6c1daeSBarry Smith 
2275c6c1daeSBarry Smith #undef __FUNCT__
2285c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVTKFWrite"
2295c6c1daeSBarry Smith /*@C
2305c6c1daeSBarry Smith    PetscViewerVTKFWrite - write binary data preceded by 32-bit int length (in bytes), does not do byte swapping.
2315c6c1daeSBarry Smith 
2325c6c1daeSBarry Smith    Logically collective on PetscViewer
2335c6c1daeSBarry Smith 
2345c6c1daeSBarry Smith    Input Parameters:
2355c6c1daeSBarry Smith +  viewer - logically collective viewer, data written from rank 0
2365c6c1daeSBarry Smith .  fp - file pointer valid on rank 0
2375c6c1daeSBarry Smith .  data - data pointer valid on rank 0
2385c6c1daeSBarry Smith .  n - number of data items
2395c6c1daeSBarry Smith -  dtype - data type
2405c6c1daeSBarry Smith 
2415c6c1daeSBarry Smith    Level: developer
2425c6c1daeSBarry Smith 
243df77caf3SBarry Smith    Notes: If PetscScalar is __float128 then the binary files are written in double precision
244df77caf3SBarry Smith 
2455c6c1daeSBarry Smith    Concepts: VTK files
2465c6c1daeSBarry Smith    Concepts: PetscViewer^creating
2475c6c1daeSBarry Smith 
2486a9046bcSBarry Smith .seealso: DMDAVTKWriteAll(), DMComplexVTKWriteAll(), PetscViewerPushFormat(), PetscViewerVTKOpen(), PetscBinaryWrite()
2495c6c1daeSBarry Smith @*/
2505c6c1daeSBarry Smith PetscErrorCode PetscViewerVTKFWrite(PetscViewer viewer,FILE *fp,const void *data,PetscInt n,PetscDataType dtype)
2515c6c1daeSBarry Smith {
2525c6c1daeSBarry Smith   PetscErrorCode ierr;
2535c6c1daeSBarry Smith   PetscMPIInt    rank;
254df77caf3SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
255df77caf3SBarry Smith   PetscInt       i;
256df77caf3SBarry Smith   double         *tmp = NULL;
257df77caf3SBarry Smith   PetscReal      *ttmp = (PetscReal*)data;
258df77caf3SBarry Smith #endif
2595c6c1daeSBarry Smith 
2605c6c1daeSBarry Smith   PetscFunctionBegin;
261ce94432eSBarry Smith   if (n < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n);
2625c6c1daeSBarry Smith   if (!n) PetscFunctionReturn(0);
263ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
2645c6c1daeSBarry Smith   if (!rank) {
2655c6c1daeSBarry Smith     size_t      count;
2665c6c1daeSBarry Smith     PetscInt    size;
2675c6c1daeSBarry Smith     PetscVTKInt bytes;
2685c6c1daeSBarry Smith     switch (dtype) {
2695c6c1daeSBarry Smith     case PETSC_DOUBLE:
2705c6c1daeSBarry Smith       size = sizeof(double);
2715c6c1daeSBarry Smith       break;
2725c6c1daeSBarry Smith     case PETSC_FLOAT:
2735c6c1daeSBarry Smith       size = sizeof(float);
2745c6c1daeSBarry Smith       break;
275df77caf3SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
276df77caf3SBarry Smith     case PETSC___FLOAT128:
277df77caf3SBarry Smith       size = sizeof(double);
278df77caf3SBarry Smith       ierr = PetscMalloc1(n,&tmp);CHKERRQ(ierr);
279df77caf3SBarry Smith       for (i=0; i<n; i++) tmp[i] = ttmp[i];
280df77caf3SBarry Smith       data = (void*) tmp;
281df77caf3SBarry Smith       break;
282df77caf3SBarry Smith #endif
2835c6c1daeSBarry Smith     case PETSC_INT:
2845c6c1daeSBarry Smith       size = sizeof(PetscInt);
2855c6c1daeSBarry Smith       break;
2865c6c1daeSBarry Smith     case PETSC_ENUM:
2875c6c1daeSBarry Smith       size = sizeof(PetscEnum);
2885c6c1daeSBarry Smith       break;
2895c6c1daeSBarry Smith     case PETSC_CHAR:
2905c6c1daeSBarry Smith       size = sizeof(char);
2915c6c1daeSBarry Smith       break;
292ce94432eSBarry Smith     default: SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Data type not supported");
2935c6c1daeSBarry Smith     }
2945c6c1daeSBarry Smith     bytes = PetscVTKIntCast(size*n);
2955c6c1daeSBarry Smith 
2965c6c1daeSBarry Smith     count = fwrite(&bytes,sizeof(int),1,fp);
297f23aa3ddSBarry Smith     if (count != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Error writing byte count");
2985c6c1daeSBarry Smith     count = fwrite(data,size,(size_t)n,fp);
299f23aa3ddSBarry Smith     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);
300df77caf3SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
301df77caf3SBarry Smith     ierr = PetscFree(tmp);CHKERRQ(ierr);
302df77caf3SBarry Smith #endif
3035c6c1daeSBarry Smith   }
3045c6c1daeSBarry Smith   PetscFunctionReturn(0);
3055c6c1daeSBarry Smith }
306