forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrangeParams.cc
281 lines (246 loc) · 8.91 KB
/
OrangeParams.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
//----------------------------------*-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/OrangeParams.cc
//---------------------------------------------------------------------------//
#include "OrangeParams.hh"
#include <fstream>
#include <initializer_list>
#include <numeric>
#include <utility>
#include <vector>
#include "celeritas_config.h"
#include "corecel/Assert.hh"
#include "corecel/OpaqueId.hh"
#include "corecel/cont/Range.hh"
#include "corecel/cont/VariantUtils.hh"
#include "corecel/data/Collection.hh"
#include "corecel/data/CollectionBuilder.hh"
#include "corecel/io/Logger.hh"
#include "corecel/io/ScopedTimeLog.hh"
#include "corecel/io/StringUtils.hh"
#include "geocel/BoundingBox.hh"
#include "OrangeData.hh" // IWYU pragma: associated
#include "OrangeInput.hh"
#include "OrangeTypes.hh"
#include "detail/DepthCalculator.hh"
#include "detail/RectArrayInserter.hh"
#include "detail/UnitInserter.hh"
#include "detail/UniverseInserter.hh"
#include "univ/detail/LogicStack.hh"
#if CELERITAS_USE_JSON
# include <nlohmann/json.hpp>
# include "OrangeInputIO.json.hh"
#endif
namespace celeritas
{
namespace
{
//---------------------------------------------------------------------------//
/*!
* Load a geometry from the given filename.
*/
OrangeInput input_from_json(std::string filename)
{
CELER_VALIDATE(CELERITAS_USE_JSON,
<< "JSON is not enabled so geometry cannot be loaded");
CELER_LOG(info) << "Loading ORANGE geometry from JSON at " << filename;
ScopedTimeLog scoped_time;
if (ends_with(filename, ".gdml"))
{
CELER_LOG(warning) << "Using ORANGE geometry with GDML suffix: trying "
"`.org.json` instead";
filename.erase(filename.end() - 5, filename.end());
filename += ".org.json";
}
else if (!ends_with(filename, ".json"))
{
CELER_LOG(warning) << "Expected '.json' extension for JSON input";
}
OrangeInput result;
#if CELERITAS_USE_JSON
std::ifstream infile(filename);
CELER_VALIDATE(infile,
<< "failed to open geometry at '" << filename << '\'');
// Use the `from_json` defined in OrangeInputIO.json to read the JSON input
nlohmann::json::parse(infile).get_to(result);
#endif
return result;
}
//---------------------------------------------------------------------------//
} // namespace
//---------------------------------------------------------------------------//
/*!
* Construct from a JSON file (if JSON is enabled).
*
* The JSON format is defined by the SCALE ORANGE exporter (not currently
* distributed).
*/
OrangeParams::OrangeParams(std::string const& json_filename)
: OrangeParams(input_from_json(json_filename))
{
}
//---------------------------------------------------------------------------//
/*!
* Construct in-memory from a Geant4 geometry (not implemented).
*
* Perhaps someday we'll implement in-memory translation...
*/
OrangeParams::OrangeParams(G4VPhysicalVolume const*)
{
CELER_NOT_IMPLEMENTED("Geant4->VecGeom geometry translation");
}
//---------------------------------------------------------------------------//
/*!
* Advanced usage: construct from explicit host data.
*
* Volume and surface labels must be unique for the time being.
*/
OrangeParams::OrangeParams(OrangeInput input)
{
CELER_VALIDATE(input, << "input geometry is incomplete");
CELER_VALIDATE(!input.universes.empty(),
<< "input geometry has no universes");
if (!input.tol)
{
input.tol = Tolerance<>::from_default();
}
// Create host data for construction, setting tolerances first
HostVal<OrangeParamsData> host_data;
host_data.scalars.tol = input.tol;
host_data.scalars.max_depth = detail::DepthCalculator{input.universes}();
// Insert all universes
{
std::vector<Label> universe_labels;
std::vector<Label> surface_labels;
std::vector<Label> volume_labels;
detail::UniverseInserter insert_universe_base{
&universe_labels, &surface_labels, &volume_labels, &host_data};
Overload insert_universe{
detail::UnitInserter{&insert_universe_base, &host_data},
detail::RectArrayInserter{&insert_universe_base, &host_data}};
for (auto const& u : input.universes)
{
std::visit(insert_universe, u);
}
univ_labels_ = LabelIdMultiMap<UniverseId>{std::move(universe_labels)};
surf_labels_ = LabelIdMultiMap<SurfaceId>{std::move(surface_labels)};
vol_labels_ = LabelIdMultiMap<VolumeId>{std::move(volume_labels)};
}
// Safety currently only works for the single-universe case
supports_safety_ = host_data.simple_units.size() == 1
&& host_data.simple_units[SimpleUnitId{0}].simple_safety;
CELER_VALIDATE(std::holds_alternative<UnitInput>(input.universes.front()),
<< "global universe is not a SimpleUnit");
bbox_ = std::get<UnitInput>(input.universes.front()).bbox;
// Update scalars *after* loading all units
CELER_VALIDATE(host_data.scalars.max_logic_depth
< detail::LogicStack::max_stack_depth(),
<< "input geometry has at least one volume with a "
"logic depth of"
<< host_data.scalars.max_logic_depth
<< " (surfaces are nested too deeply); but the logic "
"stack is limited to a depth of "
<< detail::LogicStack::max_stack_depth());
// Construct device values and device/host references
CELER_ASSERT(host_data);
data_ = CollectionMirror<OrangeParamsData>{std::move(host_data)};
CELER_ENSURE(data_);
CELER_ENSURE(vol_labels_.size() > 0);
CELER_ENSURE(bbox_);
}
//---------------------------------------------------------------------------//
/*!
* Get the label of a volume.
*/
Label const& OrangeParams::id_to_label(VolumeId vol) const
{
CELER_EXPECT(vol < vol_labels_.size());
return vol_labels_.get(vol);
}
//---------------------------------------------------------------------------//
/*!
* Locate the volume ID corresponding to a unique name.
*
* If the name isn't in the geometry, a null ID will be returned. If the name
* is not unique, a RuntimeError will be raised.
*/
VolumeId OrangeParams::find_volume(std::string const& name) const
{
auto result = vol_labels_.find_all(name);
if (result.empty())
return {};
CELER_VALIDATE(result.size() == 1,
<< "volume '" << name << "' is not unique");
return result.front();
}
//---------------------------------------------------------------------------//
/*!
* Locate the volume ID corresponding to a label.
*
* If the label isn't in the geometry, a null ID will be returned.
*/
VolumeId OrangeParams::find_volume(Label const& label) const
{
return vol_labels_.find(label);
}
//---------------------------------------------------------------------------//
/*!
* Get zero or more volume IDs corresponding to a name.
*
* This is useful for volumes that are repeated in the geometry with different
* uniquifying 'extensions'.
*/
auto OrangeParams::find_volumes(std::string const& name) const
-> SpanConstVolumeId
{
return vol_labels_.find_all(name);
}
//---------------------------------------------------------------------------//
/*!
* Get the label of a surface.
*/
Label const& OrangeParams::id_to_label(SurfaceId surf) const
{
CELER_EXPECT(surf < surf_labels_.size());
return surf_labels_.get(surf);
}
//---------------------------------------------------------------------------//
/*!
* Locate the surface ID corresponding to a label name.
*/
SurfaceId OrangeParams::find_surface(std::string const& name) const
{
auto result = surf_labels_.find_all(name);
if (result.empty())
return {};
CELER_VALIDATE(result.size() == 1,
<< "surface '" << name << "' is not unique");
return result.front();
}
//---------------------------------------------------------------------------//
/*!
* Get the label of a universe.
*/
Label const& OrangeParams::id_to_label(UniverseId univ) const
{
CELER_EXPECT(univ < univ_labels_.size());
return univ_labels_.get(univ);
}
//---------------------------------------------------------------------------//
/*!
* Locate the universe ID corresponding to a label name.
*/
UniverseId OrangeParams::find_universe(std::string const& name) const
{
auto result = univ_labels_.find_all(name);
if (result.empty())
return {};
CELER_VALIDATE(result.size() == 1,
<< "universe '" << name << "' is not unique");
return result.front();
}
//---------------------------------------------------------------------------//
} // namespace celeritas