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 245c6c1daeSBarry Smith Input Arguments: 255c6c1daeSBarry Smith + viewer - VTK viewer 265c6c1daeSBarry Smith . dm - DM on which Vec lives 2718e2ec27SBarry Smith . PetscViewerVTKWriteFunction - function to write this Vec 285c6c1daeSBarry Smith . fieldtype - Either PETSC_VTK_POINT_FIELD or PETSC_VTK_CELL_FIELD 295c6c1daeSBarry Smith - vec - Vec to write 305c6c1daeSBarry Smith 315c6c1daeSBarry Smith Level: developer 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 3618e2ec27SBarry Smith .seealso: PetscViewerVTKOpen(), DMDAVTKWriteAll(), PetscViewerVTKWriteFunction 375c6c1daeSBarry Smith @*/ 3818e2ec27SBarry Smith PetscErrorCode PetscViewerVTKAddField(PetscViewer viewer,PetscObject dm,PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject,PetscViewer),PetscViewerVTKFieldType fieldtype,PetscObject vec) 395c6c1daeSBarry Smith { 405c6c1daeSBarry Smith PetscErrorCode ierr; 415c6c1daeSBarry Smith 425c6c1daeSBarry Smith PetscFunctionBegin; 435c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 445c6c1daeSBarry Smith PetscValidHeader(dm,2); 455c6c1daeSBarry Smith PetscValidHeader(vec,4); 4618e2ec27SBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerVTKAddField_C",(PetscViewer,PetscObject,PetscErrorCode (*)(PetscObject,PetscViewer),PetscViewerVTKFieldType,PetscObject),(viewer,dm,PetscViewerVTKWriteFunction,fieldtype,vec));CHKERRQ(ierr); 475c6c1daeSBarry Smith PetscFunctionReturn(0); 485c6c1daeSBarry Smith } 495c6c1daeSBarry Smith 505c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_VTK(PetscViewer viewer) 515c6c1daeSBarry Smith { 525c6c1daeSBarry Smith PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 535c6c1daeSBarry Smith PetscErrorCode ierr; 545c6c1daeSBarry Smith 555c6c1daeSBarry Smith PetscFunctionBegin; 565c6c1daeSBarry Smith ierr = PetscFree(vtk->filename);CHKERRQ(ierr); 575c6c1daeSBarry Smith ierr = PetscFree(vtk);CHKERRQ(ierr); 58bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL);CHKERRQ(ierr); 59bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL);CHKERRQ(ierr); 60bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerVTKAddField_C",NULL);CHKERRQ(ierr); 615c6c1daeSBarry Smith PetscFunctionReturn(0); 625c6c1daeSBarry Smith } 635c6c1daeSBarry Smith 645c6c1daeSBarry Smith static PetscErrorCode PetscViewerFlush_VTK(PetscViewer viewer) 655c6c1daeSBarry Smith { 665c6c1daeSBarry Smith PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 675c6c1daeSBarry Smith PetscErrorCode ierr; 685c6c1daeSBarry Smith PetscViewerVTKObjectLink link,next; 695c6c1daeSBarry Smith 705c6c1daeSBarry Smith PetscFunctionBegin; 71ce94432eSBarry Smith if (vtk->link && (!vtk->dm || !vtk->write)) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"No fields or no grid"); 7218e2ec27SBarry Smith if (vtk->write) {ierr = (*vtk->write)(vtk->dm,viewer);CHKERRQ(ierr);} 735c6c1daeSBarry Smith for (link=vtk->link; link; link=next) { 745c6c1daeSBarry Smith next = link->next; 755c6c1daeSBarry Smith ierr = PetscObjectDestroy(&link->vec);CHKERRQ(ierr); 765c6c1daeSBarry Smith ierr = PetscFree(link);CHKERRQ(ierr); 775c6c1daeSBarry Smith } 785c6c1daeSBarry Smith ierr = PetscObjectDestroy(&vtk->dm);CHKERRQ(ierr); 790298fd71SBarry Smith vtk->write = NULL; 805c6c1daeSBarry Smith PetscFunctionReturn(0); 815c6c1daeSBarry Smith } 825c6c1daeSBarry Smith 835c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetName_VTK(PetscViewer viewer,const char name[]) 845c6c1daeSBarry Smith { 855c6c1daeSBarry Smith PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 865c6c1daeSBarry Smith PetscErrorCode ierr; 87a13bc4e3SShao-Ching Huang PetscBool isvtk,isvts,isvtu,isvtr; 885c6c1daeSBarry Smith size_t len; 895c6c1daeSBarry Smith 905c6c1daeSBarry Smith PetscFunctionBegin; 915c6c1daeSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 925c6c1daeSBarry Smith ierr = PetscFree(vtk->filename);CHKERRQ(ierr); 935c6c1daeSBarry Smith ierr = PetscStrlen(name,&len);CHKERRQ(ierr); 945c6c1daeSBarry Smith ierr = PetscStrcasecmp(name+len-4,".vtk",&isvtk);CHKERRQ(ierr); 955c6c1daeSBarry Smith ierr = PetscStrcasecmp(name+len-4,".vts",&isvts);CHKERRQ(ierr); 965c6c1daeSBarry Smith ierr = PetscStrcasecmp(name+len-4,".vtu",&isvtu);CHKERRQ(ierr); 97a13bc4e3SShao-Ching Huang ierr = PetscStrcasecmp(name+len-4,".vtr",&isvtr);CHKERRQ(ierr); 985c6c1daeSBarry Smith if (isvtk) { 996a9046bcSBarry Smith if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);} 100ce94432eSBarry 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]); 1015c6c1daeSBarry Smith } else if (isvts) { 1026a9046bcSBarry Smith if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_VTK_VTS);CHKERRQ(ierr);} 103ce94432eSBarry 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]); 1045c6c1daeSBarry Smith } else if (isvtu) { 1056a9046bcSBarry Smith if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_VTK_VTU);CHKERRQ(ierr);} 1063da621ecSMatthew 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]); 107a13bc4e3SShao-Ching Huang } else if (isvtr) { 1086a9046bcSBarry Smith if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_VTK_VTR);CHKERRQ(ierr);} 109a13bc4e3SShao-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]); 110ce94432eSBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_UNKNOWN_TYPE,"File '%s' has unrecognized extension",name); 1115c6c1daeSBarry Smith ierr = PetscStrallocpy(name,&vtk->filename);CHKERRQ(ierr); 1125c6c1daeSBarry Smith PetscFunctionReturn(0); 1135c6c1daeSBarry Smith } 1145c6c1daeSBarry Smith 1155c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetMode_VTK(PetscViewer viewer,PetscFileMode type) 1165c6c1daeSBarry Smith { 1175c6c1daeSBarry Smith PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 1185c6c1daeSBarry Smith 1195c6c1daeSBarry Smith PetscFunctionBegin; 1205c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 1215c6c1daeSBarry Smith vtk->btype = type; 1225c6c1daeSBarry Smith PetscFunctionReturn(0); 1235c6c1daeSBarry Smith } 1245c6c1daeSBarry Smith 12518e2ec27SBarry Smith PetscErrorCode PetscViewerVTKAddField_VTK(PetscViewer viewer,PetscObject dm,PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject,PetscViewer),PetscViewerVTKFieldType fieldtype,PetscObject vec) 1265c6c1daeSBarry Smith { 1275c6c1daeSBarry Smith PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 1285c6c1daeSBarry Smith PetscViewerVTKObjectLink link, tail = vtk->link; 1295c6c1daeSBarry Smith PetscErrorCode ierr; 1305c6c1daeSBarry Smith 1315c6c1daeSBarry Smith PetscFunctionBegin; 1325c6c1daeSBarry Smith if (vtk->dm) { 133ce94432eSBarry 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"); 13401d7c5c3SPatrick Sanan } else { 13501d7c5c3SPatrick Sanan ierr = PetscObjectReference(dm);CHKERRQ(ierr); 1365c6c1daeSBarry Smith vtk->dm = dm; 13701d7c5c3SPatrick Sanan } 13818e2ec27SBarry Smith vtk->write = PetscViewerVTKWriteFunction; 13995dccacaSBarry Smith ierr = PetscNew(&link);CHKERRQ(ierr); 1405c6c1daeSBarry Smith link->ft = fieldtype; 1415c6c1daeSBarry Smith link->vec = vec; 1420298fd71SBarry Smith link->next = NULL; 1435c6c1daeSBarry Smith /* Append to list */ 1445c6c1daeSBarry Smith if (tail) { 1455c6c1daeSBarry Smith while (tail->next) tail = tail->next; 1465c6c1daeSBarry Smith tail->next = link; 147a297a907SKarl Rupp } else vtk->link = link; 1485c6c1daeSBarry Smith PetscFunctionReturn(0); 1495c6c1daeSBarry Smith } 1505c6c1daeSBarry Smith 1518556b5ebSBarry Smith /*MC 1528556b5ebSBarry Smith PETSCVIEWERVTK - A viewer that writes to an VTK file 1538556b5ebSBarry Smith 1548556b5ebSBarry Smith 1558556b5ebSBarry Smith .seealso: PetscViewerVTKOpen(), PetscViewerHDF5Open(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET, 1568556b5ebSBarry Smith PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSTRING, 1578556b5ebSBarry Smith PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB, 1588556b5ebSBarry Smith PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType() 1598556b5ebSBarry Smith 1601b266c99SBarry Smith Level: beginner 1618556b5ebSBarry Smith M*/ 1628556b5ebSBarry Smith 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; 169b00a9115SJed 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 /*@C 1845c6c1daeSBarry Smith PetscViewerVTKOpen - Opens a file for VTK output. 1855c6c1daeSBarry Smith 1865c6c1daeSBarry Smith Collective on MPI_Comm 1875c6c1daeSBarry Smith 1885c6c1daeSBarry Smith Input Parameters: 1895c6c1daeSBarry Smith + comm - MPI communicator 1905c6c1daeSBarry Smith . name - name of file 1915c6c1daeSBarry Smith - type - type of file 1925c6c1daeSBarry Smith $ FILE_MODE_WRITE - create new file for binary output 1935c6c1daeSBarry Smith $ FILE_MODE_READ - open existing file for binary input (not currently supported) 1945c6c1daeSBarry Smith $ FILE_MODE_APPEND - open existing file for binary output (not currently supported) 1955c6c1daeSBarry Smith 1965c6c1daeSBarry Smith Output Parameter: 1975c6c1daeSBarry Smith . vtk - PetscViewer for VTK input/output to use with the specified file 1985c6c1daeSBarry Smith 1995c6c1daeSBarry Smith Level: beginner 2005c6c1daeSBarry Smith 2015c6c1daeSBarry Smith Note: 2025c6c1daeSBarry Smith This PetscViewer should be destroyed with PetscViewerDestroy(). 2035c6c1daeSBarry Smith 2045c6c1daeSBarry Smith Concepts: VTK files 2055c6c1daeSBarry Smith Concepts: PetscViewer^creating 2065c6c1daeSBarry Smith 2076a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 2085c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), 2095c6c1daeSBarry Smith PetscFileMode, PetscViewer 2105c6c1daeSBarry Smith @*/ 2115c6c1daeSBarry Smith PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *vtk) 2125c6c1daeSBarry Smith { 2135c6c1daeSBarry Smith PetscErrorCode ierr; 2145c6c1daeSBarry Smith 2155c6c1daeSBarry Smith PetscFunctionBegin; 2165c6c1daeSBarry Smith ierr = PetscViewerCreate(comm,vtk);CHKERRQ(ierr); 2175c6c1daeSBarry Smith ierr = PetscViewerSetType(*vtk,PETSCVIEWERVTK);CHKERRQ(ierr); 2185c6c1daeSBarry Smith ierr = PetscViewerFileSetMode(*vtk,type);CHKERRQ(ierr); 2195c6c1daeSBarry Smith ierr = PetscViewerFileSetName(*vtk,name);CHKERRQ(ierr); 2205c6c1daeSBarry Smith PetscFunctionReturn(0); 2215c6c1daeSBarry Smith } 2225c6c1daeSBarry Smith 2235c6c1daeSBarry Smith /*@C 2245c6c1daeSBarry Smith PetscViewerVTKFWrite - write binary data preceded by 32-bit int length (in bytes), does not do byte swapping. 2255c6c1daeSBarry Smith 2265c6c1daeSBarry Smith Logically collective on PetscViewer 2275c6c1daeSBarry Smith 2285c6c1daeSBarry Smith Input Parameters: 2295c6c1daeSBarry Smith + viewer - logically collective viewer, data written from rank 0 2305c6c1daeSBarry Smith . fp - file pointer valid on rank 0 2315c6c1daeSBarry Smith . data - data pointer valid on rank 0 2325c6c1daeSBarry Smith . n - number of data items 2335c6c1daeSBarry Smith - dtype - data type 2345c6c1daeSBarry Smith 2355c6c1daeSBarry Smith Level: developer 2365c6c1daeSBarry Smith 237df77caf3SBarry Smith Notes: If PetscScalar is __float128 then the binary files are written in double precision 238df77caf3SBarry Smith 2395c6c1daeSBarry Smith Concepts: VTK files 2405c6c1daeSBarry Smith Concepts: PetscViewer^creating 2415c6c1daeSBarry Smith 2426a9046bcSBarry Smith .seealso: DMDAVTKWriteAll(), DMComplexVTKWriteAll(), PetscViewerPushFormat(), PetscViewerVTKOpen(), PetscBinaryWrite() 2435c6c1daeSBarry Smith @*/ 244*94fbd55eSBarry Smith PetscErrorCode PetscViewerVTKFWrite(PetscViewer viewer,FILE *fp,const void *data,PetscInt n,MPI_Datatype dtype) 2455c6c1daeSBarry Smith { 2465c6c1daeSBarry Smith PetscErrorCode ierr; 2475c6c1daeSBarry Smith PetscMPIInt rank; 248df77caf3SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) 249*94fbd55eSBarry Smith double *tmp; 250df77caf3SBarry Smith PetscInt i; 251df77caf3SBarry Smith PetscReal *ttmp = (PetscReal*)data; 252df77caf3SBarry Smith #endif 2535c6c1daeSBarry Smith 2545c6c1daeSBarry Smith PetscFunctionBegin; 255ce94432eSBarry Smith if (n < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n); 2565c6c1daeSBarry Smith if (!n) PetscFunctionReturn(0); 257ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 2585c6c1daeSBarry Smith if (!rank) { 2595c6c1daeSBarry Smith size_t count; 260*94fbd55eSBarry Smith PetscMPIInt dsize; 2615c6c1daeSBarry Smith PetscVTKInt bytes; 262*94fbd55eSBarry Smith 263df77caf3SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) 264*94fbd55eSBarry Smith if (dtype == MPIU___FLOAT128) { 265df77caf3SBarry Smith ierr = PetscMalloc1(n,&tmp);CHKERRQ(ierr); 266df77caf3SBarry Smith for (i=0; i<n; i++) tmp[i] = ttmp[i]; 267df77caf3SBarry Smith data = (void*) tmp; 268*94fbd55eSBarry Smith dtype = MPI_DOUBLE; 2695c6c1daeSBarry Smith } 270*94fbd55eSBarry Smith #endif 271*94fbd55eSBarry Smith ierr = MPI_Type_size(dtype,&dsize); 272*94fbd55eSBarry Smith bytes = PetscVTKIntCast(dsize*n); 2735c6c1daeSBarry Smith 2745c6c1daeSBarry Smith count = fwrite(&bytes,sizeof(int),1,fp); 275f23aa3ddSBarry Smith if (count != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Error writing byte count"); 276*94fbd55eSBarry Smith count = fwrite(data,dsize,(size_t)n,fp); 277*94fbd55eSBarry Smith if ((PetscInt)count != n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Wrote %D/%D array members of size %d",(PetscInt)count,n,dsize); 278df77caf3SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) 279*94fbd55eSBarry Smith if (dtype == MPIU___FLOAT128) { 280df77caf3SBarry Smith ierr = PetscFree(tmp);CHKERRQ(ierr); 281*94fbd55eSBarry Smith } 282df77caf3SBarry Smith #endif 2835c6c1daeSBarry Smith } 2845c6c1daeSBarry Smith PetscFunctionReturn(0); 2855c6c1daeSBarry Smith } 286