OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
CSRWakeFunction.cpp
Go to the documentation of this file.
1//
2// Class CSRWakeFunction
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//
18
19#include "AbsBeamline/RBend.h"
20#include "AbsBeamline/SBend.h"
23#include "Filters/Filter.h"
25#include "Physics/Physics.h"
27#include "Utilities/Options.h"
28#include "Utilities/Util.h"
29
30#include <cmath>
31#include <fstream>
32#include <iostream>
33
35 std::vector<Filter*> filters,
36 const unsigned int& N):
38 filters_m(filters.begin(), filters.end()),
39 lineDensity_m(),
40 dlineDensitydz_m(),
41 bendRadius_m(0.0),
42 totalBendAngle_m(0.0)
43{
44 if (filters_m.size() == 0) {
45 defaultFilter_m.reset(new SavitzkyGolayFilter(7, 3, 3, 3));
46 filters_m.push_back(defaultFilter_m.get());
47 }
48
49 diffOp_m = filters_m.back();
50}
51
53 Inform msg("CSRWake ");
54
55 const double sPos = bunch->get_sPos();
56 std::pair<double, double> meshInfo;
57 calculateLineDensity(bunch, meshInfo);
58 const double &meshSpacing = meshInfo.second;
59 const double &meshOrigin = meshInfo.first + 0.5 * meshSpacing;
60
61 if (Ez_m.size() < lineDensity_m.size()) {
62 Ez_m.resize(lineDensity_m.size(), 0.0);
63 Psi_m.resize(lineDensity_m.size(), 0.0);
64 }
65
66 Vector_t smin, smax;
67 bunch->get_bounds(smin, smax);
68 double minPathLength = smin(2) + sPos - FieldBegin_m;
69 if (sPos + smax(2) < FieldBegin_m) return;
70
71 Ez_m[0] = 0.0;
72 // calculate wake field of bunch
73 for (unsigned int i = 1; i < lineDensity_m.size(); ++i) {
74 Ez_m[i] = 0.0;
75
76 double angleOfSlice = 0.0;
77 double pathLengthOfSlice = minPathLength + i * meshSpacing;
78 if (pathLengthOfSlice > 0.0)
79 angleOfSlice = pathLengthOfSlice / bendRadius_m;
80
81 calculateContributionInside(i, angleOfSlice, meshSpacing);
82 calculateContributionAfter(i, angleOfSlice, meshSpacing);
83
84 Ez_m[i] /= (4. * Physics::pi * Physics::epsilon_0);
85 }
86
87 // calculate the wake field seen by the particles
88 for (unsigned int i = 0; i < bunch->getLocalNum(); ++i) {
89 const Vector_t &R = bunch->R[i];
90 double distanceToOrigin = (R(2) - meshOrigin) / meshSpacing;
91
92 unsigned int indexz = (unsigned int)floor(distanceToOrigin);
93 double leverz = distanceToOrigin - indexz;
94 PAssert_LT(indexz, lineDensity_m.size() - 1);
95
96 bunch->Ef[i](2) += (1. - leverz) * Ez_m[indexz] + leverz * Ez_m[indexz + 1];
97 }
98
99 if (Options::csrDump) {
100 static std::string oldBendName;
101 static unsigned long counter = 0;
102
103 if (oldBendName != bendName_m) counter = 0;
104
105 const int every = 1;
106 bool print_criterion = (counter + 1) % every == 0;
107 if (print_criterion) {
108 static unsigned int file_number = 0;
109 if (counter == 0) file_number = 0;
110 if (Ippl::myNode() == 0) {
111 std::stringstream filename_str;
112 filename_str << bendName_m << "-CSRWake" << std::setw(5) << std::setfill('0') << file_number << ".txt";
113 std::string fname = Util::combineFilePath({
115 filename_str.str()
116 });
117
118 std::ofstream csr(fname);
119 csr << std::setprecision(8);
120 csr << "# " << sPos + smin(2) - FieldBegin_m << "\t" << sPos + smax(2) - FieldBegin_m << std::endl;
121 for (unsigned int i = 0; i < lineDensity_m.size(); ++ i) {
122 csr << i *meshSpacing << "\t"
123 << Ez_m[i] << "\t"
124 << lineDensity_m[i] << "\t"
126 }
127 csr.close();
128 msg << "** wrote " << fname << endl;
129 }
130 ++ file_number;
131 }
132 ++ counter;
133 oldBendName = bendName_m;
134 }
135}
136
138 if (ref->getType() == ElementType::RBEND ||
139 ref->getType() == ElementType::SBEND) {
140
141 const Bend2D *bend = static_cast<const Bend2D *>(ref);
142 double End;
143
144 bendRadius_m = bend->getBendRadius();
145 bend->getDimensions(Begin_m, End);
147 FieldBegin_m = bend->getEffectiveCenter() - Length_m / 2.0;
149 bendName_m = bend->getName();
150 }
151}
152
154 std::pair<double, double>& meshInfo) {
155 bunch->calcLineDensity(nBins_m, lineDensity_m, meshInfo);
156
157 std::vector<Filter *>::const_iterator fit;
158 for (fit = filters_m.begin(); fit != filters_m.end(); ++ fit) {
159 (*fit)->apply(lineDensity_m);
160 }
161
162 dlineDensitydz_m.assign(lineDensity_m.begin(), lineDensity_m.end());
163 diffOp_m->calc_derivative(dlineDensitydz_m, meshInfo.second);
164}
165
166void CSRWakeFunction::calculateContributionInside(size_t sliceNumber, double angleOfSlice, double meshSpacing) {
167 if (angleOfSlice > totalBendAngle_m || angleOfSlice < 0.0) return;
168
169 const double meshSpacingsup = std::pow(meshSpacing, -1. / 3.);
170 double SlippageLength = std::pow(angleOfSlice, 3) * bendRadius_m / 24.;
171 double relativeSlippageLength = SlippageLength / meshSpacing;
172 if (relativeSlippageLength > sliceNumber) {
173
174 /*
175 Break integral into sum of integrals between grid points, then
176 use linear interpolation between each grid point.
177 */
178
179 double dx1 = std::pow(sliceNumber, 2. / 3.);
180 double dx2 = std::pow(sliceNumber, 5. / 3.);
181 double dx3 = std::pow(sliceNumber - 1., 5. / 3.);
182 Ez_m[sliceNumber] += 0.3 * meshSpacingsup * dlineDensitydz_m[0] * (5. * dx1 - 3. * dx2 + 3. * dx3);
183 for (unsigned int j = 1; j < sliceNumber; ++ j) {
184 dx1 = dx2;
185 dx2 = dx3;
186 dx3 = std::pow(sliceNumber - j - 1., 5. / 3.);
187 Ez_m[sliceNumber] += 0.9 * meshSpacingsup * dlineDensitydz_m[j] * (dx1 - 2.* dx2 + dx3);
188 }
189 Ez_m[sliceNumber] += 0.9 * meshSpacingsup * dlineDensitydz_m[sliceNumber];
190
191 } else if (relativeSlippageLength < 1) {
192
193 // First do transient term.
194 if (4.0 * relativeSlippageLength <= 1) {
195
196 Ez_m[sliceNumber] += 3.0 * std::pow(SlippageLength, 2.0 / 3.0) * (lineDensity_m[sliceNumber] - lineDensity_m[sliceNumber - 1]) / meshSpacing;
197 } else {
198
199 if (4.0 * relativeSlippageLength < sliceNumber) {
200
201 int j = sliceNumber - static_cast<int>(std::floor(4.0 * relativeSlippageLength));
202 double frac = 4.0 * relativeSlippageLength - (sliceNumber - j);
203 Ez_m[sliceNumber] -= (frac * lineDensity_m[j - 1] + (1. - frac) * lineDensity_m[j]) / std::pow(SlippageLength, 1. / 3.);
204
205 }
206
207 Ez_m[sliceNumber] += (relativeSlippageLength * lineDensity_m[sliceNumber - 1] + (1. - relativeSlippageLength) * lineDensity_m[sliceNumber]) / std::pow(SlippageLength, 1. / 3.);
208
209 }
210
211 // Now do steady state term.
212 Ez_m[sliceNumber] += (0.3 / meshSpacing) * std::pow(SlippageLength, 2. / 3.) * (5. * dlineDensitydz_m[sliceNumber] - 2. * relativeSlippageLength * (dlineDensitydz_m[sliceNumber] - dlineDensitydz_m[sliceNumber - 1]));
213
214 } else {
215
216 if (4. * relativeSlippageLength < sliceNumber) {
217
218 int j = sliceNumber - static_cast<int>(std::floor(4. * relativeSlippageLength));
219 double frac = 4. * relativeSlippageLength - (sliceNumber - j);
220 Ez_m[sliceNumber] -= (frac * lineDensity_m[j - 1] + (1. - frac) * lineDensity_m[j]) / std::pow(SlippageLength, 1. / 3.);
221
222 }
223
224 int j = sliceNumber - static_cast<int>(std::floor(SlippageLength / meshSpacing));
225 double frac = relativeSlippageLength - (sliceNumber - j);
226 Ez_m[sliceNumber] += (frac * lineDensity_m[j - 1] + (1. - frac) * lineDensity_m[j]) / std::pow(SlippageLength, 1. / 3.);
227
228 double dx1 = std::pow(sliceNumber - j + frac, 2. / 3.);
229 double dx2 = std::pow(sliceNumber - j, 2. / 3.);
230 double dx3 = std::pow(sliceNumber - j + frac, 5. / 3.);
231 double dx4 = std::pow(sliceNumber - j, 5. / 3.);
232
233 Ez_m[sliceNumber] += 1.5 * meshSpacingsup * dlineDensitydz_m[j - 1] * (dx1 - dx2);
234 Ez_m[sliceNumber] += 0.3 * meshSpacingsup * (dlineDensitydz_m[j] - dlineDensitydz_m[j - 1]) * (5.*(dx1 - dx2) + 3.*(dx3 - dx4));
235
236 dx1 = dx2;
237 dx2 = dx4;
238 dx3 = std::pow(sliceNumber - j - 1., 5. / 3.);
239 Ez_m[sliceNumber] += 0.3 * meshSpacingsup * dlineDensitydz_m[j] * (5.*dx1 - 3.*dx2 + 3.*dx3);
240 for (unsigned int k = j + 1; k < sliceNumber; ++ k) {
241 dx1 = dx2;
242 dx2 = dx3;
243 dx3 = std::pow(sliceNumber - k - 1., 5. / 3.);
244 Ez_m[sliceNumber] += 0.9 * meshSpacingsup * dlineDensitydz_m[k] * (dx1 - 2.*dx2 + dx3);
245 }
246 Ez_m[sliceNumber] += 0.9 * meshSpacingsup * dlineDensitydz_m[sliceNumber];
247 }
248 double prefactor = -2. / std::pow(3. * bendRadius_m * bendRadius_m, 1. / 3.);
249 Ez_m[sliceNumber] *= prefactor;
250}
251
252void CSRWakeFunction::calculateContributionAfter(size_t sliceNumber, double angleOfSlice, double meshSpacing) {
253 if (angleOfSlice <= totalBendAngle_m) return;
254
255 double Ds_max = bendRadius_m * std::pow(totalBendAngle_m, 3) / 24. * (4. - 3.* totalBendAngle_m / angleOfSlice);
256
257 // First do contribution from particles whose retarded position is
258 // prior to the bend.
259 double Ds_max2 = bendRadius_m * std::pow(totalBendAngle_m, 2) / 6. * (3. * angleOfSlice - 2. * totalBendAngle_m);
260 int j = 0;
261 double frac = 0.0;
262 if (Ds_max2 / meshSpacing < sliceNumber) {
263 j = sliceNumber - static_cast<int>(floor(Ds_max2 / meshSpacing));
264 frac = Ds_max2 / meshSpacing - (sliceNumber - j);
265 Ez_m[sliceNumber] -= (frac * lineDensity_m[j - 1] + (1. - frac) * lineDensity_m[j]) / (2. * angleOfSlice - totalBendAngle_m);
266 }
267
268 // Now do delta function contribution for particles whose retarded position
269 // is in the bend.
270 if (Ds_max / meshSpacing < sliceNumber) {
271 j = sliceNumber - static_cast<int>(floor(Ds_max / meshSpacing));
272 frac = Ds_max / meshSpacing - (sliceNumber - j);
273 Ez_m[sliceNumber] += (frac * lineDensity_m[j - 1] + (1.0 - frac) * lineDensity_m[j]) / (2. * angleOfSlice - totalBendAngle_m);
274 }
275
276 // Now do integral contribution for particles whose retarded position is in
277 // the bend.
278
279 double angleOverlap = angleOfSlice - totalBendAngle_m;
280 int k = sliceNumber;
281 if (Ds_max / meshSpacing < sliceNumber) {
282 k = j;
283 Psi_m[k] = calcPsi(Psi_m[k], angleOverlap, meshSpacing * (k + frac));
284 if (Psi_m[k] > 0 && Psi_m[k] < totalBendAngle_m)
285 Ez_m[sliceNumber] += 0.5 * (frac * dlineDensitydz_m[sliceNumber - k - 1] + (1.0 - frac) * dlineDensitydz_m[sliceNumber - k]) / (Psi_m[k] + 2.0 * angleOverlap);
286 } else {
287 Psi_m[0] = calcPsi(Psi_m[0], angleOverlap, meshSpacing * sliceNumber);
288 if (Psi_m[0] > 0 && Psi_m[0] < totalBendAngle_m)
289 Ez_m[sliceNumber] += 0.5 * dlineDensitydz_m[0] / (Psi_m[0] + 2.0 * angleOverlap);
290 }
291
292 // Do rest of integral.
293 for (unsigned int l = sliceNumber - k + 1; l < sliceNumber; ++ l) {
294 Psi_m[l] = calcPsi(Psi_m[l], angleOverlap, meshSpacing * (sliceNumber - l));
295 if (Psi_m[l] > 0 && Psi_m[l] < totalBendAngle_m)
296 Ez_m[sliceNumber] += dlineDensitydz_m[l] / (Psi_m[l] + 2.0 * angleOverlap);
297 }
298
299 // We don't go right to the end as there is a singularity in the numerical integral that we don't quite know
300 // how to deal with properly yet. This introduces a very slight error in the calculation (fractions of a percent).
301 Psi_m[sliceNumber] = calcPsi(Psi_m[sliceNumber], angleOverlap, meshSpacing / 4.0);
302 if (Psi_m[sliceNumber] > 0 && Psi_m[sliceNumber] < totalBendAngle_m)
303 Ez_m[sliceNumber] += 0.5 * dlineDensitydz_m[sliceNumber] / (Psi_m[sliceNumber] + 2.0 * angleOverlap);
304
305 double prefactor = -4 / bendRadius_m;
306 Ez_m[sliceNumber] *= prefactor;
307}
308
309double CSRWakeFunction::calcPsi(const double& psiInitial, const double& x, const double& Ds) const {
317 const int Nmax = 100;
318 const double eps = 1e-10;
319
320 double psi = std::pow(24. * Ds / bendRadius_m, 1. / 3.);
321 if (psiInitial != 0.0) psi = psiInitial;
322
323 for (int i = 0; i < Nmax; ++i) {
324 double residual = bendRadius_m * psi * psi * psi * (psi + 4. * x) - 24. * Ds * psi - 24. * Ds * x;
325 if (std::abs(residual) < eps)
326 return psi;
327
328 psi -= residual / (4. * bendRadius_m * psi * psi * psi + 12. * x * bendRadius_m * psi * psi - 24. * Ds);
329 }
330
331 RootFinderForCSR rootFinder(bendRadius_m, 4 * x * bendRadius_m, -24 * Ds, -24 * Ds * x);
332 if (rootFinder.hasPositiveRealRoots()) {
333 return rootFinder.searchRoot(eps);
334 }
335
336 ERRORMSG("In CSRWakeFunction::calcPsi(): exceed maximum number of iterations!" << endl);
337 return psi;
338}
339
342}
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
WakeType
Definition: WakeFunction.h:28
@ CSRWakeFunction
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define PAssert_LT(a, b)
Definition: PAssert.h:106
#define ERRORMSG(msg)
Definition: IpplInfo.h:350
const std::string name
constexpr double epsilon_0
The permittivity of vacuum in As/Vm.
Definition: Physics.h:51
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:196
ParticleAttrib< Vector_t > Ef
ParticlePos_t & R
void get_bounds(Vector_t &rmin, Vector_t &rmax) const
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.
double get_sPos() const
static OpalData * getInstance()
Definition: OpalData.cpp:196
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
Definition: OpalData.cpp:661
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 calculateLineDensity(PartBunchBase< double, 3 > *bunch, std::pair< double, double > &meshInfo)
void calculateContributionAfter(size_t sliceNumber, double angleOfSlice, double meshSpacing)
std::vector< Filter * > filters_m
std::string bendName_m
LineDensity dlineDensitydz_m
double calcPsi(const double &psiInitial, const double &x, const double &Ds) const
void apply(PartBunchBase< double, 3 > *bunch) override
void calculateContributionInside(size_t sliceNumber, double angleOfSlice, double meshSpacing)
std::vector< double > Ez_m
std::vector< double > Psi_m
CSRWakeFunction(const std::string &name, std::vector< Filter * > filters, const unsigned int &N)
virtual WakeType getType() const override
LineDensity lineDensity_m
std::shared_ptr< Filter > defaultFilter_m
void initialize(const ElementBase *ref) override
double searchRoot(const double &tol)
const unsigned int nBins_m
Definition: WakeFunction.h:52
Definition: Inform.h:42
static int myNode()
Definition: IpplInfo.cpp:691