Actual source code: vtkv.c

petsc-master 2019-08-18
Report Typos and Errors
  1:  #include <../src/sys/classes/viewer/impls/vtk/vtkvimpl.h>

  3: /*MC
  4:     PetscViewerVTKWriteFunction - functional form used to provide writer to the PetscViewerVTK

  6:      Synopsis:
  7:      #include <petscviewer.h>
  8:      PetscViewerVTKWriteFunction(PetscObject object,PetscViewer viewer)

 10:      Input Parameters:
 11: +      object - the PETSc object to be written
 12: -      viewer - viewer it is to be written to

 14:    Level: developer

 16: .seealso:   PetscViewerVTKAddField()
 17: M*/

 19: /*@C
 20:    PetscViewerVTKAddField - Add a field to the viewer

 22:    Collective

 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: . checkdm - whether to check for identical dm arguments as fields are added
 30: - vec - Vec to write

 32:    Note:
 33:    This routine keeps exclusive ownership of the Vec. The caller should not use or destroy the Vec after adding it.

 35:    Level: developer

 37: .seealso: PetscViewerVTKOpen(), DMDAVTKWriteAll(), PetscViewerVTKWriteFunction, PetscViewerVTKGetDM()
 38: @*/
 39: PetscErrorCode PetscViewerVTKAddField(PetscViewer viewer,PetscObject dm,PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject,PetscViewer),PetscViewerVTKFieldType fieldtype,PetscBool checkdm,PetscObject vec)
 40: {

 47:   PetscUseMethod(viewer,"PetscViewerVTKAddField_C",(PetscViewer,PetscObject,PetscErrorCode (*)(PetscObject,PetscViewer),PetscViewerVTKFieldType,PetscBool,PetscObject),(viewer,dm,PetscViewerVTKWriteFunction,fieldtype,checkdm,vec));
 48:   return(0);
 49: }

 51: /*@C
 52:    PetscViewerVTKGetDM - get the DM associated with the viewer

 54:    Collective

 56:    Input Arguments:
 57: + viewer - VTK viewer
 58: - dm - DM associated with the viewer (as PetscObject)

 60:    Level: developer

 62: .seealso: PetscViewerVTKOpen(), DMDAVTKWriteAll(), PetscViewerVTKWriteFunction, PetscViewerVTKAddField()
 63: @*/
 64: PetscErrorCode PetscViewerVTKGetDM(PetscViewer viewer,PetscObject *dm)
 65: {

 70:   PetscUseMethod(viewer,"PetscViewerVTKGetDM_C",(PetscViewer,PetscObject*),(viewer,dm));
 71:   return(0);
 72: }

 74: static PetscErrorCode PetscViewerDestroy_VTK(PetscViewer viewer)
 75: {
 76:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
 77:   PetscErrorCode  ierr;

 80:   PetscFree(vtk->filename);
 81:   PetscFree(vtk);
 82:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL);
 83:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",NULL);
 84:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL);
 85:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetMode_C",NULL);
 86:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerVTKAddField_C",NULL);
 87:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerVTKGetDM_C",NULL);
 88:   return(0);
 89: }

 91: static PetscErrorCode PetscViewerFlush_VTK(PetscViewer viewer)
 92: {
 93:   PetscViewer_VTK          *vtk = (PetscViewer_VTK*)viewer->data;
 94:   PetscErrorCode           ierr;
 95:   PetscViewerVTKObjectLink link,next;

 98:   if (vtk->link && (!vtk->dm || !vtk->write)) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"No fields or no grid");
 99:   if (vtk->write) {(*vtk->write)(vtk->dm,viewer);}
100:   for (link=vtk->link; link; link=next) {
101:     next = link->next;
102:     PetscObjectDestroy(&link->vec);
103:     PetscFree(link);
104:   }
105:   PetscObjectDestroy(&vtk->dm);
106:   vtk->write = NULL;
107:   return(0);
108: }

