-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwrite_adaptive_grid.cpp
174 lines (170 loc) · 6.02 KB
/
write_adaptive_grid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Write a simple 3d adaptive refined grid into a single file.
Typical compilation and execution (change paths if needed):
c++ -std=c++11 -g -o write_adaptive_grid.exe ../write_adaptive_grid.cpp \
-I/usr/local/include -L/usr/local/lib -lcgns && \
./write_adaptive_grid.exe 4 && cgnscheck adaptive_grid.cgns
*/
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
// cgnslib.h file must be located in directory specified by -I during compile:
#include "cgnslib.h"
#if CGNS_VERSION < 3100
# define cgsize_t int
#endif
int main(int argc, char* argv[]) {
/*
Create A CGNS File
*/
// set file name:
char file_name[33] = "adaptive_grid.cgns";
std::printf("A file named \"%s\"\n", file_name);
std::printf(" is being creating... ");
// get file id:
int file_id;
if (cg_open(file_name, CG_MODE_WRITE, &file_id)) // check the returned ierror
cg_error_exit();
std::printf("has been created with id %d.\n", file_id);
/*
Create A CGNSBase_t
*/
// set base name:
auto base_name = std::string("BaseOfAdaptiveGrid");
assert(base_name.size() <= 32);
std::printf(" A CGNSBase_t named \"%s\"\n", base_name.c_str());
std::printf(" is being creating... ");
// set base dims:
int cell_dim{2}, phys_dim{3};
// get base id:
int base_id;
if (cg_base_write(file_id, base_name.c_str(), cell_dim, phys_dim, &base_id))
cg_error_exit();
std::printf("has been created with id %d.\n", base_id);
// set simulation type:
if (cg_simulation_type_write(file_id, base_id, CGNS_ENUMV(TimeAccurate)))
cg_error_exit();
/*
Create Multiple Levels
*/
assert(argc >= 2);
int n_levels = atoi(argv[1]);
assert(n_levels < 8);
auto time_values = std::vector<double>(n_levels);
auto zone_pointers = std::vector<char>(/* 65 */32 * n_levels, '\0');
auto head = zone_pointers.begin();
for (int level = 0; level < n_levels; level++) {
// set zone name:
auto zone_name = "Zone[" + std::to_string(level) + "]";
assert(zone_name.size() <= 32);
std::printf(" A Zone_t named \"%s\"\n", zone_name.c_str());
std::printf(" is being creating... ");
// set zone size:
int n_cells_x{2 << level}, n_cells_y{1 << level};
int n_nodes_x{n_cells_x + 1}, n_nodes_y{n_cells_y + 1};
int n_cells = n_cells_x * n_cells_y;
int n_nodes = n_nodes_x * n_nodes_y;
cgsize_t grid_size[3][1] = {n_nodes, n_cells, 0};
// get zone id:
int zone_id;
if (cg_zone_write(file_id, base_id, zone_name.c_str(), grid_size[0],
CGNS_ENUMV(Unstructured), &zone_id))
cg_error_exit();
std::printf("has been created with id %d.\n", zone_id);
// create a ZoneIterativeData_t:
cg_ziter_write(file_id, base_id, zone_id, "ZoneIterativeData");
// set nodes (coordinates):
double dx = 2.0 / n_cells_x;
double dy = 1.0 / n_cells_y;
auto coord_x = std::vector<double>(n_nodes);
auto coord_y = std::vector<double>(n_nodes);
auto coord_z = std::vector<double>(n_nodes, 1.0);
int i = 0;
double y = -dy + 1.1 * level;
for (int iy = 0; iy != n_nodes_y; ++iy) {
y += dy;
double x = -dx;
for (int ix = 0; ix != n_nodes_x; ++ix) {
x += dx;
coord_x[i] = x;
coord_y[i] = y;
++i;
}
assert(x == 2.0);
}
assert(i == n_nodes);
int coord_id;
if (cg_coord_write(file_id, base_id, zone_id,
CGNS_ENUMV(RealDouble), "CoordinateX", coord_x.data(), &coord_id))
cg_error_exit();
if (cg_coord_write(file_id, base_id, zone_id,
CGNS_ENUMV(RealDouble), "CoordinateY", coord_y.data(), &coord_id))
cg_error_exit();
if (cg_coord_write(file_id, base_id, zone_id,
CGNS_ENUMV(RealDouble), "CoordinateZ", coord_z.data(), &coord_id))
cg_error_exit();
// set cells (connectivities):
char section_name[33] = "Interior";
std::printf(" An Elements_t named \"%s\"\n", section_name);
std::printf(" is being creating... ");
cgsize_t quad_elems[n_cells][4];
int section_id;
int i_elem = 0;
for (int iy = 0; iy < n_cells_y; iy++) {
for (int ix = 0; ix < n_cells_x; ix++) {
quad_elems[i_elem][0] = n_nodes_x * iy + ix + 1;
quad_elems[i_elem][1] = quad_elems[i_elem][0] + 1;
quad_elems[i_elem][2] = quad_elems[i_elem][1] + n_nodes_x;
quad_elems[i_elem][3] = quad_elems[i_elem][2] - 1;
++i_elem;
}
}
assert(i_elem == grid_size[1][0]);
cgsize_t i_elem_first{1}, i_elem_last{i_elem};
cg_section_write(file_id, base_id, zone_id,
section_name, CGNS_ENUMV(QUAD_4), i_elem_first, i_elem_last,
0/* n_boundary_elements */, quad_elems[0], §ion_id);
std::printf("has been created with id %d.\n", section_id);
// set node data:
// set cell data:
// set iteration info:
time_values[level] = level * 0.1;
// zone_name = base_name + "/" + zone_name;
std::copy(zone_name.begin(), zone_name.end(), head);
head += /* 65 */32;
}
assert(zone_pointers.begin() + /* 65 */32 * n_levels == head);
for (int l = 0; l < n_levels; ++l) {
std::printf("%s\n", &head[/* 65 */32 * l]);
}
/*
Create A BaseIterativeData_t
*/
if (cg_biter_write(file_id, base_id, "TimeSteps", n_levels))
cg_error_exit();
// goto this BaseIterativeData_t:
if (cg_goto(file_id, base_id, "BaseIterativeData_t", 1, "end"))
cg_error_exit();
// write time values and pointers:
cgsize_t data_dim[3] = {/* 65 */32, 1, n_levels};
if (cg_array_write("TimeValues", CGNS_ENUMV(RealDouble), 1, data_dim + 2,
time_values.data()))
cg_error_exit();
if (cg_array_write("NumberOfZones", CGNS_ENUMV(Integer), 1, data_dim + 2,
std::vector<int>(n_levels, 1).data())) // {1, 1, ..., 1}
cg_error_exit();
if (cg_array_write("ZonePointers", CGNS_ENUMV(Character), 3, data_dim,
zone_pointers.data()))
cg_error_exit();
/*
Close the CGNS File
*/
std::printf("\"%s\" is being closing... ", file_name);
if (cg_close(file_id))
cg_error_exit();
std::printf("has been closed.\n\n");
return 0;
}