Skip to content

Commit fd2cc16

Browse files
authored
Test nested parameterisation and feed back to caller (#44)
1 parent a676f20 commit fd2cc16

6 files changed

Lines changed: 56 additions & 6 deletions

File tree

src/G4VG.hh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,19 @@ struct Options
8181
g4pv->SetCopyNo(copy_no);
8282
}
8383
* \endcode
84+
*
85+
* For some applications (especially medical physics), a multi-level
86+
* parameterisation can be used to create a 3-D array of voxels. VecGeom only
87+
creates a single
88+
* volume placement in this case, but it returns a reference to the placed
89+
volume and the associated parameterisation.
8490
*/
8591
struct Converted
8692
{
8793
using VGPlacedVolume = vecgeom::VPlacedVolume;
8894
using VecLv = std::vector<G4LogicalVolume const*>;
8995
using VecPv = std::vector<G4VPhysicalVolume const*>;
96+
using PlacedVolumeId = unsigned int;
9097

9198
//! World pointer (host) corresponding to input Geant4 world
9299
VGPlacedVolume* world{nullptr};
@@ -95,6 +102,8 @@ struct Converted
95102
VecLv logical_volumes;
96103
//! Geant4 PVs indexed by VecGeom PlacedVolume ID
97104
VecPv physical_volumes;
105+
//! Encountered volumes that have unsupported nested parameterisations
106+
VecPv nested_pv;
98107
};
99108

100109
//---------------------------------------------------------------------------//

src/g4vg_impl/Converter.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <G4LogicalVolumeStore.hh>
1414
#include <G4ReflectionFactory.hh>
1515
#include <G4ReplicaNavigation.hh>
16+
#include <G4VNestedParameterisation.hh>
1617
#include <G4VPVParameterisation.hh>
1718
#include <G4VPhysicalVolume.hh>
1819
#include <VecGeom/management/ReflFactory.h>
@@ -270,6 +271,7 @@ auto Converter::operator()(arg_type g4world) -> result_type
270271
result.world = world_pv;
271272
result.logical_volumes = convert_lv_->make_volume_map();
272273
result.physical_volumes = std::move(placed_volumes_);
274+
result.nested_pv = std::move(nested_);
273275

274276
G4VG_ENSURE(result.world);
275277
G4VG_ENSURE(!result.logical_volumes.empty());
@@ -336,11 +338,24 @@ auto Converter::build_with_daughters(G4LogicalVolume const* mother_g4lv)
336338
case EVolume::kParameterised:
337339
// Place each paramterized instance of the daughter
338340
G4VG_ASSERT(g4pv->GetParameterisation());
341+
if (auto* nested = dynamic_cast<G4VNestedParameterisation*>(
342+
g4pv->GetParameterisation()))
343+
{
344+
G4VG_LOG(warning)
345+
<< "Encountered nested parameterisation '"
346+
<< TypeDemangler<G4VNestedParameterisation>{}(*nested)
347+
<< "' for physical volume '" << g4pv->GetName()
348+
<< "' (corresponding LV: "
349+
<< PrintableLV{g4pv->GetLogicalVolume()} << "): "
350+
<< "only one instance will be placed, and "
351+
"solid/material changes will be ignored";
352+
nested_.push_back(g4pv);
353+
}
339354
place_daughter(g4pv, ParamUpdater{g4pv->GetParameterisation()});
340355
break;
341356
default:
342357
G4VG_LOG(error)
343-
<< "Unsupported type '"
358+
<< "Unsupported custom placement type '"
344359
<< TypeDemangler<G4VPhysicalVolume>{}(*g4pv)
345360
<< "' for physical volume '" << g4pv->GetName()
346361
<< "' (corresponding LV: "

src/g4vg_impl/Converter.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Converter
5959
std::unique_ptr<LogicalVolumeConverter> convert_lv_;
6060
std::unordered_set<VGLogicalVolume const*> built_daughters_;
6161
VecPv placed_volumes_;
62+
result_type::VecPv nested_;
6263

6364
VGLogicalVolume* build_with_daughters(G4LogicalVolume const* mother_g4lv);
6465
};

test/Custom.test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,7 @@ G4VPhysicalVolume* NestedReplicaParametrizationTest::build_world()
306306

307307
TEST_F(NestedReplicaParametrizationTest, default_options)
308308
{
309-
auto result = this->run(Options{});
310-
result.print_ref();
309+
auto result = this->run(Options{.verbose = true});
311310

312311
TestResult ref;
313312
ref.lv_name = {
@@ -352,6 +351,7 @@ TEST_F(NestedReplicaParametrizationTest, default_options)
352351
1,
353352
0,
354353
};
354+
ref.nested = {"voxel:8"};
355355

356356
result.expect_eq(ref);
357357
}

test/TestBase.cc

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <G4LogicalVolume.hh>
1010
#include <G4Material.hh>
1111
#include <G4RunManager.hh>
12+
#include <G4VNestedParameterisation.hh>
1213
#include <VecGeom/management/GeoManager.h>
1314
#include <VecGeom/volumes/LogicalVolume.h>
1415
#include <VecGeom/volumes/PlacedVolume.h>
@@ -43,9 +44,12 @@ void TestResult::print_ref() const
4344
<< repr(pv_name)
4445
<< ";\n"
4546
"ref.copy_no = "
46-
<< repr(copy_no)
47-
<< ";\n"
48-
"result.expect_eq(ref);\n"
47+
<< repr(copy_no) << ";\n";
48+
if (!nested.empty())
49+
{
50+
cout << "ref.nested = " << repr(nested) << ";\n";
51+
}
52+
cout << "result.expect_eq(ref);\n"
4953
"/***** END REFERENCE RESULT *****/\n";
5054
// clang-format: on
5155
}
@@ -77,6 +81,7 @@ void TestResult::expect_eq(TestResult const& ref) const
7781
}
7882
EXPECT_EQ(pv_name, ref.pv_name) << repr(pv_name);
7983
EXPECT_EQ(copy_no, ref.copy_no) << repr(copy_no);
84+
EXPECT_EQ(nested, ref.nested) << repr(nested);
8085
}
8186

8287
//---------------------------------------------------------------------------//
@@ -202,6 +207,25 @@ void TestBase::run_impl(Options const& options, TestResult& result)
202207
// "stamped" instance of a replica/parameterised volume)
203208
result.copy_no.push_back(vgpv->GetCopyNo());
204209
}
210+
211+
// Process nested volume
212+
for (auto* g4pv : converted.nested_pv)
213+
{
214+
ASSERT_TRUE(g4pv);
215+
std::ostringstream os;
216+
217+
os << g4pv->GetName() << ':';
218+
if (auto* nested = dynamic_cast<G4VNestedParameterisation*>(
219+
g4pv->GetParameterisation()))
220+
{
221+
os << nested->GetNumberOfMaterials();
222+
}
223+
else
224+
{
225+
os << "ERROR";
226+
}
227+
result.nested.push_back(std::move(os).str());
228+
}
205229
}
206230

207231
//---------------------------------------------------------------------------//

test/TestBase.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct TestResult
2525
std::vector<double> solid_capacity;
2626
std::vector<std::string> pv_name;
2727
std::vector<int> copy_no;
28+
std::vector<std::string> nested;
2829

2930
void print_ref() const;
3031
void expect_eq(TestResult const& reference) const;

0 commit comments

Comments
 (0)