xref: /petsc/src/sys/classes/viewer/impls/cgns/cgnsv.c (revision 2d30e087755efd99e28fdfe792ffbeb2ee1ea928)
1 #include <petsc/private/viewercgnsimpl.h> /*I "petscviewer.h" I*/
2 #if defined(PETSC_HDF5_HAVE_PARALLEL)
3 #include <pcgnslib.h>
4 #else
5 #include <cgnslib.h>
6 #endif
7 
8 static PetscErrorCode PetscViewerSetFromOptions_CGNS(PetscViewer v, PetscOptionItems *PetscOptionsObject) {
9   PetscFunctionBegin;
10   PetscOptionsHeadBegin(PetscOptionsObject, "CGNS Viewer Options");
11   PetscOptionsHeadEnd();
12   PetscFunctionReturn(0);
13 }
14 
15 static PetscErrorCode PetscViewerView_CGNS(PetscViewer v, PetscViewer viewer) {
16   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)v->data;
17 
18   PetscFunctionBegin;
19   if (cgv->filename) PetscCall(PetscViewerASCIIPrintf(viewer, "Filename: %s\n", cgv->filename));
20   PetscFunctionReturn(0);
21 }
22 
23 static PetscErrorCode PetscViewerFileClose_CGNS(PetscViewer viewer) {
24   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
25 
26   PetscFunctionBegin;
27   if (cgv->output_times) {
28     size_t     size, width = 32;
29     char      *solnames;
30     PetscReal *times;
31     cgsize_t   num_times;
32     PetscCall(PetscSegBufferGetSize(cgv->output_times, &size));
33     PetscCall(PetscSegBufferExtractInPlace(cgv->output_times, &times));
34     num_times = size;
35     PetscCallCGNS(cg_biter_write(cgv->file_num, cgv->base, "TimeIterValues", num_times));
36     PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "BaseIterativeData_t", 1, NULL));
37     PetscCallCGNS(cg_array_write("TimeValues", CGNS_ENUMV(RealDouble), 1, &num_times, times));
38     PetscCall(PetscSegBufferDestroy(&cgv->output_times));
39     PetscCallCGNS(cg_ziter_write(cgv->file_num, cgv->base, cgv->zone, "ZoneIterativeData"));
40     PetscCallCGNS(cg_goto(cgv->file_num, cgv->base, "Zone_t", cgv->zone, "ZoneIterativeData_t", 1, NULL));
41     PetscCall(PetscMalloc(size * width + 1, &solnames));
42     for (size_t i = 0; i < size; i++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "FlowSolution%-20zu", i));
43     cgsize_t shape[2] = {(cgsize_t)width, (cgsize_t)size};
44     PetscCallCGNS(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++) PetscCall(PetscSNPrintf(&solnames[i * width], width + 1, "%-32s", "CellInfo"));
47     PetscCallCGNS(cg_array_write("FlowSolutionCellInfoPointers", CGNS_ENUMV(Character), 2, shape, solnames));
48     PetscCall(PetscFree(solnames));
49 
50     PetscCallCGNS(cg_simulation_type_write(cgv->file_num, cgv->base, CGNS_ENUMV(TimeAccurate)));
51   }
52   PetscCall(PetscFree(cgv->filename));
53 #if defined(PETSC_HDF5_HAVE_PARALLEL)
54   PetscCallCGNS(cgp_close(cgv->file_num));
55 #else
56   if (cgv->file_num) PetscCallCGNS(cg_close(cgv->file_num));
57 #endif
58   cgv->file_num = 0;
59   PetscFunctionReturn(0);
60 }
61 
62 static PetscErrorCode PetscViewerDestroy_CGNS(PetscViewer viewer) {
63   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
64 
65   PetscFunctionBegin;
66   PetscCall(PetscViewerFileClose_CGNS(viewer));
67   PetscCall(PetscFree(cgv->node_l2g));
68   PetscCall(PetscFree(cgv->nodal_field));
69   PetscCall(PetscFree(cgv));
70   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL));
71   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL));
72   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL));
73   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL));
74   PetscFunctionReturn(0);
75 }
76 
77 static PetscErrorCode PetscViewerFileSetMode_CGNS(PetscViewer viewer, PetscFileMode type) {
78   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
79 
80   PetscFunctionBegin;
81   cgv->btype = type;
82   PetscFunctionReturn(0);
83 }
84 
85 static PetscErrorCode PetscViewerFileGetMode_CGNS(PetscViewer viewer, PetscFileMode *type) {
86   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
87 
88   PetscFunctionBegin;
89   *type = cgv->btype;
90   PetscFunctionReturn(0);
91 }
92 
93 static PetscErrorCode PetscViewerFileSetName_CGNS(PetscViewer viewer, const char *filename) {
94   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
95 
96   PetscFunctionBegin;
97   if (cgv->file_num) PetscCallCGNS(cg_close(cgv->file_num));
98   PetscCall(PetscFree(cgv->filename));
99   PetscCall(PetscStrallocpy(filename, &cgv->filename));
100 
101   switch (cgv->btype) {
102   case FILE_MODE_READ: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "FILE_MODE_READ not yet implemented"); break;
103   case FILE_MODE_WRITE:
104 #if defined(PETSC_HDF5_HAVE_PARALLEL)
105     PetscCallCGNS(cgp_mpi_comm(PetscObjectComm((PetscObject)viewer)));
106     PetscCallCGNS(cgp_open(filename, CG_MODE_WRITE, &cgv->file_num));
107 #else
108     PetscCallCGNS(cg_open(filename, CG_MODE_WRITE, &cgv->file_num));
109 #endif
110     break;
111   case FILE_MODE_UNDEFINED: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
112   default: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[cgv->btype]);
113   }
114   PetscFunctionReturn(0);
115 }
116 
117 static PetscErrorCode PetscViewerFileGetName_CGNS(PetscViewer viewer, const char **filename) {
118   PetscViewer_CGNS *cgv = (PetscViewer_CGNS *)viewer->data;
119 
120   PetscFunctionBegin;
121   *filename = cgv->filename;
122   PetscFunctionReturn(0);
123 }
124 
125 /*MC
126    PETSCVIEWERCGNS - A viewer for CGNS files
127 
128   Level: beginner
129 
130 .seealso: `PetscViewerCreate()`, `VecView()`, `DMView()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`
131 M*/
132 
133 PETSC_EXTERN PetscErrorCode PetscViewerCreate_CGNS(PetscViewer v) {
134   PetscViewer_CGNS *cgv;
135 
136   PetscFunctionBegin;
137   PetscCall(PetscNewLog(v, &cgv));
138 
139   v->data                = cgv;
140   v->ops->destroy        = PetscViewerDestroy_CGNS;
141   v->ops->setfromoptions = PetscViewerSetFromOptions_CGNS;
142   v->ops->view           = PetscViewerView_CGNS;
143   cgv->btype             = FILE_MODE_UNDEFINED;
144   cgv->filename          = NULL;
145 
146   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_CGNS));
147   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_CGNS));
148   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_CGNS));
149   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_CGNS));
150   PetscFunctionReturn(0);
151 }
152