Actual source code: vtkv.c

petsc-3.3-p7 2013-05-11
  1: #include "../src/sys/viewer/impls/vtk/vtkvimpl.h" /*I "petscviewer.h" I*/

  5: /*@C
  6:    PetscViewerVTKAddField - Add a field to the viewer

  8:    Collective

 10:    Input Arguments:
 11: + viewer - VTK viewer
 12: . dm - DM on which Vec lives
 13: . func - function to write this Vec
 14: . fieldtype - Either PETSC_VTK_POINT_FIELD or PETSC_VTK_CELL_FIELD
 15: - vec - Vec to write

 17:    Level: developer

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

 22: .seealso: PetscViewerVTKOpen(), DMDAVTKWriteAll()
 23: @*/
 24: PetscErrorCode PetscViewerVTKAddField(PetscViewer viewer,PetscObject dm,PetscViewerVTKWriteFunction func,PetscViewerVTKFieldType fieldtype,PetscObject vec)
 25: {

 32:   PetscUseMethod(viewer,"PetscViewerVTKAddField_C",(PetscViewer,PetscObject,PetscViewerVTKWriteFunction,PetscViewerVTKFieldType,PetscObject),(viewer,dm,func,fieldtype,vec));
 33:   return(0);
 34: }

 38: static PetscErrorCode PetscViewerDestroy_VTK(PetscViewer viewer)
 39: {
 40:  PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
 41:  PetscErrorCode  ierr;

 44:  PetscFree(vtk->filename);
 45:  PetscFree(vtk);
 46:  PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileSetName_C","",PETSC_NULL);
 47:  PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileSetMode_C","",PETSC_NULL);
 48:  PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerVTKAddField_C","",PETSC_NULL);
 49:  return(0);
 50: }

 54: static PetscErrorCode PetscViewerFlush_VTK(PetscViewer viewer)
 55: {
 56:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
 58:   PetscViewerVTKObjectLink   link,next;

 61:   if (vtk->link && (!vtk->dm || !vtk->dmwriteall)) SETERRQ(((PetscObject)viewer)->comm,PETSC_ERR_ARG_WRONGSTATE,"No fields or no grid");
 62:   if (vtk->dmwriteall) {(*vtk->dmwriteall)(vtk->dm,viewer);}
 63:   for (link=vtk->link; link; link=next) {
 64:     next = link->next;
 65:     PetscObjectDestroy(&link->vec);
 66:     PetscFree(link);
 67:   }
 68:   PetscObjectDestroy(&vtk->dm);
 69:   vtk->dmwriteall = PETSC_NULL;
 70:   return(0);
 71: }

 73: EXTERN_C_BEGIN
 76: PetscErrorCode  PetscViewerFileSetName_VTK(PetscViewer viewer,const char name[])
 77: {
 78:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
 79:   PetscErrorCode  ierr;
 80:   PetscBool       isvtk, isvts;
 81:   size_t          len;

 84:   PetscViewerFlush(viewer);
 85:   PetscFree(vtk->filename);
 86:   PetscStrlen(name,&len);
 87:   PetscStrcasecmp(name+len-4,".vtk",&isvtk);
 88:   PetscStrcasecmp(name+len-4,".vts",&isvts);
 89:   if (isvtk) {
 90:     if (viewer->format == PETSC_VIEWER_DEFAULT) {PetscViewerSetFormat(viewer,PETSC_VIEWER_ASCII_VTK);}
 91:     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]);
 92:   } else if (isvts) {
 93:     if (viewer->format == PETSC_VIEWER_DEFAULT) {PetscViewerSetFormat(viewer,PETSC_VIEWER_VTK_VTS);}
 94:     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]);
 95:   } else SETERRQ1(((PetscObject)viewer)->comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"File '%s' has unrecognized extension",name);
 96:   PetscStrallocpy(name,&vtk->filename);
 97:   return(0);
 98: }
 99: EXTERN_C_END

101: EXTERN_C_BEGIN
104: PetscErrorCode  PetscViewerFileSetMode_VTK(PetscViewer viewer,PetscFileMode type)
105: {
106:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;

110:   vtk->btype = type;
111:   return(0);
112: }
113: EXTERN_C_END

115: EXTERN_C_BEGIN
118: PetscErrorCode  PetscViewerVTKAddField_VTK(PetscViewer viewer,PetscObject dm,PetscViewerVTKWriteFunction dmwriteall,PetscViewerVTKFieldType fieldtype,PetscObject vec)
119: {
120:   PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data;
121:   PetscViewerVTKObjectLink link, tail = vtk->link;

125:   if (vtk->dm) {
126:     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");
127:   }
128:   vtk->dm = dm;
129:   vtk->dmwriteall = dmwriteall;
130:   PetscMalloc(sizeof(struct _n_PetscViewerVTKObjectLink),&link);
131:   link->ft = fieldtype;
132:   link->vec = vec;
133:   link->next = PETSC_NULL;
134:   /* Append to list */
135:   if (tail) {
136:     while(tail->next) tail = tail->next;
137:     tail->next = link;
138:   } else {
139:     vtk->link = link;
140:   }
141:   return(0);
142: }
143: EXTERN_C_END

145: EXTERN_C_BEGIN
148: PetscErrorCode  PetscViewerCreate_VTK(PetscViewer v)
149: {
150:   PetscViewer_VTK *vtk;
151:   PetscErrorCode  ierr;

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

156:   v->data         = (void*)vtk;
157:   v->ops->destroy = PetscViewerDestroy_VTK;
158:   v->ops->flush   = PetscViewerFlush_VTK;
159:   v->iformat      = 0;
160:   vtk->btype     = (PetscFileMode) -1;
161:   vtk->filename  = 0;

163:   PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileSetName_C","PetscViewerFileSetName_VTK",
164:                                            PetscViewerFileSetName_VTK);
165:   PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileSetMode_C","PetscViewerFileSetMode_VTK",
166:                                            PetscViewerFileSetMode_VTK);
167:   PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerVTKAddField_C","PetscViewerVTKAddField_VTK",
168:                                            PetscViewerVTKAddField_VTK);
169:   return(0);
170: }
171: EXTERN_C_END

175: /*@C
176:    PetscViewerVTKOpen - Opens a file for VTK output.

178:    Collective on MPI_Comm

180:    Input Parameters:
181: +  comm - MPI communicator
182: .  name - name of file
183: -  type - type of file
184: $    FILE_MODE_WRITE - create new file for binary output
185: $    FILE_MODE_READ - open existing file for binary input (not currently supported)
186: $    FILE_MODE_APPEND - open existing file for binary output (not currently supported)

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

191:    Level: beginner

193:    Note:
194:    This PetscViewer should be destroyed with PetscViewerDestroy().

196:    Concepts: VTK files
197:    Concepts: PetscViewer^creating

199: .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
200:           VecView(), MatView(), VecLoad(), MatLoad(),
201:           PetscFileMode, PetscViewer
202: @*/
203: PetscErrorCode PetscViewerVTKOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *vtk)
204: {

208:   PetscViewerCreate(comm,vtk);
209:   PetscViewerSetType(*vtk,PETSCVIEWERVTK);
210:   PetscViewerFileSetMode(*vtk,type);
211:   PetscViewerFileSetName(*vtk,name);
212:   return(0);
213: }