22#include "gsl/gsl_fft_real.h"
23#include "gsl/gsl_fft_halfcomplex.h"
30#include <boost/range/combine.hpp>
59 std::vector<Filter *> filters,
78 direction_m(direction),
79 constLength_m(constLength),
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;
108 std::pair<int, int> dist;
115 dist.first = locBunchRange *
Ippl::myNode() + (1 - tmp) * rem;
116 dist.second = dist.first + locBunchRange - 1;
129 double spacing, mindist;
130 std::vector<double> OutEnergy(
NBin_m);
139 spacing =
std::abs(rmax(2) - rmin(2));
143 spacing = rmax(0) * rmax(0) + rmax(1) * rmax(1);
148 "Invalid direction specified");
167 std::pair<double, double> meshInfo;
170#ifdef ENABLE_WAKE_DEBUG
171 *
gmsg <<
"* ************* W A K E ************************************************************ " <<
endl;
173 *
gmsg <<
"* ********************************************************************************** " <<
endl;
177 for (std::vector<Filter *>::const_iterator fit =
filters_m.begin(); fit !=
filters_m.end(); ++fit) {
193 for (
unsigned int i = 0; i < bunch->
getLocalNum(); i++) {
197 int idx = (int)(
floor((bunch->
R[i](2) - mindist) / spacing));
201 double dE = OutEnergy[idx];
202 bunch->
Ef[i](2) += dE;
208 for (
unsigned int i = 0; i < bunch->
getLocalNum(); i++) {
211 int idx = (int)(
floor((bunch->
R[i](2) - mindist) / spacing));
215 double dE = OutEnergy[idx];
218 double dist =
sqrt(bunch->
R[i](0) * bunch->
R[i](0) + bunch->
R[i](1) * bunch->
R[i](1));
220 bunch->
Ef[i](0) += dE * bunch->
R[i](0) / dist;
221 bunch->
Ef[i](1) += dE * bunch->
R[i](1) / dist;
228 "Invalid direction specified");
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";
263 const double* lambda,
267 std::vector<double> pLambda(N);
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);
274 for (
int i = 0; i <
NBin_m; i ++) {
275 pLambda[N-i-1] = 0.0;
276 pLambda[i] = lambda[i];
280 gsl_fft_real_transform(pLambda.data(), 1, N,
real, work);
281 gsl_fft_real_wavetable_free(
real);
285 for (
int i = 1; i < N; i += 2) {
286 double temp = pLambda[i];
292 hc = gsl_fft_halfcomplex_wavetable_alloc(N);
294 gsl_fft_halfcomplex_inverse(pLambda.data(), 1, N, hc, work);
297 for (
int i = 0; i <
NBin_m; i ++) {
298 OutEnergy[i] = -charge * K * pLambda[i] / (2.0 *
NBin_m) * N;
307 gsl_fft_halfcomplex_wavetable_free(hc);
308 gsl_fft_real_workspace_free(work);
323 std::vector<double> lambda,
327 std::vector<double> pLambda(N);
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);
334 for (
int i = 0; i <
NBin_m; i ++) {
335 pLambda[N-i-1] = 0.0;
336 pLambda[i] = lambda[i];
340 gsl_fft_real_transform(pLambda.data(), 1, N,
real, work);
341 gsl_fft_real_wavetable_free(
real);
346 for (
int i = 1; i < N; i += 2) {
347 double temp = pLambda[i];
353 hc = gsl_fft_halfcomplex_wavetable_alloc(N);
355 gsl_fft_halfcomplex_inverse(pLambda.data(), 1, N, hc, work);
358 for (
int i = 0; i <
NBin_m; i ++) {
359 OutEnergy[i] = -charge * K * pLambda[i] / (2.0 *
NBin_m) * N;
368 gsl_fft_halfcomplex_wavetable_free(hc);
369 gsl_fft_real_workspace_free(work);
380 double a = 1, b = 1000000;
381 unsigned int N = 1000000;
384 gsl_fft_real_wavetable *
real = gsl_fft_real_wavetable_alloc(M);
385 gsl_fft_real_workspace *work = gsl_fft_real_workspace_alloc(M);
388 const int lowIndex = myDist.first;
389 const int hiIndex = myDist.second;
391 for (
int i = 0; i < M; i ++) {
400 for (
int i = lowIndex; i <= hiIndex; i ++) {
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) {
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"
434 <<
"# tau = " <<
tau_m <<
"\n"
436 <<
"# spacing = " << spacing <<
"\n"
437 <<
"# Lbunch = " <<
NBin_m <<
"\n";
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"
444 << wf[(i+1)/2] <<
"\n";
451 gsl_fft_real_wavetable_free(
real);
452 gsl_fft_real_workspace_free(work);
464 gsl_fft_real_wavetable *
real;
465 gsl_fft_real_workspace *work;
472 "Open file operation failed, please check if '"
477 msg <<
" SSDS1 read = " <<
name <<
endl;
478 if (
name.compare(
"SDDS1") != 0) {
480 " No SDDS1 File. A SDDS1 file should start with a SDDS1 String. Check file '"
484 for (
int i = 0; i < 6; i++) {
485 fs.getline(temp, 256);
486 msg <<
"line " << i <<
" : " << temp <<
endl;
490 msg <<
" header read" <<
endl;
493 " The particle number should be bigger than zero! Please check the first line of file '"
497 msg <<
" Np = " << Np <<
endl;
498 std::vector<double> wake(Np);
499 std::vector<double> dist(Np);
502 for (
auto [d, w] : boost::combine(dist, wake)) {
504 fs >> d >> w >> dummy;
508 " End of file reached before the whole wakefield is imported, please check file '"
516 for (
int i = 0; i <
NBin_m; i ++) {
518 while(dist[j] < i * spacing) {
523 FftWField_m[i] = wake[j] + ((wake[j+1] - wake[j]) / (dist[j+1] - dist[j]) * (i * spacing - dist[j]));
527 work = gsl_fft_real_workspace_alloc(
NBin_m);
531 gsl_fft_real_wavetable_free(
real);
532 gsl_fft_real_workspace_free(work);
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)
Tps< T > sqrt(const Tps< T > &x)
Square root.
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
constexpr double c
The velocity of light in m/s.
ParticleAttrib< Vector_t > Ef
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)
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