Actual source code: cgnsv.c

  1: #include <petsc/private/viewercgnsimpl.h>
  2: #if defined(PETSC_HDF5_HAVE_PARALLEL)
  3:   #include <pcgnslib.h>
  4: #else
  5:   #include <cgnslib.h>
  6: #endif

  8: static PetscErrorCode PetscViewerSetFromOptions_CGNS(PetscViewer v, PetscOptionItems *PetscOptionsObject)
  9: {
 10:   PetscOptionsHeadBegin(PetscOptionsObject, "CGNS Viewer Options");
 11:   PetscOptionsHeadEnd();
 12:   return 0;
 13: }

 15: static PetscErrorCode PetscViewerView_CGNS(PetscViewer v, PetscViewer viewer)
 16: {
 17:   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)v->data;

 19:   if (cgv->filename) PetscViewerASCIIPrintf(viewer, "Filename: %s\n", cgv->filename);
 20:   return 0;
 21: }

 23: static PetscErrorCode PetscViewerFileClose_CGNS(PetscViewer viewer)
 24: {
 25:   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;

 27:   if (cgv->output_times) {
 28:     size_t     size, width = 32;
 29:     char      *solnames;
 30:     PetscReal *times;
 31:     cgsize_t   num_times;
 32:     PetscSegBufferGetSize(cgv->output_times, &size);
 33:     PetscSegBufferExtractInPlace(cgv->output_times, &times);
 34:     num_times = size;
 35:     cg_biter_write(cgv->file_num, cgv->base, "TimeIterValues", num_times);
 36:     cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL);
 37:     cg_array_write("TimeValues", CGNS_ENUMV(RealDouble), 1, &num_times, times);
 38:     PetscSegBufferDestroy(&cgv->output_times);
 39:     cg_ziter_write(cgv->file_num, cgv->base, cgv->zone, "ZoneIterativeData");
 40:     cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL);
 41:     PetscMalloc(size * width + 1, &solnames);
 42:     for (size_t i = 0; i < size; i++) PetscSNPrintf(&solnames[i * width], width + 1, "FlowSolution%-20zu", i);
 43:     cgsize_t shape[2] = {(cgsize_t)width, (cgsize_t)size};
 44:     cg_array_write("FlowSolutionPointers", CGNS_ENUMV(Character), 2, shape, solnames);
 45:     // The VTK reader looks for names like FlowSolution*Pointers.
 46:     for (size_t i = 0; i < size; i++) PetscSNPrintf(&solnames[i * width], width + 1, "%-32s", "CellInfo");
 47:     cg_array_write("FlowSolutionCellInfoPointers", CGNS_ENUMV(Character), 2, shape, solnames);
 48:     PetscFree(solnames);

 50:     cg_simulation_type_write(cgv->file_num, cgv->base, CGNS_ENUMV(TimeAccurate));
 51:   }
 52:   PetscFree(cgv->filename);
 53: #if defined(PETSC_HDF5_HAVE_PARALLEL)
 54:   cgp_close(cgv->file_num);
 55: #else
 56:   if (cgv->file_num) cg_close(cgv->file_num);
 57: #endif
 58:   cgv->file_num = 0;
 59:   return 0;
 60: }

 62: static PetscErrorCode PetscViewerDestroy_CGNS(PetscViewer viewer)
 63: {
 64:   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;

 66:   PetscViewerFileClose_CGNS(viewer);
 67:   PetscFree(cgv->node_l2g);
 68:   PetscFree(cgv->nodal_field);
 69:   PetscFree(cgv);
 70:   PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL);
 71:   PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL);
 72:   PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL);
 73:   PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL);
 74:   return 0;
 75: }

 77: static PetscErrorCode PetscViewerFileSetMode_CGNS(PetscViewer viewer, PetscFileMode type)
 78: {
 79:   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;

 81:   cgv->btype = type;
 82:   return 0;
 83: }

 85: static PetscErrorCode PetscViewerFileGetMode_CGNS(PetscViewer viewer, PetscFileMode *type)
 86: {
 87:   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;

 89:   *type = cgv->btype;
 90:   return 0;
 91: }

 93: static PetscErrorCode PetscViewerFileSetName_CGNS(PetscViewer viewer, const char *filename)
 94: {
 95:   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;

 97:   if (cgv->file_num) cg_close(cgv->file_num);
 98:   PetscFree(cgv->filename);
 99:   PetscStrallocpy(filename, &cgv->filename);

101:   switch (cgv->btype) {
102:   case FILE_MODE_READ:
103:     SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "FILE_MODE_READ not yet implemented");
104:     break;
105:   case FILE_MODE_WRITE:
106: #if defined(PETSC_HDF5_HAVE_PARALLEL)
107:     cgp_mpi_comm(PetscObjectComm((PetscObject)viewer));
108:     cgp_open(filename, CG_MODE_WRITE, &cgv->file_num);
109: #else
110:     cg_open(filename, CG_MODE_WRITE, &cgv->file_num);
111: #endif
112:     break;
113:   case FILE_MODE_UNDEFINED:
114:     SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
115:   default:
116:     SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[cgv->btype]);
117:   }
118:   return 0;
119: }

121: static PetscErrorCode PetscViewerFileGetName_CGNS(PetscViewer viewer, const char **filename)
122: {
123:   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;

125:   *filename = cgv->filename;
126:   return 0;
127: }

129: /*MC
130:    PETSCVIEWERCGNS - A viewer for CGNS files

132:   Level: beginner

134: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `VecView()`, `DMView()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`
135: M*/

137: PETSC_EXTERN PetscErrorCode PetscViewerCreate_CGNS(PetscViewer v)
138: {
139:   PetscViewer_CGNS *cgv;

141:   PetscNew(&cgv);

143:   v->data                = cgv;
144:   v->ops->destroy        = PetscViewerDestroy_CGNS;
145:   v->ops->setfromoptions = PetscViewerSetFromOptions_CGNS;
146:   v->ops->view           = PetscViewerView_CGNS;
147:   cgv->btype             = FILE_MODE_UNDEFINED;
148:   cgv->filename          = NULL;

150:   PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_CGNS);
151:   PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_CGNS);
152:   PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_CGNS);
153:   PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_CGNS);
154:   return 0;
155: }