110: PetscErrorCode  PetscViewerFileSetName_VTK(PetscViewer viewer,const char name[])
111: {
112:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
113:   PetscErrorCode  ierr;
114:   PetscBool       isvtk,isvts,isvtu,isvtr;
115:   size_t          len;

118:   PetscViewerFlush(viewer);
119:   PetscFree(vtk->filename);
120:   PetscStrlen(name,&len);
121:   if (!len) {
122:     isvtk = PETSC_TRUE;
123:   } else {
124:     PetscStrcasecmp(name+len-4,".vtk",&isvtk);
125:     PetscStrcasecmp(name+len-4,".vts",&isvts);
126:     PetscStrcasecmp(name+len-4,".vtu",&isvtu);
127:     PetscStrcasecmp(name+len-4,".vtr",&isvtr);
128:   }
129:   if (isvtk) {
130:     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_ASCII_VTK;
131:     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]);
132:   } else if (isvts) {
133:     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTS;
134:     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]);
135:   } else if (isvtu) {
136:     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTU;
137:     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]);
138:   } else if (isvtr) {
139:     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTR;
140:     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]);
141:   } else SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_UNKNOWN_TYPE,"File '%s' has unrecognized extension",name);
142:   PetscStrallocpy(len ? name : "stdout",&vtk->filename);
143:   return(0);
144: }

146: PetscErrorCode  PetscViewerFileGetName_VTK(PetscViewer viewer,const char **name)
147: {
148:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
150:   *name = vtk->filename;
151:   return(0);
152: }

154: PetscErrorCode  PetscViewerFileSetMode_VTK(PetscViewer viewer,PetscFileMode type)
155: {
156:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;

159:   vtk->btype = type;
160:   return(0);
161: }

163: PetscErrorCode  PetscViewerFileGetMode_VTK(PetscViewer viewer,PetscFileMode *type)
164: {
165:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;

168:   *type = vtk->btype;
169:   return(0);
170: }

172: PetscErrorCode  PetscViewerVTKAddField_VTK(PetscViewer viewer,PetscObject dm,PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject,PetscViewer),PetscViewerVTKFieldType fieldtype,PetscBool checkdm,PetscObject vec)
173: {
174:   PetscViewer_VTK          *vtk = (PetscViewer_VTK*)viewer->data;
175:   PetscViewerVTKObjectLink link, tail = vtk->link;
176:   PetscErrorCode           ierr;

179:   if (vtk->dm) {
180:     if (checkdm && dm != vtk->dm) SETERRQ(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.");
181:   } else {
182:     PetscObjectReference(dm);
183:     vtk->dm = dm;
184:   }
185:   vtk->write = PetscViewerVTKWriteFunction;
186:   PetscNew(&link);
187:   link->ft   = fieldtype;
188:   link->vec  = vec;
189:   link->next = NULL;
190:   /* Append to list */
191:   if (tail) {
192:     while (tail->next) tail = tail->next;
193:     tail->next = link;
194:   } else vtk->link = link;
195:   return(0);
196: }

198: PetscErrorCode PetscViewerVTKGetDM_VTK(PetscViewer viewer,PetscObject *dm)
199: {
200:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;

203:   *dm = vtk->dm;
204:   return(0);
205: }

207: /*MC
208:    PETSCVIEWERVTK - A viewer that writes to a VTK file


211: .seealso:  PetscViewerVTKOpen(), PetscViewerHDF5Open(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
212:            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSTRING,
213:            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
214:            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()

216:   Level: beginner
217: M*/

219: PETSC_EXTERN PetscErrorCode PetscViewerCreate_VTK(PetscViewer v)
220: {
221:   PetscViewer_VTK *vtk;
222:   PetscErrorCode  ierr;

225:   PetscNewLog(v,&vtk);

227:   v->data         = (void*)vtk;
228:   v->ops->destroy = PetscViewerDestroy_VTK;
229:   v->ops->flush   = PetscViewerFlush_VTK;
230:   vtk->btype      = (PetscFileMode) -1;
231:   vtk->filename   = 0;

233:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_VTK);
234:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_VTK);
235:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_VTK);
236:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_VTK);
237:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerVTKAddField_C",PetscViewerVTKAddField_VTK);
238:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerVTKGetDM_C",PetscViewerVTKGetDM_VTK);
239:   return(0);
240: }

