BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSBunchPtc.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 "BDSBunchPtc.hh"
20#include "BDSDebug.hh"
21#include "BDSException.hh"
22#include "BDSGlobalConstants.hh"
23#include "BDSParticleCoordsFull.hh"
24#include "BDSUtilities.hh"
25
26#include "parser/beam.h"
27
28#include "CLHEP/Units/PhysicalConstants.h"
29
30#include <fstream>
31#include <regex>
32#include <string>
33
34BDSBunchPtc::BDSBunchPtc():
35 BDSBunch("ptc"),
36 matchDistrFileLength(false),
37 nRays(0),
38 fileName("./inrays.madx"),
39 iRay(0),
40 loopedOver(false),
41 beta(1.0)
42{;}
43
44BDSBunchPtc::~BDSBunchPtc()
45{
46 for (std::vector<double*>::iterator i = ptcData.begin();i!=ptcData.end();++i)
47 {delete[] *i;}
48}
49
51{
52 // open file and read line by line and extract values
53 std::ifstream ifstr(fileName);
54
55 if (!ifstr)
56 {throw BDSException(__METHOD_NAME__, "\"" + fileName + "\" file doesn't exist - exiting as no input");}
57
58 std::string line;
59 // read single line
60 while (std::getline(ifstr,line))
61 {
62 int isComment = line.compare(0, 1, "!");
63 if (isComment == 0)
64 {continue;}
65 else
66 {
67 // variable for storage
68 double x = 0.0;
69 double y = 0.0;
70 double px = 0.0;
71 double py = 0.0;
72 double t = 0.0;
73 double pt = 0.0;
74
75 // create regular expressions
76 std::regex rex("\\sx\\s*=\\s*([0-9eE.+-]+)");
77 std::regex rey("\\sy\\s*=\\s*([0-9eE.+-]+)");
78 std::regex repx("px\\s*=\\s*([0-9eE.+-]+)");
79 std::regex repy("py\\s*=\\s*([0-9eE.+-]+)");
80 std::regex ret("\\st\\s*=\\s*([0-9eE.+-]+)");
81 std::regex rept("pt\\s*=\\s*([0-9eE.+-]+)");
82
83 // return search match objects
84 std::smatch smx;
85 std::smatch smy;
86 std::smatch smpx;
87 std::smatch smpy;
88 std::smatch smt;
89 std::smatch smpt;
90
91 // perform search
92 std::regex_search(line,smx, rex);
93 std::regex_search(line,smy, rey);
94 std::regex_search(line,smpx,repx);
95 std::regex_search(line,smpy,repy);
96 std::regex_search(line,smt, ret);
97 std::regex_search(line,smpt, rept);
98
99 if(smx.size() == 2) x = std::stod(smx[1]);
100 if(smy.size() == 2) y = std::stod(smy[1]);
101 if(smpx.size() == 2) px = std::stod(smpx[1]);
102 if(smpy.size() == 2) py = std::stod(smpy[1]);
103 if(smt.size() == 2) t = std::stod(smt[1]);
104 if(smpt.size() == 2) pt = std::stod(smpt[1]);
105
106#ifdef BDSDEBUG
107 G4cout << __METHOD_NAME__ << "read line " << line << G4endl;
108 G4cout << __METHOD_NAME__ << "values " << x << " " << px << " " << y << " " << py << " " << t << " " << pt << G4endl;
109#endif
110
111 double* values = new double[6];
112 values[0] = x;
113 values[1] = px;
114 values[2] = y;
115 values[3] = py;
116 values[4] = t;
117 values[5] = pt;
118
119 // append values to storage vector
120 ptcData.push_back(values);
121 }
122 }
123
124 // set number of available rays in options
125 nRays = ptcData.size();
126
129}
130
132 const GMAD::Beam& beam,
133 const BDSBunchType& distrType,
134 G4Transform3D beamlineTransformIn,
135 const G4double beamlineSIn)
136{
137#ifdef BDSDEBUG
138 G4cout << __METHOD_NAME__ << " " << beam.distrFile << G4endl;
139#endif
140
141 BDSBunch::SetOptions(beamParticle, beam, distrType, beamlineTransformIn, beamlineSIn);
143 beta = beamParticle->Beta();
144 SetDistrFile((G4String)beam.distrFile);
145 LoadPtcFile();
146}
147
148void BDSBunchPtc::SetDistrFile(const G4String& distrFileName)
149{
150 fileName = BDS::GetFullPath(distrFileName);
151}
152
154{
155 // if all particles are read, start at 0 again and note it down in bool flag
156 if (iRay == nRays)
157 {
158 iRay = 0;
159 loopedOver = true;
160 }
161
162 G4double x = ptcData[iRay][0] * CLHEP::m + X0;
163 G4double y = ptcData[iRay][2] * CLHEP::m + Y0;
164 G4double z = ptcData[iRay][4] * CLHEP::m + Z0;
165 G4double xp = ptcData[iRay][1] * CLHEP::rad + Xp0;
166 G4double yp = ptcData[iRay][3] * CLHEP::rad + Yp0;
167 G4double t = (z-Z0)*CLHEP::m / CLHEP::c_light + T0 * CLHEP::s;
168 G4double zp = CalculateZp(xp,yp,Zp0);
169
170 G4double betaSquared = std::pow(beta,2.0);
171 G4double ptcVal = ptcData[iRay][5] * betaSquared;
172 G4double E = E0 * (ptcVal + 1.0);
173
174 BDSParticleCoordsFull result(x,y,z,xp,yp,zp,t,S0+z,E,/*weight=*/1.0);
175
176 iRay++;
177 if (loopedOver)
178 {
179 G4cout << __METHOD_NAME__ << "End of file reached. Returning to beginning of file." << G4endl;
180 loopedOver = false; // reset flag until next time
181 }
182
183 return result;
184}
185
std::vector< double * > ptcData
Data.
Definition: BDSBunchPtc.hh:57
G4bool loopedOver
Whether we've reset to loop over the file again.
Definition: BDSBunchPtc.hh:58
G4String fileName
File name.
Definition: BDSBunchPtc.hh:55
void LoadPtcFile()
Load the PTC file into memory.
Definition: BDSBunchPtc.cc:50
G4bool matchDistrFileLength
Whether to only run the number of particles in the file.
Definition: BDSBunchPtc.hh:53
virtual BDSParticleCoordsFull GetNextParticleLocal()
Definition: BDSBunchPtc.cc:153
G4int nRays
Number of rays in file (1 counting).
Definition: BDSBunchPtc.hh:54
void SetDistrFile(const G4String &distrFileNameIn)
Assign the distribution file by finding the full path of it.
Definition: BDSBunchPtc.cc:148
G4double beta
Velocity w.r.t. speed of light. Needed to convert mom. to energy.
Definition: BDSBunchPtc.hh:59
G4int iRay
Iterator counter for current ray.
Definition: BDSBunchPtc.hh:56
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
Definition: BDSBunchPtc.cc:131
The base class for bunch distribution generators.
Definition: BDSBunch.hh:47
G4double Yp0
Centre of distributions.
Definition: BDSBunch.hh:166
G4double T0
Centre of distributions.
Definition: BDSBunch.hh:164
G4double S0
Centre of distributions.
Definition: BDSBunch.hh:163
G4double Z0
Centre of distributions.
Definition: BDSBunch.hh:162
G4double X0
Centre of distributions.
Definition: BDSBunch.hh:160
G4double Zp0
Centre of distributions.
Definition: BDSBunch.hh:167
G4double Xp0
Centre of distributions.
Definition: BDSBunch.hh:165
G4double E0
Centre of distributions.
Definition: BDSBunch.hh:168
G4double Y0
Centre of distributions.
Definition: BDSBunch.hh:161
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
void SetNumberToGenerate(G4int number)
Setter.
static BDSGlobalConstants * Instance()
Access method.
A set of particle coordinates including energy and weight.
Wrapper for particle definition.
G4double Beta() const
Accessor.
Improve type-safety of native enum data type in C++.
bool matchDistrFileLength
beam parameters
Definition: beamBase.h:55
std::string distrFile
beam parameters
Definition: beamBase.h:52
Beam class.
Definition: beam.h:44
G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false)