OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
Astra1DDynamic.h
Go to the documentation of this file.
1#ifndef OPALX_AstraFIELDMAP1DDYNAMIC_HH
2#define OPALX_AstraFIELDMAP1DDYNAMIC_HH
3
4#include "Fields/Fieldmap.h"
5#include "Physics/Physics.h"
6
7#include <Kokkos_Core.hpp>
8#include <Kokkos_DualView.hpp>
9
10class Astra1DDynamic : public Fieldmap {
11public:
20 virtual bool getFieldstrength(
22 Vector_t<double, 3>& B) const override;
23
33 virtual bool getFieldDerivative(
35 const DiffDirection& dir) const override;
36
42 virtual void getFieldDimensions(double& zBegin, double& zEnd) const override;
43
54 virtual void getFieldDimensions(
55 double& xIni, double& xFinal, double& yIni, double& yFinal, double& zIni,
56 double& zFinal) const override;
57
59 virtual void swap() override;
60
62 virtual void getInfo(Inform* msg) override;
63
69 virtual double getFrequency() const override;
70
76 virtual void setFrequency(double freq) override;
77
85 bool isInside(const Vector_t<double, 3>& r) const override {
86 return r(2) >= zbegin_m && r(2) < zend_m;
87 // && sqrt(r(0) * r(0) + r(1) * r(1)) < rend_m;
88 }
89
90 template <class ViewType>
91 KOKKOS_INLINE_FUNCTION static void computeField(
93 const ViewType& FourCoefs, double zbegin, double length, double xlrep, int accuracy) {
94 const double RR2 = R(0) * R(0) + R(1) * R(1);
95
96 const double two_pi = Physics::two_pi;
97 const double pi = Physics::pi;
98 const double dk = Physics::two_pi / length;
99 const double kz = two_pi * (R(2) - zbegin) / length + pi;
100
101 double ez = FourCoefs(0);
102 double ezp = 0.0;
103 double ezpp = 0.0;
104 double ezppp = 0.0;
105
106 int n = 1;
107 for (int l = 1; l < accuracy; ++l, n += 2) {
108 const double base = dk * l;
109
110 const double coskzl = Kokkos::cos(kz * l);
111 const double sinkzl = Kokkos::sin(kz * l);
112
113 ez += FourCoefs(n) * coskzl - FourCoefs(n + 1) * sinkzl;
114 ezp += base * (-FourCoefs(n) * sinkzl - FourCoefs(n + 1) * coskzl);
115 ezpp += base * base * (-FourCoefs(n) * coskzl + FourCoefs(n + 1) * sinkzl);
116 ezppp += base * base * base * (FourCoefs(n) * sinkzl + FourCoefs(n + 1) * coskzl);
117 }
118
119 const double f = -(ezpp + ez * xlrep * xlrep) / 16.0;
120 const double fp = -(ezppp + ezp * xlrep * xlrep) / 16.0;
121
122 const double EfieldR = -(ezp / 2.0 + fp * RR2);
123 const double BfieldT = (ez / 2.0 + f * RR2) * xlrep / Physics::c;
124
125 E(0) += EfieldR * R(0);
126 E(1) += EfieldR * R(1);
127 E(2) += ez + 4.0 * f * RR2;
128
129 B(0) -= BfieldT * R(1);
130 B(1) += BfieldT * R(0);
131 }
132
133 template <class ViewType>
134 KOKKOS_INLINE_FUNCTION static void computeRFField(
136 const ViewType& FourCoefs, double zbegin, double zend, double length, double xlrep,
137 int accuracy, double electricScale, double magneticScale, double startField,
138 double endField) {
139 if (R(2) < startField || R(2) >= endField) {
140 return;
141 }
142
143 if (R(2) < zbegin || R(2) >= zend) {
144 return;
145 }
146
147 Vector_t<double, 3> tmpE(0.0), tmpB(0.0);
148
149 computeField(R, tmpE, tmpB, FourCoefs, zbegin, length, xlrep, accuracy);
150
151 E += electricScale * tmpE;
152 B += magneticScale * tmpB;
153 }
154
155 template <class ViewType>
156 KOKKOS_INLINE_FUNCTION static void computeTravelingWaveField(
158 const ViewType& FourCoefs, double zbegin, double zend, double length, double xlrep,
159 int accuracy, double entryElectricScale, double entryMagneticScale,
160 double core1ElectricScale, double core1MagneticScale, double core2ElectricScale,
161 double core2MagneticScale, double exitElectricScale, double exitMagneticScale,
162 double startCoreField, double startExitField, double mappedStartExitField,
163 double periodLength, double cellLength, double elementLength) {
164 if (R(2) < -0.5 * periodLength || R(2) + 0.5 * periodLength >= elementLength) {
165 return;
166 }
167
168 Vector_t<double, 3> tmpR({R(0), R(1), R(2) + 0.5 * periodLength});
169 Vector_t<double, 3> tmpE(0.0), tmpB(0.0);
170
171 if (tmpR(2) < startCoreField) {
172 if (!(tmpR(2) >= zbegin && tmpR(2) < zend)) {
173 return;
174 }
175
176 computeField(tmpR, tmpE, tmpB, FourCoefs, zbegin, length, xlrep, accuracy);
177 E += entryElectricScale * tmpE;
178 B += entryMagneticScale * tmpB;
179 } else if (tmpR(2) < startExitField) {
180 tmpR(2) -= startCoreField;
181 const double z = tmpR(2);
182
183 tmpR(2) = tmpR(2) - periodLength * Kokkos::floor(tmpR(2) / periodLength);
184 tmpR(2) += startCoreField;
185
186 if (!(tmpR(2) >= zbegin && tmpR(2) < zend)) {
187 return;
188 }
189
190 computeField(tmpR, tmpE, tmpB, FourCoefs, zbegin, length, xlrep, accuracy);
191 E += core1ElectricScale * tmpE;
192 B += core1MagneticScale * tmpB;
193
194 tmpE = 0.0;
195 tmpB = 0.0;
196
197 tmpR(2) = z + cellLength;
198 tmpR(2) = tmpR(2) - periodLength * Kokkos::floor(tmpR(2) / periodLength);
199 tmpR(2) += startCoreField;
200
201 if (!(tmpR(2) >= zbegin && tmpR(2) < zend)) {
202 return;
203 }
204
205 computeField(tmpR, tmpE, tmpB, FourCoefs, zbegin, length, xlrep, accuracy);
206 E += core2ElectricScale * tmpE;
207 B += core2MagneticScale * tmpB;
208 } else {
209 tmpR(2) -= mappedStartExitField;
210
211 if (!(tmpR(2) >= zbegin && tmpR(2) < zend)) {
212 return;
213 }
214
215 computeField(tmpR, tmpE, tmpB, FourCoefs, zbegin, length, xlrep, accuracy);
216 E += exitElectricScale * tmpE;
217 B += exitMagneticScale * tmpB;
218 }
219 }
220
226 void applyField(std::shared_ptr<ParticleContainer_t> pc, double) override;
227
240 void applyRFField(
241 std::shared_ptr<ParticleContainer_t> pc, double electricScale, double magneticScale,
242 double startField, double endField);
243
279 std::shared_ptr<ParticleContainer_t> pc, double entryElectricScale,
280 double entryMagneticScale, double core1ElectricScale, double core1MagneticScale,
281 double core2ElectricScale, double core2MagneticScale, double exitElectricScale,
282 double exitMagneticScale, double startField, double startCoreField,
283 double startExitField, double mappedStartExitField, double periodLength,
284 double cellLength, double elementLength);
285
286 virtual void getOnaxisEz(std::vector<std::pair<double, double>>& F) override;
287
288private:
289 Astra1DDynamic(const std::string& filename);
291
292 void readMap() override;
293 void freeMap() override;
294
302 Kokkos::DualView<double*> FourCoefs_m;
303
305
307 double zbegin_m;
308 double zend_m;
311 double xlrep_m;
312
314 double length_m;
315
318
321
323 friend class Fieldmap;
324};
325
326#endif
ippl::Vector< T, Dim > Vector_t
DiffDirection
Definition Fieldmap.h:54
double xlrep_m
Wave number (omega / c)
static KOKKOS_INLINE_FUNCTION void computeTravelingWaveField(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B, const ViewType &FourCoefs, double zbegin, double zend, double length, double xlrep, int accuracy, double entryElectricScale, double entryMagneticScale, double core1ElectricScale, double core1MagneticScale, double core2ElectricScale, double core2MagneticScale, double exitElectricScale, double exitMagneticScale, double startCoreField, double startExitField, double mappedStartExitField, double periodLength, double cellLength, double elementLength)
void freeMap() override
Pure virtual method to free the map data.
virtual double getFrequency() const override
Get the frequency.
Kokkos::DualView< double * > FourCoefs_m
Fourier coefficients of Ez(z) (device-accessible) Stored as: [a0, a1, b1, a2, b2, ....
static KOKKOS_INLINE_FUNCTION void computeField(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B, const ViewType &FourCoefs, double zbegin, double length, double xlrep, int accuracy)
void applyRFField(std::shared_ptr< ParticleContainer_t > pc, double electricScale, double magneticScale, double startField, double endField)
Apply RF-scaled Astra1DDynamic field to all particles.
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F) override
static KOKKOS_INLINE_FUNCTION void computeRFField(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B, const ViewType &FourCoefs, double zbegin, double zend, double length, double xlrep, int accuracy, double electricScale, double magneticScale, double startField, double endField)
int accuracy_m
Number of Fourier modes used.
virtual void setFrequency(double freq) override
Set the frequency.
virtual void getInfo(Inform *msg) override
Print info about the field map.
virtual void swap() override
Swap coordinates.
bool isInside(const Vector_t< double, 3 > &r) const override
Checks if the given coordinate is inside the volume covered by the fieldmap.
void applyField(std::shared_ptr< ParticleContainer_t > pc, double) override
Apply the FM to all the particles.
void applyTravelingWave(std::shared_ptr< ParticleContainer_t > pc, double entryElectricScale, double entryMagneticScale, double core1ElectricScale, double core1MagneticScale, double core2ElectricScale, double core2MagneticScale, double exitElectricScale, double exitMagneticScale, double startField, double startCoreField, double startExitField, double mappedStartExitField, double periodLength, double cellLength, double elementLength)
Apply the traveling-wave RF field map to all particles.
int num_gridpz_m
Number of grid points in z-direction (input sampling)
double length_m
Effective periodic length of the field map [m].
void readMap() override
Pure virtual method to read the map data. Called by the public static readMap().
double zbegin_m
Z Bounds relative to element edge.
virtual void getFieldDimensions(double &zBegin, double &zEnd) const override
Get the longitudinal dimensions of the field.
virtual bool getFieldDerivative(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B, const DiffDirection &dir) const override
Get the field derivative with respect to a direction.
virtual bool getFieldstrength(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B) const override
Get the field strength at a given point.
Abstract base class for all field maps. It acts as a factory for creating specific field map types ba...
Definition Fieldmap.h:62
constexpr double two_pi
The value of.
Definition Physics.h:40
constexpr double c
The velocity of light in m/s.
Definition Physics.h:60
constexpr double pi
The value of.
Definition Physics.h:36