forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenericGeoTestBase.cc
156 lines (137 loc) · 5.08 KB
/
GenericGeoTestBase.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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023-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/GenericGeoTestBase.cc
//---------------------------------------------------------------------------//
#include "GenericGeoTestBase.hh"
#include "celeritas_config.h"
#if CELERITAS_USE_GEANT4
# include <G4LogicalVolume.hh>
# include <G4LogicalVolumeStore.hh>
# include "celeritas/ext/VisitGeantVolumes.hh"
#endif
#include "corecel/io/Repr.hh"
#include "corecel/math/ArrayUtils.hh"
#include "celeritas/ext/GeantGeoUtils.hh"
#include "CheckedGeoTrackView.hh"
using std::cout;
using namespace std::literals;
using GeantVolResult = celeritas::test::GenericGeoGeantImportVolumeResult;
namespace celeritas
{
namespace test
{
//---------------------------------------------------------------------------//
void GenericGeoTrackingResult::print_expected()
{
cout
<< "/*** ADD THE FOLLOWING UNIT TEST CODE ***/\n"
"static char const* const expected_volumes[] = "
<< repr(this->volumes)
<< ";\n"
"EXPECT_VEC_EQ(expected_volumes, result.volumes);\n"
"static real_type const expected_distances[] = "
<< repr(this->distances)
<< ";\n"
"EXPECT_VEC_SOFT_EQ(expected_distances, result.distances);\n"
"static real_type const expected_hw_safety[] = "
<< repr(this->halfway_safeties)
<< ";\n"
"EXPECT_VEC_SOFT_EQ(expected_hw_safety, result.halfway_safeties);\n"
"/*** END CODE ***/\n";
}
//---------------------------------------------------------------------------//
void GeantVolResult::print_expected() const
{
cout << "/*** ADD THE FOLLOWING UNIT TEST CODE ***/\n"
"static int const expected_volumes[] = "
<< repr(this->volumes)
<< ";\n"
"EXPECT_VEC_EQ(expected_volumes, result.volumes);\n"
"EXPECT_EQ(0, result.missing_names.size()) << "
"repr(result.missing_names);\n";
if (!this->missing_names.empty())
{
cout << "/* Currently missing: " << repr(this->missing_names)
<< " */\n";
}
cout << "/*** END CODE ***/\n";
}
//---------------------------------------------------------------------------//
GeantVolResult GeantVolResult::from_import(GeoParamsInterface const& geom,
G4VPhysicalVolume const* world)
{
CELER_VALIDATE(world, << "world volume is nullptr");
#if CELERITAS_USE_GEANT4
using Result = GenericGeoGeantImportVolumeResult;
G4LogicalVolumeStore* lv_store = G4LogicalVolumeStore::GetInstance();
CELER_ASSERT(lv_store);
Result result;
result.volumes.assign(lv_store->size(), Result::empty);
visit_geant_volumes(
[&result, &geom](G4LogicalVolume const& lv) {
// Add pointer as GDML writer does, to emulate accel/SharedParams
auto name = make_gdml_name(lv);
if (name.empty())
{
return;
}
auto i = static_cast<std::size_t>(lv.GetInstanceID());
CELER_ASSERT(i < result.volumes.size());
auto label = Label::from_geant(name);
auto id = geom.find_volume(label);
if (!id)
{
result.volumes[i] = Result::missing;
result.missing_names.push_back(to_string(label));
return;
}
result.volumes[i] = static_cast<int>(id.unchecked_get());
},
*world->GetLogicalVolume());
// Trim leading 'empty' values
auto first_nonempty = std::find_if(
result.volumes.begin(), result.volumes.end(), [](int i) {
return i != Result::empty;
});
result.volumes.erase(result.volumes.begin(), first_nonempty);
return result;
#else
CELER_DISCARD(geom);
CELER_NOT_CONFIGURED("Geant4");
#endif
}
//---------------------------------------------------------------------------//
GeantVolResult GeantVolResult::from_pointers(GeoParamsInterface const& geom,
G4VPhysicalVolume const* world)
{
CELER_VALIDATE(world, << "world volume is nullptr");
#if CELERITAS_USE_GEANT4
using Result = GenericGeoGeantImportVolumeResult;
Result result;
for (G4LogicalVolume* lv : celeritas::geant_logical_volumes())
{
if (!lv)
{
result.volumes.push_back(Result::empty);
continue;
}
auto id = geom.find_volume(lv);
result.volumes.push_back(id ? static_cast<int>(id.unchecked_get())
: Result::missing);
if (!id)
{
result.missing_names.push_back(lv->GetName());
}
}
return result;
#else
CELER_DISCARD(geom);
CELER_NOT_CONFIGURED("Geant4");
#endif
}
//---------------------------------------------------------------------------//
} // namespace test
} // namespace celeritas