forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsgTestUtils.cc
252 lines (232 loc) · 7.64 KB
/
CsgTestUtils.cc
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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file orange/orangeinp/CsgTestUtils.cc
//---------------------------------------------------------------------------//
#include "CsgTestUtils.hh"
#include <iomanip>
#include <sstream>
#include <variant>
#include "celeritas_config.h"
#include "corecel/io/Join.hh"
#include "corecel/io/Repr.hh"
#include "orange/orangeinp/CsgTree.hh"
#include "orange/orangeinp/detail/ConvexSurfaceState.hh"
#include "orange/orangeinp/detail/CsgUnit.hh"
#include "orange/surf/SurfaceIO.hh"
#include "orange/transform/TransformIO.hh"
#if CELERITAS_USE_JSON
# include <nlohmann/json.hpp>
# include "orange/orangeinp/CsgTreeIO.json.hh"
#endif
namespace celeritas
{
namespace orangeinp
{
namespace test
{
//---------------------------------------------------------------------------//
std::string to_json_string(CsgTree const& tree)
{
#if CELERITAS_USE_JSON
nlohmann::json obj{tree};
return obj.dump();
#else
CELER_DISCARD(tree);
return {};
#endif
}
//---------------------------------------------------------------------------//
} // namespace test
namespace detail
{
namespace test
{
//---------------------------------------------------------------------------//
std::vector<int> to_vec_int(std::vector<NodeId> const& nodes)
{
std::vector<int> result;
for (auto nid : nodes)
{
result.push_back(nid ? nid.unchecked_get() : -1);
}
return result;
}
//---------------------------------------------------------------------------//
std::vector<std::string> surface_strings(CsgUnit const& u)
{
std::vector<std::string> result;
for (auto const& vs : u.surfaces)
{
result.push_back(std::visit(
[](auto&& surf) {
std::ostringstream os;
os << std::setprecision(5) << surf;
return os.str();
},
vs));
}
return result;
}
//---------------------------------------------------------------------------//
std::string tree_string(CsgUnit const& u)
{
return ::celeritas::orangeinp::test::to_json_string(u.tree);
}
//---------------------------------------------------------------------------//
std::vector<std::string> md_strings(CsgUnit const& u)
{
std::vector<std::string> result;
for (auto const& md_set : u.metadata)
{
result.push_back(to_string(join(md_set.begin(), md_set.end(), ',')));
}
return result;
}
//---------------------------------------------------------------------------//
std::vector<real_type> flattened_bboxes(CsgUnit const& u)
{
std::vector<real_type> result;
for (auto const& bbox : u.bboxes)
{
result.insert(result.end(), bbox.lower().begin(), bbox.lower().end());
result.insert(result.end(), bbox.upper().begin(), bbox.upper().end());
}
return result;
}
//---------------------------------------------------------------------------//
std::vector<int> volume_nodes(CsgUnit const& u)
{
std::vector<int> result;
for (auto nid : u.volumes)
{
result.push_back(nid ? nid.unchecked_get() : -1);
}
return result;
}
//---------------------------------------------------------------------------//
std::vector<std::string> fill_strings(CsgUnit const& u)
{
std::vector<std::string> result;
for (auto const& f : u.fills)
{
if (std::holds_alternative<std::monostate>(f))
{
result.push_back("<UNASSIGNED>");
}
else if (auto* mid = std::get_if<MaterialId>(&f))
{
result.push_back("m" + std::to_string(mid->unchecked_get()));
}
else if (auto* d = std::get_if<Daughter>(&f))
{
std::ostringstream os;
os << '{';
if (auto u = d->universe_id)
{
os << u.unchecked_get();
}
else
{
os << "<MISSING>";
}
os << ',';
if (auto t = d->transform_id)
{
if (t < u.transforms.size())
{
std::visit([&os](auto&& transform) { os << transform; },
u.transforms[t.unchecked_get()]);
}
else
{
os << "<INVALID: " << t.unchecked_get() << ">";
}
}
else
{
os << "<MISSING>";
}
result.push_back(os.str());
}
}
return result;
}
//---------------------------------------------------------------------------//
std::vector<real_type> flattened(BoundingZone const& bz)
{
std::vector<real_type> result;
for (auto const* bb : {&bz.interior, &bz.exterior})
{
result.insert(result.end(), bb->lower().begin(), bb->lower().end());
result.insert(result.end(), bb->upper().begin(), bb->upper().end());
}
result.push_back(bz.negated ? -1 : 1);
return result;
}
//---------------------------------------------------------------------------//
void print_expected(CsgUnit const& u)
{
std::cout << R"cpp(
/***** EXPECTED UNIT *****/
)cpp"
<< "static char const * const expected_surface_strings[] = "
<< repr(surface_strings(u)) << ";\n"
<< "static char const * const expected_md_strings[] = "
<< repr(md_strings(u)) << ";\n"
<< "static real_type const expected_flattened_bboxes[] = "
<< repr(flattened_bboxes(u)) << ";\n"
<< "static int const expected_volume_nodes[] = "
<< repr(volume_nodes(u)) << ";\n"
<< "static char const * const expected_fill_strings[] = "
<< repr(fill_strings(u)) << ";\n"
<< "static char const expected_tree_string[] = R\"json("
<< tree_string(u) << ")json\";\n"
<< R"cpp(
EXPECT_VEC_EQ(expected_surface_strings, surface_strings(u));
EXPECT_VEC_EQ(expected_md_strings, md_strings(u));
EXPECT_VEC_SOFT_EQ(expected_flattened_bboxes, flattened_bboxes(u));
EXPECT_VEC_EQ(expected_volume_nodes, volume_nodes(u));
EXPECT_VEC_EQ(expected_fill_strings, fill_strings(u));
if (CELERITAS_USE_JSON)
{
EXPECT_JSON_EQ(expected_tree_string, tree_string(u));
}
)cpp"
<< "EXPECT_EQ(NodeId{";
if (u.exterior)
{
std::cout << u.exterior.unchecked_get();
}
std::cout << "}, u.exterior);\n"
<< "/*************************/\n"
<< std::endl;
}
//---------------------------------------------------------------------------//
void print_expected(ConvexSurfaceState const& css)
{
std::cout << R"cpp(
/***** EXPECTED STATE *****/
// clang-format off
)cpp"
<< "static real_type const expected_local_bz[] = "
<< repr(flattened(css.local_bzone)) << ";\n"
<< "static real_type const expected_global_bz[] = "
<< repr(flattened(css.global_bzone)) << ";\n"
<< "static int const expected_nodes[] = "
<< repr(to_vec_int(css.nodes)) << ";"
<< R"cpp(
// clang-format on
EXPECT_VEC_SOFT_EQ(expected_local_bz, flattened(css.local_bzone));
EXPECT_VEC_SOFT_EQ(expected_global_bz, flattened(css.global_bzone));
EXPECT_VEC_EQ(expected_nodes, to_vec_int(css.nodes));
/*************************/
)cpp" << std::endl;
}
//---------------------------------------------------------------------------//
} // namespace test
} // namespace detail
} // namespace orangeinp
} // namespace celeritas