Date: 2013-07-02T14:38:11+00:00

Actual source code: vtkv.c
petsc-3.4.2 2013-07-02

      1: #include "../src/sys/classes/viewer/impls/vtk/vtkvimpl.h" /*I "petscviewer.h" I*/

      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*/

     21: /*@C
     22:    PetscViewerVTKAddField - Add a field to the viewer

     24:    Collective

     26:    Input Arguments:
     27: + viewer - VTK viewer
     28: . dm - DM on which Vec lives
     29: . PetscViewerVTKWriteFunction - function to write this Vec
     30: . fieldtype - Either PETSC_VTK_POINT_FIELD or PETSC_VTK_CELL_FIELD
     31: - vec - Vec to write

     33:    Level: developer

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

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

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

     54: static PetscErrorCode PetscViewerDestroy_VTK(PetscViewer viewer)
     55: {
     56:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
     57:   PetscErrorCode  ierr;

     60:   PetscFree(vtk->filename);
     61:   PetscFree(vtk);
     62:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL);
     63:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL);
     64:   PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerVTKAddField_C",NULL);
     65:   return(0);
     66: }

     70: static PetscErrorCode PetscViewerFlush_VTK(PetscViewer viewer)
     71: {
     72:   PetscViewer_VTK          *vtk = (PetscViewer_VTK*)viewer->data;
     73:   PetscErrorCode           ierr;
     74:   PetscViewerVTKObjectLink link,next;

     77:   if (vtk->link && (!vtk->dm || !vtk->write)) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"No fields or no grid");
     78:   if (vtk->write) {(*vtk->write)(vtk->dm,viewer);}
     79:   for (link=vtk->link; link; link=next) {
     80:     next = link->next;
     81:     PetscObjectDestroy(&link->vec);
     82:     PetscFree(link);
     83:   }
     84:   PetscObjectDestroy(&vtk->dm);
     85:   vtk->write = NULL;
     86:   return(0);
     87: }

     91: PetscErrorCode  PetscViewerFileSetName_VTK(PetscViewer viewer,const char name[])
     92: {
     93:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
     94:   PetscErrorCode  ierr;
     95:   PetscBool       isvtk,isvts,isvtu;
     96:   size_t          len;

     99:   PetscViewerFlush(viewer);
    100:   PetscFree(vtk->filename);
    101:   PetscStrlen(name,&len);
    102:   PetscStrcasecmp(name+len-4,".vtk",&isvtk);
    103:   PetscStrcasecmp(name+len-4,".vts",&isvts);
    104:   PetscStrcasecmp(name+len-4,".vtu",&isvtu);
    105:   if (isvtk) {
    106:     if (viewer->format == PETSC_VIEWER_DEFAULT) {PetscViewerSetFormat(viewer,PETSC_VIEWER_ASCII_VTK);}
    107:     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]);
    108:   } else if (isvts) {
    109:     if (viewer->format == PETSC_VIEWER_DEFAULT) {PetscViewerSetFormat(viewer,PETSC_VIEWER_VTK_VTS);}
    110:     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]);
    111:   } else if (isvtu) {
    112:     if (viewer->format == PETSC_VIEWER_DEFAULT) {PetscViewerSetFormat(viewer,PETSC_VIEWER_VTK_VTU);}
    113:     if (viewer->format != PETSC_VIEWER_VTK_VTU) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_INCOMP,"Cannot use file '%s' with format %s, should have '.vts' extension",name,PetscViewerFormats[viewer->format]);
    114:   } else SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_UNKNOWN_TYPE,"File '%s' has unrecognized extension",name);
    115:   PetscStrallocpy(name,&vtk->filename);
    116:   return(0);
    117: }

    121: PetscErrorCode  PetscViewerFileSetMode_VTK(PetscViewer viewer,PetscFileMode type)
    122: {
    123:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;

    127:   vtk->btype = type;
    128:   return(0);
    129: }

    133: PetscErrorCode  PetscViewerVTKAddField_VTK(PetscViewer viewer,PetscObject dm,PetscErrorCode (*PetscViewerVTKWriteFunction)(PetscObject,PetscViewer),PetscViewerVTKFieldType fieldtype,PetscObject vec)
    134: {
    135:   PetscViewer_VTK          *vtk = (PetscViewer_VTK*)viewer->data;
    136:   PetscViewerVTKObjectLink link, tail = vtk->link;
    137:   PetscErrorCode           ierr;

    140:   if (vtk->dm) {
    141:     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");
    142:   }
    143:   vtk->dm    = dm;
    144:   vtk->write = PetscViewerVTKWriteFunction;
    145:   PetscMalloc(sizeof(struct _n_PetscViewerVTKObjectLink),&link);
    146:   link->ft   = fieldtype;
    147:   link->vec  = vec;
    148:   link->next = NULL;
    149:   /* Append to list */
    150:   if (tail) {
    151:     while (tail->next) tail = tail->next;
    152:     tail->next = link;
    153:   } else vtk->link = link;
    154:   return(0);
    155: }

    159: PETSC_EXTERN PetscErrorCode PetscViewerCreate_VTK(PetscViewer v)
    160: {
    161:   PetscViewer_VTK *vtk;
    162:   PetscErrorCode  ierr;

    165:   PetscNewLog(v,PetscViewer_VTK,&vtk);

    167:   v->data         = (void*)vtk;
    168:   v->ops->destroy = PetscViewerDestroy_VTK;
    169:   v->ops->flush   = PetscViewerFlush_VTK;
    170:   vtk->btype      = (PetscFileMode) -1;
    171:   vtk->filename   = 0;

    173:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_VTK);
    174:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_VTK);
    175:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerVTKAddField_C",PetscViewerVTKAddField_VTK);
    176:   return(0);
    177: }

    181: /*@C
    182:    PetscViewerVTKOpen - Opens a file for VTK output.

    184:    Collective on MPI_Comm

    186:    Input Parameters:
    187: +  comm - MPI communicator
    188: .  name - name of file
    189: -  type - type of file
    190: $    FILE_MODE_WRITE - create new file for binary output
    191: $    FILE_MODE_READ - open existing file for binary input (not currently supported)
    192: $    FILE_MODE_APPEND - open existing file for binary output (not currently supported)

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

    197:    Level: beginner

    199:    Note:
    200:    This PetscViewer should be destroyed with PetscViewerDestroy().

    202:    Concepts: VTK files
    203:    Concepts: PetscViewer^creating

    205: .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
    206:           VecView(), MatView(), VecLoad(), MatLoad(),
    207:           PetscFileMode, PetscViewer
    208: @*/
    209: PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *vtk)
    210: {

    214:   PetscViewerCreate(comm,vtk);
    215:   PetscViewerSetType(*vtk,PETSCVIEWERVTK);
    216:   PetscViewerFileSetMode(*vtk,type);
    217:   PetscViewerFileSetName(*vtk,name);
    218:   return(0);
    219: }

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

    226:    Logically collective on PetscViewer

    228:    Input Parameters:
    229: +  viewer - logically collective viewer, data written from rank 0
    230: .  fp - file pointer valid on rank 0
    231: .  data - data pointer valid on rank 0
    232: .  n - number of data items
    233: -  dtype - data type

    235:    Level: developer

    237:    Concepts: VTK files
    238:    Concepts: PetscViewer^creating

    240: .seealso: DMDAVTKWriteAll(), DMComplexVTKWriteAll(), PetscViewerSetFormat(), PetscViewerVTKOpen(), PetscBinaryWrite()
    241: @*/
    242: PetscErrorCode PetscViewerVTKFWrite(PetscViewer viewer,FILE *fp,const void *data,PetscInt n,PetscDataType dtype)
    243: {
    245:   PetscMPIInt    rank;

    248:   if (n < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n);
    249:   if (!n) return(0);
    250:   MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);
    251:   if (!rank) {
    252:     size_t      count;
    253:     PetscInt    size;
    254:     PetscVTKInt bytes;
    255:     switch (dtype) {
    256:     case PETSC_DOUBLE:
    257:       size = sizeof(double);
    258:       break;
    259:     case PETSC_FLOAT:
    260:       size = sizeof(float);
    261:       break;
    262:     case PETSC_INT:
    263:       size = sizeof(PetscInt);
    264:       break;
    265:     case PETSC_ENUM:
    266:       size = sizeof(PetscEnum);
    267:       break;
    268:     case PETSC_CHAR:
    269:       size = sizeof(char);
    270:       break;
    271:     default: SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Data type not supported");
    272:     }
    273:     bytes = PetscVTKIntCast(size*n);

    275:     count = fwrite(&bytes,sizeof(int),1,fp);
    276:     if (count != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Error writing byte count");
    277:     count = fwrite(data,size,(size_t)n,fp);
    278:     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);
    279:   }
    280:   return(0);
    281: }