Actual source code: vtkv.c

petsc-3.9.4 2018-09-11
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: - vec - Vec to write

 31:    Level: developer

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

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

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

 50: static PetscErrorCode PetscViewerDestroy_VTK(PetscViewer viewer)
 51: {
 52:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
 53:   PetscErrorCode  ierr;

 56:   PetscFree(vtk->filename);
 57:   PetscFree(vtk);
 58:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL);
 59:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",NULL);
 60:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL);
 61:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetMode_C",NULL);
 62:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerVTKAddField_C",NULL);
 63:   return(0);
 64: }

 66: static PetscErrorCode PetscViewerFlush_VTK(PetscViewer viewer)
 67: {
 68:   PetscViewer_VTK          *vtk = (PetscViewer_VTK*)viewer->data;
 69:   PetscErrorCode           ierr;
 70:   PetscViewerVTKObjectLink link,next;

 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) {(*vtk->write)(vtk->dm,viewer);}
 75:   for (link=vtk->link; link; link=next) {
 76:     next = link->next;
 77:     PetscObjectDestroy(&link->vec);
 78:     PetscFree(link);
 79:   }
 80:   PetscObjectDestroy(&vtk->dm);
 81:   vtk->write = NULL;
 82:   return(0);
 83: }

 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;

 93:   PetscViewerFlush(viewer);
 94:   PetscFree(vtk->filename);
 95:   PetscStrlen(name,&len);
 96:   PetscStrcasecmp(name+len-4,".vtk",&isvtk);
 97:   PetscStrcasecmp(name+len-4,".vts",&isvts);
 98:   PetscStrcasecmp(name+len-4,".vtu",&isvtu);
 99:   PetscStrcasecmp(name+len-4,".vtr",&isvtr);
100:   if (isvtk) {
101:     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_ASCII_VTK;
102:     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]);
103:   } else if (isvts) {
104:     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTS;
105:     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]);
106:   } else if (isvtu) {
107:     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTU;
108:     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]);
109:   } else if (isvtr) {
110:     if (viewer->format == PETSC_VIEWER_DEFAULT) viewer->format = PETSC_VIEWER_VTK_VTR;
111:     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]);
112:   } else SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_UNKNOWN_TYPE,"File '%s' has unrecognized extension",name);
113:   PetscStrallocpy(name,&vtk->filename);
114:   return(0);
115: }

117: PetscErrorCode  PetscViewerFileGetName_VTK(PetscViewer viewer,const char **name)
118: {
119:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
121:   *name = vtk->filename;
122:   return(0);
123: }

125: PetscErrorCode  PetscViewerFileSetMode_VTK(PetscViewer viewer,PetscFileMode type)
126: {
127:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;

130:   vtk->btype = type;
131:   return(0);
132: }

134: PetscErrorCode  PetscViewerFileGetMode_VTK(PetscViewer viewer,PetscFileMode *type)
135: {
136:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;

139:   *type = vtk->btype;
140:   return(0);
141: }

143: PetscErrorCode  PetscViewerVTKAddField_VTK(PetscViewer viewer,PetscObject dm,PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject,PetscViewer),PetscViewerVTKFieldType fieldtype,PetscObject vec)
144: {
145:   PetscViewer_VTK          *vtk = (PetscViewer_VTK*)viewer->data;
146:   PetscViewerVTKObjectLink link, tail = vtk->link;
147:   PetscErrorCode           ierr;

150:   if (vtk->dm) {
151:     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");
152:   } else {
153:     PetscObjectReference(dm);
154:     vtk->dm = dm;
155:   }
156:   vtk->write = PetscViewerVTKWriteFunction;
157:   PetscNew(&link);
158:   link->ft   = fieldtype;
159:   link->vec  = vec;
160:   link->next = NULL;
161:   /* Append to list */
162:   if (tail) {
163:     while (tail->next) tail = tail->next;
164:     tail->next = link;
165:   } else vtk->link = link;
166:   return(0);
167: }

169: /*MC
170:    PETSCVIEWERVTK - A viewer that writes to an VTK file


173: .seealso:  PetscViewerVTKOpen(), PetscViewerHDF5Open(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
174:            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSTRING,
175:            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
176:            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()

178:   Level: beginner
179: M*/

