forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeantTestBase.cc
258 lines (233 loc) · 8.92 KB
/
GeantTestBase.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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022-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 celeritas/GeantTestBase.cc
//---------------------------------------------------------------------------//
#include "GeantTestBase.hh"
#include <string>
#include "celeritas_cmake_strings.h"
#include "celeritas_config.h"
#include "corecel/io/Logger.hh"
#include "celeritas/em/UrbanMscParams.hh"
#include "celeritas/ext/GeantImporter.hh"
#include "celeritas/ext/GeantPhysicsOptions.hh"
#include "celeritas/ext/GeantSetup.hh"
#include "celeritas/ext/ScopedGeantExceptionHandler.hh"
#include "celeritas/global/ActionRegistry.hh"
#include "celeritas/global/alongstep/AlongStepGeneralLinearAction.hh"
#include "celeritas/io/ImportData.hh"
#include "celeritas/track/TrackInitParams.hh"
#if CELERITAS_CORE_GEO != CELERITAS_CORE_GEO_ORANGE
# include "celeritas/geo/GeoParams.hh"
#endif
namespace celeritas
{
namespace test
{
//---------------------------------------------------------------------------//
namespace
{
//---------------------------------------------------------------------------//
//! Test for equality of two C strings
bool cstring_equal(char const* lhs, char const* rhs)
{
return std::strcmp(lhs, rhs) == 0;
}
//---------------------------------------------------------------------------//
} // namespace
struct GeantTestBase::ImportHelper
{
// NOTE: the import function must be static for now so that Vecgeom or
// other clients can access Geant4 after importing the data.
std::unique_ptr<GeantImporter> import;
std::string geometry_basename{};
GeantPhysicsOptions options{};
GeantImportDataSelection selection{};
std::unique_ptr<ScopedGeantExceptionHandler> scoped_exceptions;
ImportData imported;
};
class GeantTestBase::CleanupGeantEnvironment : public ::testing::Environment
{
public:
void SetUp() override {}
void TearDown() override
{
ImportHelper& i = GeantTestBase::import_helper();
i = {};
}
};
//---------------------------------------------------------------------------//
//! Whether Geant4 dependencies match those on the CI build
bool GeantTestBase::is_ci_build()
{
return CELERITAS_REAL_TYPE == CELERITAS_REAL_TYPE_DOUBLE
&& CELERITAS_CORE_GEO != CELERITAS_CORE_GEO_GEANT4
&& CELERITAS_UNITS == CELERITAS_UNITS_CGS
&& cstring_equal(celeritas_core_rng, "xorwow")
&& (cstring_equal(celeritas_clhep_version, "2.4.6.0")
|| cstring_equal(celeritas_clhep_version, "2.4.6.4"))
&& (cstring_equal(celeritas_geant4_version, "11.0.3")
|| cstring_equal(celeritas_geant4_version, "11.0.4"));
}
//---------------------------------------------------------------------------//
//! Whether Geant4 dependencies match those on Wildstyle
bool GeantTestBase::is_wildstyle_build()
{
return GeantTestBase::is_ci_build();
}
//---------------------------------------------------------------------------//
//! Whether Geant4 dependencies match those on Summit
bool GeantTestBase::is_summit_build()
{
return GeantTestBase::is_ci_build();
}
//---------------------------------------------------------------------------//
//! Get the Geant4 top-level geometry element (immutable)
G4VPhysicalVolume const* GeantTestBase::get_world_volume() const
{
return GeantImporter::get_world_volume();
}
//---------------------------------------------------------------------------//
//! Get the Geant4 top-level geometry element
G4VPhysicalVolume const* GeantTestBase::get_world_volume()
{
// Load geometry
this->imported_data();
return const_cast<GeantTestBase const*>(this)->get_world_volume();
}
//---------------------------------------------------------------------------//
// PROTECTED MEMBER FUNCTIONS
//---------------------------------------------------------------------------//
auto GeantTestBase::build_geant_options() const -> GeantPhysicsOptions
{
GeantPhysicsOptions options;
options.em_bins_per_decade = 14;
options.rayleigh_scattering = false;
return options;
}
//---------------------------------------------------------------------------//
auto GeantTestBase::build_init() -> SPConstTrackInit
{
TrackInitParams::Input input;
input.capacity = 4096 * 2;
input.max_events = 4096;
input.track_order = TrackOrder::unsorted;
return std::make_shared<TrackInitParams>(input);
}
//---------------------------------------------------------------------------//
auto GeantTestBase::build_along_step() -> SPConstAction
{
auto& action_reg = *this->action_reg();
auto msc = UrbanMscParams::from_import(
*this->particle(), *this->material(), this->imported_data());
auto result = AlongStepGeneralLinearAction::from_params(
action_reg.next_id(),
*this->material(),
*this->particle(),
msc,
this->imported_data().em_params.energy_loss_fluct);
CELER_ASSERT(result);
CELER_ASSERT(result->has_fluct()
== this->build_geant_options().eloss_fluctuation);
CELER_ASSERT(
result->has_msc()
== (this->build_geant_options().msc != MscModelSelection::none));
action_reg.insert(result);
return result;
}
//---------------------------------------------------------------------------//
auto GeantTestBase::build_fresh_geometry(std::string_view filename)
-> SPConstGeoI
{
#if CELERITAS_CORE_GEO == CELERITAS_CORE_GEO_ORANGE
// Load fake version of geometry
return Base::build_fresh_geometry(filename);
#else
// Import geometry from Geant4
CELER_LOG(info) << "Importing Geant4 geometry instead of loading from "
<< filename;
auto* world = this->get_world_volume();
CELER_EXPECT(world);
return std::make_shared<GeoParams>(world);
#endif
}
//---------------------------------------------------------------------------//
// Lazily set up and load geant4
auto GeantTestBase::imported_data() const -> ImportData const&
{
GeantPhysicsOptions opts = this->build_geant_options();
GeantImportDataSelection sel = this->build_import_data_selection();
ImportHelper& i = GeantTestBase::import_helper();
if (!i.import)
{
i.geometry_basename = this->geometry_basename();
i.options = opts;
std::string gdml_inp = this->test_data_path(
"celeritas", (i.geometry_basename + ".gdml").c_str());
i.import = std::make_unique<GeantImporter>(
GeantSetup{gdml_inp.c_str(), i.options});
i.scoped_exceptions = std::make_unique<ScopedGeantExceptionHandler>();
i.imported = (*i.import)(sel);
i.options.verbose = false;
}
else
{
// Verbosity change is allowable
opts.verbose = false;
static char const explanation[]
= " (Geant4 cannot be set up twice in one execution: see issue "
"#462)";
CELER_VALIDATE(this->geometry_basename() == i.geometry_basename,
<< "cannot load new geometry '"
<< this->geometry_basename() << "' when another '"
<< i.geometry_basename << "' was already set up"
<< explanation);
CELER_VALIDATE(opts == i.options,
<< "cannot change physics options after "
<< explanation);
if (sel != i.selection)
{
// Reload with new selection
i.imported = (*i.import)(sel);
i.selection = sel;
}
}
return i.imported;
}
//---------------------------------------------------------------------------//
GeantImportDataSelection GeantTestBase::build_import_data_selection() const
{
return {};
}
//---------------------------------------------------------------------------//
auto GeantTestBase::import_helper() -> ImportHelper&
{
static bool registered_cleanup = false;
if (!registered_cleanup)
{
/*! Always reset Geant4 at end of testing before global destructors.
*
* This is needed because Geant4 is filled with static data, so we must
* destroy our references before it gets cleaned up.
*/
CELER_LOG(debug) << "Registering CleanupGeoEnvironment";
::testing::AddGlobalTestEnvironment(new CleanupGeantEnvironment());
registered_cleanup = true;
}
// Delayed initialization
static ImportHelper i;
return i;
}
//---------------------------------------------------------------------------//
std::ostream& operator<<(std::ostream& os, PrintableBuildConf const&)
{
os << "RNG=\"" << celeritas_core_rng << "\", CLHEP=\""
<< celeritas_clhep_version << "\", Geant4=\""
<< celeritas_geant4_version << '"';
return os;
}
//---------------------------------------------------------------------------//
} // namespace test
} // namespace celeritas