BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSBeamlineIntegral.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 "BDSBeamlineIntegral.hh"
19#include "BDSCavityFieldType.hh"
20#include "BDSComponentFactory.hh"
21#include "BDSDebug.hh"
22#include "BDSException.hh"
23#include "BDSFieldEMRFCavity.hh"
24#include "BDSGlobalConstants.hh"
25#include "BDSParticleDefinition.hh"
26#include "BDSUtilities.hh"
27
28#include "G4Types.hh"
29
30#include "parser/element.h"
31#include "parser/elementtype.h"
32
33#include "CLHEP/Units/PhysicalConstants.h"
34#include "CLHEP/Units/SystemOfUnits.h"
35
36#include <cmath>
37
38BDSBeamlineIntegral::BDSBeamlineIntegral(const BDSParticleDefinition& incomingParticle,
39 G4double T0In,
40 G4double integratedArcLength,
41 G4bool integrateKineticEnergyIn):
42 integrateKineticEnergy(integrateKineticEnergyIn),
43 synchronousTAtEnd(T0In),
44 synchronousTAtMiddleOfLastElement(T0In),
45 arcLength(integratedArcLength),
46 designParticle(incomingParticle),
47 changeOfEnergyEncountered(false),
48 rigidityCount(0)
49{;}
50
51BDSBeamlineIntegral::~BDSBeamlineIntegral()
52{;}
53
54void BDSBeamlineIntegral::Integrate(const GMAD::Element& componentAsDefined)
55{
56 // length
57 G4double thisComponentArcLength = componentAsDefined.l*CLHEP::m;
58 if (componentAsDefined.type == GMAD::ElementType::_RBEND)
59 {// reuse component factor code to work out angle / field and true arc length
60 G4double componentChordLength;
61 G4double field;
62 G4double angle;
64 designParticle.BRho(),
65 thisComponentArcLength,
66 componentChordLength,
67 field,
68 angle);
69 }
70 arcLength += thisComponentArcLength;
71
72 G4double v0 = designParticle.Velocity(); // current velocity at the entrance of element
73
74 // calculate change in velocity and kinetic energy
75 G4double dEk = 0;
76
77 switch (componentAsDefined.type)
78 {
79 case GMAD::ElementType::_RF:
80 {
81 G4double particleCharge = designParticle.Charge();
82 G4double phase = componentAsDefined.phase * CLHEP::rad;
83 G4double frequency = componentAsDefined.frequency * CLHEP::hertz;
84 // field model - reproduce behaviour in component factory of default in global constants
85 G4String compCFTN = componentAsDefined.cavityFieldType;
86 G4String cftName = compCFTN.empty() ? BDSGlobalConstants::Instance()->CavityFieldType() : compCFTN;
88 BDSFieldType fieldType = BDS::FieldTypeFromCavityFieldType(tp);
89 G4double eField = BDSComponentFactory::EFieldFromElement(&componentAsDefined, fieldType, thisComponentArcLength, designParticle);
90 switch (tp.underlying())
91 {
92 case BDSCavityFieldType::constantinz:
93 {
94 dEk = particleCharge * eField * thisComponentArcLength * std::cos(phase);
95 break;
96 }
97 case BDSCavityFieldType::pillbox:
98 {
99 if (!BDS::IsFinite(frequency)) // protect against zero division
100 {throw BDSException(__METHOD_NAME__, "for a pillbox cavity field, the frequency must be non-zero");}
101 G4double transitTimeFactor = BDSFieldEMRFCavity::TransitTimeFactor(frequency, phase, thisComponentArcLength, designParticle.Beta());
102 dEk = particleCharge * eField * thisComponentArcLength * transitTimeFactor;
103 break;
104 }
105 default:
106 {break;}
107 }
108 break;
109 }
110 default:
111 {break;} // no action
112 }
113
114 G4bool changeOfEnergyEncounteredThisTime = BDS::IsFinite(dEk, 1e-7);
115 changeOfEnergyEncountered = changeOfEnergyEncountered || changeOfEnergyEncounteredThisTime;
116 // count the number of times rigidity changes so we can uniquely name reused components
117 if (changeOfEnergyEncounteredThisTime)
118 {rigidityCount++;}
119
120 // momentum and therefore BRho
122 {designParticle.ApplyChangeInKineticEnergy(dEk);}
123
124 G4double v1 = designParticle.Velocity(); // velocity at the end
125 G4double vMean = (v1 + v0) / 2.0;
126 G4double dT = thisComponentArcLength / vMean;
127
128 G4double dTMiddle = 0.5*thisComponentArcLength / (0.5*(v1-v0) + v0);
129 synchronousTAtMiddleOfLastElement = synchronousTAtEnd + dTMiddle;
130
131 // time - now at the start of the next component / end of this component
132 synchronousTAtEnd += dT;
133}
134
136{
137 BDSBeamlineIntegral copy(*this);
138 copy.Integrate(*el);
139 G4double synchronousTAtMiddle = copy.synchronousTAtMiddleOfLastElement;
140 return synchronousTAtMiddle;
141}
142
144{
145 BDSBeamlineIntegral copy(*this);
146 copy.Integrate(*el);
147 G4double synchronousTAtEndLocal = copy.synchronousTAtEnd;
148 return synchronousTAtEndLocal;
149}
A class that holds the current integrated quantities along a beam line.
void Integrate(const GMAD::Element &componentAsDefined)
G4bool integrateKineticEnergy
On by default, but allow control to not integrate it for old behaviour.
G4double ProvideSynchronousTAtEndOfNextElement(const GMAD::Element *el) const
G4int rigidityCount
Which change in rigidity we're at. Starts at 0, +1 after each change. Used for naming.
G4double ProvideSynchronousTAtCentreOfNextElement(const GMAD::Element *el) const
static void CalculateAngleAndFieldRBend(const GMAD::Element *el, G4double brhoIn, G4double &arcLength, G4double &chordLength, G4double &field, G4double &angle)
static G4double EFieldFromElement(GMAD::Element const *el, BDSFieldType fieldType, G4double cavityLength, const BDSParticleDefinition &incomingParticle)
General exception with possible name of object and message.
static G4double TransitTimeFactor(G4double frequency, G4double phase, G4double zLength, G4double beta)
General function put here as it represents the equations in this class.
static BDSGlobalConstants * Instance()
Access method.
Wrapper for particle definition.
G4double Charge() const
Accessor.
G4double BRho() const
Accessor.
G4double Beta() const
Accessor.
G4double Velocity() const
Accessor.
void ApplyChangeInKineticEnergy(G4double dEk)
Utility function to update quantities by adding on dEK (can be negative).
Improve type-safety of native enum data type in C++.
type underlying() const
return underlying value (can be used in switch statement)
BDSCavityFieldType DetermineCavityFieldType(G4String cavityFieldType)
function to determine the enum type of the cavity field type (case-insensitive)
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())
Element class.
Definition element.h:47
std::string cavityFieldType
Name for type of field to use in a cavity.
Definition element.h:263
double phase
phase of rf cavity (rad)
Definition element.h:81
double l
length in metres
Definition element.h:53
ElementType type
element enum
Definition element.h:48
double frequency
frequency for rf cavity in Hz
Definition element.h:80