BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSCurvilinearFactory.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 "BDSCurvilinearFactory.hh"
19#include "BDSExtent.hh"
20#include "BDSGlobalConstants.hh"
21#include "BDSSimpleComponent.hh"
22#include "BDSTiltOffset.hh"
23#include "BDSUtilities.hh"
24
25#include "globals.hh"
26#include "G4CutTubs.hh"
27#include "G4LogicalVolume.hh"
28#include "G4ThreeVector.hh"
29#include "G4Tubs.hh"
30#include "G4VSolid.hh"
31
32#include "CLHEP/Units/SystemOfUnits.h"
33
34#include <cmath>
35
36BDSCurvilinearFactory::BDSCurvilinearFactory():
37 lengthSafety(BDSGlobalConstants::Instance()->LengthSafety())
38{;}
39
40BDSCurvilinearFactory::~BDSCurvilinearFactory()
41{;}
42
44 G4double chordLength,
45 G4double radius)
46{
47 G4double halfLength = chordLength * 0.5 - lengthSafety;
48 G4Tubs* solid = new G4Tubs(name + "_solid", // name
49 0, // inner radius
50 radius, // outer radius
51 halfLength, // z half width
52 0, // start angle
53 CLHEP::twopi); // sweep angle
54
55 G4ThreeVector inputFaceNormal = G4ThreeVector(0, 0,-1);
56 G4ThreeVector outputFaceNormal = G4ThreeVector(0, 0, 1);
57
58 return CommonConstruction(name, chordLength, chordLength, radius,
59 solid, 0);
60}
61
63 const G4double arcLength,
64 const G4double chordLength,
65 const G4double radius,
66 const G4double angle,
67 const G4ThreeVector& inputFaceNormal,
68 const G4ThreeVector& outputFaceNormal,
69 const BDSTiltOffset* tiltOffset)
70{
71 // angle is finite!
72 // factor of 0.8 here is arbitrary tolerance as g4 cut tubs seems to fail
73 // with cutting entrance / exit planes close to limit.
74 // s = r*theta -> r = s/theta
75 G4double radiusFromAngleLength = radius;
76 if (BDS::IsFinite(angle)) // it could be 0
77 {radiusFromAngleLength = std::abs(chordLength / angle) * 0.8;}
78 G4double radiusLocal = std::min(radius, radiusFromAngleLength);
79
80 // copy in case we need to modify in the case of tilt offset
81 G4ThreeVector inputface = inputFaceNormal;
82 G4ThreeVector outputface = outputFaceNormal;
83
84 if (tiltOffset)
85 {// could be nullptr
86 G4double tilt = tiltOffset->GetTilt();
87 if (BDS::IsFinite(tilt))
88 {// rotate normal faces
89 inputface = inputface.rotateZ(tilt);
90 outputface = outputface.rotateZ(tilt);
91 }
92 }
93
94 G4double halfLength = chordLength * 0.5 - lengthSafety;
95 G4CutTubs* solid = new G4CutTubs(name + "_solid", // name
96 0, // inner radius
97 radiusLocal, // outer radius
98 halfLength, // half length (z)
99 0, // rotation start angle
100 CLHEP::twopi, // rotation sweep angle
101 inputface, // input face normal vector
102 outputface); // output face normal vector
103
104 return CommonConstruction(name, arcLength, chordLength, radiusLocal, solid, angle);
105}
106
108 G4double arcLength,
109 G4double chordLength,
110 G4double radius,
111 G4double angle,
112 const BDSTiltOffset* tiltOffset)
113{
114 std::pair<G4ThreeVector,G4ThreeVector> faces = BDS::CalculateFaces(-0.5*angle, -0.5*angle);
115 G4ThreeVector inputFaceNormal = faces.first;
116 G4ThreeVector outputFaceNormal = faces.second;
117
118 return CreateCurvilinearVolume(name, arcLength, chordLength, radius, angle, inputFaceNormal, outputFaceNormal, tiltOffset);
119}
120
122 G4double arcLength,
123 G4double chordLength,
124 G4double radius,
125 G4VSolid* solid,
126 G4double angle)
127{
128 // nullptr for material ONLY ok in parallel world!
129 G4LogicalVolume* lv = new G4LogicalVolume(solid, // solid
130 nullptr, // material
131 name + "_lv"); // name
132
133 // always debug visualisation for read out geometry - only viewed via explicit commands
134 lv->SetVisAttributes(BDSGlobalConstants::Instance()->VisibleDebugVisAttr());
135
136 // we don't specify the face normals as they are w.r.t the incoming or outgoing
137 // reference trajectory - to which the curvilinear faces will be perpendicular
138 BDSSimpleComponent* result = new BDSSimpleComponent(name,
139 arcLength,
140 angle,
141 solid,
142 lv,
143 BDSExtent(radius, radius, chordLength*0.5));
144 // we don't bother calling Initialise() as we don't need to set the vis attributes (waste of memory)
145 return result;
146}
BDSSimpleComponent * CommonConstruction(const G4String &name, G4double arcLength, G4double chordLength, G4double radius, G4VSolid *solid, G4double angle)
BDSSimpleComponent * CreateCurvilinearVolume(const G4String &name, G4double chordLength, G4double radius)
Build a straight curvilinear volume.
const G4double lengthSafety
Cache of length safety from BDSGlobalConstants.
Holder for +- extents in 3 dimensions.
Definition BDSExtent.hh:38
A class that holds global options and constants.
static BDSGlobalConstants * Instance()
Access method.
A BDSAcceleratorComponent wrapper for BDSGeometryComponent.
A holder for any placement offsets and rotations for a BDSAcceleratorComponent.
G4double GetTilt() const
Accessor.
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())
std::pair< G4ThreeVector, G4ThreeVector > CalculateFaces(G4double angleInIn, G4double angleOutIn)
Calculate input and output normal vector.