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 Level: developer 15 16 .seealso: PetscViewerVTKAddField() 17 M*/ 18 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 static PetscErrorCode PetscViewerDestroy_VTK(PetscViewer viewer) 51 { 52 PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 53 PetscErrorCode ierr; 54 55 PetscFunctionBegin; 56 ierr = PetscFree(vtk->filename);CHKERRQ(ierr); 57 ierr = PetscFree(vtk);CHKERRQ(ierr); 58 ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL);CHKERRQ(ierr); 59 ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",NULL);CHKERRQ(ierr); 60 ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL);CHKERRQ(ierr); 61 ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetMode_C",NULL);CHKERRQ(ierr); 62 ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerVTKAddField_C",NULL);CHKERRQ(ierr); 63 PetscFunctionReturn(0); 64 } 65 66 static PetscErrorCode PetscViewerFlush_VTK(PetscViewer viewer) 67 { 68 PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 69 PetscErrorCode ierr; 70 PetscViewerVTKObjectLink link,next; 71 72 PetscFunctionBegin; 73 if (vtk->link && (!vtk->dm || !vtk->write)) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"No fields or no grid"); 74 if (vtk->write) {ierr = (*vtk->write)(vtk->dm,viewer);CHKERRQ(ierr);} 75 for (link=vtk->link; link; link=next) { 76 next = link->next; 77 ierr = PetscObjectDestroy(&link->vec);CHKERRQ(ierr); 78 ierr = PetscFree(link);CHKERRQ(ierr); 79 } 80 ierr = PetscObjectDestroy(&vtk->dm);CHKERRQ(ierr); 81 vtk->write = NULL; 82 PetscFunctionReturn(0); 83 } 84 85 PetscErrorCode PetscViewerFileSetName_VTK(PetscViewer viewer,const char name[]) 86 { 87 PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 88 PetscErrorCode ierr; 89 PetscBool isvtk,isvts,isvtu,isvtr; 90 size_t len; 91 92 PetscFunctionBegin; 93 ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 94 ierr = PetscFree(vtk->filename);CHKERRQ(ierr); 95 ierr = PetscStrlen(name,&len);CHKERRQ(ierr); 96 if (!len) { 97 isvtk = PETSC_TRUE; 98 } else { 99 ierr = PetscStrcasecmp(name+len-4,".vtk",&isvtk);CHKERRQ(ierr); 100 ierr = PetscStrcasecmp(name+len-4,".vts",&isvts);CHKERRQ(ierr); 101 ierr = PetscStrcasecmp(name+len-4,".vtu",&isvtu);CHKERRQ(ierr); 102 ierr = PetscStrcasecmp(name+len-4,".vtr",&isvtr);CHKERRQ(ierr); 103 } 104 if (isvtk) { 105 if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_ASCII_VTK; 106 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]); 107 } else if (isvts) { 108 if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTS; 109 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]); 110 } else if (isvtu) { 111 if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTU; 112 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]); 113 } else if (isvtr) { 114 if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTR; 115 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]); 116 } else SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_UNKNOWN_TYPE,"File '%s' has unrecognized extension",name); 117 ierr = PetscStrallocpy(len ? name : "stdout",&vtk->filename);CHKERRQ(ierr); 118 PetscFunctionReturn(0); 119 } 120 121 PetscErrorCode PetscViewerFileGetName_VTK(PetscViewer viewer,const char **name) 122 { 123 PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 124 PetscFunctionBegin; 125 *name = vtk->filename; 126 PetscFunctionReturn(0); 127 } 128 129 PetscErrorCode PetscViewerFileSetMode_VTK(PetscViewer viewer,PetscFileMode type) 130 { 131 PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 132 133 PetscFunctionBegin; 134 vtk->btype = type; 135 PetscFunctionReturn(0); 136 } 137 138 PetscErrorCode PetscViewerFileGetMode_VTK(PetscViewer viewer,PetscFileMode *type) 139 { 140 PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 141 142 PetscFunctionBegin; 143 *type = vtk->btype; 144 PetscFunctionReturn(0); 145 } 146 147 PetscErrorCode PetscViewerVTKAddField_VTK(PetscViewer viewer,PetscObject dm,PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject,PetscViewer),PetscViewerVTKFieldType fieldtype,PetscObject vec) 148 { 149 PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; 150 PetscViewerVTKObjectLink link, tail = vtk->link; 151 PetscErrorCode ierr; 152 153 PetscFunctionBegin; 154 if (vtk->dm) { 155 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"); 156 } else { 157 ierr = PetscObjectReference(dm);CHKERRQ(ierr); 158 vtk->dm = dm; 159 } 160 vtk->write = PetscViewerVTKWriteFunction; 161 ierr = PetscNew(&link);CHKERRQ(ierr); 162 link->ft = fieldtype; 163 link->vec = vec; 164 link->next = NULL; 165 /* Append to list */ 166 if (tail) { 167 while (tail->next) tail = tail->next; 168 tail->next = link; 169 } else vtk->link = link; 170 PetscFunctionReturn(0); 171 } 172 173 /*MC 174 PETSCVIEWERVTK - A viewer that writes to an VTK file 175 176 177 .seealso: PetscViewerVTKOpen(), PetscViewerHDF5Open(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET, 178 PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSTRING, 179 PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB, 180 PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType() 181 182 Level: beginner 183 M*/ 184 185 PETSC_EXTERN PetscErrorCode PetscViewerCreate_VTK(PetscViewer v) 186 { 187 PetscViewer_VTK *vtk; 188 PetscErrorCode ierr; 189 190 PetscFunctionBegin; 191 ierr = PetscNewLog(v,&vtk);CHKERRQ(ierr); 192 193 v->data = (void*)vtk; 194 v->ops->destroy = PetscViewerDestroy_VTK; 195 v->ops->flush = PetscViewerFlush_VTK; 196 vtk->btype = (PetscFileMode) -1; 197 vtk->filename = 0; 198 199 ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_VTK);CHKERRQ(ierr); 200 ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_VTK);CHKERRQ(ierr); 201 ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_VTK);CHKERRQ(ierr); 202 ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_VTK);CHKERRQ(ierr); 203 ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerVTKAddField_C",PetscViewerVTKAddField_VTK);CHKERRQ(ierr); 204 PetscFunctionReturn(0); 205 } 206 207 /*@C 208 PetscViewerVTKOpen - Opens a file for VTK output. 209 210 Collective on MPI_Comm 211 212 Input Parameters: 213 + comm - MPI communicator 214 . name - name of file 215 - type - type of file 216 $ FILE_MODE_WRITE - create new file for binary output 217 $ FILE_MODE_READ - open existing file for binary input (not currently supported) 218 $ FILE_MODE_APPEND - open existing file for binary output (not currently supported) 219 220 Output Parameter: 221 . vtk - PetscViewer for VTK input/output to use with the specified file 222 223 Level: beginner 224 225 Note: 226 This PetscViewer should be destroyed with PetscViewerDestroy(). 227 228 Concepts: VTK files 229 Concepts: PetscViewer^creating 230 231 .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 232 VecView(), MatView(), VecLoad(), MatLoad(), 233 PetscFileMode, PetscViewer 234 @*/ 235 PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *vtk) 236 { 237 PetscErrorCode ierr; 238 239 PetscFunctionBegin; 240 ierr = PetscViewerCreate(comm,vtk);CHKERRQ(ierr); 241 ierr = PetscViewerSetType(*vtk,PETSCVIEWERVTK);CHKERRQ(ierr); 242 ierr = PetscViewerFileSetMode(*vtk,type);CHKERRQ(ierr); 243 ierr = PetscViewerFileSetName(*vtk,name);CHKERRQ(ierr); 244 PetscFunctionReturn(0); 245 } 246 247 /*@C 248 PetscViewerVTKFWrite - write binary data preceded by 32-bit int length (in bytes), does not do byte swapping. 249 250 Logically collective on PetscViewer 251 252 Input Parameters: 253 + viewer - logically collective viewer, data written from rank 0 254 . fp - file pointer valid on rank 0 255 . data - data pointer valid on rank 0 256 . n - number of data items 257 - dtype - data type 258 259 Level: developer 260 261 Notes: 262 If PetscScalar is __float128 then the binary files are written in double precision 263 264 Concepts: VTK files 265 Concepts: PetscViewer^creating 266 267 .seealso: DMDAVTKWriteAll(), DMComplexVTKWriteAll(), PetscViewerPushFormat(), PetscViewerVTKOpen(), PetscBinaryWrite() 268 @*/ 269 PetscErrorCode PetscViewerVTKFWrite(PetscViewer viewer,FILE *fp,const void *data,PetscInt n,MPI_Datatype dtype) 270 { 271 PetscErrorCode ierr; 272 PetscMPIInt rank; 273 MPI_Datatype vdtype=dtype; 274 #if defined(PETSC_USE_REAL___FLOAT128) 275 double *tmp; 276 PetscInt i; 277 PetscReal *ttmp = (PetscReal*)data; 278 #endif 279 280 PetscFunctionBegin; 281 if (n < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n); 282 if (!n) PetscFunctionReturn(0); 283 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 284 if (!rank) { 285 size_t count; 286 PetscMPIInt dsize; 287 PetscVTKInt bytes; 288 289 #if defined(PETSC_USE_REAL___FLOAT128) 290 if (dtype == MPIU___FLOAT128) { 291 ierr = PetscMalloc1(n,&tmp);CHKERRQ(ierr); 292 for (i=0; i<n; i++) tmp[i] = ttmp[i]; 293 data = (void*) tmp; 294 vdtype = MPI_DOUBLE; 295 } 296 #endif 297 ierr = MPI_Type_size(vdtype,&dsize);CHKERRQ(ierr); 298 bytes = PetscVTKIntCast(dsize*n); 299 300 count = fwrite(&bytes,sizeof(int),1,fp); 301 if (count != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Error writing byte count"); 302 count = fwrite(data,dsize,(size_t)n,fp); 303 if ((PetscInt)count != n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Wrote %D/%D array members of size %d",(PetscInt)count,n,dsize); 304 #if defined(PETSC_USE_REAL___FLOAT128) 305 if (dtype == MPIU___FLOAT128) { 306 ierr = PetscFree(tmp);CHKERRQ(ierr); 307 } 308 #endif 309 } 310 PetscFunctionReturn(0); 311 } 312