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) 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 "BDSBeamlineIntegral.hh"
20#include "BDSCavityFieldType.hh"
21#include "BDSComponentFactory.hh"
22#include "BDSDebug.hh"
23#include "BDSException.hh"
24#include "BDSParticleDefinition.hh"
25#include "BDSUtilities.hh"
26
27#include "G4Types.hh"
28
29#include "parser/element.h"
30#include "parser/elementtype.h"
31
32#include "CLHEP/Units/PhysicalConstants.h"
33#include "CLHEP/Units/SystemOfUnits.h"
34
35#include <cmath>
36
37
38BDSBeamlineIntegral::BDSBeamlineIntegral(const BDSParticleDefinition& incomingParticle,
39 G4double T0In,
40 G4double integratedArcLength):
41 synchronousTAtEnd(T0In),
42 synchronousTAtMiddleOfLastElement(T0In),
43 arcLength(integratedArcLength),
44 designParticle(incomingParticle)
45{;}
46
47BDSBeamlineIntegral::~BDSBeamlineIntegral()
48{;}
49
50void BDSBeamlineIntegral::Integrate(const GMAD::Element& componentAsDefined)
51{
52 // length
53 G4double thisComponentArcLength = componentAsDefined.l*CLHEP::m;
54 arcLength += thisComponentArcLength;
55
56 G4double v0 = designParticle.Velocity(); // velocity at entrance of element
57
58 // calculate change in velocity and kinetic energy
59 G4double dEk = 0;
60
61 G4double particleCharge = designParticle.Charge();
62 G4double phase = componentAsDefined.phase * CLHEP::rad;
63 G4double cosPhase = std::cos(phase);
64 G4double frequency = componentAsDefined.frequency * CLHEP::hertz;
65
66 switch (componentAsDefined.type)
67 {
68 case GMAD::ElementType::_RF:
69 {
70 // field model
72 G4double eField = BDSComponentFactory::EFieldFromElement(&componentAsDefined, thisComponentArcLength);
73 switch (tp.underlying())
74 {
75 case BDSCavityFieldType::constantinz:
76 {
77 dEk = particleCharge * eField * thisComponentArcLength * cosPhase;
78 break;
79 }
80 case BDSCavityFieldType::pillbox:
81 {
82 if (!BDS::IsFinite(frequency)) // protect against zero division
83 {throw BDSException(__METHOD_NAME__, "for a pillbox cavity field, the frequency must be non-zero");}
84 G4double rfWavelength = CLHEP::c_light / frequency;
85 G4double piGOverBetaLambda = CLHEP::pi * thisComponentArcLength / designParticle.Beta() * rfWavelength;
86 G4double transitTimeFactor = std::sin(piGOverBetaLambda) / piGOverBetaLambda;
87 dEk = particleCharge * eField * thisComponentArcLength * transitTimeFactor * cosPhase;
88 break;
89 }
90 default:
91 {break;}
92 }
93 break;
94 }
95 default:
96 {break;} // no action
97 }
98
99 // momentum and therefore BRho
100 designParticle.ApplyChangeInKineticEnergy(dEk);
101
102 G4double v1 = designParticle.Velocity(); // velocity at the end
103 G4double vMean = (v1 + v0) / 2.0;
104 G4double dT = thisComponentArcLength / vMean;
105
106 G4double dTMiddle = thisComponentArcLength / ( (0.5*(v1-v0) + v0 ) / 2.0);
107 synchronousTAtMiddleOfLastElement = synchronousTAtEnd + dTMiddle;
108
109 // time - now at the start of the next component / end of this component
110 synchronousTAtEnd += dT;
111}
void Integrate(const GMAD::Element &componentAsDefined)
static G4double EFieldFromElement(GMAD::Element const *el, G4double cavityLength)
General exception with possible name of object and message.
Definition: BDSException.hh:35
Wrapper for particle definition.
G4double Charge() 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:43
std::string cavityFieldType
Name for type of field to use in a cavity.
Definition: element.h:225
double phase
phase of rf cavity (rad)
Definition: element.h:77
double l
length in metres
Definition: element.h:49
ElementType type
element enum
Definition: element.h:44
double frequency
frequency for rf cavity in Hz
Definition: element.h:76