OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestMonitor.cpp
Go to the documentation of this file.
1
45#include <gtest/gtest.h>
46
47#include "Ippl.h"
48
49#define private public
50#include "AbsBeamline/Monitor.h"
51#undef private
52
53#include "Fields/EMField.h"
54
55#include <memory>
56
57// ---------------------------------------------------------------------------
58// Dummy field
59// ---------------------------------------------------------------------------
60class DummyMonitorField : public EMField {
61public:
62 void scale(double) override {}
63};
64
65// ---------------------------------------------------------------------------
66// Minimal concrete Monitor
67// ---------------------------------------------------------------------------
68class TestMonitor : public Monitor {
69public:
70 TestMonitor() : Monitor("test_monitor") { geom_.setElementLength(0.01); }
71
72 explicit TestMonitor(const std::string& name) : Monitor(name) { geom_.setElementLength(0.01); }
73
74 ElementBase* clone() const override { return new TestMonitor(*this); }
75
76 StraightGeometry& getGeometry() override { return geom_; }
77
78 const StraightGeometry& getGeometry() const override { return geom_; }
79
80 EMField& getField() override { return field_; }
81
82 const EMField& getField() const override { return field_; }
83
84 Plane getPlane() const override { return plane_; }
85
86 void setPlane(Plane plane) { plane_ = plane; }
87
88 void setLength(double length) { geom_.setElementLength(length); }
89
90private:
94};
95
96// ---------------------------------------------------------------------------
97// Fixture
98// ---------------------------------------------------------------------------
99class MonitorTest : public ::testing::Test {
100protected:
101 static void SetUpTestSuite() {
102 int argc = 0;
103 char** argv = nullptr;
104 ippl::initialize(argc, argv);
105 }
106
107 static void TearDownTestSuite() { ippl::finalize(); }
108};
109
110// ---------------------------------------------------------------------------
111// Basic API
112// ---------------------------------------------------------------------------
114 TestMonitor monitor;
115
116 EXPECT_EQ(monitor.getType(), ElementType::MONITOR);
117}
118
120 TestMonitor monitor;
121
122 EXPECT_FALSE(monitor.bends());
123}
124
125TEST_F(MonitorTest, RequiredNumberOfTimeStepsIsOne) {
126 TestMonitor monitor;
127
128 EXPECT_EQ(monitor.getRequiredNumberOfTimeSteps(), 1);
129}
130
131TEST_F(MonitorTest, GetFieldExtendUsesMonitorHalfLength) {
132 TestMonitor monitor;
133
134 double zBegin = 0.0;
135 double zEnd = 0.0;
136
137 monitor.getFieldExtend(zBegin, zEnd);
138
139 EXPECT_DOUBLE_EQ(zBegin, -0.005);
140 EXPECT_DOUBLE_EQ(zEnd, 0.005);
141}
142
143// ---------------------------------------------------------------------------
144// Collection type bookkeeping
145// ---------------------------------------------------------------------------
146TEST_F(MonitorTest, DefaultCollectionTypeIsSpatial) {
147 TestMonitor monitor;
148
149 EXPECT_EQ(monitor.type_m, CollectionType::SPATIAL);
150}
151
152TEST_F(MonitorTest, SetCollectionTypeUpdatesCollectionMode) {
153 TestMonitor monitor;
154
156
157 EXPECT_EQ(monitor.type_m, CollectionType::TEMPORAL);
158
160
161 EXPECT_EQ(monitor.type_m, CollectionType::SPATIAL);
162}
163
164// ---------------------------------------------------------------------------
165// Plane bookkeeping
166// ---------------------------------------------------------------------------
167TEST_F(MonitorTest, PlaneDefaultsToOff) {
168 TestMonitor monitor;
169
170 EXPECT_EQ(monitor.getPlane(), Monitor::OFF);
171}
172
173TEST_F(MonitorTest, PlaneCanBeSetInConcreteTestMonitor) {
174 TestMonitor monitor;
175
176 monitor.setPlane(Monitor::XY);
177
178 EXPECT_EQ(monitor.getPlane(), Monitor::XY);
179}
180
181// ---------------------------------------------------------------------------
182// Geometry / containment
183// ---------------------------------------------------------------------------
184TEST_F(MonitorTest, IsInsideAcceptsPointsWithinElementLength) {
185 TestMonitor monitor;
186 monitor.setLength(0.02);
187
188 EXPECT_TRUE(monitor.isInside(Vector_t<double, 3>(0.0, 0.0, 0.0)));
189 EXPECT_TRUE(monitor.isInside(Vector_t<double, 3>(0.0, 0.0, 0.009)));
190 EXPECT_TRUE(monitor.isInside(Vector_t<double, 3>(0.0, 0.0, -0.009)));
191}
192
193TEST_F(MonitorTest, IsInsideRejectsPointsOutsideElementLength) {
194 TestMonitor monitor;
195 monitor.setLength(0.02);
196
197 EXPECT_FALSE(monitor.isInside(Vector_t<double, 3>(0.0, 0.0, 0.011)));
198 EXPECT_FALSE(monitor.isInside(Vector_t<double, 3>(0.0, 0.0, -0.011)));
199}
200
201// ---------------------------------------------------------------------------
202// Safe early returns
203// ---------------------------------------------------------------------------
204TEST_F(MonitorTest, ApplyNullParticleContainerReturnsFalse) {
205 TestMonitor monitor;
206
207 EXPECT_FALSE(monitor.apply(std::shared_ptr<ParticleContainer_t>()));
208}
209
210TEST_F(MonitorTest, ApplyToReferenceParticleWithoutReferenceBunchReturnsFalse) {
211 TestMonitor monitor;
212
213 Vector_t<double, 3> R(0.0);
214 Vector_t<double, 3> P(0.0);
215 Vector_t<double, 3> E(1.0);
216 Vector_t<double, 3> B(2.0);
217
218 monitor.goOnline(0.0);
219
220 EXPECT_FALSE(monitor.applyToReferenceParticle(R, P, 0.0, E, B));
221}
222
223TEST_F(MonitorTest, FieldOnlyApplyOverloadReturnsFalseAndLeavesFieldsUnchanged) {
224 TestMonitor monitor;
225
226 Vector_t<double, 3> R(0.0);
227 Vector_t<double, 3> P(0.0);
228
229 Vector_t<double, 3> E(0.0);
230 E(0) = 1.0;
231 E(1) = 2.0;
232 E(2) = 3.0;
233
234 Vector_t<double, 3> B(0.0);
235 B(0) = 4.0;
236 B(1) = 5.0;
237 B(2) = 6.0;
238
239 EXPECT_FALSE(monitor.apply(R, P, 0.0, E, B));
240
241 EXPECT_DOUBLE_EQ(E(0), 1.0);
242 EXPECT_DOUBLE_EQ(E(1), 2.0);
243 EXPECT_DOUBLE_EQ(E(2), 3.0);
244
245 EXPECT_DOUBLE_EQ(B(0), 4.0);
246 EXPECT_DOUBLE_EQ(B(1), 5.0);
247 EXPECT_DOUBLE_EQ(B(2), 6.0);
248}
249
250TEST_F(MonitorTest, GoOfflineWithoutLossDataSinkIsSafe) {
251 TestMonitor monitor;
252
253 EXPECT_NO_THROW(monitor.goOffline());
254}
ippl::Vector< T, Dim > Vector_t
TEST_F(MonitorTest, GetType)
void scale(double) override
Scale the field.
Abstract base class for electromagnetic fields.
Definition EMField.h:171
static void TearDownTestSuite()
static void SetUpTestSuite()
CollectionType type_m
Definition Monitor.h:109
virtual void goOnline(const double &kineticEnergy) override
Definition Monitor.cpp:358
Plane
Plane selection.
Definition Monitor.h:36
@ XY
Monitor acts on both planes.
Definition Monitor.h:44
@ OFF
Monitor is off (inactive).
Definition Monitor.h:38
virtual ElementType getType() const override
Definition Monitor.cpp:383
virtual bool isInside(const Vector_t< double, 3 > &r) const override
Definition Monitor.h:122
virtual void goOffline() override
Definition Monitor.cpp:360
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) override
Apply to reference particle with position R and momemtum P.
Definition Monitor.cpp:259
virtual int getRequiredNumberOfTimeSteps() const override
Definition Monitor.h:120
virtual bool bends() const override
Definition Monitor.cpp:375
virtual void getFieldExtend(double &zBegin, double &zEnd) const override
Return the field-support extent of the component.
Definition Monitor.cpp:377
void setCollectionType(CollectionType type)
Definition Monitor.h:118
virtual bool apply(const std::shared_ptr< ParticleContainer_t > &pc) override
Apply to all particles. Kernel launch moved inside the function.
Definition Monitor.cpp:58
A geometry representing a straight line.
virtual void setElementLength(double length)
Set design length.
TestMonitor(const std::string &name)
const StraightGeometry & getGeometry() const override
Get geometry. Version for const object.
DummyMonitorField field_
StraightGeometry geom_
const EMField & getField() const override
Return field.
Plane getPlane() const override
Get plane on which monitor observes.
StraightGeometry & getGeometry() override
Get geometry.
void setPlane(Plane plane)
void setLength(double length)
ElementBase * clone() const override
Return clone.
EMField & getField() override
Return field.