xref: /petsc/src/sys/classes/viewer/impls/vtk/vtkv.c (revision cac4c232dc4f93991e342196e27ef7b0655dac7b)
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 /*@C
205c6c1daeSBarry Smith    PetscViewerVTKAddField - Add a field to the viewer
215c6c1daeSBarry Smith 
225c6c1daeSBarry Smith    Collective
235c6c1daeSBarry Smith 
244165533cSJose E. Roman    Input Parameters:
255c6c1daeSBarry Smith + viewer - VTK viewer
265c6c1daeSBarry Smith . dm - DM on which Vec lives
2718e2ec27SBarry Smith . PetscViewerVTKWriteFunction - function to write this Vec
28e630c359SToby Isaac . fieldnum - which field of the DM to write (PETSC_DEFAULT if the whle vector should be written)
295c6c1daeSBarry Smith . fieldtype - Either PETSC_VTK_POINT_FIELD or PETSC_VTK_CELL_FIELD
30a8f87f1dSPatrick Sanan . checkdm - whether to check for identical dm arguments as fields are added
31e630c359SToby Isaac - vec - Vec from which to write
325c6c1daeSBarry Smith 
335c6c1daeSBarry Smith    Note:
345c6c1daeSBarry Smith    This routine keeps exclusive ownership of the Vec. The caller should not use or destroy the Vec after adding it.
355c6c1daeSBarry Smith 
36a8f87f1dSPatrick Sanan    Level: developer
37a8f87f1dSPatrick Sanan 
38a8f87f1dSPatrick Sanan .seealso: PetscViewerVTKOpen(), DMDAVTKWriteAll(), PetscViewerVTKWriteFunction, PetscViewerVTKGetDM()
395c6c1daeSBarry Smith @*/
40e630c359SToby Isaac PetscErrorCode PetscViewerVTKAddField(PetscViewer viewer,PetscObject dm,PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject,PetscViewer),PetscInt fieldnum,PetscViewerVTKFieldType fieldtype,PetscBool checkdm,PetscObject vec)
415c6c1daeSBarry Smith {
425c6c1daeSBarry Smith   PetscFunctionBegin;
435c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
445c6c1daeSBarry Smith   PetscValidHeader(dm,2);
45064a246eSJacob Faibussowitsch   PetscValidHeader(vec,7);
46*cac4c232SBarry Smith   PetscUseMethod(viewer,"PetscViewerVTKAddField_C",(PetscViewer,PetscObject,PetscErrorCode (*)(PetscObject,PetscViewer),PetscInt,PetscViewerVTKFieldType,PetscBool,PetscObject),(viewer,dm,PetscViewerVTKWriteFunction,fieldnum,fieldtype,checkdm,vec));
47a8f87f1dSPatrick Sanan   PetscFunctionReturn(0);
48a8f87f1dSPatrick Sanan }
49a8f87f1dSPatrick Sanan 
50a8f87f1dSPatrick Sanan /*@C
51a8f87f1dSPatrick Sanan    PetscViewerVTKGetDM - get the DM associated with the viewer
52a8f87f1dSPatrick Sanan 
53a8f87f1dSPatrick Sanan    Collective
54a8f87f1dSPatrick Sanan 
554165533cSJose E. Roman    Input Parameters:
56a8f87f1dSPatrick Sanan + viewer - VTK viewer
57a8f87f1dSPatrick Sanan - dm - DM associated with the viewer (as PetscObject)
58a8f87f1dSPatrick Sanan 
59a8f87f1dSPatrick Sanan    Level: developer
60a8f87f1dSPatrick Sanan 
61a8f87f1dSPatrick Sanan .seealso: PetscViewerVTKOpen(), DMDAVTKWriteAll(), PetscViewerVTKWriteFunction, PetscViewerVTKAddField()
62a8f87f1dSPatrick Sanan @*/
63a8f87f1dSPatrick Sanan PetscErrorCode PetscViewerVTKGetDM(PetscViewer viewer,PetscObject *dm)
64a8f87f1dSPatrick Sanan {
65a8f87f1dSPatrick Sanan   PetscFunctionBegin;
66a8f87f1dSPatrick Sanan   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
67*cac4c232SBarry Smith   PetscUseMethod(viewer,"PetscViewerVTKGetDM_C",(PetscViewer,PetscObject*),(viewer,dm));
685c6c1daeSBarry Smith   PetscFunctionReturn(0);
695c6c1daeSBarry Smith }
705c6c1daeSBarry Smith 
715c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_VTK(PetscViewer viewer)
725c6c1daeSBarry Smith {
735c6c1daeSBarry Smith   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
745c6c1daeSBarry Smith 
755c6c1daeSBarry Smith   PetscFunctionBegin;
769566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtk->filename));
779566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtk));
789566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL));
799566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",NULL));
809566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL));
819566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetMode_C",NULL));
829566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerVTKAddField_C",NULL));
839566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerVTKGetDM_C",NULL));
845c6c1daeSBarry Smith   PetscFunctionReturn(0);
855c6c1daeSBarry Smith }
865c6c1daeSBarry Smith 
875c6c1daeSBarry Smith static PetscErrorCode PetscViewerFlush_VTK(PetscViewer viewer)
885c6c1daeSBarry Smith {
895c6c1daeSBarry Smith   PetscViewer_VTK          *vtk = (PetscViewer_VTK*)viewer->data;
905c6c1daeSBarry Smith   PetscViewerVTKObjectLink link,next;
915c6c1daeSBarry Smith 
925c6c1daeSBarry Smith   PetscFunctionBegin;
932c71b3e2SJacob Faibussowitsch   PetscCheckFalse(vtk->link && (!vtk->dm || !vtk->write),PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"No fields or no grid");
949566063dSJacob Faibussowitsch   if (vtk->write) PetscCall((*vtk->write)(vtk->dm,viewer));
955c6c1daeSBarry Smith   for (link=vtk->link; link; link=next) {
965c6c1daeSBarry Smith     next = link->next;
979566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&link->vec));
989566063dSJacob Faibussowitsch     PetscCall(PetscFree(link));
995c6c1daeSBarry Smith   }
1009566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&vtk->dm));
1010298fd71SBarry Smith   vtk->write = NULL;
1021873fc9fSToby Isaac   vtk->link  = NULL;
1035c6c1daeSBarry Smith   PetscFunctionReturn(0);
1045c6c1daeSBarry Smith }
1055c6c1daeSBarry Smith 
1065c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName_VTK(PetscViewer viewer,const char name[])
1075c6c1daeSBarry Smith {
1085c6c1daeSBarry Smith   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
109a13bc4e3SShao-Ching Huang   PetscBool       isvtk,isvts,isvtu,isvtr;
1105c6c1daeSBarry Smith   size_t          len;
1115c6c1daeSBarry Smith 
1125c6c1daeSBarry Smith   PetscFunctionBegin;
1139566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
1149566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtk->filename));
1159566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name,&len));
116fa17ad41SStefano Zampini   if (!len) {
117fa17ad41SStefano Zampini     isvtk = PETSC_TRUE;
118fa17ad41SStefano Zampini   } else {
1199566063dSJacob Faibussowitsch     PetscCall(PetscStrcasecmp(name+len-4,".vtk",&isvtk));
1209566063dSJacob Faibussowitsch     PetscCall(PetscStrcasecmp(name+len-4,".vts",&isvts));
1219566063dSJacob Faibussowitsch     PetscCall(PetscStrcasecmp(name+len-4,".vtu",&isvtu));
1229566063dSJacob Faibussowitsch     PetscCall(PetscStrcasecmp(name+len-4,".vtr",&isvtr));
123fa17ad41SStefano Zampini   }
1245c6c1daeSBarry Smith   if (isvtk) {
1258ec8862eSJed Brown     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_ASCII_VTK_DEPRECATED;
1262c71b3e2SJacob Faibussowitsch     PetscCheckFalse(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]);
1275c6c1daeSBarry Smith   } else if (isvts) {
12862015213SLisandro Dalcin     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTS;
1292c71b3e2SJacob Faibussowitsch     PetscCheckFalse(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]);
1305c6c1daeSBarry Smith   } else if (isvtu) {
13162015213SLisandro Dalcin     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTU;
1322c71b3e2SJacob Faibussowitsch     PetscCheckFalse(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]);
133a13bc4e3SShao-Ching Huang   } else if (isvtr) {
13462015213SLisandro Dalcin     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTR;
1352c71b3e2SJacob Faibussowitsch     PetscCheckFalse(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]);
13698921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_UNKNOWN_TYPE,"File '%s' has unrecognized extension",name);
1379566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(len ? name : "stdout",&vtk->filename));
1385c6c1daeSBarry Smith   PetscFunctionReturn(0);
1395c6c1daeSBarry Smith }
1405c6c1daeSBarry Smith 
1415ea299a2SLisandro Dalcin PetscErrorCode  PetscViewerFileGetName_VTK(PetscViewer viewer,const char **name)
1425ea299a2SLisandro Dalcin {
1435ea299a2SLisandro Dalcin   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
1445ea299a2SLisandro Dalcin   PetscFunctionBegin;
1455ea299a2SLisandro Dalcin   *name = vtk->filename;
1465ea299a2SLisandro Dalcin   PetscFunctionReturn(0);
1475ea299a2SLisandro Dalcin }
1485ea299a2SLisandro Dalcin 
1495c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetMode_VTK(PetscViewer viewer,PetscFileMode type)
1505c6c1daeSBarry Smith {
1515c6c1daeSBarry Smith   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
1525c6c1daeSBarry Smith 
1535c6c1daeSBarry Smith   PetscFunctionBegin;
1545c6c1daeSBarry Smith   vtk->btype = type;
1555c6c1daeSBarry Smith   PetscFunctionReturn(0);
1565c6c1daeSBarry Smith }
1575c6c1daeSBarry Smith 
1585ea299a2SLisandro Dalcin PetscErrorCode  PetscViewerFileGetMode_VTK(PetscViewer viewer,PetscFileMode *type)
1595ea299a2SLisandro Dalcin {
1605ea299a2SLisandro Dalcin   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
1615ea299a2SLisandro Dalcin 
1625ea299a2SLisandro Dalcin   PetscFunctionBegin;
1635ea299a2SLisandro Dalcin   *type = vtk->btype;
1645ea299a2SLisandro Dalcin   PetscFunctionReturn(0);
1655ea299a2SLisandro Dalcin }
1665ea299a2SLisandro Dalcin 
167e630c359SToby Isaac PetscErrorCode  PetscViewerVTKAddField_VTK(PetscViewer viewer,PetscObject dm,PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject,PetscViewer),PetscInt fieldnum,PetscViewerVTKFieldType fieldtype,PetscBool checkdm,PetscObject vec)
1685c6c1daeSBarry Smith {
1695c6c1daeSBarry Smith   PetscViewer_VTK          *vtk = (PetscViewer_VTK*)viewer->data;
1705c6c1daeSBarry Smith   PetscViewerVTKObjectLink link, tail = vtk->link;
1715c6c1daeSBarry Smith 
1725c6c1daeSBarry Smith   PetscFunctionBegin;
1735c6c1daeSBarry Smith   if (vtk->dm) {
1742c71b3e2SJacob Faibussowitsch     PetscCheckFalse(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.");
17501d7c5c3SPatrick Sanan   } else {
1769566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference(dm));
1775c6c1daeSBarry Smith     vtk->dm = dm;
17801d7c5c3SPatrick Sanan   }
17918e2ec27SBarry Smith   vtk->write  = PetscViewerVTKWriteFunction;
1809566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
1815c6c1daeSBarry Smith   link->ft    = fieldtype;
1825c6c1daeSBarry Smith   link->vec   = vec;
183e630c359SToby Isaac   link->field = fieldnum;
1840298fd71SBarry Smith   link->next  = NULL;
1855c6c1daeSBarry Smith   /* Append to list */
1865c6c1daeSBarry Smith   if (tail) {
1875c6c1daeSBarry Smith     while (tail->next) tail = tail->next;
1885c6c1daeSBarry Smith     tail->next = link;
189a297a907SKarl Rupp   } else vtk->link = link;
1905c6c1daeSBarry Smith   PetscFunctionReturn(0);
1915c6c1daeSBarry Smith }
1925c6c1daeSBarry Smith 
193a8f87f1dSPatrick Sanan PetscErrorCode PetscViewerVTKGetDM_VTK(PetscViewer viewer,PetscObject *dm)
194a8f87f1dSPatrick Sanan {
195a8f87f1dSPatrick Sanan   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
196a8f87f1dSPatrick Sanan 
197a8f87f1dSPatrick Sanan   PetscFunctionBegin;
198a8f87f1dSPatrick Sanan   *dm = vtk->dm;
199a8f87f1dSPatrick Sanan   PetscFunctionReturn(0);
200a8f87f1dSPatrick Sanan }
201a8f87f1dSPatrick Sanan 
2028556b5ebSBarry Smith /*MC
203a8f87f1dSPatrick Sanan    PETSCVIEWERVTK - A viewer that writes to a VTK file
2048556b5ebSBarry Smith 
2058556b5ebSBarry Smith .seealso:  PetscViewerVTKOpen(), PetscViewerHDF5Open(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
2068556b5ebSBarry Smith            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSTRING,
2078556b5ebSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
2088556b5ebSBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
2098556b5ebSBarry Smith 
2101b266c99SBarry Smith   Level: beginner
2118556b5ebSBarry Smith M*/
2128556b5ebSBarry Smith 
2138cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_VTK(PetscViewer v)
2145c6c1daeSBarry Smith {
2155c6c1daeSBarry Smith   PetscViewer_VTK *vtk;
2165c6c1daeSBarry Smith 
2175c6c1daeSBarry Smith   PetscFunctionBegin;
2189566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(v,&vtk));
2195c6c1daeSBarry Smith 
2205c6c1daeSBarry Smith   v->data         = (void*)vtk;
2215c6c1daeSBarry Smith   v->ops->destroy = PetscViewerDestroy_VTK;
2225c6c1daeSBarry Smith   v->ops->flush   = PetscViewerFlush_VTK;
2237e4fd573SVaclav Hapla   vtk->btype      = FILE_MODE_UNDEFINED;
22402c9f0b5SLisandro Dalcin   vtk->filename   = NULL;
2255c6c1daeSBarry Smith 
2269566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_VTK));
2279566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_VTK));
2289566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_VTK));
2299566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_VTK));
2309566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v,"PetscViewerVTKAddField_C",PetscViewerVTKAddField_VTK));
2319566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v,"PetscViewerVTKGetDM_C",PetscViewerVTKGetDM_VTK));
2325c6c1daeSBarry Smith   PetscFunctionReturn(0);
2335c6c1daeSBarry Smith }
2345c6c1daeSBarry Smith 
2355c6c1daeSBarry Smith /*@C
2365c6c1daeSBarry Smith    PetscViewerVTKOpen - Opens a file for VTK output.
2375c6c1daeSBarry Smith 
238d083f849SBarry Smith    Collective
2395c6c1daeSBarry Smith 
2405c6c1daeSBarry Smith    Input Parameters:
2415c6c1daeSBarry Smith +  comm - MPI communicator
2425c6c1daeSBarry Smith .  name - name of file
2435c6c1daeSBarry Smith -  type - type of file
2445c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
2455c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input (not currently supported)
2465c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output (not currently supported)
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith    Output Parameter:
2495c6c1daeSBarry Smith .  vtk - PetscViewer for VTK input/output to use with the specified file
2505c6c1daeSBarry Smith 
2515c6c1daeSBarry Smith    Level: beginner
2525c6c1daeSBarry Smith 
2535c6c1daeSBarry Smith    Note:
2545c6c1daeSBarry Smith    This PetscViewer should be destroyed with PetscViewerDestroy().
2555c6c1daeSBarry Smith 
2566a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
2575c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(),
2585c6c1daeSBarry Smith           PetscFileMode, PetscViewer
2595c6c1daeSBarry Smith @*/
2605c6c1daeSBarry Smith PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *vtk)
2615c6c1daeSBarry Smith {
2625c6c1daeSBarry Smith   PetscFunctionBegin;
2639566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm,vtk));
2649566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*vtk,PETSCVIEWERVTK));
2659566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(*vtk,type));
2669566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(*vtk,name));
2675c6c1daeSBarry Smith   PetscFunctionReturn(0);
2685c6c1daeSBarry Smith }
2695c6c1daeSBarry Smith 
2705c6c1daeSBarry Smith /*@C
2715c6c1daeSBarry Smith    PetscViewerVTKFWrite - write binary data preceded by 32-bit int length (in bytes), does not do byte swapping.
2725c6c1daeSBarry Smith 
2735c6c1daeSBarry Smith    Logically collective on PetscViewer
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith    Input Parameters:
2765c6c1daeSBarry Smith +  viewer - logically collective viewer, data written from rank 0
2775c6c1daeSBarry Smith .  fp - file pointer valid on rank 0
2785c6c1daeSBarry Smith .  data - data pointer valid on rank 0
2795c6c1daeSBarry Smith .  n - number of data items
2805c6c1daeSBarry Smith -  dtype - data type
2815c6c1daeSBarry Smith 
2825c6c1daeSBarry Smith    Level: developer
2835c6c1daeSBarry Smith 
28495452b02SPatrick Sanan    Notes:
28595452b02SPatrick Sanan     If PetscScalar is __float128 then the binary files are written in double precision
286df77caf3SBarry Smith 
28785f1c47aSJed Brown .seealso: DMDAVTKWriteAll(), DMPlexVTKWriteAll(), PetscViewerPushFormat(), PetscViewerVTKOpen(), PetscBinaryWrite()
2885c6c1daeSBarry Smith @*/
28994fbd55eSBarry Smith PetscErrorCode PetscViewerVTKFWrite(PetscViewer viewer,FILE *fp,const void *data,PetscInt n,MPI_Datatype dtype)
2905c6c1daeSBarry Smith {
2915c6c1daeSBarry Smith   PetscMPIInt    rank;
2921a996575SSatish Balay   MPI_Datatype   vdtype=dtype;
293df77caf3SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
29494fbd55eSBarry Smith   double         *tmp;
295df77caf3SBarry Smith   PetscInt       i;
296df77caf3SBarry Smith   PetscReal      *ttmp = (PetscReal*)data;
297df77caf3SBarry Smith #endif
2985c6c1daeSBarry Smith 
2995c6c1daeSBarry Smith   PetscFunctionBegin;
3002c71b3e2SJacob Faibussowitsch   PetscCheckFalse(n < 0,PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %" PetscInt_FMT,n);
3015c6c1daeSBarry Smith   if (!n) PetscFunctionReturn(0);
3029566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank));
303dd400576SPatrick Sanan   if (rank == 0) {
3045c6c1daeSBarry Smith     size_t      count;
30594fbd55eSBarry Smith     PetscMPIInt dsize;
3065c6c1daeSBarry Smith     PetscVTKInt bytes;
30794fbd55eSBarry Smith 
308df77caf3SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
30994fbd55eSBarry Smith     if (dtype == MPIU___FLOAT128) {
3109566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n,&tmp));
311df77caf3SBarry Smith       for (i=0; i<n; i++) tmp[i] = ttmp[i];
312df77caf3SBarry Smith       data  = (void*) tmp;
3131a996575SSatish Balay       vdtype = MPI_DOUBLE;
3145c6c1daeSBarry Smith     }
31594fbd55eSBarry Smith #endif
3169566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_size(vdtype,&dsize));
31794fbd55eSBarry Smith     bytes = PetscVTKIntCast(dsize*n);
3185c6c1daeSBarry Smith 
3195c6c1daeSBarry Smith     count = fwrite(&bytes,sizeof(int),1,fp);
3202c71b3e2SJacob Faibussowitsch     PetscCheckFalse(count != 1,PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Error writing byte count");
32194fbd55eSBarry Smith     count = fwrite(data,dsize,(size_t)n,fp);
3222c71b3e2SJacob Faibussowitsch     PetscCheckFalse((PetscInt)count != n,PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Wrote %" PetscInt_FMT "/%" PetscInt_FMT " array members of size %d",(PetscInt)count,n,dsize);
323df77caf3SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
32494fbd55eSBarry Smith     if (dtype == MPIU___FLOAT128) {
3259566063dSJacob Faibussowitsch       PetscCall(PetscFree(tmp));
32694fbd55eSBarry Smith     }
327df77caf3SBarry Smith #endif
3285c6c1daeSBarry Smith   }
3295c6c1daeSBarry Smith   PetscFunctionReturn(0);
3305c6c1daeSBarry Smith }
331