forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrangeGeoTestBase.cc
333 lines (293 loc) · 9.82 KB
/
OrangeGeoTestBase.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//----------------------------------*-C++-*----------------------------------//
// Copyright 2021-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/OrangeGeoTestBase.cc
//---------------------------------------------------------------------------//
#include "OrangeGeoTestBase.hh"
#include <fstream>
#include <sstream>
#include <utility>
#include "celeritas_config.h"
#include "corecel/ScopedLogStorer.hh"
#include "corecel/data/Ref.hh"
#include "corecel/io/Join.hh"
#include "corecel/io/Logger.hh"
#include "geocel/Types.hh"
#include "orange/OrangeInput.hh"
#include "orange/OrangeParams.hh"
#include "orange/detail/UniverseIndexer.hh"
#include "orange/surf/LocalSurfaceVisitor.hh"
#include "orange/surf/Sphere.hh"
#include "orange/surf/SurfaceIO.hh"
#include "TestMacros.hh"
namespace celeritas
{
namespace test
{
namespace
{
//---------------------------------------------------------------------------//
OrangeInput to_input(UnitInput u)
{
OrangeInput result;
result.universes.push_back(std::move(u));
result.tol = Tolerance<>::from_default();
return result;
}
//---------------------------------------------------------------------------//
} // namespace
//---------------------------------------------------------------------------//
/*!
* Convert a vector of senses to a string.
*/
std::string OrangeGeoTestBase::senses_to_string(Span<Sense const> senses)
{
std::ostringstream os;
os << '{' << join(senses.begin(), senses.end(), ' ', [](Sense s) {
return to_char(s);
}) << '}';
return os.str();
}
//---------------------------------------------------------------------------//
/*!
* Convert a string to a vector of senses.
*/
std::vector<Sense> OrangeGeoTestBase::string_to_senses(std::string const& s)
{
std::vector<Sense> result(s.size());
std::transform(s.begin(), s.end(), result.begin(), [](char c) {
CELER_EXPECT(c == '+' || c == '-');
return c == '+' ? Sense::outside : Sense::inside;
});
return result;
}
//---------------------------------------------------------------------------//
/*!
* Load `test/geocel/data/{filename}` GDML input using Geant4.
*/
void OrangeGeoTestBase::build_gdml_geometry(std::string const& filename)
{
CELER_EXPECT(!params_);
CELER_VALIDATE(CELERITAS_USE_GEANT4,
<< "Geant4 is not enabled so geometry cannot be loaded");
ScopedLogStorer scoped_log_{&celeritas::world_logger()};
params_
= std::make_unique<Params>(this->test_data_path("geocel", filename));
static std::string const expected_log_levels[] = {"info"};
EXPECT_VEC_EQ(expected_log_levels, scoped_log_.levels()) << scoped_log_;
ASSERT_TRUE(this->geometry());
}
//---------------------------------------------------------------------------//
/*!
* Load a geometry from the given JSON filename.
*/
void OrangeGeoTestBase::build_geometry(std::string const& filename)
{
CELER_EXPECT(!params_);
CELER_VALIDATE(CELERITAS_USE_JSON,
<< "JSON is not enabled so geometry cannot be loaded");
ScopedLogStorer scoped_log_{&celeritas::world_logger()};
params_
= std::make_unique<Params>(this->test_data_path("orange", filename));
static std::string const expected_log_levels[] = {"info"};
EXPECT_VEC_EQ(expected_log_levels, scoped_log_.levels());
ASSERT_TRUE(this->geometry());
}
//---------------------------------------------------------------------------//
/*!
* Construct a geometry with one infinite volume.
*/
void OrangeGeoTestBase::build_geometry(OneVolInput inp)
{
CELER_EXPECT(!params_);
UnitInput input;
input.label = "one volume";
// Fake bbox for sampling
input.bbox = {{-0.5, -0.5, -0.5}, {0.5, 0.5, 0.5}};
input.volumes = {[&inp] {
VolumeInput vi;
vi.logic = {logic::ltrue};
vi.flags = (inp.complex_tracking ? static_cast<logic_int>(
VolumeInput::Flags::internal_surfaces)
: 0);
vi.zorder = ZOrder::media;
vi.label = "infinite";
return vi;
}()};
return this->build_geometry(std::move(input));
}
//---------------------------------------------------------------------------//
/*!
* Construct a geometry with one infinite volume.
*/
void OrangeGeoTestBase::build_geometry(TwoVolInput inp)
{
CELER_EXPECT(!params_);
CELER_EXPECT(inp.radius > 0);
UnitInput input;
input.label = "two volumes";
input.bbox = {{-inp.radius, -inp.radius, -inp.radius},
{inp.radius, inp.radius, inp.radius}};
input.surfaces = {Sphere({0, 0, 0}, inp.radius)};
input.surface_labels = {Label("sphere")};
input.volumes = [&input] {
std::vector<VolumeInput> result;
VolumeInput vi;
vi.faces = {LocalSurfaceId{0}};
vi.zorder = ZOrder::media;
// Outside
vi.logic = {0};
vi.label = "outside";
vi.bbox = BBox::from_infinite();
result.push_back(vi);
// Inside
vi.logic = {0, logic::lnot};
vi.label = "inside";
vi.bbox = input.bbox;
result.push_back(vi);
return result;
}();
return this->build_geometry(std::move(input));
}
//---------------------------------------------------------------------------//
/*!
* Construct a geometry from a single global unit.
*/
void OrangeGeoTestBase::build_geometry(UnitInput input)
{
CELER_EXPECT(input);
params_ = std::make_unique<Params>(to_input(std::move(input)));
ASSERT_TRUE(this->geometry());
}
//---------------------------------------------------------------------------//
/*!
* Lazily create and get a single-serving host state.
*/
auto OrangeGeoTestBase::host_state() -> HostStateRef const&
{
CELER_EXPECT(params_);
if (!host_state_)
{
host_state_ = HostStateStore(this->host_params(), 1);
}
return host_state_.ref();
}
//---------------------------------------------------------------------------//
/*!
* Access the params data on the host.
*/
auto OrangeGeoTestBase::host_params() const -> HostParamsRef const&
{
CELER_EXPECT(params_);
return params_->host_ref();
}
//---------------------------------------------------------------------------//
/*!
* Print geometry description.
*
* This is just developer-oriented code until we get the full ORANGE metadata
* ported.
*/
void OrangeGeoTestBase::describe(std::ostream& os) const
{
CELER_EXPECT(params_);
// TODO: update when multiple units are in play
auto const& host_ref = this->host_params();
CELER_ASSERT(host_ref.simple_units.size() == 1);
LocalSurfaceVisitor visit{host_ref, SimpleUnitId{0}};
os << "# Surfaces\n";
// Loop over all surfaces and apply
for (auto id : range(LocalSurfaceId{this->params().num_surfaces()}))
{
os << " - " << this->id_to_label(UniverseId{0}, id) << "(" << id.get()
<< "): ";
visit([&os](auto const& surf) { os << surf; }, id);
os << '\n';
}
}
//---------------------------------------------------------------------------//
/*!
* Return the number of volumes.
*/
VolumeId::size_type OrangeGeoTestBase::num_volumes() const
{
CELER_EXPECT(params_);
return params_->num_volumes();
}
//---------------------------------------------------------------------------//
/*!
* Find the surface from its label (nullptr allowed)
*/
SurfaceId OrangeGeoTestBase::find_surface(std::string const& label) const
{
CELER_EXPECT(params_);
SurfaceId surface_id = params_->find_surface(label);
CELER_VALIDATE(surface_id,
<< "nonexistent surface label '" << label << '\'');
return surface_id;
}
//---------------------------------------------------------------------------//
/*!
* Find the volume from its label (nullptr allowed)
*/
VolumeId OrangeGeoTestBase::find_volume(std::string const& label) const
{
CELER_EXPECT(params_);
VolumeId volume_id = params_->find_volume(label);
CELER_VALIDATE(volume_id, << "nonexistent volume label '" << label << '\'');
return volume_id;
}
//---------------------------------------------------------------------------//
/*!
* Surface name (or sentinel if no surface).
*/
std::string
OrangeGeoTestBase::id_to_label(UniverseId uid, LocalSurfaceId surfid) const
{
if (!surfid)
return "[none]";
detail::UniverseIndexer ui(this->params().host_ref().universe_indexer_data);
return params_->id_to_label(ui.global_surface(uid, surfid)).name;
}
//---------------------------------------------------------------------------//
/*!
* Surface name (or sentinel if no surface) within UniverseId{0}.
*/
std::string OrangeGeoTestBase::id_to_label(LocalSurfaceId surfid) const
{
return this->id_to_label(UniverseId{0}, surfid);
}
//---------------------------------------------------------------------------//
/*!
* Volume name (or sentinel if no volume).
*/
std::string
OrangeGeoTestBase::id_to_label(UniverseId uid, LocalVolumeId volid) const
{
if (!volid)
return "[none]";
detail::UniverseIndexer ui(this->params().host_ref().universe_indexer_data);
return params_->id_to_label(ui.global_volume(uid, volid)).name;
}
//---------------------------------------------------------------------------//
/*!
* Volume name (or sentinel if no volume) within UniverseId{0}.
*/
std::string OrangeGeoTestBase::id_to_label(LocalVolumeId volid) const
{
return this->id_to_label(UniverseId{0}, volid);
}
//---------------------------------------------------------------------------//
/*!
* Return the geometry that was created.
*/
auto OrangeGeoTestBase::build_geometry() -> SPConstGeo
{
CELER_EXPECT(params_);
return params_;
}
//---------------------------------------------------------------------------//
} // namespace test
} // namespace celeritas