27 #include "gsl/gsl_fft_real.h"
28 #include "gsl/gsl_fft_halfcomplex.h"
53 std::vector<Filter *> filters,
72 direction_m(direction),
73 constLength_m(constLength),
75 filters_m(filters.
begin(), filters.
end()) {
76 #ifdef ENABLE_WAKE_DEBUG
77 *
gmsg <<
"* ************* W A K E ************************************************************ " <<
endl;
78 *
gmsg <<
"* Entered GreenWakeFunction::GreenWakeFunction " <<
'\n';
79 *
gmsg <<
"* ********************************************************************************** " <<
endl;
102 std::pair<int, int> dist;
109 dist.first = locBunchRange *
Ippl::myNode() + (1 - tmp) * rem;
110 dist.second = dist.first + locBunchRange - 1;
123 double spacing, mindist;
124 std::vector<double> OutEnergy(
NBin_m);
133 spacing =
std::abs(rmax(2) - rmin(2));
136 spacing = rmax(0) * rmax(0) + rmax(1) * rmax(1);
157 std::pair<double, double> meshInfo;
160 #ifdef ENABLE_WAKE_DEBUG
161 *
gmsg <<
"* ************* W A K E ************************************************************ " <<
endl;
163 *
gmsg <<
"* ********************************************************************************** " <<
endl;
167 for(std::vector<Filter *>::const_iterator fit =
filters_m.begin(); fit !=
filters_m.end(); ++fit) {
183 for(
unsigned int i = 0; i < bunch->
getLocalNum(); i++) {
187 int idx = (int)(
floor((bunch->
R[i](2) - mindist) / spacing));
191 double dE = OutEnergy[idx];
192 bunch->
Ef[i](2) += dE;
198 for(
unsigned int i = 0; i < bunch->
getLocalNum(); i++) {
201 int idx = (int)(
floor((bunch->
R[i](2) - mindist) / spacing));
205 double dE = OutEnergy[idx];
208 double dist =
sqrt(bunch->
R[i](0) * bunch->
R[i](0) + bunch->
R[i](1) * bunch->
R[i](1));
210 bunch->
Ef[i](0) += dE * bunch->
R[i](0) / dist;
211 bunch->
Ef[i](1) += dE * bunch->
R[i](1) / dist;
220 #ifdef ENABLE_WAKE_DUMP
221 ofstream f2(
"OutEnergy.dat");
222 f2 <<
"# Energy of the Wake calculated in Opal\n"
223 <<
"# Z0 = " <<
Z0_m <<
"\n"
224 <<
"# radius = " << radius <<
"\n"
225 <<
"# sigma = " << sigma <<
"\n"
226 <<
"# c = " <<
c <<
"\n"
227 <<
"# acMode = " << acMode <<
"\n"
228 <<
"# tau = " << tau <<
"\n"
229 <<
"# direction = " << direction <<
"\n"
230 <<
"# spacing = " << spacing <<
"\n"
231 <<
"# Lbunch = " <<
NBin_m <<
"\n";
232 for(
int i = 0; i <
NBin_m; i++) {
233 f2 << i + 1 <<
" " << OutEnergy[i] <<
"\n";
251 const double *lambda,
255 std::vector<double> pLambda(N);
257 gsl_fft_halfcomplex_wavetable *hc;
258 gsl_fft_real_wavetable *
real = gsl_fft_real_wavetable_alloc(N);
259 gsl_fft_real_workspace *work = gsl_fft_real_workspace_alloc(N);
262 for(
int i = 0; i <
NBin_m; i ++) {
263 pLambda[N-i-1] = 0.0;
264 pLambda[i] = lambda[i];
268 gsl_fft_real_transform(pLambda.data(), 1, N,
real, work);
269 gsl_fft_real_wavetable_free(
real);
273 for(
int i = 1; i < N; i += 2) {
274 double temp = pLambda[i];
280 hc = gsl_fft_halfcomplex_wavetable_alloc(N);
282 gsl_fft_halfcomplex_inverse(pLambda.data(), 1, N, hc, work);
285 for(
int i = 0; i <
NBin_m; i ++) {
286 OutEnergy[i] = -charge * K * pLambda[i] / (2.0 *
NBin_m) * N;
295 gsl_fft_halfcomplex_wavetable_free(hc);
296 gsl_fft_real_workspace_free(work);
311 std::vector<double> lambda,
315 std::vector<double> pLambda(N);
317 gsl_fft_halfcomplex_wavetable *hc;
318 gsl_fft_real_wavetable *
real = gsl_fft_real_wavetable_alloc(N);
319 gsl_fft_real_workspace *work = gsl_fft_real_workspace_alloc(N);
322 for(
int i = 0; i <
NBin_m; i ++) {
323 pLambda[N-i-1] = 0.0;
324 pLambda[i] = lambda[i];
328 gsl_fft_real_transform(pLambda.data(), 1, N,
real, work);
329 gsl_fft_real_wavetable_free(
real);
334 for(
int i = 1; i < N; i += 2) {
335 double temp = pLambda[i];
341 hc = gsl_fft_halfcomplex_wavetable_alloc(N);
343 gsl_fft_halfcomplex_inverse(pLambda.data(), 1, N, hc, work);
346 for(
int i = 0; i <
NBin_m; i ++) {
347 OutEnergy[i] = -charge * K * pLambda[i] / (2.0 *
NBin_m) * N;
356 gsl_fft_halfcomplex_wavetable_free(hc);
357 gsl_fft_real_workspace_free(work);
368 double a = 1, b = 1000000;
369 unsigned int N = 1000000;
372 gsl_fft_real_wavetable *
real = gsl_fft_real_wavetable_alloc(M);
373 gsl_fft_real_workspace *work = gsl_fft_real_workspace_alloc(M);
376 const int lowIndex = myDist.first;
377 const int hiIndex = myDist.second;
379 for(
int i = 0; i < M; i ++) {
388 for(
int i = lowIndex; i <= hiIndex; i ++) {
405 #ifdef ENABLE_WAKE_TESTS_FFT_OUT
406 std::vector<double> wf(2*
NBin_m-1);
407 for(
int i = 0; i < 2 *
NBin_m - 1; ++ i) {
415 #ifdef ENABLE_WAKE_TESTS_FFT_OUT
416 ofstream f2(
"FFTwake.dat");
417 f2 <<
"# FFT of the Wake calculated in Opal" <<
"\n"
418 <<
"# Z0 = " <<
Z0_m <<
"\n"
419 <<
"# radius = " <<
radius_m <<
"\n"
420 <<
"# sigma = " <<
sigma_m <<
"\n"
422 <<
"# tau = " <<
tau_m <<
"\n"
424 <<
"# spacing = " << spacing <<
"\n"
425 <<
"# Lbunch = " <<
NBin_m <<
"\n";
427 f2 <<
"0\t" <<
FftWField_m[0] <<
"\t0.0\t" << wf[0] <<
"\n";
428 for(
int i = 1; i < M; i += 2) {
429 f2 << (i + 1) / 2 <<
"\t"
432 << wf[(i+1)/2] <<
"\n";
439 gsl_fft_real_wavetable_free(
real);
440 gsl_fft_real_workspace_free(work);
452 gsl_fft_real_wavetable *
real;
453 gsl_fft_real_workspace *work;
460 "Open file operation failed, please check if \""
465 msg <<
" SSDS1 read = " <<
name <<
endl;
466 if(
name.compare(
"SDDS1") != 0) {
468 " No SDDS1 File. A SDDS1 file should start with a SDDS1 String. Check file \""
472 for(
int i = 0; i < 6; i++) {
473 fs.getline(temp, 256);
474 msg <<
"line " << i <<
" : " << temp <<
endl;
478 msg <<
" header read" <<
endl;
481 " The particle number should be bigger than zero! Please check the first line of file \""
485 msg <<
" Np = " << Np <<
endl;
486 std::vector<double> wake(Np);
487 std::vector<double> dist(Np);
490 for(
int i = 0; i < Np; i ++) {
492 fs >> dist[i] >> wake[i] >> dummy;
496 " End of file reached before the whole wakefield is imported, please check file \""
504 for(
int i = 0; i <
NBin_m; i ++) {
506 while(dist[j] < i * spacing) {
511 FftWField_m[i] = wake[j] + ((wake[j+1] - wake[j]) / (dist[j+1] - dist[j]) * (i * spacing - dist[j]));
515 work = gsl_fft_real_workspace_alloc(
NBin_m);
519 gsl_fft_real_wavetable_free(
real);
520 gsl_fft_real_workspace_free(work);
524 return "GreenWakeFunction";
Tps< T > sqrt(const Tps< T > &x)
Square root.
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
FLieGenerator< T, N > real(const FLieGenerator< std::complex< T >, N > &)
Take real part of a complex generator.
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
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)
Inform & endl(Inform &inf)
constexpr double c
The velocity of light in m/s.
ParticleAttrib< Vector_t > Ef
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.
void get_bounds(Vector_t &rmin, Vector_t &rmax)
std::vector< double > FftWField_m
FFT of the zero padded wakefield.
void setWakeFromFile(int NBin, double spacing)
reads in the wakefield from file
int direction_m
direction either 1="Longitudinal" 2= "Transversal"
int NBin_m
divides the particle bunch in NBin slices
std::string filename_m
filename of the wakefield
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
std::vector< double > lineDensity_m
save the line Density of the particle bunch
GreenWakeFunction(const std::string &name, std::vector< Filter * > filters, int NBIN, double Z0, double radius, double sigma, int acMode, double tau, int direction, bool constLength, std::string fname)
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
void apply(PartBunchBase< double, 3 > *bunch)
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.
virtual const std::string getType() const
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
std::vector< Filter * > filters_m
const unsigned int nBins_m