BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSBunchSixTrack.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
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 "BDSBunchSixTrack.hh"
20#include "BDSDebug.hh"
21#include "BDSException.hh"
22#include "BDSParticleCoordsFull.hh"
23
24#include "parser/beam.h"
25
26#include "CLHEP/Units/SystemOfUnits.h"
27
28#include <cmath>
29#include <fstream>
30
31BDSBunchSixTrack::BDSBunchSixTrack():
32 BDSBunch("sixtrack")
33{
34 useCurvilinear = true; // need to always convert from curvilinear
35 iPart = 0;
36 nPart = 0;
37}
38
39BDSBunchSixTrack::~BDSBunchSixTrack()
40{
41 for (auto i : sixtrackData)
42 {delete i;}
43}
44
46 const GMAD::Beam& beam,
47 const BDSBunchType& distrType,
48 G4Transform3D beamlineTransformIn,
49 const G4double beamlineSIn)
50{
51 BDSBunch::SetOptions(beamParticle, beam, distrType, beamlineTransformIn, beamlineSIn);
52 fileName = G4String(beam.distrFile);
53 LoadSixTrackFile();
54}
55
57{
58 G4double s = sixtrackData[iPart][2] * CLHEP::m;
59 G4double x = sixtrackData[iPart][3] * CLHEP::mm;
60 G4double xp = sixtrackData[iPart][4] * CLHEP::mrad;
61 G4double y = sixtrackData[iPart][5] * CLHEP::mm;
62 G4double yp = sixtrackData[iPart][6] * CLHEP::mrad;
63 G4double E = E0 * (1 + sixtrackData[iPart][7]);
64 G4double zp = CalculateZp(xp,yp,Zp0);
65
66 iPart++;
67
68 // if all particles are read, start at 0 again
69 if (iPart == nPart)
70 {
71 iPart=0;
72 G4cout << __METHOD_NAME__ << "End of file reached. Returning to beginning of file." << G4endl;
73 }
74 return BDSParticleCoordsFull(x,y,s,xp,yp,zp,T0,s,E,/*weight=*/1.0);
75}
76
77void BDSBunchSixTrack::LoadSixTrackFile()
78{
79 // SixTrack file is always LPI file
80 // header LPI : 1=sixtrackParticleID, 2=turn, 3=s [m], 4=x[mm], 5=xp[mrad], 6=y[mm], 7=yp[mrad], 8=dE/E, 9=type, 10=turns in machine after first hits on collimators
81
82 // open file and read line by line and extract values
83 std::ifstream infile(fileName.c_str());
84
85 if (!infile)
86 {throw BDSException(__METHOD_NAME__, "\"" + fileName + "\" file doesn't exist - exiting as no input");}
87 else
88 {G4cout << __METHOD_NAME__ << "Reading as SixTrack input " << fileName << "\"" << G4endl;}
89
90 // variable for storage
91 double sixtrackParticleID = 0.0;
92 double turn = 0.0;
93 double s = 0.0;
94 double x = 0.0;
95 double xp = 0.0;
96 double y = 0.0;
97 double yp = 0.0;
98 double en = 0.0;
99 double type = 0.0;
100 double turns = 0.0;
101
102 // read single line
103 while (infile >> sixtrackParticleID >> turn >> s >> x >> xp >> y >> yp >> en >> type >> turns)
104 {
105 double* values = new double[10];
106
107 // append values to storage vector
108 values[0] = sixtrackParticleID;
109 values[1] = turn;
110 values[2] = s;
111 values[3] = x;
112 values[4] = xp;
113 values[5] = y;
114 values[6] = yp;
115 values[7] = en;
116 values[8] = type;
117 values[9] = turns;
118
119 sixtrackData.push_back(values);
120 }
121
122 nPart = sixtrackData.size();
123 infile.close();
124}
125
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
G4int iPart
Current ray.
virtual BDSParticleCoordsFull GetNextParticleLocal()
The base class for bunch distribution generators.
Definition: BDSBunch.hh:47
G4double T0
Centre of distributions.
Definition: BDSBunch.hh:164
G4double Zp0
Centre of distributions.
Definition: BDSBunch.hh:167
G4double E0
Centre of distributions.
Definition: BDSBunch.hh:168
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
Definition: BDSBunch.cc:78
static G4double CalculateZp(G4double xp, G4double yp, G4double Zp0)
Calculate zp safely based on other components.
Definition: BDSBunch.cc:317
General exception with possible name of object and message.
Definition: BDSException.hh:35
A set of particle coordinates including energy and weight.
Wrapper for particle definition.
Improve type-safety of native enum data type in C++.
std::string distrFile
beam parameters
Definition: beamBase.h:52
Beam class.
Definition: beam.h:44