BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSElement.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSAcceleratorModel.hh"
20#include "BDSDebug.hh"
21#include "BDSExtent.hh"
22#include "BDSElement.hh"
23#include "BDSException.hh"
24#include "BDSGeometryExternal.hh"
25#include "BDSGeometryFactory.hh"
26#include "BDSSDType.hh"
27
28#include "G4String.hh"
29#include "G4Types.hh"
30
31#include <iomanip>
32#include <set>
33#include <vector>
34
35class G4LogicalVolume;
36
37BDSElement::BDSElement(const G4String& nameIn,
38 G4double arcLengthIn,
39 G4double horizontalWidthIn,
40 const G4String& geometryIn,
41 G4double angleIn,
42 std::vector<G4String>* namedVacuumVolumesIn,
43 G4bool autoColourGeometryIn,
44 G4bool markAsCollimatorIn,
45 G4bool stripOuterVolumeIn):
46 BDSAcceleratorComponent(nameIn, arcLengthIn, angleIn, markAsCollimatorIn ? "element-collimator" : "element"),
47 horizontalWidth(horizontalWidthIn),
48 geometryFileName(geometryIn),
49 autoColourGeometry(autoColourGeometryIn),
50 markAsCollimator(markAsCollimatorIn),
51 stripOuterVolume(stripOuterVolumeIn),
52 geometry(nullptr)
53{
54 if (namedVacuumVolumesIn)
55 {namedVacuumVolumes = *namedVacuumVolumesIn;}
56}
57
59{
61 BuildContainerLogicalVolume(); // pure virtual provided by derived class
63 // we purposively don't attach vis attributes to the container here as it would overwrite ones from geometry loading
64}
65
67{
68 // The horizontalWidth here is a suggested horizontalWidth for the factory. Each sub-factory may treat this
69 // differently.
70 BDSSDType sensitivityToAttach = markAsCollimator ? BDSSDType::collimatorcomplete : BDSSDType::energydep;
71 // The field isn't constructed here. It's registered through the base class SetField method in the component
72 // factory and built and attached in the Initialise() method of the base class. However, before Initialise()
73 // is called, the member fieldInfo will be set. We pass it in here to make sure we don't reuse the same
74 // geometry when a different field is required.
76 geometryFileName,
77 nullptr,
78 autoColourGeometry,
80 horizontalWidth,
81 &namedVacuumVolumes,
82 true,
83 sensitivityToAttach,
84 BDSSDType::energydepvacuum,
85 stripOuterVolume,
87
88 if (!geometry)
89 {throw BDSException(__METHOD_NAME__, "Error loading geometry in component \"" + name + "\"");}
90
91 // We don't register the geometry as a daughter as the geometry factory retains
92 // ownership of the geometry and will clean it up at the end.
93
94 // make the beam pipe container, this object's container
95 containerLogicalVolume = geometry->GetContainerLogicalVolume();
96 containerSolid = geometry->GetContainerSolid();
97 containerAssembly = geometry->GetContainerAssemblyVolume();
99
100 // register named vacuum volumes that have been identified
102
103 if (markAsCollimator)
104 {// label volumes as belonging to a collimator
105 auto collimatorVolumeSet = BDSAcceleratorModel::Instance()->VolumeSet("collimators");
106 for (auto vol : GetAcceleratorMaterialLogicalVolumes())
107 {collimatorVolumeSet->insert(vol);}
108 }
109
110 // set placement offset from geom so it's placed correctly in the beam line
112
113 // update extents
115
116 const BDSExtent geomExtent = geometry->GetExtent();
117 BDSExtent nominalExt = BDSExtent(horizontalWidth*0.5, horizontalWidth*0.5, chordLength*0.5);
118 if (nominalExt.TransverselyGreaterThan(geomExtent))
119 {SetExtent(nominalExt);}
120
121 // check extent of geometry as compared to user input length of component in
122 // beam line. If longer (including some numerical tolerance), warn user
123 G4double extLength = GetExtent().DZ();
124 G4double tolerance = 1*CLHEP::micrometer;
125 if ((extLength - chordLength) > tolerance)
126 {
127 auto flagsCache(G4cerr.flags());
128 G4cerr << std::setprecision(15); // precise print out to aid user
129 G4cerr.setf( std::ios::fixed, std:: ios::floatfield );
130 G4cerr << "BDSElement> The loaded geometry is larger than the specified length"
131 << " of the element, which will cause overlaps!" << G4endl
132 << "Calculated extent along z of geometry: " << extLength << " mm" << G4endl;
133 G4cerr << "Arc length " << arcLength << " mm" << G4endl;
134 G4cerr << "Bending angle " << angle << " rad" << G4endl;
135 G4cerr << "Chord length " << chordLength << " mm" << G4endl;
136 G4cerr << "Chord length must be >= geometry length" << G4endl;
137 G4cerr << "Possible overlaps in element \"" << name << "\"" << G4endl << G4endl << G4endl;
138 // we don't force an exit here as our testing might not be exact for angled components
139 // for now, we leave it to the user to ensure this is acceptable
140 //throw BDSException(__METHOD_NAME__, "overlaps in element \"" + name + "\"");
141 G4cerr.flags(flagsCache);
142 }
143}
144
145std::set<G4VPhysicalVolume*> BDSElement::GetAllPhysicalVolumes() const
146{
147 return geometry ? geometry->GetAllPhysicalVolumes() : std::set<G4VPhysicalVolume*>();
148}
149
150std::set<G4RotationMatrix*> BDSElement::GetAllRotationMatrices() const
151{
152 return geometry ? geometry->GetAllRotationMatrices() : std::set<G4RotationMatrix*>();
153}
154
155std::set<G4VisAttributes*> BDSElement::GetAllVisAttributes() const
156{
157 return geometry ? geometry->GetAllVisAttributes() : std::set<G4VisAttributes*>();
158}
159
160std::set<G4UserLimits*> BDSElement::GetAllUserLimits() const
161{
162 return geometry ? geometry->GetAllUserLimits() : std::set<G4UserLimits*>();
163}
164
165std::set<BDSGeometryComponent*> BDSElement::GetAllDaughters() const
166{
167 return geometry ? geometry->GetAllDaughters() : std::set<BDSGeometryComponent*>();
168}
169
170std::set<G4VSolid*> BDSElement::GetAllSolids() const
171{
172 return geometry ? geometry->GetAllSolids() : std::set<G4VSolid*>();
173}
174
175std::set<G4LogicalVolume*> BDSElement::GetAllLogicalVolumes() const
176{
177 return geometry ? geometry->GetAllLogicalVolumes() : std::set<G4LogicalVolume*>();
178}
179
180std::set<G4LogicalVolume*> BDSElement::GetAllBiasingVolumes() const
181{
182 return geometry ? geometry->GetAllBiasingVolumes() : std::set<G4LogicalVolume*>();
183}
184
185std::map<G4LogicalVolume*, BDSSDType> BDSElement::GetAllSensitiveVolumes() const
186{
187 return geometry ? geometry->GetAllSensitiveVolumes() : std::map<G4LogicalVolume*, BDSSDType>();
188}
189
191{
192 if (geometry)
194}
195
197{
199 if (geometry)
201}
Abstract class that represents a component of an accelerator.
G4UserLimits * userLimits
Cache of user limits.
const G4double arcLength
Const protected member variable that may not be changed by derived classes.
void SetAcceleratorVacuumLogicalVolume(G4LogicalVolume *accVacLVIn)
const G4String name
Const protected member variable that may not be changed by derived classes.
G4double angle
Protected member variable that can be modified by derived classes.
virtual std::set< G4LogicalVolume * > GetAcceleratorMaterialLogicalVolumes() const
Return a set of logical volumes excluding the ones in the 'vacuum' set.
virtual void AttachUserLimits() const
Doesn't change member variables, but may change their contents.
G4double chordLength
Protected member variable that can be modified by derived classes.
std::set< G4LogicalVolume * > * VolumeSet(const G4String &name)
Returns pointer to a set of logical volumes. If no set by that name exits, create it.
virtual std::set< BDSGeometryComponent * > GetAllDaughters() const
Overloads of functions in BDSGeometryComponent.
Definition: BDSElement.cc:165
virtual std::set< G4VSolid * > GetAllSolids() const
Overloads of functions in BDSGeometryComponent.
Definition: BDSElement.cc:170
virtual void ExcludeLogicalVolumeFromBiasing(G4LogicalVolume *lv)
Overloads of functions in BDSGeometryComponent.
Definition: BDSElement.cc:190
BDSGeometryExternal * geometry
Cache of the constructed geometry. Used to forward onto various BDSGeometryComponent functions.
Definition: BDSElement.hh:94
virtual std::set< G4LogicalVolume * > GetAllBiasingVolumes() const
Overloads of functions in BDSGeometryComponent.
Definition: BDSElement.cc:180
virtual void Build()
Definition: BDSElement.cc:58
virtual void BuildContainerLogicalVolume()
This does the full construction. Loads the external geometry and field if there is one.
Definition: BDSElement.cc:66
virtual std::set< G4UserLimits * > GetAllUserLimits() const
Overloads of functions in BDSGeometryComponent.
Definition: BDSElement.cc:160
virtual void AttachSensitiveDetectors()
Overloads of functions in BDSGeometryComponent.
Definition: BDSElement.cc:196
virtual std::map< G4LogicalVolume *, BDSSDType > GetAllSensitiveVolumes() const
Overloads of functions in BDSGeometryComponent.
Definition: BDSElement.cc:185
virtual std::set< G4LogicalVolume * > GetAllLogicalVolumes() const
Overloads of functions in BDSGeometryComponent.
Definition: BDSElement.cc:175
virtual std::set< G4VisAttributes * > GetAllVisAttributes() const
Overloads of functions in BDSGeometryComponent.
Definition: BDSElement.cc:155
virtual std::set< G4RotationMatrix * > GetAllRotationMatrices() const
Overloads of functions in BDSGeometryComponent.
Definition: BDSElement.cc:150
virtual std::set< G4VPhysicalVolume * > GetAllPhysicalVolumes() const
Overloads of functions in BDSGeometryComponent.
Definition: BDSElement.cc:145
General exception with possible name of object and message.
Definition: BDSException.hh:35
Holder for +- extents in 3 dimensions.
Definition: BDSExtent.hh:39
G4bool TransverselyGreaterThan(const BDSExtent &r) const
Comparison operator for x,y only. Ignores z (length).
Definition: BDSExtent.hh:102
G4double DZ() const
The difference in a dimension.
Definition: BDSExtent.hh:85
virtual std::set< BDSGeometryComponent * > GetAllDaughters() const
Accessor - see member for more info.
G4LogicalVolume * GetContainerLogicalVolume() const
Accessor - see member for more info.
G4bool ContainerIsAssembly() const
Whether the container is an assembly. If not, it's a logical volume.
void InheritExtents(BDSGeometryComponent const *const anotherComponent)
Update the extents of this object with those of another object.
BDSExtent GetExtent() const
Accessor - see member for more info.
void SetPlacementOffset(const G4ThreeVector &offsetIn)
Set the offset from 0,0,0 that the object should ideally be placed in its parent.
G4AssemblyVolume * GetContainerAssemblyVolume() const
Accessor - see member for more info.
virtual std::set< G4RotationMatrix * > GetAllRotationMatrices() const
Accessor - see member for more info.
virtual void AttachSensitiveDetectors()
Attach a sensitive detector class to all registered sensitive volumes in this component.
virtual std::set< G4LogicalVolume * > GetAllBiasingVolumes() const
Return all logical volumes that should be used for biasing minus any that are in the excluded set.
virtual std::set< G4UserLimits * > GetAllUserLimits() const
Accessor - see member for more info.
virtual std::map< G4LogicalVolume *, BDSSDType > GetAllSensitiveVolumes() const
Access all sensitive volumes belonging to this component.
virtual std::set< G4LogicalVolume * > GetAllLogicalVolumes() const
Access all logical volumes belonging to this component.
virtual std::set< G4VisAttributes * > GetAllVisAttributes() const
Accessor - see member for more info.
virtual std::set< G4VSolid * > GetAllSolids() const
Accessor - see member for more info.
G4VSolid * GetContainerSolid() const
Accessor - see member for more info.
G4ThreeVector GetPlacementOffset() const
Accessor - see member for more info.
virtual void ExcludeLogicalVolumeFromBiasing(G4LogicalVolume *lv)
virtual std::set< G4VPhysicalVolume * > GetAllPhysicalVolumes() const
Accessor - see member for more info.
void SetExtent(const BDSExtent &extIn)
Set extent.
G4bool containerIsAssembly
True if the 'container' is really an assembly; false if an LV.
const std::set< G4LogicalVolume * > & VacuumVolumes() const
Access the vacuum volumes.
BDSGeometryExternal * BuildGeometry(G4String componentName, const G4String &formatAndFilePath, std::map< G4String, G4Colour * > *colourMapping=nullptr, G4bool autoColour=true, G4double suggestedLength=0, G4double suggestedHorizontalWidth=0, std::vector< G4String > *namedVacuumVolumes=nullptr, G4bool makeSensitive=true, BDSSDType sensitivityType=BDSSDType::energydep, BDSSDType vacuumSensitivityType=BDSSDType::energydepvacuum, G4bool stripOuterVolumeAndMakeAssembly=false, G4UserLimits *userLimitsToAttachToAllLVs=nullptr, G4bool dontReloadGeometry=false)
static BDSGeometryFactory * Instance()
Singleton accessor.
Improve type-safety of native enum data type in C++.