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