PetIBM 0.5.4
Toolbox and applications of the immersed-boundary method for distributed-memory architectures
main.cpp
Go to the documentation of this file.
1
10#include <fstream>
11#include <sstream>
12#include <string>
13
14#include <petscsys.h>
15#include <yaml-cpp/yaml.h>
16
17#include <petibm/mesh.h>
18#include <petibm/parser.h>
19#include <petibm/type.h>
20
42PetscErrorCode writeSingleXDMF(const std::string &directory,
43 const std::string &name, const PetscInt &dim,
45 const PetscInt &bg, const PetscInt &ed,
46 const PetscInt &step);
47
48int main(int argc, char **argv)
49{
50 PetscErrorCode ierr;
51
52 ierr = PetscInitialize(&argc, &argv, nullptr, nullptr); CHKERRQ(ierr);
53
54 YAML::Node setting;
55 ierr = petibm::parser::getSettings(setting); CHKERRQ(ierr);
56
58 ierr = petibm::mesh::createMesh(PETSC_COMM_WORLD, setting, mesh);
59 CHKERRQ(ierr);
60
61 PetscInt bg;
62 PetscInt ed;
63 PetscInt step;
64 PetscBool isSet;
65
66 // get the range of solutions that are going to be calculated
67 ierr = PetscOptionsGetInt(nullptr, nullptr, "-bg", &bg, &isSet);
68 CHKERRQ(ierr);
69 if (!isSet) bg = setting["parameters"]["startStep"].as<PetscInt>(0);
70 ierr = PetscOptionsGetInt(nullptr, nullptr, "-ed", &ed, &isSet);
71 CHKERRQ(ierr);
72 if (!isSet) ed = bg + setting["parameters"]["nt"].as<PetscInt>();
73 ierr = PetscOptionsGetInt(nullptr, nullptr, "-step", &step, &isSet);
74 CHKERRQ(ierr);
75 if (!isSet) step = setting["parameters"]["nsave"].as<PetscInt>();
76
77 // u
78 ierr = writeSingleXDMF(setting["output"].as<std::string>(), "u",
79 mesh->dim, mesh->n[0], bg, ed, step); CHKERRQ(ierr);
80
81 // v
82 ierr = writeSingleXDMF(setting["output"].as<std::string>(), "v",
83 mesh->dim, mesh->n[1], bg, ed, step); CHKERRQ(ierr);
84
85 // p
86 ierr = writeSingleXDMF(setting["output"].as<std::string>(), "p",
87 mesh->dim, mesh->n[3], bg, ed, step); CHKERRQ(ierr);
88
89 // wz
91
92 wn[0] = mesh->n[4][0];
93 wn[1] = mesh->n[4][1];
94 wn[2] = mesh->n[3][2];
95 ierr = writeSingleXDMF(setting["output"].as<std::string>(), "wz",
96 mesh->dim, wn, bg, ed, step); CHKERRQ(ierr);
97
98 if (mesh->dim == 3)
99 {
100 // w
101 ierr = writeSingleXDMF(setting["output"].as<std::string>(), "w",
102 mesh->dim, mesh->n[2], bg, ed, step);
103 CHKERRQ(ierr);
104
105 // wx
106 wn[0] = mesh->n[3][0];
107 wn[1] = mesh->n[4][1];
108 wn[2] = mesh->n[4][2];
109 ierr = writeSingleXDMF(setting["output"].as<std::string>(), "wx",
110 mesh->dim, wn, bg, ed, step); CHKERRQ(ierr);
111
112 // wy
113 wn[0] = mesh->n[4][0];
114 wn[1] = mesh->n[3][1];
115 wn[2] = mesh->n[4][2];
116 ierr = writeSingleXDMF(setting["output"].as<std::string>(), "wy",
117 mesh->dim, wn, bg, ed, step); CHKERRQ(ierr);
118 }
119
120 // manually destroy PETSc objects inside the mesh instance
121 ierr = mesh->destroy(); CHKERRQ(ierr);
122
123 ierr = PetscFinalize(); CHKERRQ(ierr);
124
125 return 0;
126} // main
127
128PetscErrorCode writeSingleXDMF(const std::string &directory,
129 const std::string &name, const PetscInt &dim,
130 const petibm::type::IntVec1D &n,
131 const PetscInt &bg, const PetscInt &ed,
132 const PetscInt &step)
133{
134 PetscErrorCode ierr;
135
136 PetscFunctionBeginUser;
137
138 PetscViewer viewer;
139
140 std::string file = directory + "/" + name + ".xmf";
141
142 ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD, file.c_str(), &viewer);
143 CHKERRQ(ierr);
144
145 // write header
146 ierr = PetscViewerASCIIPrintf(viewer, "<?xml version=\'1.0\' ?>\n\n");
147 CHKERRQ(ierr);
148
149 // write macro definitions
150 ierr = PetscViewerASCIIPrintf(viewer,
151 "<!DOCTYPE Xdmf SYSTEM \"Xdmf.dtd\" [\n");
152 CHKERRQ(ierr);
153 ierr = PetscViewerASCIIPrintf(viewer, "\t<!ENTITY CaseDir \"./\">\n");
154 CHKERRQ(ierr);
155
156 // always use 3D XDMF format, so both Visit and Paraview works
157 for (int i = 0; i < 3; ++i)
158 {
159 ierr = PetscViewerASCIIPrintf(
160 viewer, "\t<!ENTITY N%s \"%d\">\n",
161 petibm::type::dir2str[petibm::type::Dir(i)].c_str(), n[i]);
162 CHKERRQ(ierr);
163 }
164
165 // topology
166 ierr = PetscViewerASCIIPrintf(
167 viewer,
168 "\t<!ENTITY Topo \"<Topology TopologyType=\'3DRectMesh\' "
169 "Dimensions=\'&Nz; &Ny; &Nx;\'/>\">\n"); CHKERRQ(ierr);
170
171 // geometry
172 ierr = PetscViewerASCIIPrintf(viewer, "\t<!ENTITY Geo\n"); CHKERRQ(ierr);
173 ierr = PetscViewerASCIIPrintf(viewer,
174 "\t\t\"<Geometry GeometryType=\'VXVYVZ\'>\n");
175 CHKERRQ(ierr);
176
177 for (int i = 0; i < dim; ++i)
178 {
179 std::string dir = petibm::type::dir2str[petibm::type::Dir(i)];
180 ierr = PetscViewerASCIIPrintf(
181 viewer,
182 "\t\t\t"
183 "<DataItem Dimensions=\'&N%s;\' Format=\'HDF\' Precision=\'8\'>\n"
184 "\t\t\t\t&CaseDir;/grid.h5:/%s/%s\n"
185 "\t\t\t</DataItem>\n",
186 dir.c_str(), name.c_str(), dir.c_str()); CHKERRQ(ierr);
187 }
188
189 if (dim == 2) // if dim == 2, use dummy z-axis
190 {
191 ierr = PetscViewerASCIIPrintf(
192 viewer,
193 "\t\t\t"
194 "<DataItem Dimensions=\'&Nz;\' Format=\'XML\' Precision=\'8\'>\n"
195 "\t\t\t\t0.0\n"
196 "\t\t\t</DataItem>\n"); CHKERRQ(ierr);
197 }
198 ierr = PetscViewerASCIIPrintf(viewer, "\t\t</Geometry>\"\n");
199 CHKERRQ(ierr);
200 ierr = PetscViewerASCIIPrintf(viewer, "\t>\n"); CHKERRQ(ierr);
201
202 // the end of entity definitions
203 ierr = PetscViewerASCIIPrintf(viewer, "]>\n\n"); CHKERRQ(ierr);
204
205 // write Xdmf block
206 ierr = PetscViewerASCIIPrintf(viewer, "<Xdmf Version=\"3.0\">\n");
207 CHKERRQ(ierr);
208
209 // write Domain block
210 ierr = PetscViewerASCIIPrintf(viewer, "\t<Domain>\n"); CHKERRQ(ierr);
211
212 // write temporal grid collection
213 ierr = PetscViewerASCIIPrintf(
214 viewer,
215 "\t"
216 "<Grid GridType=\"Collection\" CollectionType=\"Temporal\">\n");
217 CHKERRQ(ierr);
218
219 // write each step
220 for (PetscInt t = bg; t <= ed; t += step)
221 {
222 ierr = PetscViewerASCIIPrintf(
223 viewer,
224 "\t\t"
225 "<Grid GridType=\"Uniform\" Name=\"%s Grid\">\n",
226 name.c_str()); CHKERRQ(ierr);
227
228 ierr = PetscViewerASCIIPrintf(viewer,
229 "\t\t\t"
230 "<Time Value=\"%07d\" />\n",
231 t); CHKERRQ(ierr);
232 ierr = PetscViewerASCIIPrintf(viewer, "\t\t\t&Topo; &Geo;\n");
233 CHKERRQ(ierr);
234 ierr = PetscViewerASCIIPrintf(
235 viewer,
236 "\t\t\t"
237 "<Attribute Name=\"%s\" AttributeType=\"Scalar\" "
238 "Center=\"Node\">\n",
239 name.c_str()); CHKERRQ(ierr);
240 ierr =
241 PetscViewerASCIIPrintf(viewer,
242 "\t\t\t\t"
243 "<DataItem Dimensions=\"&Nz; &Ny; &Nx;\" ");
244 CHKERRQ(ierr);
245 ierr = PetscViewerASCIIPrintf(
246 viewer, "Format=\"HDF\" NumberType=\"Float\" Precision=\"8\">\n");
247 CHKERRQ(ierr);
248 ierr = PetscViewerASCIIPrintf(viewer,
249 "\t\t\t\t\t"
250 "&CaseDir;/%07d.h5:/%s\n",
251 t, name.c_str()); CHKERRQ(ierr);
252 ierr = PetscViewerASCIIPrintf(viewer,
253 "\t\t\t\t"
254 "</DataItem>\n"); CHKERRQ(ierr);
255 ierr = PetscViewerASCIIPrintf(viewer,
256 "\t\t\t"
257 "</Attribute>\n"); CHKERRQ(ierr);
258 ierr = PetscViewerASCIIPrintf(viewer,
259 "\t\t"
260 "</Grid>\n"); CHKERRQ(ierr);
261 }
262
263 ierr = PetscViewerASCIIPrintf(viewer, "\t</Grid>\n"); CHKERRQ(ierr);
264 ierr = PetscViewerASCIIPrintf(viewer, "\t</Domain>\n"); CHKERRQ(ierr);
265 ierr = PetscViewerASCIIPrintf(viewer, "</Xdmf>\n"); CHKERRQ(ierr);
266
267 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
268
269 PetscFunctionReturn(0);
270} // writeSingleXDMF
int main(int argc, char **argv)
Definition: main.cpp:48
PetscErrorCode writeSingleXDMF(const std::string &directory, const std::string &name, const PetscInt &dim, const petibm::type::IntVec1D &n, const PetscInt &bg, const PetscInt &ed, const PetscInt &step)
Definition: main.cpp:128
std::shared_ptr< mesh::MeshBase > Mesh
Type definition of Mesh.
Definition: mesh.h:348
PetscErrorCode createMesh(const MPI_Comm &comm, const YAML::Node &node, type::Mesh &mesh)
Factory function for creating a Mesh object.
Definition: mesh.cpp:86
PetscErrorCode getSettings(YAML::Node &node)
Get configuration settings.
Definition: parser.cpp:175
Dir
Legal physical directions.
Definition: type.h:68
std::vector< PetscInt > IntVec1D
1D std::vector holding PetscInt.
Definition: type.h:133
std::map< Dir, std::string > dir2str
Mapping between Dir and std::string.
Definition: type.cpp:18
Prototype of mesh::MeshBase, type::Mesh, and factory function.
Prototypes of parser functions.
Definition of user-defined types for convenience.