Repository navigation
Expand file tree
/
Copy pathxdg.cpp
More file actions
300 lines (255 loc) · 7.99 KB
/
Copy pathxdg.cpp
File metadata and controls
300 lines (255 loc) · 7.99 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "openmc/xdg.h"
#include <algorithm> // for remove_if
#include <memory> // for shared_ptr
#include <string> // for string
#include <utility> // for pair
#include <vector> // for vector
#include <fmt/core.h>
#ifdef OPENMC_XDG_ENABLED
#include "xdg/config.h"
#include "xdg/xdg.h"
#endif
#include "openmc/constants.h"
#include "openmc/container_util.h"
#include "openmc/error.h"
#include "openmc/file_utils.h"
#include "openmc/geometry.h"
#include "openmc/geometry_aux.h"
#include "openmc/hdf5_interface.h"
#include "openmc/material.h"
#include "openmc/settings.h"
#include "openmc/string_utils.h"
namespace openmc {
#ifdef OPENMC_XDG_ENABLED
const bool XDG_ENABLED = true;
#else
const bool XDG_ENABLED = false;
#endif
} // namespace openmc
#ifdef OPENMC_XDG_ENABLED
namespace openmc {
//==============================================================================
// XDG Mesh implementation
//==============================================================================
const std::string XDGMesh::mesh_interface = "xdg";
XDGMesh::XDGMesh(pugi::xml_node node) : UnstructuredMesh(node)
{
std::string mesh_lib = get_node_value(node, "library", true, true);
if (mesh_lib == "moab") {
mesh_library_ = xdg::MeshLibrary::MOAB;
} else if (mesh_lib == "libmesh") {
mesh_library_ = xdg::MeshLibrary::LIBMESH;
} else {
fatal_error(fmt::format("Invalid mesh library specified in XML: {}. "
"Valid options are 'moab' and 'libmesh'.",
mesh_lib));
}
initialize();
}
XDGMesh::XDGMesh(hid_t group) : UnstructuredMesh(group)
{
std::string mesh_lib;
read_dataset(group, "library", mesh_lib);
if (mesh_lib == "moab") {
mesh_library_ = xdg::MeshLibrary::MOAB;
} else if (mesh_lib == "libmesh") {
mesh_library_ = xdg::MeshLibrary::LIBMESH;
} else {
fatal_error(fmt::format("Invalid mesh library specified in HDF5: {}. "
"Valid options are 'moab' and 'libmesh'.",
mesh_lib));
}
initialize();
}
XDGMesh::XDGMesh(const std::string& filename, double length_multiplier)
{
filename_ = filename;
set_length_multiplier(length_multiplier);
initialize();
}
XDGMesh::XDGMesh(
std::shared_ptr<xdg::XDG> external_xdg, double length_multiplier)
{
xdg_ = external_xdg;
filename_ = "unknown (external file)";
set_length_multiplier(length_multiplier);
initialize();
}
void XDGMesh::initialize()
{
interface_ = "xdg";
// Externally supplied instances already have an initialized mesh manager.
if (!xdg_) {
if (!xdg::XDGConfig::config().mesh_manager_enabled(mesh_library_)) {
fatal_error(fmt::format("Mesh library {} is not enabled in XDG.",
mesh_library_ == xdg::MeshLibrary::LIBMESH ? "libMesh" : "MOAB"));
}
xdg_ = xdg::XDG::create(mesh_library_);
if (!file_exists(filename_)) {
fatal_error(fmt::format(
"Mesh file \"{}\" for mesh {} does not exist", filename_, id_));
}
xdg_->mesh_manager()->load_file(filename_);
xdg_->mesh_manager()->init();
xdg_->mesh_manager()->parse_metadata();
}
// TODO: replace with direct method in XDG when available
n_surface_bins_ = 0;
for (auto vol : xdg_->mesh_manager()->volumes()) {
int n_elem = xdg_->mesh_manager()->num_volume_elements(vol);
// The implicit complement has no elements or element type.
if (n_elem == 0)
continue;
xdg::VolumeElementType elem_type =
xdg_->mesh_manager()->get_volume_element_type(vol);
if (elem_type == xdg::VolumeElementType::TET) {
n_surface_bins_ += 4 * n_elem;
} else if (elem_type == xdg::VolumeElementType::HEX) {
n_surface_bins_ += 6 * n_elem;
} else {
fatal_error(fmt::format("Unsupported volume element type {} for "
"surface bin counting in XDGMesh.",
static_cast<int>(elem_type)));
}
}
auto global_bbox = xdg_->mesh_manager()->global_bounding_box();
double length_multiplier =
length_multiplier_ > 0.0 ? length_multiplier_ : 1.0;
Position lower_left =
length_multiplier *
Position(global_bbox.min_x, global_bbox.min_y, global_bbox.min_z);
Position upper_right =
length_multiplier *
Position(global_bbox.max_x, global_bbox.max_y, global_bbox.max_z);
lower_left_ = {lower_left.x, lower_left.y, lower_left.z};
upper_right_ = {upper_right.x, upper_right.y, upper_right.z};
}
void XDGMesh::prepare_for_point_location()
{
xdg_->prepare_raytracer();
}
Position XDGMesh::sample_element(int32_t bin, uint64_t* seed) const
{
auto vertices = xdg_->mesh_manager()->element_vertices(bin_to_mesh_id(bin));
if (vertices.size() != 4) {
fatal_error("Sampling from an XDG mesh is only supported for linear "
"tetrahedra.");
}
Position sampled_position = this->sample_tet<xdg::Vertex>(vertices, seed);
if (length_multiplier_ > 0.0) {
sampled_position *= length_multiplier_;
}
return sampled_position;
}
void XDGMesh::bins_crossed(Position r0, Position r1, const Direction& u,
vector<int>& bins, vector<double>& lengths) const
{
xdg::Position p0 {r0.x, r0.y, r0.z};
xdg::Position p1 {r1.x, r1.y, r1.z};
if (length_multiplier_ > 0.0) {
p0 /= length_multiplier_;
p1 /= length_multiplier_;
}
double inv_length = 1 / (p1 - p0).length();
auto track_segments = xdg_->segments(p0, p1);
// remove elements with lengths of zero
track_segments.erase(
std::remove_if(track_segments.begin(), track_segments.end(),
[](const std::pair<xdg::MeshID, double>& p) { return p.second == 0.0; }),
track_segments.end());
for (const auto& track_segment : track_segments) {
bins.push_back(mesh_id_to_bin(track_segment.first));
lengths.push_back(track_segment.second * inv_length);
}
}
int XDGMesh::get_bin(Position r) const
{
xdg::Position p {r.x, r.y, r.z};
if (length_multiplier_ > 0.0) {
p /= length_multiplier_;
}
return mesh_id_to_bin(xdg_->find_element(p));
}
int XDGMesh::n_bins() const
{
return xdg_->mesh_manager()->num_volume_elements();
}
int XDGMesh::n_surface_bins() const
{
return n_surface_bins_;
}
std::pair<vector<double>, vector<double>> XDGMesh::plot(
Position plot_ll, Position plot_ur) const
{
fatal_error("Plot of XDGMesh mesh not implemented");
return {};
}
std::string XDGMesh::library() const
{
if (mesh_library_ == xdg::MeshLibrary::LIBMESH) {
return "libmesh";
} else if (mesh_library_ == xdg::MeshLibrary::MOAB) {
return "moab";
}
fatal_error("Invalid mesh library specified for XDGMesh.");
return {};
}
void XDGMesh::write(const std::string& base_filename) const
{
warning("XDGMesh mesh write from C++ not implemented");
}
Position XDGMesh::centroid(int bin) const
{
auto element_vertices =
xdg_->mesh_manager()->element_vertices(bin_to_mesh_id(bin));
xdg::Vertex center {0.0, 0.0, 0.0};
for (const auto& v : element_vertices) {
center += v;
}
center /= double(element_vertices.size());
if (length_multiplier_ > 0.0) {
center *= length_multiplier_;
}
return {center[0], center[1], center[2]};
}
int XDGMesh::n_vertices() const
{
return xdg_->mesh_manager()->num_vertices();
}
Position XDGMesh::vertex(int id) const
{
xdg::MeshID mesh_id = xdg_->mesh_manager()->vertex_id(id);
auto v = xdg_->mesh_manager()->vertex_coordinates(mesh_id);
if (length_multiplier_ > 0.0) {
v *= length_multiplier_;
}
return {v[0], v[1], v[2]};
}
std::vector<int> XDGMesh::connectivity(int id) const
{
auto conn = xdg_->mesh_manager()->element_connectivity(bin_to_mesh_id(id));
for (auto& c : conn) {
c = xdg_->mesh_manager()->vertex_index(c);
}
return conn;
}
double XDGMesh::volume(int bin) const
{
double vol = xdg_->mesh_manager()->element_volume(bin_to_mesh_id(bin));
if (length_multiplier_ > 0.0) {
vol *= length_multiplier_ * length_multiplier_ * length_multiplier_;
}
return vol;
}
xdg::MeshID XDGMesh::bin_to_mesh_id(int bin) const
{
return xdg_->mesh_manager()->element_id(bin);
}
int32_t XDGMesh::mesh_id_to_bin(xdg::MeshID id) const
{
if (id < 0)
return -1;
return xdg_->mesh_manager()->element_index(id);
}
} // namespace openmc
#endif // XDG