33#include "gtest/gtest.h"
44 MockComponent(
const MockComponent& )
46 ~MockComponent()
override =
default;
51 bool apply(
const std::shared_ptr<ParticleContainer_t>& )
override {
return false; }
60 if (r(0) < 0. || r(0) > 1. || r(1) < -1. || r(1) > 0. || r(2) < 0. || r(2) > 1.) {
79 bool bends()
const override {
return true; }
86 std::unique_ptr<NullField> field_m;
89 void setOneAttribute(
DumpEMFields* dump,
const std::string& name,
const double value) {
93 void setAttributesCart(
94 DumpEMFields* dump,
const double x0,
const double dx,
const double nx,
const double y0,
95 const double dy,
const double ny,
const double z0,
const double dz,
const double nz,
96 const double t0,
const double dt,
const double nt,
const std::string& filename) {
97 setOneAttribute(dump,
"X_START", x0);
98 setOneAttribute(dump,
"DX", dx);
99 setOneAttribute(dump,
"X_STEPS", nx);
100 setOneAttribute(dump,
"Y_START", y0);
101 setOneAttribute(dump,
"DY", dy);
102 setOneAttribute(dump,
"Y_STEPS", ny);
103 setOneAttribute(dump,
"Z_START", z0);
104 setOneAttribute(dump,
"DZ", dz);
105 setOneAttribute(dump,
"Z_STEPS", nz);
106 setOneAttribute(dump,
"T_START", t0);
107 setOneAttribute(dump,
"DT", dt);
108 setOneAttribute(dump,
"T_STEPS", nt);
112 void setOriginCyl(
DumpEMFields* dump,
const double xc,
const double yc,
const double zc) {
113 setOneAttribute(dump,
"CYL_ORIGIN_X", xc);
114 setOneAttribute(dump,
"CYL_ORIGIN_Y", yc);
115 setOneAttribute(dump,
"CYL_ORIGIN_Z", zc);
118 void setAttributesCyl(
119 DumpEMFields* dump,
const double r0,
const double dr,
const double nr,
120 const double phi0,
const double dphi,
const double nphi,
const double y0,
121 const double dy,
const double ny,
const double t0,
const double dt,
const double nt,
122 const std::string& filename) {
123 setOneAttribute(dump,
"R_START", r0);
124 setOneAttribute(dump,
"DR", dr);
125 setOneAttribute(dump,
"R_STEPS",
nr);
126 setOneAttribute(dump,
"PHI_START", phi0);
127 setOneAttribute(dump,
"DPHI", dphi);
128 setOneAttribute(dump,
"PHI_STEPS", nphi);
129 setOneAttribute(dump,
"Y_START", y0);
130 setOneAttribute(dump,
"DY", dy);
131 setOneAttribute(dump,
"Y_STEPS", ny);
132 setOneAttribute(dump,
"T_START", t0);
133 setOneAttribute(dump,
"DT", dt);
134 setOneAttribute(dump,
"T_STEPS", nt);
139 TEST(TestDumpEMFields, ConstructorDestructor) {
145 setAttributesCart(dump2, 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
"/dev/null");
151 void execute_throws(
DumpEMFields* dump,
const std::string& reason) {
154 EXPECT_TRUE(
false) << reason;
160 TEST(TestDumpEMFields, executeTest) {
161 gmsg =
new Inform(
nullptr, -1);
164 execute_throws(&dump1,
"should throw due to nsteps < 1");
165 setAttributesCart(&dump1, 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
"/dev/null");
167 std::set<std::shared_ptr<Component>>
elements;
168 elements.insert(std::make_shared<MockComponent>());
171 &dump1, -1., -1., 1., -1., -1., 1., -1., -1., 1., 1., 1., 1.,
"/dev/null");
174 &dump1, -1., -1., 0., -1., -1., 1., -1., -1., 1., 1., 1., 1.,
"/dev/null");
175 execute_throws(&dump1,
"should throw due to nsteps x < 1");
177 &dump1, -1., -1., 1., -1., -1., 0., -1., -1., 1., 1., 1., 1.,
"/dev/null");
178 execute_throws(&dump1,
"should throw due to nsteps y < 1");
180 &dump1, -1., -1., 1., -1., -1., 1., -1., -1., 0., 1., 1., 1.,
"/dev/null");
181 execute_throws(&dump1,
"should throw due to nsteps z < 1");
183 &dump1, -1., -1., 1., -1., -1., 1., -1., -1., 1., 1., 1., 0.,
"/dev/null");
184 execute_throws(&dump1,
"should throw due to nsteps t < 1");
186 &dump1, -1., -1., 1., -1., -1., 1., -1., -1., 1.5, 2., 2., 1.,
"/dev/null");
187 execute_throws(&dump1,
"should throw due to nsteps not integer");
191 void clear_files(std::set<std::string>
const& files) {
194 for (
const std::string& fname : files) {
199 TEST(TestDumpEMFields, writeFieldsCartTest) {
201 std::filesystem::create_directory(auxDirectory);
202 gmsg =
new Inform(
nullptr, -1);
203 std::string fname1 =
"test5";
204 std::string fname2 =
"test6";
205 std::string fname3 =
"test7";
206 std::string fname4 =
"test8";
208 clear_files({fname1, fname2, fname3, fname4});
210 setAttributesCart(&dump1, 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., fname1);
213 setAttributesCart(&dump2, 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., fname2);
216 setAttributesCart(&dump3, 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., fname3);
219 setAttributesCart(&dump4, 0.1, 0.1, 3., -0.1, 0.2, 2., 0.2, 0.3, 2., 1., 1., 2., fname4);
221 std::set<std::shared_ptr<Component>>
elements;
222 elements.insert(std::make_shared<MockComponent>());
226 EXPECT_TRUE(
false) <<
"Threw OpalException on writefields: " << exc.
what() << std::endl;
229 EXPECT_TRUE(fin1.good());
231 EXPECT_TRUE(fin2.good());
233 EXPECT_FALSE(fin3.good());
235 EXPECT_TRUE(fin4.good());
238 EXPECT_EQ(n_lines, 24);
239 std::string test_line;
240 for (
size_t i = 0; i < 12; ++i) {
241 std::getline(fin4, test_line);
243 std::vector line(10, 0.);
244 for (
size_t line_index = 0; line_index < 24; ++line_index) {
246 for (
size_t i = 0; i < 10; ++i) {
249 if (line_index == 0) {
250 EXPECT_NEAR(line[0], 0.1, tol);
251 EXPECT_NEAR(line[1], -0.1, tol);
252 EXPECT_NEAR(line[2], 0.2, tol);
253 EXPECT_NEAR(line[3], 1., tol);
256 EXPECT_NEAR(line[4], line[0], tol);
257 EXPECT_NEAR(line[5], line[1], tol);
258 EXPECT_NEAR(line[6], line[2], tol);
260 EXPECT_NEAR(line[4], 0., tol);
261 EXPECT_NEAR(line[5], 0., tol);
262 EXPECT_NEAR(line[6], 0., tol);
268 clear_files({fname1, fname2, fname3, fname4});
272 TEST(TestDumpEMFields, writeFieldsCylTest) {
274 std::filesystem::create_directory(auxDirectory);
275 gmsg =
new Inform(
nullptr, -1);
277 std::string fnameCyl =
"testCyl";
279 clear_files({fnameCyl});
282 &dump, 0.1, 0.1, 3., 90. *
Units::deg2rad, 45. *
Units::deg2rad, 16, 0.2, 0.3, 2.,
283 1., 1., 2., fnameCyl);
286 std::set<std::shared_ptr<Component>>
elements;
287 elements.insert(std::make_shared<MockComponent>());
291 EXPECT_TRUE(
false) <<
"Threw OpalException on writefields: " << exc.
what() << std::endl;
294 EXPECT_TRUE(fin.good());
297 EXPECT_EQ(n_lines, 192);
298 std::string test_line;
299 for (
size_t i = 0; i < 12; ++i) {
300 std::getline(fin, test_line);
302 std::vector line(10, 0.);
303 for (
size_t line_index = 0; line_index < 24; ++line_index) {
304 constexpr double tol = 1
e-9;
305 for (
size_t i = 0; i < 10; ++i) {
308 if (line_index == 0) {
309 EXPECT_NEAR(line[0], 0.1, tol);
310 EXPECT_NEAR(line[1], 90, tol);
311 EXPECT_NEAR(line[2], 0.2, tol);
312 EXPECT_NEAR(line[3], 1., tol);
314 while (line[1] > 360.) {
318 EXPECT_NEAR(line[4] * line[4] + line[5] * line[5], line[0] * line[0], tol);
319 EXPECT_NEAR(line[6], line[2], tol);
321 EXPECT_NEAR(line[4], 0., tol);
322 EXPECT_NEAR(line[5], 0., tol);
323 EXPECT_NEAR(line[6], 0., tol);
329 clear_files({fnameCyl});
335 TEST(TestDumpEMFields, writeFieldsCylOriginTest) {
337 std::filesystem::create_directory(auxDirectory);
338 gmsg =
new Inform(
nullptr, -1);
340 std::string fnameCyl1 =
"testCyl1";
341 std::string fnameCyl2 =
"testCyl2";
343 clear_files({fnameCyl1, fnameCyl2});
346 &dump1, 0.1, 0., 1., 45. *
Units::deg2rad, 0., 1, -0.1, 0., 1., 1., 0., 1.,
351 &dump2, 0.1, 0., 1., 45. *
Units::deg2rad, 0., 1, -0.1, 0., 1., 1., 0., 1.,
353 setOriginCyl(&dump2, 0.01, 0.02, 0.03);
356 std::set<std::shared_ptr<Component>>
elements;
357 elements.insert(std::make_shared<MockComponent>());
361 EXPECT_TRUE(
false) <<
"Threw OpalException on writefields: " << exc.
what() << std::endl;
365 EXPECT_TRUE(fin1.good());
368 EXPECT_EQ(n_lines, 1);
369 std::string test_line;
370 for (
size_t i = 0; i < 12; ++i) {
371 std::getline(fin1, test_line);
373 std::vector line1(10, 0.);
374 for (
size_t i = 0; i < 10; ++i) {
379 EXPECT_TRUE(fin2.good());
381 EXPECT_EQ(n_lines, 1);
382 for (
size_t i = 0; i < 12; ++i) {
383 std::getline(fin2, test_line);
385 std::vector line2(10, 0.);
386 for (
size_t i = 0; i < 10; ++i) {
389 EXPECT_EQ(line1[0], line2[0]);
390 EXPECT_EQ(line1[1], line2[1]);
391 EXPECT_EQ(line1[2], line2[2]);
392 EXPECT_EQ(line1[3], line2[3]);
393 EXPECT_NE(line1[4], line2[4]);
394 EXPECT_NE(line1[5], line2[5]);
395 EXPECT_NE(line1[6], line2[6]);
396 EXPECT_NE(line1[7], line2[7]);
397 EXPECT_NE(line1[8], line2[8]);
398 EXPECT_NE(line1[9], line2[9]);
399 clear_files({fnameCyl1, fnameCyl2});
403 TEST(TestDumpEMFields, BadGrid) {
405 std::filesystem::create_directory(auxDirectory);
406 gmsg =
new Inform(
nullptr, -1);
408 std::string fnameCyl =
"testCyl";
410 clear_files({fnameCyl});
413 &dump, 0.1, 0.1, 3., 90. *
Units::deg2rad, 45. *
Units::deg2rad, 16, 0.2, 0.3, 2.,
414 1., 1., 2., fnameCyl);
417 std::set<std::shared_ptr<Component>>
elements;
418 elements.insert(std::make_shared<MockComponent>());
423 TEST(TestDumpEMFields, CouldNotOpenFile) {
425 std::filesystem::create_directory(auxDirectory);
426 gmsg =
new Inform(
nullptr, -1);
427 std::string fnameCyl =
".";
430 &dump, 0.1, 0.1, 3., 90. *
Units::deg2rad, 45. *
Units::deg2rad, 16, 0.2, 0.3, 2.,
431 1., 1., 2., fnameCyl);
433 std::set<std::shared_ptr<Component>>
elements;
434 elements.insert(std::make_shared<MockComponent>());
439 TEST(TestDumpEMFields, WriteFailure) {
441 std::filesystem::create_directory(auxDirectory);
442 gmsg =
new Inform(
nullptr, -1);
443 std::string fnameCyl =
"testCyl";
446 &dump, 0.1, 0.1, 3., 90. *
Units::deg2rad, 45. *
Units::deg2rad, 16, 0.2, 0.3, 2.,
447 1., 1., 2., fnameCyl);
450 std::set<std::shared_ptr<Component>>
elements;
451 elements.insert(std::make_shared<MockComponent>());
Defines the abstract interface for a single beamline component in the accelerator model.
ippl::Vector< T, Dim > Vector_t
TEST(IndexMapTest, RebuildsReferencePathModelFromOrderedRanges)
Abstract base class for accelerator geometry classes.
virtual bool applyToReferenceParticle(const Vector_t< double, 3 > &R, const Vector_t< double, 3 > &P, const double &t, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B)
Apply to reference particle with position R and momemtum P.
virtual void finalise()=0
virtual void initialise(PartBunch_t *bunch, double &startField, double &endField)=0
virtual EMField & getField()=0
Return field.
virtual void getFieldExtend(double &zBegin, double &zEnd) const =0
Return the field-support extent of the component.
virtual bool bends() const =0
virtual bool apply(const std::shared_ptr< ParticleContainer_t > &pc)
Apply to all particles. Kernel launch moved inside the function.
static void writeFields(const std::set< std::shared_ptr< Component > > &elements)
Abstract base class for electromagnetic fields.
virtual void accept(BeamlineVisitor &visitor) const =0
Apply visitor.
virtual ElementBase * clone() const =0
Return clone.
virtual BGeometryBase & getGeometry()=0
Get geometry.
A zero electromagnetic field.
Geometry representing an identity transform.
virtual Attribute * findAttribute(const std::string &name)
Find an attribute by name.
static OpalData * getInstance()
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
virtual const std::string & what() const
Return the message string for the exception.
void setString(Attribute &attr, const std::string &val)
Set string value.
void setReal(Attribute &attr, double val)
Set real value.
void setPredefinedString(Attribute &attr, const std::string &val)
Set predefined string value.
constexpr double e
The value of.
constexpr double MVpm2Vpm
std::string combineFilePath(std::initializer_list< std::string > ilist)