BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSParallelWorldUtilities.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSAcceleratorModel.hh"
19#include "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSGlobalConstants.hh"
22#include "BDSLinkDetectorConstruction.hh"
23#include "BDSParallelWorldCurvilinear.hh"
24#include "BDSParallelWorldCurvilinearBridge.hh"
25#include "BDSParallelWorldImportance.hh"
26#include "BDSParallelWorldInfo.hh"
27#include "BDSParallelWorldPlacementFields.hh"
28#include "BDSParallelWorldSampler.hh"
29#include "BDSParallelWorldUtilities.hh"
30#include "BDSParser.hh"
31#include "BDSSamplerType.hh"
32#include "BDSUtilities.hh"
33
34#include "parser/element.h"
35#include "parser/fastlist.h"
36#include "parser/placement.h"
37
38#include "G4GeometrySampler.hh"
39#include "G4ImportanceBiasing.hh"
40#include "G4IStore.hh"
41#include "G4ParallelWorldPhysics.hh"
42#include "G4VModularPhysicsList.hh"
43#include "G4VUserDetectorConstruction.hh"
44#include "G4VUserParallelWorld.hh"
45
46#include "globals.hh"
47
48#include <map>
49#include <vector>
50
51std::vector<BDSParallelWorldInfo> BDS::NumberOfExtraWorldsRequired()
52{
53 std::vector<BDSParallelWorldInfo> worlds;
54
55 const std::vector<GMAD::Placement> placements = BDSParser::Instance()->GetPlacements();
56 for (const auto& pl : placements)
57 {// loop over all placement definitions and see if any are beam line placements
58 if (!pl.sequence.empty()) // it's a beam line placement
59 {// curvilinear world required at least
60 G4bool samplerWorldRequired = false;
61 for (const auto& el : BDSParser::Instance()->GetSequence(pl.sequence))
62 {// do any elements require a sampler -> sampler world required
63 BDSSamplerType sType = BDS::DetermineSamplerType(el.samplerType);
64 if (sType != BDSSamplerType::none)
65 {
66 samplerWorldRequired = true;
67 break; // no need to loop over rest of sequence
68 }
69 }
70 BDSParallelWorldInfo info(pl.sequence, true, samplerWorldRequired, pl.name);
71 worlds.push_back(info);
72 }
73 }
74 return worlds;
75}
76
77std::vector<G4VUserParallelWorld*> BDS::ConstructAndRegisterParallelWorlds(G4VUserDetectorConstruction* massWorld,
78 G4bool buildSamplerWorld,
79 G4bool buildPlacementFieldsWorld)
80{
81 BDSAcceleratorModel* acceleratorModel = BDSAcceleratorModel::Instance();
82
83 // registry of all created worlds that require the physics process so
84 // that their boundaries affect tracking
85 std::vector<G4VUserParallelWorld*> worldsRequiringPhysics;
86
87 // standard worlds
88 if (buildSamplerWorld) // optional
89 {
90 auto samplerWorld = new BDSParallelWorldSampler("main");
91 massWorld->RegisterParallelWorld(samplerWorld);
92 auto massWorldBDS = dynamic_cast<BDSLinkDetectorConstruction*>(massWorld);
93 if (massWorldBDS)
94 {
95 G4int samplerWorldID = massWorld->GetNumberOfParallelWorld() - 1;
96 massWorldBDS->SetSamplerWorldID(samplerWorldID);
97 }
98 acceleratorModel->RegisterParallelWorld(samplerWorld);
99 worldsRequiringPhysics.push_back(dynamic_cast<G4VUserParallelWorld*>(samplerWorld));
100 }
101
102 auto curvilinearWorld = new BDSParallelWorldCurvilinear("main");
103 auto curvilinearBridgeWorld = new BDSParallelWorldCurvilinearBridge("main");
104 massWorld->RegisterParallelWorld(curvilinearWorld);
105 massWorld->RegisterParallelWorld(curvilinearBridgeWorld);
106
107 // G4VUserDetectorConstruction doesn't delete parallel worlds so we should
108 acceleratorModel->RegisterParallelWorld(curvilinearWorld);
109 acceleratorModel->RegisterParallelWorld(curvilinearBridgeWorld);
110
111 // extra worlds for additional beam line placements
112 std::vector<BDSParallelWorldInfo> worldInfos = BDS::NumberOfExtraWorldsRequired();
113
114 for (const auto& info : worldInfos)
115 {
116 if (info.curvilinearWorld)
117 {
118 auto cLWorld = new BDSParallelWorldCurvilinear(info.curvilinearWorldName);
119 auto cLBridgeWorld = new BDSParallelWorldCurvilinearBridge(info.curvilinearWorldName);
120 massWorld->RegisterParallelWorld(cLWorld);
121 massWorld->RegisterParallelWorld(cLBridgeWorld);
122 acceleratorModel->RegisterParallelWorld(cLWorld);
123 acceleratorModel->RegisterParallelWorld(cLBridgeWorld);
124 }
125 if (info.samplerWorld)
126 {
127 auto sWorld = new BDSParallelWorldSampler(info.sequenceName);
128 acceleratorModel->RegisterParallelWorld(sWorld);
129 worldsRequiringPhysics.push_back(dynamic_cast<G4VUserParallelWorld*>(sWorld));
130 massWorld->RegisterParallelWorld(sWorld);
131 }
132 }
133
134 // only create the importance parallel world if the file is specified
135 if (BDSGlobalConstants::Instance()->UseImportanceSampling())
136 {
137 G4String importanceWorldGeometryFile = BDSGlobalConstants::Instance()->ImportanceWorldGeometryFile();
138 G4String importanceVolumeMapFile = BDSGlobalConstants::Instance()->ImportanceVolumeMapFile();
139 auto importanceWorld = new BDSParallelWorldImportance("main",
140 importanceWorldGeometryFile,
141 importanceVolumeMapFile);
142 acceleratorModel->RegisterParallelWorld(importanceWorld);
143 massWorld->RegisterParallelWorld(importanceWorld);
144 worldsRequiringPhysics.push_back(dynamic_cast<G4VUserParallelWorld*>(importanceWorld));
145 }
146
147 // optional parallel world for coordinate transforms for fields attached to placements of geometry
148 if (buildPlacementFieldsWorld)
149 {
150 auto placementFieldsPW = new BDSParallelWorldPlacementFields("placement_fields");
151 acceleratorModel->RegisterParallelWorld(placementFieldsPW);
152 massWorld->RegisterParallelWorld(placementFieldsPW);
153 }
154
155 return worldsRequiringPhysics;
156}
157
158std::vector<G4ParallelWorldPhysics*> BDS::ConstructParallelWorldPhysics(const std::vector<G4VUserParallelWorld*>& worlds)
159{
160 std::vector<G4ParallelWorldPhysics*> result;
161 for (auto world : worlds)
162 {result.push_back(new G4ParallelWorldPhysics(world->GetName()));}
163 return result;
164}
165
166void BDS::RegisterSamplerPhysics(const std::vector<G4ParallelWorldPhysics*>& processes,
167 G4VModularPhysicsList* physicsList)
168{
169 for (auto process : processes)
170 {physicsList->RegisterPhysics(process);}
171}
172
173void BDS::AddIStore(const std::vector<G4VUserParallelWorld*>& worlds)
174 {
176 //only add importance store if the world exists
177 if (importanceWorld)
178 {importanceWorld->AddIStore();}
179 else
180 {throw BDSException(__METHOD_NAME__, "Importance sampling world not found.");}
181 }
182
183void BDS::RegisterImportanceBiasing(const std::vector<G4VUserParallelWorld*>& worlds,
184 G4VModularPhysicsList* physList)
185{
187
188 // create world geometry sampler
189 G4GeometrySampler* pgs = new G4GeometrySampler(importanceWorld->GetWorldVolume(), "neutron");
190 pgs->SetParallel(true);
191 physList->RegisterPhysics(new G4ImportanceBiasing(pgs,importanceWorld->GetName()));
192}
193
194BDSParallelWorldImportance* BDS::GetImportanceSamplingWorld(const std::vector<G4VUserParallelWorld*>& worlds)
195{
196 // get importance world
197 G4String importanceWorldName = "importanceWorld_main";
198 G4VUserParallelWorld* importanceWorld = nullptr;
199 for (auto world : worlds)
200 {
201 if (std::strcmp(world->GetName(),importanceWorldName) == 0)
202 {importanceWorld = world; break;}
203 }
204 BDSParallelWorldImportance* iworld = dynamic_cast<BDSParallelWorldImportance*>(importanceWorld);
205 return iworld;
206}
A holder class for all representations of the accelerator model created in BDSIM.
General exception with possible name of object and message.
static BDSGlobalConstants * Instance()
Access method.
Construction of the geometry in the case of a link model.
A parallel world for bridiging curvilinear volumes.
A parallel world for curvilinear coordinates.
Class that constructs a parallel importance world.
void AddIStore()
Create IStore for all importance sampling geometry cells.
G4VPhysicalVolume * GetWorldVolume()
World volume getter required in parallel world utilities.
A simple class to hold what parallel geometry is required for each sequence.
A parallel world for placement field transform shapes.
A parallel world for sampler planes.
const GMAD::FastList< GMAD::Element > & GetSequence(const std::string &name)
Return sequence.
Definition BDSParser.hh:81
static BDSParser * Instance()
Access method.
Definition BDSParser.cc:29
std::vector< GMAD::Placement > GetPlacements() const
Return the parser list of that object.
Definition BDSParser.hh:106
std::vector< G4ParallelWorldPhysics * > ConstructParallelWorldPhysics(const std::vector< G4VUserParallelWorld * > &worlds)
Construct the parallel physics process for each sampler world.
void RegisterSamplerPhysics(const std::vector< G4ParallelWorldPhysics * > &processes, G4VModularPhysicsList *physicsList)
Register each parallel physics process to the main physics list.
std::vector< BDSParallelWorldInfo > NumberOfExtraWorldsRequired()
void RegisterImportanceBiasing(const std::vector< G4VUserParallelWorld * > &worlds, G4VModularPhysicsList *physicsList)
Create importance geometry sampler and register importance biasing with physics list.
std::vector< G4VUserParallelWorld * > ConstructAndRegisterParallelWorlds(G4VUserDetectorConstruction *massWorld, G4bool buildSamplerWorld, G4bool buildPlacementFieldsWorld)
BDSParallelWorldImportance * GetImportanceSamplingWorld(const std::vector< G4VUserParallelWorld * > &worlds)
Get importance sampling world from list of all parallel worlds.
void AddIStore(const std::vector< G4VUserParallelWorld * > &worlds)
Get store, and prepare importance sampling for importance geometry sampler.