OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
CSRIGFWakeFunction.cpp
Go to the documentation of this file.
1 //
2 // Class CSRIGFWakeFunction
3 //
4 // Copyright (c) 2008 - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
5 // All rights reserved
6 //
7 // This file is part of OPAL.
8 //
9 // OPAL is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16 //
21 #include "Filters/Filter.h"
22 #include "Filters/SavitzkyGolay.h"
23 #include "Physics/Physics.h"
24 #include "AbsBeamline/RBend.h"
25 #include "AbsBeamline/SBend.h"
26 #include "Utilities/Options.h"
27 #include "Utilities/Util.h"
28 
29 #include <iostream>
30 #include <fstream>
31 #include <cmath>
32 
33 CSRIGFWakeFunction::CSRIGFWakeFunction(const std::string &name, std::vector<Filter *> filters, const unsigned int &N):
34  WakeFunction(name, N),
35  filters_m(filters.begin(), filters.end()),
36  lineDensity_m(),
37  dlineDensitydz_m(),
38  bendRadius_m(0.0),
39  totalBendAngle_m(0.0)
40 {
41  if (filters_m.size() == 0) {
42  defaultFilter_m.reset(new SavitzkyGolayFilter(7, 3, 3, 3));
43  filters_m.push_back(defaultFilter_m.get());
44  }
45 
46  diffOp_m = filters_m.back();
47 }
48 
50  Inform msg("CSRWake ");
51 
52  std::pair<double, double> meshInfo;
53  calculateLineDensity(bunch, meshInfo);
54  const double &meshOrigin = meshInfo.first;
55  const double &meshSpacing = meshInfo.second;
56  const unsigned int numOfSlices = lineDensity_m.size();
57 
58  if (Ez_m.size() < numOfSlices) {
59  Ez_m.resize(numOfSlices, 0.0);
60  Chi_m.resize(numOfSlices, 0.0);
61  Grn_m.resize(numOfSlices, 0.0);
62  Psi_m.resize(numOfSlices, 0.0);
63  }
64 
65  for (unsigned int i = 0; i < numOfSlices; ++i) {
66  Ez_m[i] = 0.0;
67  }
68 
69  Vector_t smin, smax;
70  bunch->get_bounds(smin, smax);
71  double minPathLength = smin(2) + bunch->get_sPos() - FieldBegin_m;
72  for (unsigned int i = 1; i < numOfSlices; i++) {
73  double pathLengthOfSlice = minPathLength + i * meshSpacing;
74  double angleOfSlice = pathLengthOfSlice/bendRadius_m;
75  if (angleOfSlice > 0.0 && angleOfSlice <= totalBendAngle_m){
76  calculateGreenFunction(bunch, meshSpacing);
77  }
78  // convolute with line density
79  calculateContributionInside(i, angleOfSlice, meshSpacing);
80  calculateContributionAfter(i, angleOfSlice, meshSpacing);
82  }
83 
84  // calculate the wake field seen by the particles
85  for (unsigned int i = 0; i < bunch->getLocalNum(); ++i) {
86  const Vector_t &R = bunch->R[i];
87  unsigned int indexz = (unsigned int)floor((R(2) - meshOrigin) / meshSpacing);
88  double leverz = (R(2) - meshOrigin) / meshSpacing - indexz;
89  PAssert_LT(indexz + 1, numOfSlices);
90 
91  bunch->Ef[i](2) += (1. - leverz) * Ez_m[indexz] + leverz * Ez_m[indexz + 1];
92  }
93 
94  if (Options::csrDump) {
95  static std::string oldBendName;
96  static unsigned long counter = 0;
97 
98  if (oldBendName != bendName_m) counter = 0;
99 
100  const int every = 1;
101  bool print_criterion = (counter + 1) % every == 0;
102  if (print_criterion) {
103  static unsigned int file_number = 0;
104  if (counter == 0) file_number = 0;
105  double spos = bunch->get_sPos();
106  if (Ippl::myNode() == 0) {
107  std::stringstream filename_str;
108  filename_str << bendName_m << "-CSRWake" << std::setw(5) << std::setfill('0') << file_number << ".txt";
109 
110  std::string fname = Util::combineFilePath({
112  filename_str.str()
113  });
114 
115  std::ofstream csr(fname);
116  csr << spos << ", " << FieldBegin_m << ", " << smin(2) << ", " << smax(2) << ", " << meshSpacing*64 << std::endl;
117  for (unsigned int i = 0; i < lineDensity_m.size(); ++ i) {
118  csr << i *meshSpacing << "\t"
119  << Ez_m[i] << "\t"
120  << lineDensity_m[i] << std::endl;
121  }
122  csr.close();
123  msg << "** wrote " << fname << endl;
124  }
125  ++ file_number;
126  }
127  ++ counter;
128  oldBendName = bendName_m;
129  }
130 }
131 
133  if (ref->getType() == ElementBase::RBEND ||
134  ref->getType() == ElementBase::SBEND) {
135 
136  const Bend2D *bend = static_cast<const Bend2D *>(ref);
137  double End;
138 
139  bendRadius_m = bend->getBendRadius();
140  bend->getDimensions(Begin_m, End);
141  Length_m = bend->getEffectiveLength();
142  FieldBegin_m = bend->getEffectiveCenter() - Length_m / 2.0;
144  bendName_m = bend->getName();
145  }
146 }
147 
148 void CSRIGFWakeFunction::calculateLineDensity(PartBunchBase<double, 3> *bunch, std::pair<double, double> &meshInfo) {
149  bunch->calcLineDensity(nBins_m, lineDensity_m, meshInfo);
150 
151  // the following is only needed for after dipole
152  std::vector<Filter *>::const_iterator fit;
153  for (fit = filters_m.begin(); fit != filters_m.end(); ++ fit) {
154  (*fit)->apply(lineDensity_m);
155  }
156  dlineDensitydz_m.assign(lineDensity_m.begin(), lineDensity_m.end());
157  diffOp_m->calc_derivative(dlineDensitydz_m, meshInfo.second);
158 }
159 
161 {
162  unsigned int numOfSlices = lineDensity_m.size();
163  double gamma = bunch->get_meanKineticEnergy()/(bunch->getM()*1e-6)+1.0;
164  double xmu_const = 3.0 * gamma * gamma * gamma / (2.0 * bendRadius_m);
165  double chi_const = 9.0 / 16.0 * (6.0 - std::log(27.0 / 4.0));
166 
167  for (unsigned int i = 0; i < numOfSlices; ++i) {
168  Chi_m[i] = 0.0;
169  double z = i * meshSpacing;
170  double xmu = xmu_const * z;
171  double b = std::sqrt(xmu * xmu + 1.0) + xmu;
172  if (xmu < 1e-3)
173  Chi_m[i] = chi_const + 0.5 * std::pow(xmu, 2) - 7.0 / 54.0 * std::pow(xmu, 4) + 140.0 / 2187.0 * std::pow(xmu, 6);
174  else
175  Chi_m[i] = 9.0 / 16.0 * (3.0 * (-2.0 * xmu * std::pow(b, 1.0/3.0) + std::pow(b, 2.0/3.0) + std::pow(b, 4.0/3.0)) +
176  std::log(std::pow((1 - std::pow(b, 2.0 / 3.0)) / xmu, 2) / (1 + std::pow(b, 2.0 / 3.0) + std::pow(b, 4.0 / 3.0))));
177  }
178  double grn_const = -16.0/(27.0 * gamma * gamma * meshSpacing);
179  Grn_m[0] = grn_const * (Chi_m[1] - Chi_m[0]);
180  Grn_m[numOfSlices - 1] = 0.0;
181  for (unsigned int i = 1; i < numOfSlices - 1; ++i) {
182  Grn_m[i] = grn_const * (Chi_m[i + 1] - 2.0 * Chi_m[i] + Chi_m[i - 1]);
183  }
184 }
185 
186 void CSRIGFWakeFunction::calculateContributionInside(size_t sliceNumber, double angleOfSlice, double /*meshSpacing*/)
187 {
188  if (angleOfSlice > totalBendAngle_m || angleOfSlice < 0.0) return;
189  int startSliceNum = 0;
190  for (int j = sliceNumber; j >= startSliceNum; j--)
191  Ez_m[sliceNumber] += lineDensity_m[j] * Grn_m[sliceNumber - j];
192 }
193 
194 void CSRIGFWakeFunction::calculateContributionAfter(size_t sliceNumber, double angleOfSlice, double meshSpacing) {
195  if (angleOfSlice <= totalBendAngle_m) return;
196 
197  double Ds_max = bendRadius_m * std::pow(totalBendAngle_m, 3) / 24. * (4. - 3.* totalBendAngle_m / angleOfSlice);
198 
199  // First do contribution from particles whose retarded position is
200  // prior to the bend.
201  double Ds_max2 = bendRadius_m * std::pow(totalBendAngle_m, 2) / 6. * (3. * angleOfSlice - 2. * totalBendAngle_m);
202  int j = 0;
203  double frac = 0.0;
204  if (Ds_max2 / meshSpacing < sliceNumber) {
205  j = sliceNumber - static_cast<int>(floor(Ds_max2 / meshSpacing));
206  frac = Ds_max2 / meshSpacing - (sliceNumber - j);
207  Ez_m[sliceNumber] -= (frac * lineDensity_m[j - 1] + (1. - frac) * lineDensity_m[j]) / (2. * angleOfSlice - totalBendAngle_m);
208  }
209 
210  // Now do delta function contribution for particles whose retarded position
211  // is in the bend.
212  if (Ds_max / meshSpacing < sliceNumber) {
213  j = sliceNumber - static_cast<int>(floor(Ds_max / meshSpacing));
214  frac = Ds_max / meshSpacing - (sliceNumber - j);
215  Ez_m[sliceNumber] += (frac * lineDensity_m[j - 1] + (1.0 - frac) * lineDensity_m[j]) / (2. * angleOfSlice - totalBendAngle_m);
216  }
217 
218  // Now do integral contribution for particles whose retarded position is in
219  // the bend.
220 
221  double angleOverlap = angleOfSlice - totalBendAngle_m;
222  int k = sliceNumber;
223  if (Ds_max / meshSpacing < sliceNumber) {
224  k = j;
225  Psi_m[k] = calcPsi(Psi_m[k], angleOverlap, meshSpacing * (k + frac));
226  if (Psi_m[k] > 0 && Psi_m[k] < totalBendAngle_m)
227  Ez_m[sliceNumber] += 0.5 * (frac * dlineDensitydz_m[sliceNumber - k - 1] + (1.0 - frac) * dlineDensitydz_m[sliceNumber - k]) / (Psi_m[k] + 2.0 * angleOverlap);
228  } else {
229  Psi_m[0] = calcPsi(Psi_m[0], angleOverlap, meshSpacing * sliceNumber);
230  if (Psi_m[0] > 0 && Psi_m[0] < totalBendAngle_m)
231  Ez_m[sliceNumber] += 0.5 * dlineDensitydz_m[0] / (Psi_m[0] + 2.0 * angleOverlap);
232  }
233 
234  // Do rest of integral.
235  for (unsigned int l = sliceNumber - k + 1; l < sliceNumber; ++ l) {
236  Psi_m[l] = calcPsi(Psi_m[l], angleOverlap, meshSpacing * (sliceNumber - l));
237  if (Psi_m[l] > 0 && Psi_m[l] < totalBendAngle_m)
238  Ez_m[sliceNumber] += dlineDensitydz_m[l] / (Psi_m[l] + 2.0 * angleOverlap);
239  }
240 
241  // We don't go right to the end as there is a singularity in the numerical integral that we don't quite know
242  // how to deal with properly yet. This introduces a very slight error in the calculation (fractions of a percent).
243  Psi_m[sliceNumber] = calcPsi(Psi_m[sliceNumber], angleOverlap, meshSpacing / 4.0);
244  if (Psi_m[sliceNumber] > 0 && Psi_m[sliceNumber] < totalBendAngle_m)
245  Ez_m[sliceNumber] += 0.5 * dlineDensitydz_m[sliceNumber] / (Psi_m[sliceNumber] + 2.0 * angleOverlap);
246 
247  double prefactor = -4 / bendRadius_m;
248  Ez_m[sliceNumber] *= prefactor;
249 }
250 
251 double CSRIGFWakeFunction::calcPsi(const double &psiInitial, const double &x, const double &Ds) const {
259  const int Nmax = 100;
260  const double eps = 1e-10;
261  double psi = std::pow(24. * Ds / bendRadius_m, 1. / 3.);
262  if (psiInitial != 0.0) psi = psiInitial;
263 
264  for (int i = 0; i < Nmax; ++i) {
265  double residual = bendRadius_m * psi * psi * psi * (psi + 4. * x) - 24. * Ds * psi - 24. * Ds * x;
266  if (std::abs(residual) < eps)
267  return psi;
268  psi -= residual / (4. * bendRadius_m * psi * psi * psi + 12. * x * bendRadius_m * psi * psi - 24. * Ds);
269  }
270  RootFinderForCSR rootFinder(bendRadius_m, 4 * x * bendRadius_m, -24 * Ds, -24 * Ds * x);
271  if (rootFinder.hasPositiveRealRoots()) {
272  return rootFinder.searchRoot(eps);
273  }
274 
275  ERRORMSG("In CSRWakeFunction::calcPsi(): exceed maximum number of iterations!" << endl);
276  return psi;
277 }
278 const std::string CSRIGFWakeFunction::getType() const {
279  return "CSRIGFWakeFunction";
280 }
Tps< T > log(const Tps< T > &x)
Natural logarithm.
Definition: TpsMath.h:182
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define ERRORMSG(msg)
Definition: IpplInfo.h:350
#define PAssert_LT(a, b)
Definition: PAssert.h:106
const std::string name
constexpr double epsilon_0
The permittivity of vacuum in As/Vm.
Definition: Physics.h:57
constexpr double e
The value of.
Definition: Physics.h:39
constexpr double pi
The value of.
Definition: Physics.h:30
bool csrDump
Definition: Options.cpp:67
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition: Util.cpp:139
ParticleAttrib< Vector_t > Ef
double get_meanKineticEnergy() const
ParticlePos_t & R
size_t getLocalNum() const
void calcLineDensity(unsigned int nBins, std::vector< double > &lineDensity, std::pair< double, double > &meshInfo)
calculates the 1d line density (not normalized) and append it to a file.
void get_bounds(Vector_t &rmin, Vector_t &rmax)
double get_sPos() const
double getM() const
static OpalData * getInstance()
Definition: OpalData.cpp:195
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
Definition: OpalData.cpp:650
Definition: Bend2D.h:51
virtual void getDimensions(double &sBegin, double &sEnd) const override
Definition: Bend2D.h:284
double getEffectiveLength() const
Definition: Bend2D.h:300
double getEffectiveCenter() const
Definition: Bend2D.h:295
double getBendRadius() const
Definition: Bend2D.h:290
double getBendAngle() const
Definition: BendBase.h:92
virtual const std::string & getName() const
Get element name.
virtual void calc_derivative(std::vector< double > &histogram, const double &h)=0
void calculateGreenFunction(PartBunchBase< double, 3 > *bunch, double meshSpacing)
void initialize(const ElementBase *ref)
void calculateLineDensity(PartBunchBase< double, 3 > *bunch, std::pair< double, double > &meshInfo)
std::vector< double > Chi_m
std::vector< double > Grn_m
std::vector< Filter * > filters_m
CSRIGFWakeFunction(const std::string &name, std::vector< Filter * > filters, const unsigned int &N)
void apply(PartBunchBase< double, 3 > *bunch)
void calculateContributionAfter(size_t sliceNumber, double angleOfSlice, double meshSpacing)
std::vector< double > Psi_m
virtual const std::string getType() const
std::vector< double > Ez_m
std::shared_ptr< Filter > defaultFilter_m
LineDensity dlineDensitydz_m
void calculateContributionInside(size_t sliceNumber, double angleOfSlice, double meshSpacing)
double calcPsi(const double &psiInitial, const double &x, const double &Ds) const
double searchRoot(const double &tol)
const unsigned int nBins_m
Definition: WakeFunction.h:43
Definition: Inform.h:42
static int myNode()
Definition: IpplInfo.cpp:691