15c6c1daeSBarry 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: 718e2ec27SBarry 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) { 1075c6c1daeSBarry Smith if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(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) { 1105c6c1daeSBarry Smith if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(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) { 1135c6c1daeSBarry Smith if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(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) { 116a13bc4e3SShao-Ching Huang if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(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"); 1465c6c1daeSBarry Smith } 1475c6c1daeSBarry Smith vtk->dm = dm; 14818e2ec27SBarry Smith vtk->write = PetscViewerVTKWriteFunction; 1495c6c1daeSBarry Smith ierr = PetscMalloc(sizeof(struct _n_PetscViewerVTKObjectLink),&link);CHKERRQ(ierr); 1505c6c1daeSBarry Smith link->ft = fieldtype; 1515c6c1daeSBarry Smith link->vec = vec; 1520298fd71SBarry Smith link->next = NULL; 1535c6c1daeSBarry Smith /* Append to list */ 1545c6c1daeSBarry Smith if (tail) { 1555c6c1daeSBarry Smith while (tail->next) tail = tail->next; 1565c6c1daeSBarry Smith tail->next = link; 157a297a907SKarl Rupp } else vtk->link = link; 1585c6c1daeSBarry Smith PetscFunctionReturn(0); 1595c6c1daeSBarry Smith } 1605c6c1daeSBarry Smith 1615c6c1daeSBarry Smith #undef __FUNCT__ 1625c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_VTK" 1638cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_VTK(PetscViewer v) 1645c6c1daeSBarry Smith { 1655c6c1daeSBarry Smith PetscViewer_VTK *vtk; 1665c6c1daeSBarry Smith PetscErrorCode ierr; 1675c6c1daeSBarry Smith 1685c6c1daeSBarry Smith PetscFunctionBegin; 169*b00a9115SJed Brown ierr = PetscNewLog(v,&vtk);CHKERRQ(ierr); 1705c6c1daeSBarry Smith 1715c6c1daeSBarry Smith v->data = (void*)vtk; 1725c6c1daeSBarry Smith v->ops->destroy = PetscViewerDestroy_VTK; 1735c6c1daeSBarry Smith v->ops->flush = PetscViewerFlush_VTK; 1745c6c1daeSBarry Smith vtk->btype = (PetscFileMode) -1; 1755c6c1daeSBarry Smith vtk->filename = 0; 1765c6c1daeSBarry Smith 177bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_VTK);CHKERRQ(ierr); 178bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_VTK);CHKERRQ(ierr); 179bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerVTKAddField_C",PetscViewerVTKAddField_VTK);CHKERRQ(ierr); 1805c6c1daeSBarry Smith PetscFunctionReturn(0); 1815c6c1daeSBarry Smith } 1825c6c1daeSBarry Smith 1835c6c1daeSBarry Smith #undef __FUNCT__ 1845c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVTKOpen" 1855c6c1daeSBarry Smith /*@C 1865c6c1daeSBarry Smith PetscViewerVTKOpen - Opens a file for VTK output. 1875c6c1daeSBarry Smith 1885c6c1daeSBarry Smith Collective on MPI_Comm 1895c6c1daeSBarry Smith 1905c6c1daeSBarry Smith Input Parameters: 1915c6c1daeSBarry Smith + comm - MPI communicator 1925c6c1daeSBarry Smith . name - name of file 1935c6c1daeSBarry Smith - type - type of file 1945c6c1daeSBarry Smith $ FILE_MODE_WRITE - create new file for binary output 1955c6c1daeSBarry Smith $ FILE_MODE_READ - open existing file for binary input (not currently supported) 1965c6c1daeSBarry Smith $ FILE_MODE_APPEND - open existing file for binary output (not currently supported) 1975c6c1daeSBarry Smith 1985c6c1daeSBarry Smith Output Parameter: 1995c6c1daeSBarry Smith . vtk - PetscViewer for VTK input/output to use with the specified file 2005c6c1daeSBarry Smith 2015c6c1daeSBarry Smith Level: beginner 2025c6c1daeSBarry Smith 2035c6c1daeSBarry Smith Note: 2045c6c1daeSBarry Smith This PetscViewer should be destroyed with PetscViewerDestroy(). 2055c6c1daeSBarry Smith 2065c6c1daeSBarry Smith Concepts: VTK files 2075c6c1daeSBarry Smith Concepts: PetscViewer^creating 2085c6c1daeSBarry Smith 2095c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(), 2105c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), 2115c6c1daeSBarry Smith PetscFileMode, PetscViewer 2125c6c1daeSBarry Smith @*/ 2135c6c1daeSBarry Smith PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *vtk) 2145c6c1daeSBarry Smith { 2155c6c1daeSBarry Smith PetscErrorCode ierr; 2165c6c1daeSBarry Smith 2175c6c1daeSBarry Smith PetscFunctionBegin; 2185c6c1daeSBarry Smith ierr = PetscViewerCreate(comm,vtk);CHKERRQ(ierr); 2195c6c1daeSBarry Smith ierr = PetscViewerSetType(*vtk,PETSCVIEWERVTK);CHKERRQ(ierr); 2205c6c1daeSBarry Smith ierr = PetscViewerFileSetMode(*vtk,type);CHKERRQ(ierr); 2215c6c1daeSBarry Smith ierr = PetscViewerFileSetName(*vtk,name);CHKERRQ(ierr); 2225c6c1daeSBarry Smith PetscFunctionReturn(0); 2235c6c1daeSBarry Smith } 2245c6c1daeSBarry Smith 2255c6c1daeSBarry Smith #undef __FUNCT__ 2265c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVTKFWrite" 2275c6c1daeSBarry Smith /*@C 2285c6c1daeSBarry Smith PetscViewerVTKFWrite - write binary data preceded by 32-bit int length (in bytes), does not do byte swapping. 2295c6c1daeSBarry Smith 2305c6c1daeSBarry Smith Logically collective on PetscViewer 2315c6c1daeSBarry Smith 2325c6c1daeSBarry Smith Input Parameters: 2335c6c1daeSBarry Smith + viewer - logically collective viewer, data written from rank 0 2345c6c1daeSBarry Smith . fp - file pointer valid on rank 0 2355c6c1daeSBarry Smith . data - data pointer valid on rank 0 2365c6c1daeSBarry Smith . n - number of data items 2375c6c1daeSBarry Smith - dtype - data type 2385c6c1daeSBarry Smith 2395c6c1daeSBarry Smith Level: developer 2405c6c1daeSBarry Smith 2415c6c1daeSBarry Smith Concepts: VTK files 2425c6c1daeSBarry Smith Concepts: PetscViewer^creating 2435c6c1daeSBarry Smith 2445c6c1daeSBarry Smith .seealso: DMDAVTKWriteAll(), DMComplexVTKWriteAll(), PetscViewerSetFormat(), PetscViewerVTKOpen(), PetscBinaryWrite() 2455c6c1daeSBarry Smith @*/ 2465c6c1daeSBarry Smith PetscErrorCode PetscViewerVTKFWrite(PetscViewer viewer,FILE *fp,const void *data,PetscInt n,PetscDataType dtype) 2475c6c1daeSBarry Smith { 2485c6c1daeSBarry Smith PetscErrorCode ierr; 2495c6c1daeSBarry Smith PetscMPIInt rank; 2505c6c1daeSBarry Smith 2515c6c1daeSBarry Smith PetscFunctionBegin; 252ce94432eSBarry Smith if (n < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n); 2535c6c1daeSBarry Smith if (!n) PetscFunctionReturn(0); 254ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 2555c6c1daeSBarry Smith if (!rank) { 2565c6c1daeSBarry Smith size_t count; 2575c6c1daeSBarry Smith PetscInt size; 2585c6c1daeSBarry Smith PetscVTKInt bytes; 2595c6c1daeSBarry Smith switch (dtype) { 2605c6c1daeSBarry Smith case PETSC_DOUBLE: 2615c6c1daeSBarry Smith size = sizeof(double); 2625c6c1daeSBarry Smith break; 2635c6c1daeSBarry Smith case PETSC_FLOAT: 2645c6c1daeSBarry Smith size = sizeof(float); 2655c6c1daeSBarry Smith break; 2665c6c1daeSBarry Smith case PETSC_INT: 2675c6c1daeSBarry Smith size = sizeof(PetscInt); 2685c6c1daeSBarry Smith break; 2695c6c1daeSBarry Smith case PETSC_ENUM: 2705c6c1daeSBarry Smith size = sizeof(PetscEnum); 2715c6c1daeSBarry Smith break; 2725c6c1daeSBarry Smith case PETSC_CHAR: 2735c6c1daeSBarry Smith size = sizeof(char); 2745c6c1daeSBarry Smith break; 275ce94432eSBarry Smith default: SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Data type not supported"); 2765c6c1daeSBarry Smith } 2775c6c1daeSBarry Smith bytes = PetscVTKIntCast(size*n); 2785c6c1daeSBarry Smith 2795c6c1daeSBarry Smith count = fwrite(&bytes,sizeof(int),1,fp); 280f23aa3ddSBarry Smith if (count != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Error writing byte count"); 2815c6c1daeSBarry Smith count = fwrite(data,size,(size_t)n,fp); 282f23aa3ddSBarry 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); 2835c6c1daeSBarry Smith } 2845c6c1daeSBarry Smith PetscFunctionReturn(0); 2855c6c1daeSBarry Smith } 286