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