181: PETSC_EXTERN PetscErrorCode PetscViewerCreate_VTK(PetscViewer v)
182: {
183:   PetscViewer_VTK *vtk;
184:   PetscErrorCode  ierr;

187:   PetscNewLog(v,&vtk);

189:   v->data         = (void*)vtk;
190:   v->ops->destroy = PetscViewerDestroy_VTK;
191:   v->ops->flush   = PetscViewerFlush_VTK;
192:   vtk->btype      = (PetscFileMode) -1;
193:   vtk->filename   = 0;

195:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_VTK);
196:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_VTK);
197:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_VTK);
198:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_VTK);
199:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerVTKAddField_C",PetscViewerVTKAddField_VTK);
200:   return(0);
201: }

203: /*@C
204:    PetscViewerVTKOpen - Opens a file for VTK output.

206:    Collective on MPI_Comm

208:    Input Parameters:
209: +  comm - MPI communicator
210: .  name - name of file
211: -  type - type of file
212: $    FILE_MODE_WRITE - create new file for binary output
213: $    FILE_MODE_READ - open existing file for binary input (not currently supported)
214: $    FILE_MODE_APPEND - open existing file for binary output (not currently supported)

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

219:    Level: beginner

221:    Note:
222:    This PetscViewer should be destroyed with PetscViewerDestroy().

224:    Concepts: VTK files
225:    Concepts: PetscViewer^creating

227: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
228:           VecView(), MatView(), VecLoad(), MatLoad(),
229:           PetscFileMode, PetscViewer
230: @*/
231: PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *vtk)
232: {

236:   PetscViewerCreate(comm,vtk);
237:   PetscViewerSetType(*vtk,PETSCVIEWERVTK);
238:   PetscViewerFileSetMode(*vtk,type);
239:   PetscViewerFileSetName(*vtk,name);
240:   return(0);
241: }

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

246:    Logically collective on PetscViewer

248:    Input Parameters:
249: +  viewer - logically collective viewer, data written from rank 0
250: .  fp - file pointer valid on rank 0
251: .  data - data pointer valid on rank 0
252: .  n - number of data items
253: -  dtype - data type

255:    Level: developer

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

259:    Concepts: VTK files
260:    Concepts: PetscViewer^creating

262: .seealso: DMDAVTKWriteAll(), DMComplexVTKWriteAll(), PetscViewerPushFormat(), PetscViewerVTKOpen(), PetscBinaryWrite()
263: @*/
264: PetscErrorCode PetscViewerVTKFWrite(PetscViewer viewer,FILE *fp,const void *data,PetscInt n,MPI_Datatype dtype)
265: {
267:   PetscMPIInt    rank;
268:   MPI_Datatype   vdtype=dtype;
269: #if defined(PETSC_USE_REAL___FLOAT128)
270:   double         *tmp;
271:   PetscInt       i;
272:   PetscReal      *ttmp = (PetscReal*)data;
273: #endif

276:   if (n < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n);
277:   if (!n) return(0);
278:   MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);
279:   if (!rank) {
280:     size_t      count;
281:     PetscMPIInt dsize;
282:     PetscVTKInt bytes;

284: #if defined(PETSC_USE_REAL___FLOAT128)
285:     if (dtype == MPIU___FLOAT128) {
286:       PetscMalloc1(n,&tmp);
287:       for (i=0; i<n; i++) tmp[i] = ttmp[i];
288:       data  = (void*) tmp;
289:       vdtype = MPI_DOUBLE;
290:     }
291: #endif
292:     MPI_Type_size(vdtype,&dsize);
293:     bytes = PetscVTKIntCast(dsize*n);

295:     count = fwrite(&bytes,sizeof(int),1,fp);
296:     if (count != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Error writing byte count");
297:     count = fwrite(data,dsize,(size_t)n,fp);
298:     if ((PetscInt)count != n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Wrote %D/%D array members of size %d",(PetscInt)count,n,dsize);
299: #if defined(PETSC_USE_REAL___FLOAT128)
300:     if (dtype == MPIU___FLOAT128) {
301:       PetscFree(tmp);
302:     }
303: #endif
304:   }
305:   return(0);
306: }