OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
#GreenWakeFunction.cpp#
Go to the documentation of this file.
1//
2// Class GreenWakeFunction
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
21
22#include "gsl/gsl_fft_real.h"
23#include "gsl/gsl_fft_halfcomplex.h"
24
25#include <fstream>
26#include <istream>
27#include <iostream> // Needed for stream I/O
28#include <iomanip> // Needed for I/O manipulators
29
30#include <boost/range/combine.hpp>
31
32//IFF: TEST
33//#define ENABLE_WAKE_DEBUG
34//#define ENABLE_WAKE_DUMP
35//#define ENABLE_WAKE_TESTS_FFT_OUT
36
37
38const std::map<WakeDirection, std::string> GreenWakeFunction::wakeDirectiontoString_s = {
39 {WakeDirection::TRANSVERSAL, "TRANSVERSAL"},
40 {WakeDirection::LONGITUDINAL, "LONGITUDINAL"},
41};
42
59 std::vector<Filter *> filters,
60 int NBIN,
61 double Z0,
62 double radius,
63 double sigma,
64 int acMode,
65 double tau,
66 WakeDirection direction,
67 bool constLength,
68 std::string fname):
70 lineDensity_m(),
71 //~ FftWField_m(0),
72 NBin_m(NBIN),
73 Z0_m(Z0),
74 radius_m(radius),
75 sigma_m(sigma),
76 acMode_m(acMode),
77 tau_m(tau),
78 direction_m(direction),
79 constLength_m(constLength),
80 filename_m(fname),
81 filters_m(filters.begin(), filters.end()) {
82#ifdef ENABLE_WAKE_DEBUG
83 *gmsg << "* ************* W A K E ************************************************************ " << endl;
84 *gmsg << "* Entered GreenWakeFunction::GreenWakeFunction " << '\n';
85 *gmsg << "* ********************************************************************************** " << endl;
86#endif
87}
88
90 //~ if (FftWField_m != 0) {
91 //~ delete[] FftWField_m;
92 //~ }
93}
94
95
106std::pair<int, int> GreenWakeFunction::distrIndices(int vectLen) {
107
108 std::pair<int, int> dist;
109
110 //IFF: properly distribute remainder
111 int rem = vectLen - (vectLen / Ippl::getNodes()) * Ippl::getNodes();
112 int tmp = (rem > Ippl::myNode()) ? 1 : 0;
113 int locBunchRange = vectLen / Ippl::getNodes() + tmp;
114
115 dist.first = locBunchRange * Ippl::myNode() + (1 - tmp) * rem;
116 dist.second = dist.first + locBunchRange - 1;
117
118 return dist;
119}
120
122
123 Vector_t rmin, rmax;
124 double charge = bunch->getChargePerParticle();
125 // CKR: was bunch->Q[1] changed it;
126 //FIXME: why 1? bunch,getTotalCharge()
127 // or bunch->getChargePerParticle()?
128 double K = 0; // constant to normalize the lineDensity_m to 1
129 double spacing, mindist;
130 std::vector<double> OutEnergy(NBin_m);
131
132 bunch->calcBeamParameters();
133 bunch->get_bounds(rmin, rmax);
134 //FIXME IFF: do we have unitless r's here? is that what we want?
135
136 mindist = rmin(2);
137 switch(direction_m) {
139 spacing = std::abs(rmax(2) - rmin(2));
140 break; //FIXME: Kann mann das Spacing immer ändern?
141 }
143 spacing = rmax(0) * rmax(0) + rmax(1) * rmax(1);
144 break;
145 }
146 default: {
147 throw GeneralClassicException("GreenWakeFunction::apply",
148 "Invalid direction specified");
149 }
150 }
151 PAssert(NBin_m > 0);
152 spacing /= (NBin_m - 1); //FIXME: why -1? CKR: because grid spacings = grid points - 1
153
154 // Calculate the Wakefield if needed
155 if (FftWField_m.empty()) {
156 FftWField_m.resize(2*NBin_m-1);
157 if (!filename_m.empty()) {
158 setWakeFromFile(NBin_m, spacing);
159 } else {
160 CalcWakeFFT(spacing);
161 }
162 } else if (!constLength_m) {
163 CalcWakeFFT(spacing);
164 }
165
166 // Calculate the line density of the particle bunch
167 std::pair<double, double> meshInfo;
168 bunch->calcLineDensity(nBins_m, lineDensity_m, meshInfo);
169
170#ifdef ENABLE_WAKE_DEBUG
171 *gmsg << "* ************* W A K E ************************************************************ " << endl;
172 *gmsg << "* GreenWakeFunction::apply lineDensity_m.size() = " << lineDensity_m.size() << endl;
173 *gmsg << "* ********************************************************************************** " << endl;
174#endif
175
176 // smooth the line density of the particle bunch
177 for (std::vector<Filter *>::const_iterator fit = filters_m.begin(); fit != filters_m.end(); ++fit) {
178 (*fit)->apply(lineDensity_m);
179 }
180
181 for (unsigned int i = 0; i < lineDensity_m.size(); i++) {
182 K += lineDensity_m[i];
183 }
184 K = 1 / K;
185
186 // compute the kick due to the wakefield
187 compEnergy(K, charge, lineDensity_m, OutEnergy.data());
188
189 // Add the right OutEnergy[i] to all the particles
190 //FIXME: can we specify LONG AND TRANS?
191 switch(direction_m) {
193 for (unsigned int i = 0; i < bunch->getLocalNum(); i++) {
194
195 //FIXME: Stimmt das????????? (von den einheiten)
196 // calculate bin containing particle
197 int idx = (int)(floor((bunch->R[i](2) - mindist) / spacing));
198 //IFF: should be ok
199 if (idx == NBin_m) idx--;
200 PAssert(idx >= 0 && idx < NBin_m);
201 double dE = OutEnergy[idx];
202 bunch->Ef[i](2) += dE;
203
204 }
205 break;
206 }
208 for (unsigned int i = 0; i < bunch->getLocalNum(); i++) {
209
210 // calculate bin containing particle
211 int idx = (int)(floor((bunch->R[i](2) - mindist) / spacing));
212 //IFF: should be ok
213 if (idx == NBin_m) idx--;
214 PAssert(idx >= 0 && idx < NBin_m);
215 double dE = OutEnergy[idx];
216
217 // ACHTUNG spacing auch in transversal richtung
218 double dist = sqrt(bunch->R[i](0) * bunch->R[i](0) + bunch->R[i](1) * bunch->R[i](1));
219 PAssert(dist > 0);
220 bunch->Ef[i](0) += dE * bunch->R[i](0) / dist;
221 bunch->Ef[i](1) += dE * bunch->R[i](1) / dist;
222
223 }
224 break;
225 }
226 default: {
227 throw GeneralClassicException("GreenWakeFunction::apply",
228 "Invalid direction specified");
229 }
230 }
231
232#ifdef ENABLE_WAKE_DUMP
233 ofstream f2("OutEnergy.dat");
234 f2 << "# Energy of the Wake calculated in Opal\n"
235 << "# Z0 = " << Z0_m << "\n"
236 << "# radius = " << radius << "\n"
237 << "# sigma = " << sigma << "\n"
238 << "# c = " << c << "\n"
239 << "# acMode = " << acMode << "\n"
240 << "# tau = " << tau << "\n"
241 << "# direction = " << direction << "\n"
242 << "# spacing = " << spacing << "\n"
243 << "# Lbunch = " << NBin_m << "\n";
244 for (int i = 0; i < NBin_m; i++) {
245 f2 << i + 1 << " " << OutEnergy[i] << "\n";
246 }
247 f2.flush();
248 f2.close();
249#endif
250}
251
262 const double charge,
263 const double* lambda,
264 double* OutEnergy) {
265 int N = 2 * NBin_m - 1;
266 // Allocate Space for the zero padded lambda and its Fourier Transformed
267 std::vector<double> pLambda(N);
268
269 gsl_fft_halfcomplex_wavetable *hc;
270 gsl_fft_real_wavetable *real = gsl_fft_real_wavetable_alloc(N);
271 gsl_fft_real_workspace *work = gsl_fft_real_workspace_alloc(N);
272
273 // fill the arrays with data
274 for (int i = 0; i < NBin_m; i ++) {
275 pLambda[N-i-1] = 0.0;
276 pLambda[i] = lambda[i];
277 }
278
279 //FFT of the lambda
280 gsl_fft_real_transform(pLambda.data(), 1, N, real, work);
281 gsl_fft_real_wavetable_free(real);
282
283 // convolution -> multiplication in Fourier space
284 pLambda[0] *= FftWField_m[0];
285 for (int i = 1; i < N; i += 2) {
286 double temp = pLambda[i];
287 pLambda[i] = FftWField_m[i] * pLambda[i] - FftWField_m[i+1] * pLambda[i+1];
288 pLambda[i+1] = FftWField_m[i] * pLambda[i+1] + FftWField_m[i+1] * temp;
289 }
290
291 // inverse transform to get c, the convolution of a and b;
292 hc = gsl_fft_halfcomplex_wavetable_alloc(N);
293
294 gsl_fft_halfcomplex_inverse(pLambda.data(), 1, N, hc, work);
295
296 // Write the result to the output:
297 for (int i = 0; i < NBin_m; i ++) {
298 OutEnergy[i] = -charge * K * pLambda[i] / (2.0 * NBin_m) * N; // CKR: I doubt that the multiplication with N is correct,
299 // put it here to get the same result as with FFTW
300 // My suspicion: S. Pauli has forgotten that if you
301 // do an fft followed by an inverse fft you'll get
302 // N times your original data
303
304 }
305
306
307 gsl_fft_halfcomplex_wavetable_free(hc);
308 gsl_fft_real_workspace_free(work);
309}
310
322 const double charge,
323 std::vector<double> lambda,
324 double* OutEnergy) {
325 int N = 2 * NBin_m - 1;
326 // Allocate Space for the zero padded lambda and its Fourier Transformed
327 std::vector<double> pLambda(N);
328
329 gsl_fft_halfcomplex_wavetable *hc;
330 gsl_fft_real_wavetable *real = gsl_fft_real_wavetable_alloc(N);
331 gsl_fft_real_workspace *work = gsl_fft_real_workspace_alloc(N);
332
333 // fill the arrays with data
334 for (int i = 0; i < NBin_m; i ++) {
335 pLambda[N-i-1] = 0.0;
336 pLambda[i] = lambda[i];
337 }
338
339 //FFT of the lambda
340 gsl_fft_real_transform(pLambda.data(), 1, N, real, work);
341 gsl_fft_real_wavetable_free(real);
342
343
344 // Convolution -> just a multiplication in Fourier space
345 pLambda[0] *= FftWField_m[0];
346 for (int i = 1; i < N; i += 2) {
347 double temp = pLambda[i];
348 pLambda[i] = FftWField_m[i] * pLambda[i] - FftWField_m[i+1] * pLambda[i+1];
349 pLambda[i+1] = FftWField_m[i] * pLambda[i+1] + FftWField_m[i+1] * temp;
350 }
351
352 // IFFT
353 hc = gsl_fft_halfcomplex_wavetable_alloc(N);
354
355 gsl_fft_halfcomplex_inverse(pLambda.data(), 1, N, hc, work);
356
357 // Write the result to the output:
358 for (int i = 0; i < NBin_m; i ++) {
359 OutEnergy[i] = -charge * K * pLambda[i] / (2.0 * NBin_m) * N; // CKR: I doubt that the multiplication with N is correct,
360 // put it here to get the same result as with FFTW
361 // My suspicion: S. Pauli has forgotten that if you
362 // do an fft followed by an inverse fft you'll get
363 // N times your original data
364
365 }
366
367
368 gsl_fft_halfcomplex_wavetable_free(hc);
369 gsl_fft_real_workspace_free(work);
370}
371
378void GreenWakeFunction::CalcWakeFFT(double spacing) {
379 // Set integration properties
380 double a = 1, b = 1000000;
381 unsigned int N = 1000000;
382 int M = 2 * NBin_m - 1;
383
384 gsl_fft_real_wavetable *real = gsl_fft_real_wavetable_alloc(M);
385 gsl_fft_real_workspace *work = gsl_fft_real_workspace_alloc(M);
386
387 const std::pair<int, int> myDist = distrIndices(NBin_m);
388 const int lowIndex = myDist.first;
389 const int hiIndex = myDist.second;
390
391 for (int i = 0; i < M; i ++) {
392 FftWField_m[i] = 0.0;
393 }
394
399 // if (Ippl::myNode() != Ippl::getNodes()-1) {
400 for (int i = lowIndex; i <= hiIndex; i ++) {
401 Wake w(i * spacing, Z0_m, radius_m, sigma_m, acMode_m, tau_m, direction_m);
402 FftWField_m[i] = simpson(w, a, b, N);
403 }
404 // } else {
405 // //IFF: changed to <= with new distr
406 // for (int i = lowIndex; i <= hiIndex; i ++) {
407 // Wake w(i*spacing, Z0_m, radius_m, sigma_m, acMode_m, tau_m, direction_m);
408 // FftWField[i] = simpson(w,a,b,N);
409 // }
410 // }
411
416
417#ifdef ENABLE_WAKE_TESTS_FFT_OUT
418 std::vector<double> wf(2*NBin_m-1);
419 for (int i = 0; i < 2 * NBin_m - 1; ++ i) {
420 wf[i] = FftWField_m[i];
421 }
422#endif
423 // calculate the FFT of the Wakefield
424 gsl_fft_real_transform(FftWField_m.data(), 1, M, real, work);
425
426
427#ifdef ENABLE_WAKE_TESTS_FFT_OUT
428 ofstream f2("FFTwake.dat");
429 f2 << "# FFT of the Wake calculated in Opal" << "\n"
430 << "# Z0 = " << Z0_m << "\n"
431 << "# radius = " << radius_m << "\n"
432 << "# sigma = " << sigma_m << "\n"
433 << "# mode = " << acMode_m << "\n"
434 << "# tau = " << tau_m << "\n"
435 << "# direction = " << direction_m << "\n"
436 << "# spacing = " << spacing << "\n"
437 << "# Lbunch = " << NBin_m << "\n";
438
439 f2 << "0\t" << FftWField_m[0] << "\t0.0\t" << wf[0] << "\n";
440 for (int i = 1; i < M; i += 2) {
441 f2 << (i + 1) / 2 << "\t"
442 << FftWField_m[i] << "\t"
443 << FftWField_m[i + 1] << "\t"
444 << wf[(i+1)/2] << "\n";
445 }
446
447
448 f2.flush();
449 f2.close();
450#endif
451 gsl_fft_real_wavetable_free(real);
452 gsl_fft_real_workspace_free(work);
453}
454
458void GreenWakeFunction::setWakeFromFile(int NBin_m, double spacing) {
459 Inform msg("readSDDS ");
460 std::string name;
461 char temp[256];
462 int Np;
463 double dummy;
464 gsl_fft_real_wavetable *real;
465 gsl_fft_real_workspace *work;
466 std::ifstream fs;
467
468 fs.open(filename_m.c_str());
469
470 if (fs.fail()) {
471 throw GeneralClassicException("GreenWakeFunction::setWakeFromFile",
472 "Open file operation failed, please check if '"
473 + filename_m + "' really exists.");
474 }
475
476 fs >> name;
477 msg << " SSDS1 read = " << name << endl;
478 if (name.compare("SDDS1") != 0) {
479 throw GeneralClassicException("GreenWakeFunction::setWakeFromFile",
480 " No SDDS1 File. A SDDS1 file should start with a SDDS1 String. Check file '"
481 + filename_m + "' ");
482 }
483
484 for (int i = 0; i < 6; i++) {
485 fs.getline(temp, 256);
486 msg << "line " << i << " : " << temp << endl;
487 }
488
489 fs >> Np;
490 msg << " header read" << endl;
491 if (Np <= 0) {
492 throw GeneralClassicException("GreenWakeFunction::setWakeFromFile",
493 " The particle number should be bigger than zero! Please check the first line of file '"
494 + filename_m + "'.");
495 }
496
497 msg << " Np = " << Np << endl;
498 std::vector<double> wake(Np);
499 std::vector<double> dist(Np);
500
501 // read the wakefunction
502 for (auto [d, w] : boost::combine(dist, wake)) {
503 if (!fs.eof()) {
504 fs >> d >> w >> dummy;
505 }
506 if (fs.eof()) {
507 throw GeneralClassicException("GreenWakeFunction::setWakeFromFile",
508 " End of file reached before the whole wakefield is imported, please check file '"
509 + filename_m + "'.");
510 }
511 }
512 // if needed interpolate the wake in a way that the wake form the file fits to the wake needs in the code (??)
513
514 FftWField_m.resize(NBin_m);
515
516 for (int i = 0; i < NBin_m; i ++) {
517 int j = 0;
518 while(dist[j] < i * spacing) {
519 j++;
520 }
521 -- j; // i * spacing should probably between dist[j] and dist[j+1]
522 // linear interpolation
523 FftWField_m[i] = wake[j] + ((wake[j+1] - wake[j]) / (dist[j+1] - dist[j]) * (i * spacing - dist[j]));
524 }
525
526 real = gsl_fft_real_wavetable_alloc(NBin_m);
527 work = gsl_fft_real_workspace_alloc(NBin_m);
528
529 gsl_fft_real_transform(FftWField_m.data(), 1, NBin_m, real, work);
530
531 gsl_fft_real_wavetable_free(real);
532 gsl_fft_real_workspace_free(work);
533}
534
537}
538
540 return wakeDirectiontoString_s.at(direction);
541}
FLieGenerator< T, N > real(const FLieGenerator< std::complex< T >, N > &)
Take real part of a complex generator.
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
WakeDirection
WakeType
Definition: WakeFunction.h:28
@ GreenWakeFunction
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
Definition: GlobalComm.hpp:55
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
std::complex< double > a
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define PAssert(c)
Definition: PAssert.h:102
const std::string name
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
FRONT * fs
Definition: hypervolume.cpp:59
ParticleAttrib< Vector_t > Ef
ParticlePos_t & R
void get_bounds(Vector_t &rmin, Vector_t &rmax) const
double getChargePerParticle() const
get the macro particle charge
size_t getLocalNum() const
void calcBeamParameters()
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.
std::vector< double > FftWField_m
FFT of the zero padded wakefield.
void setWakeFromFile(int NBin, double spacing)
reads in the wakefield from file
int NBin_m
divides the particle bunch in NBin slices
std::string filename_m
filename of the wakefield
static const std::map< WakeDirection, std::string > wakeDirectiontoString_s
void compEnergy(const double K, const double charge, const double *lambda, double *OutEnergy)
just a Testfunction! Calculate the energy of the Wakefunction with the lambda
WakeDirection direction_m
direction either "Longitudinal" - "Transversal"
virtual WakeType getType() const override
void apply(PartBunchBase< double, 3 > *bunch) override
std::vector< double > lineDensity_m
save the line Density of the particle bunch
double simpson(F &f, double a, double b, unsigned int N)
Simpson-Integration from the function f from a to b with N steps.
bool constLength_m
true if the length of the particle bunch is considered as constant
int acMode_m
conductivity either 1="AC" or 2="DC"
double sigma_m
material constant
void CalcWakeFFT(double spacing)
Calculate the FFT of the Wakefunction.
GreenWakeFunction(const std::string &name, std::vector< Filter * > filters, int NBIN, double Z0, double radius, double sigma, int acMode, double tau, WakeDirection direction, bool constLength, std::string fname)
double Z0_m
impedance
std::vector< Filter * > filters_m
std::pair< int, int > distrIndices(int vectLen)
given a vector of length N, distribute the indexes among the available processors
double tau_m
material constant
static std::string getWakeDirectionString(const WakeDirection &direction)
const unsigned int nBins_m
Definition: WakeFunction.h:52
Definition: Inform.h:42
static int getNodes()
Definition: IpplInfo.cpp:670
static int myNode()
Definition: IpplInfo.cpp:691
Inform * gmsg
Definition: Main.cpp:61