242: /*@C
243:    PetscViewerVTKOpen - Opens a file for VTK output.

245:    Collective

247:    Input Parameters:
248: +  comm - MPI communicator
249: .  name - name of file
250: -  type - type of file
251: $    FILE_MODE_WRITE - create new file for binary output
252: $    FILE_MODE_READ - open existing file for binary input (not currently supported)
253: $    FILE_MODE_APPEND - open existing file for binary output (not currently supported)

255:    Output Parameter:
256: .  vtk - PetscViewer for VTK input/output to use with the specified file

258:    Level: beginner

260:    Note:
261:    This PetscViewer should be destroyed with PetscViewerDestroy().


264: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
265:           VecView(), MatView(), VecLoad(), MatLoad(),
266:           PetscFileMode, PetscViewer
267: @*/
268: PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *vtk)
269: {

273:   PetscViewerCreate(comm,vtk);
274:   PetscViewerSetType(*vtk,PETSCVIEWERVTK);
275:   PetscViewerFileSetMode(*vtk,type);
276:   PetscViewerFileSetName(*vtk,name);
277:   return(0);
278: }

280: /*@C
281:    PetscViewerVTKFWrite - write binary data preceded by 32-bit int length (in bytes), does not do byte swapping.

283:    Logically collective on PetscViewer

285:    Input Parameters:
286: +  viewer - logically collective viewer, data written from rank 0
287: .  fp - file pointer valid on rank 0
288: .  data - data pointer valid on rank 0
289: .  n - number of data items
290: -  dtype - data type

292:    Level: developer

294:    Notes:
295:     If PetscScalar is __float128 then the binary files are written in double precision


298: .seealso: DMDAVTKWriteAll(), DMComplexVTKWriteAll(), PetscViewerPushFormat(), PetscViewerVTKOpen(), PetscBinaryWrite()
299: @*/
300: PetscErrorCode PetscViewerVTKFWrite(PetscViewer viewer,FILE *fp,const void *data,PetscInt n,MPI_Datatype dtype)
301: {
303:   PetscMPIInt    rank;
304:   MPI_Datatype   vdtype=dtype;
305: #if defined(PETSC_USE_REAL___FLOAT128)
306:   double         *tmp;
307:   PetscInt       i;
308:   PetscReal      *ttmp = (PetscReal*)data;
309: #endif

312:   if (n < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n);
313:   if (!n) return(0);
314:   MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);
315:   if (!rank) {
316:     size_t      count;
317:     PetscMPIInt dsize;
318:     PetscVTKInt bytes;

320: #if defined(PETSC_USE_REAL___FLOAT128)
321:     if (dtype == MPIU___FLOAT128) {
322:       PetscMalloc1(n,&tmp);
323:       for (i=0; i<n; i++) tmp[i] = ttmp[i];
324:       data  = (void*) tmp;
325:       vdtype = MPI_DOUBLE;
326:     }
327: #endif
328:     MPI_Type_size(vdtype,&dsize);
329:     bytes = PetscVTKIntCast(dsize*n);

331:     count = fwrite(&bytes,sizeof(int),1,fp);
332:     if (count != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Error writing byte count");
333:     count = fwrite(data,dsize,(size_t)n,fp);
334:     if ((PetscInt)count != n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Wrote %D/%D array members of size %d",(PetscInt)count,n,dsize);
335: #if defined(PETSC_USE_REAL___FLOAT128)
336:     if (dtype == MPIU___FLOAT128) {
337:       PetscFree(tmp);
338:     }
339: #endif
340:   }
341:   return(0);
342: }