BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSBeamPipeFactoryRhombus.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 "BDSBeamPipeFactoryRhombus.hh"
19#include "BDSBeamPipeFactoryPoints.hh"
20
21#include "G4TwoVector.hh"
22#include "G4Types.hh"
23
24#include <algorithm>
25#include <cmath>
26#include <vector>
27
28BDSBeamPipeFactoryRhombus::BDSBeamPipeFactoryRhombus()
29{;}
30
31void BDSBeamPipeFactoryRhombus::GenerateRhombus(std::vector<G4TwoVector>& vec,
32 G4double x1,
33 G4double y1,
34 G4double cornerRadius,
35 G4int pointsPerTwoPi)
36{
37 G4double frac = cornerRadius / std::max(std::abs(x1), std::abs(y1));
38 if (frac > 1e-3)
39 {// build with corner radius
40 // The rhombus may be asymmetric, in which case building a pi/2 range of curve
41 // won't result in a smooth transition to the straight sections. We need to work
42 // out what range of angle to cover to come to the right tangent.
43 // consider top curved point -> alpha = angle between y axis and (0,y1) -> (x1,0) line
44 G4double alpha = std::atan2(std::abs(x1), std::abs(y1));
45 G4double halfAngle = CLHEP::halfpi - alpha;
46 std::vector<G4TwoVector> topBit;
47 G4double nPointsTopDouble = (2*halfAngle/CLHEP::twopi) * pointsPerTwoPi;
48 G4int nPointsTop = std::max(4, (G4int)std::ceil(nPointsTopDouble)); // ensure at least 4 points
49 G4double currentAngle = -halfAngle;
50 G4double dAngle = 2*halfAngle / (G4double)nPointsTop;
51 G4TwoVector rotationPointTop(0, y1-cornerRadius);
52 for (G4int i = 0; i <= nPointsTop; i++)
53 {
54 G4TwoVector r(0, cornerRadius);
55 r.rotate(-currentAngle);
56 topBit.push_back(rotationPointTop + r);
57 currentAngle += dAngle;
58 }
59
60 std::vector<G4TwoVector> rightBit;
61 G4double nPointsRightDouble = (2*alpha/CLHEP::twopi) * pointsPerTwoPi;
62 G4int nPointsRight = std::max(4, (G4int)std::ceil(nPointsRightDouble)); // ensure at least 4 points
63 currentAngle = -alpha;
64 dAngle = 2*alpha / (G4double)nPointsRight;
65 G4TwoVector rotationPointRight(x1-cornerRadius, 0);
66 for (G4int i = 0; i <= nPointsRight; i++)
67 {
68 G4TwoVector r(cornerRadius, 0);
69 r.rotate(-currentAngle);
70 topBit.push_back(rotationPointRight + r);
71 currentAngle += dAngle;
72 }
73
74 for (const auto& p : topBit)
75 {vec.push_back(p);}
76 for (const auto& p : rightBit)
77 {vec.push_back(p);}
78 for (const auto& p : topBit)
79 {vec.emplace_back(-p.x(), -p.y());}
80 for (const auto& p : rightBit)
81 {vec.emplace_back(-p.x(), -p.y());}
82 std::reverse(vec.begin(), vec.end());
83 }
84 else
85 {// build just as a rhombus
86 AppendPoint(vec, x1, 0);
87 AppendPoint(vec, 0, -y1);
88 AppendPoint(vec, -x1, 0);
89 AppendPoint(vec, 0, y1);
90 }
91}
92
94 G4double aper2,
95 G4double aper3,
96 G4double distance)
97{
98 G4double a1 = aper1 + distance;
99 G4double a2 = aper2 + distance;
100 G4double a3 = aper3 > 0 ? aper3 + distance : aper3;
101
102 BDS::ThreePoints result = {a1,a2,a3};
103 return result;
104}
105
107 G4double aper2,
108 G4double aper3,
109 G4double /*aper4*/,
110 G4double beamPipeThickness,
111 G4int pointsPerTwoPi)
112{
113 GenerateRhombus(vacuumEdge, aper1, aper2, aper3, pointsPerTwoPi);
114 BDS::ThreePoints ai = ExpandRhombus(aper1, aper2, aper3, lengthSafetyLarge);
115 GenerateRhombus(beamPipeInnerEdge, ai.aper1, ai.aper2, ai.aper3, pointsPerTwoPi);
116 BDS::ThreePoints ao = ExpandRhombus(aper1, aper2, aper3, lengthSafetyLarge + beamPipeThickness);
117 GenerateRhombus(beamPipeOuterEdge, ao.aper1, ao.aper2, ao.aper3, pointsPerTwoPi);
118 BDS::ThreePoints ac = ExpandRhombus(aper1, aper2, aper3, beamPipeThickness + 2*lengthSafetyLarge);
119 GenerateRhombus(containerEdge, ac.aper1, ac.aper2, ac.aper3, pointsPerTwoPi);
120 BDS::ThreePoints acs = ExpandRhombus(aper1, aper2, aper3, beamPipeThickness + 3*lengthSafetyLarge);
121 GenerateRhombus(containerSubtractionEdge, acs.aper1, acs.aper2, acs.aper3, pointsPerTwoPi);
122
123 extentX = acs.aper1;
124 extentY = acs.aper2;
125}
126
128 G4double aper2,
129 G4double /*aper3*/,
130 G4double /*aper4*/,
131 G4double beamPipeThickness)
132{
133 G4double dThickness = beamPipeThickness + 2*lengthSafetyLarge;
134
135 G4double result = std::max(aper1 + dThickness, aper2 + dThickness);
136 result += lengthSafetyLarge;
137 return result;
138}
std::vector< G4TwoVector > containerSubtractionEdge
Vector of x,y coordinates for the container subtraction volume.
std::vector< G4TwoVector > beamPipeInnerEdge
Vector of x,y coordinates for beam pipe inner edge.
G4double extentY
Extents prepared by GeneratePoints function as only it knows the extents.
G4double extentX
Extents prepared by GeneratePoints function as only it knows the extents.
std::vector< G4TwoVector > vacuumEdge
Vector of x,y coordinates for vacuum extruded solid edge.
std::vector< G4TwoVector > containerEdge
Vector of x,y coordinates for the container volume.
std::vector< G4TwoVector > beamPipeOuterEdge
Vector of x,y coordinates for beam pipe outer edge.
BDS::ThreePoints ExpandRhombus(G4double aper1, G4double aper2, G4double aper3, G4double distance)
Expand the rhombus as needed.
void GenerateRhombus(std::vector< G4TwoVector > &vec, G4double x1, G4double y1, G4double cornerRadius, G4int pointsPerTwoPi)
Generate either 4 points or set of points with curved edges.
virtual void GeneratePoints(G4double aper1, G4double aper2, G4double aper3, G4double aper4, G4double beamPipeThickness, G4int pointsPerTwoPi=40)
Generate all the sets of required points for each surface.
virtual G4double CalculateIntersectionRadius(G4double aper1, G4double aper2, G4double aper3, G4double aper4, G4double beamPipeThickness)
Calculate the radius of the solid used for intersection for angled faces.
G4double lengthSafetyLarge
Cache of global constants variable.