Line data Source code
1 : /** Unit tests for break condition, observer, boundary and tool modules */
2 :
3 : #include "crpropa/module/BreakCondition.h"
4 : #include "crpropa/module/Observer.h"
5 : #include "crpropa/module/Boundary.h"
6 : #include "crpropa/module/Tools.h"
7 : #include "crpropa/module/RestrictToRegion.h"
8 : #include "crpropa/ParticleID.h"
9 : #include "crpropa/Geometry.h"
10 :
11 : #include "gtest/gtest.h"
12 :
13 : namespace crpropa {
14 :
15 : //** ========================= Break conditions ============================= */
16 1 : TEST(MinimumEnergy, test) {
17 1 : MinimumEnergy minEnergy(5);
18 1 : Candidate c;
19 :
20 1 : c.current.setEnergy(5.1);
21 1 : minEnergy.process(&c);
22 1 : EXPECT_TRUE(c.isActive());
23 :
24 1 : c.current.setEnergy(4.9);
25 1 : minEnergy.process(&c);
26 1 : EXPECT_FALSE(c.isActive());
27 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
28 2 : }
29 :
30 1 : TEST(MinimumChargeNumber, test) {
31 1 : MinimumChargeNumber minChargeNumber(20);
32 1 : Candidate c;
33 :
34 1 : c.current.setId(nucleusId(56, 26));
35 1 : minChargeNumber.process(&c);
36 1 : EXPECT_TRUE(c.isActive());
37 :
38 1 : c.current.setId(-nucleusId(56, 26));
39 1 : minChargeNumber.process(&c);
40 1 : EXPECT_TRUE(c.isActive());
41 :
42 1 : c.current.setId(nucleusId(4, 2));
43 1 : minChargeNumber.process(&c);
44 1 : EXPECT_FALSE(c.isActive());
45 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
46 :
47 1 : c.setActive(true);
48 1 : c.removeProperty("Rejected");
49 :
50 1 : c.current.setId(-nucleusId(4, 2));
51 1 : minChargeNumber.process(&c);
52 1 : EXPECT_FALSE(c.isActive());
53 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
54 2 : }
55 :
56 1 : TEST(MinimumEnergyPerParticleId, test) {
57 1 : MinimumEnergyPerParticleId minEnergy(1);
58 1 : minEnergy.add(22, 10);
59 1 : minEnergy.add(12, 2);
60 1 : minEnergy.add(11, 20);
61 :
62 1 : Candidate c;
63 :
64 1 : c.current.setEnergy(20);
65 1 : c.current.setId(22);
66 1 : minEnergy.process(&c);
67 1 : EXPECT_TRUE(c.isActive());
68 :
69 1 : c.current.setEnergy(5);
70 1 : c.current.setId(22);
71 1 : minEnergy.process(&c);
72 1 : EXPECT_FALSE(c.isActive());
73 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
74 :
75 1 : c.setActive(true);
76 1 : c.removeProperty("Rejected");
77 :
78 1 : c.current.setEnergy(10);
79 1 : c.current.setId(11);
80 1 : minEnergy.process(&c);
81 1 : EXPECT_FALSE(c.isActive());
82 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
83 :
84 1 : c.setActive(true);
85 1 : c.removeProperty("Rejected");
86 :
87 1 : c.current.setEnergy(5);
88 1 : c.current.setId(12);
89 1 : minEnergy.process(&c);
90 1 : EXPECT_TRUE(c.isActive());
91 :
92 1 : c.current.setEnergy(0.1);
93 1 : c.current.setId(12);
94 1 : minEnergy.process(&c);
95 1 : EXPECT_FALSE(c.isActive());
96 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
97 1 : }
98 :
99 1 : TEST(MaximumTrajectoryLength, test) {
100 1 : MaximumTrajectoryLength maxLength(10);
101 1 : Candidate c;
102 :
103 1 : c.setTrajectoryLength(9.9);
104 1 : maxLength.process(&c);
105 1 : EXPECT_TRUE(c.isActive());
106 :
107 1 : c.setTrajectoryLength(10.1);
108 1 : maxLength.process(&c);
109 1 : EXPECT_FALSE(c.isActive());
110 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
111 2 : }
112 :
113 1 : TEST(MaximumTrajectoryLength, observer) {
114 1 : MaximumTrajectoryLength maxLength(12);
115 1 : maxLength.addObserverPosition(Vector3d(10, 0, 0));
116 1 : Candidate c;
117 1 : c.current.setPosition(Vector3d(5, 0, 0));
118 :
119 1 : c.setTrajectoryLength(5);
120 1 : maxLength.process(&c);
121 1 : EXPECT_TRUE(c.isActive());
122 :
123 1 : c.setTrajectoryLength(8);
124 1 : maxLength.process(&c);
125 1 : EXPECT_FALSE(c.isActive());
126 2 : }
127 :
128 1 : TEST(MinimumRedshift, test) {
129 1 : MinimumRedshift minZ; // default minimum redshift of 0
130 1 : Candidate c;
131 :
132 1 : c.setRedshift(0.1);
133 1 : minZ.process(&c);
134 1 : EXPECT_TRUE(c.isActive());
135 :
136 1 : c.setRedshift(0);
137 1 : minZ.process(&c);
138 1 : EXPECT_FALSE(c.isActive());
139 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
140 2 : }
141 :
142 1 : TEST(DetectionLength, test) {
143 1 : DetectionLength detL(10);
144 1 : detL.setMakeRejectedInactive(false);
145 1 : Candidate c;
146 1 : c.current.setPosition(Vector3d(5,0,0));
147 :
148 1 : c.setTrajectoryLength(2);
149 1 : detL.process(&c);
150 1 : EXPECT_TRUE(c.isActive());
151 :
152 1 : c.setCurrentStep(10);
153 1 : c.setTrajectoryLength(12);
154 1 : detL.process(&c);
155 1 : EXPECT_TRUE(c.isActive());
156 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
157 2 : }
158 :
159 : //** ============================= Observers ================================ */
160 1 : TEST(ObserverFeature, SmallSphere) {
161 : // detect if the current position is inside and the previous outside of the sphere
162 1 : Observer obs;
163 1 : obs.add(new ObserverSurface(new Sphere (Vector3d(0, 0, 0), 1)));
164 1 : Candidate c;
165 1 : c.setNextStep(10);
166 :
167 : // no detection: particle was inside already
168 1 : c.current.setPosition(Vector3d(0.9, 0, 0));
169 1 : c.previous.setPosition(Vector3d(0.95, 0, 0));
170 1 : obs.process(&c);
171 1 : EXPECT_TRUE(c.isActive());
172 :
173 : // limit step
174 1 : EXPECT_NEAR(c.getNextStep(), 0.1, 0.001);
175 :
176 : // detection: particle just entered
177 1 : c.current.setPosition(Vector3d(0.9, 0, 0));
178 1 : c.previous.setPosition(Vector3d(1.1, 0, 0));
179 1 : obs.process(&c);
180 1 : EXPECT_FALSE(c.isActive());
181 1 : }
182 :
183 1 : TEST(ObserverFeature, LargeSphere) {
184 : // detect if the current position is outside and the previous inside of the sphere
185 1 : Observer obs;
186 1 : obs.add(new ObserverSurface(new Sphere (Vector3d(0, 0, 0), 10)));
187 1 : Candidate c;
188 1 : c.setNextStep(10);
189 :
190 : // no detection: particle was outside already
191 1 : c.current.setPosition(Vector3d(11, 0, 0));
192 1 : c.previous.setPosition(Vector3d(10.5, 0, 0));
193 1 : obs.process(&c);
194 1 : EXPECT_TRUE(c.isActive());
195 :
196 : // limit step
197 1 : EXPECT_DOUBLE_EQ(c.getNextStep(), 1);
198 :
199 : // detection: particle just left
200 1 : c.current.setPosition(Vector3d(11, 0, 0));
201 1 : c.previous.setPosition(Vector3d(9.5, 0, 0));
202 1 : obs.process(&c);
203 1 : EXPECT_FALSE(c.isActive());
204 1 : }
205 :
206 1 : TEST(ObserverFeature, Point) {
207 1 : Observer obs;
208 1 : obs.add(new Observer1D());
209 1 : Candidate c;
210 1 : c.setNextStep(10);
211 :
212 : // no detection, limit step
213 1 : c.current.setPosition(Vector3d(5, 0, 0));
214 1 : obs.process(&c);
215 1 : EXPECT_TRUE(c.isActive());
216 :
217 : // limit step
218 1 : EXPECT_DOUBLE_EQ(5, c.getNextStep());
219 :
220 : // detection
221 1 : c.current.setPosition(Vector3d(0, 0, 0));
222 1 : obs.process(&c);
223 1 : EXPECT_FALSE(c.isActive());
224 1 : }
225 :
226 1 : TEST(ObserverFeature, DetectAll) {
227 : // DetectAll should detect all candidates
228 1 : Observer obs;
229 1 : obs.add(new ObserverDetectAll());
230 1 : Candidate c;
231 1 : obs.process(&c);
232 1 : EXPECT_FALSE(c.isActive());
233 1 : }
234 :
235 1 : TEST(ObserverFeature, TimeEvolution) {
236 1 : Observer obs;
237 1 : obs.setDeactivateOnDetection(false);
238 2 : obs.setFlag("Detected", "Detected");
239 : //min = 5, max = min + (numb-1)*dist = 5 + 1*5 = 10, detection can happen at [5, 10]
240 1 : obs.add(new ObserverTimeEvolution(5, 5, 2));
241 1 : Candidate c;
242 1 : c.setNextStep(10);
243 1 : c.setTrajectoryLength(3);
244 :
245 : // Simulate simple detections to guarantee ObserverTimeEvolution.checkDetection is working:
246 : // no detection, limit next step
247 1 : obs.process(&c);
248 1 : EXPECT_TRUE(c.isActive());
249 :
250 : // limit step
251 1 : EXPECT_DOUBLE_EQ(2, c.getNextStep());
252 :
253 : // detection one
254 1 : c.setCurrentStep(0.1);
255 1 : c.setTrajectoryLength(5);
256 1 : obs.process(&c);
257 1 : EXPECT_TRUE(c.isActive());
258 2 : EXPECT_TRUE(c.hasProperty("Detected"));
259 :
260 : // no detection expected
261 1 : obs.setDeactivateOnDetection(true); // set this to true, so it deactivates if a detection happens (not expected)
262 1 : c.setTrajectoryLength(8);
263 1 : obs.process(&c);
264 1 : EXPECT_TRUE(c.isActive());
265 :
266 : // detection two
267 1 : c.setCurrentStep(0.1);
268 1 : c.setTrajectoryLength(10.05);
269 1 : obs.process(&c);
270 1 : EXPECT_FALSE(c.isActive());
271 2 : EXPECT_TRUE(c.hasProperty("Detected"));
272 1 : }
273 :
274 1 : TEST(ObserverFeature, TimeEvolutionLog) {
275 1 : Observer obs;
276 1 : obs.setDeactivateOnDetection(false);
277 2 : obs.setFlag("Detected", "Detected");
278 : // usage of a log scaling for the observer
279 : bool log = true;
280 1 : obs.add(new ObserverTimeEvolution(10, 1000, 3, log));
281 1 : Candidate c;
282 : // choose a stepsize that is larger then distance to next detection at 10 to check step limitation
283 1 : c.setNextStep(10);
284 : // set length before next detection
285 1 : c.setTrajectoryLength(3);
286 :
287 : // Simulate simple detections to guarantee ObserverTimeEvolution.checkDetection is working:
288 : // no detection, limit next step
289 1 : obs.process(&c);
290 1 : EXPECT_TRUE(c.isActive());
291 :
292 : // limit step (should be 10-3=7)
293 1 : EXPECT_DOUBLE_EQ(7, c.getNextStep());
294 :
295 : // detection one
296 1 : c.setCurrentStep(0.1); // set small to be barely over first detection length
297 1 : c.setTrajectoryLength(10); // set to first detection length
298 1 : obs.process(&c);
299 1 : EXPECT_TRUE(c.isActive());
300 2 : EXPECT_TRUE(c.hasProperty("Detected"));
301 :
302 : // no detection expected
303 1 : obs.setDeactivateOnDetection(true); // set this to true, so it deactivates if a detection happens (not expected)
304 1 : c.setTrajectoryLength(80); // set to something between 10 and 100 (first and second detection)
305 1 : obs.process(&c);
306 1 : EXPECT_TRUE(c.isActive());
307 1 : obs.setDeactivateOnDetection(false); // reset to false again for future detection
308 :
309 : // detection two
310 1 : c.setCurrentStep(0.1);
311 1 : c.setTrajectoryLength(100);
312 1 : obs.process(&c);
313 1 : EXPECT_TRUE(c.isActive());
314 2 : EXPECT_TRUE(c.hasProperty("Detected"));
315 :
316 : // detection two
317 1 : obs.setDeactivateOnDetection(true); // deactivate here since it is the last detection
318 1 : c.setCurrentStep(0.1);
319 1 : c.setTrajectoryLength(1000.05);
320 1 : obs.process(&c);
321 1 : EXPECT_FALSE(c.isActive()); // not active anymore
322 2 : EXPECT_TRUE(c.hasProperty("Detected"));
323 1 : }
324 :
325 1 : TEST(ObserverFeature, TimeEvolutionArray) {
326 : // here it should be tested if the observer can be constructed with an array
327 1 : std::vector<double> times = {1, 2, 3};
328 1 : ObserverTimeEvolution obs(times);
329 1 : EXPECT_FALSE(obs.empty());
330 2 : EXPECT_TRUE(times == obs.getTimes()); // element wise comparison
331 :
332 1 : times.push_back(4);
333 1 : obs.addTime(4);
334 1 : EXPECT_FALSE(obs.empty());
335 2 : EXPECT_TRUE(times == obs.getTimes());
336 :
337 : // test clear:
338 1 : obs.clear();
339 1 : EXPECT_TRUE(obs.empty());
340 :
341 : // test addTimeRange for linear ranges
342 1 : times = {5, 6, 7, 8, 9, 10};
343 1 : obs.clear(); // empty detList
344 1 : EXPECT_TRUE(obs.empty());
345 1 : obs.addTimeRange(5, 10, 6, false);
346 1 : EXPECT_FALSE(obs.empty());
347 2 : EXPECT_TRUE(times == obs.getTimes());
348 :
349 : // test addTimeRange for logarithmic ranges
350 1 : times = {1, 10, 100, 1000};
351 1 : obs.clear(); // empty detList
352 1 : EXPECT_TRUE(obs.empty());
353 1 : obs.addTimeRange(1, 1000, 4, true);
354 1 : EXPECT_FALSE(obs.empty());
355 : // should be equal to above times array, but isnt, even though the values are the same
356 5 : for (int i=0; i<times.size(); i++)
357 4 : EXPECT_NEAR(times[i], obs.getTimes()[i], 0.01);
358 :
359 : // now check if constructDetListIfEmpty is working properly:
360 2 : ObserverTimeEvolution obs2(5, 10, 6, false);
361 2 : times = {5, 6, 7, 8, 9, 10};
362 :
363 : // check if no array is created while calling getTimes
364 1 : EXPECT_TRUE(obs2.empty());
365 2 : EXPECT_TRUE(times == obs2.getTimes());
366 1 : EXPECT_TRUE(obs2.empty());
367 :
368 : // check if array is created when calling addTime without array
369 1 : times.push_back(11);
370 1 : obs2.addTime(11);
371 1 : EXPECT_FALSE(obs2.empty());
372 2 : EXPECT_TRUE(times == obs2.getTimes());
373 1 : }
374 :
375 : //** ========================= Boundaries =================================== */
376 2 : TEST(PeriodicBox, high) {
377 : // Tests if the periodical boundaries place the particle back inside the box and translate the initial position accordingly.
378 : Vector3d origin(2, 2, 2);
379 : Vector3d size(2, 2, 2);
380 1 : PeriodicBox box(origin, size);
381 :
382 1 : Candidate c;
383 1 : c.current.setPosition(Vector3d(4.5, 4.3, 4.4));
384 1 : c.created.setPosition(Vector3d(3, 3, 3));
385 :
386 1 : box.process(&c);
387 :
388 1 : EXPECT_DOUBLE_EQ(2.5, c.current.getPosition().x);
389 1 : EXPECT_DOUBLE_EQ(1, c.created.getPosition().x);
390 1 : EXPECT_DOUBLE_EQ(2.3, c.current.getPosition().y);
391 1 : EXPECT_DOUBLE_EQ(1, c.created.getPosition().y);
392 1 : EXPECT_DOUBLE_EQ(2.4, c.current.getPosition().z);
393 1 : EXPECT_DOUBLE_EQ(1, c.created.getPosition().z);
394 2 : }
395 :
396 2 : TEST(PeriodicBox, low) {
397 : // Tests if the periodical boundaries place the particle back inside the box and translate the initial position accordingly.
398 : Vector3d origin(0, 0, 0);
399 : Vector3d size(2, 2, 2);
400 1 : PeriodicBox box(origin, size);
401 :
402 1 : Candidate c;
403 1 : c.current.setPosition(Vector3d(-2.5, -0.3, -0.4));
404 1 : c.created.setPosition(Vector3d(1, 1, 1));
405 :
406 1 : box.process(&c);
407 :
408 1 : EXPECT_DOUBLE_EQ(1.5, c.current.getPosition().x);
409 1 : EXPECT_DOUBLE_EQ(5, c.created.getPosition().x);
410 1 : EXPECT_DOUBLE_EQ(1.7, c.current.getPosition().y);
411 1 : EXPECT_DOUBLE_EQ(3, c.created.getPosition().y);
412 1 : EXPECT_DOUBLE_EQ(1.6, c.current.getPosition().z);
413 1 : EXPECT_DOUBLE_EQ(3, c.created.getPosition().z);
414 2 : }
415 :
416 2 : TEST(ReflectiveShell, inside) {
417 : // Tests if the reflective boundaries place the particle back inside the shell
418 : Vector3d center(0, 0, 0);
419 : double radius = 100;
420 1 : ReflectiveShell shell(center, radius);
421 :
422 1 : Candidate c;
423 1 : c.setCurrentStep(20);
424 1 : c.previous.setPosition(Vector3d(80, 20, 30));
425 1 : c.previous.setDirection(Vector3d(10, -1, -1));
426 : // un-reflected new position (outside shell) after full step
427 1 : Vector3d currentPosition = c.previous.getPosition() + c.previous.getDirection() * c.getCurrentStep() / c.previous.getDirection().getR();
428 1 : c.current.setPosition(currentPosition);
429 1 : c.current.setDirection(Vector3d(10, -1, -1));
430 : // process reflection
431 1 : shell.process(&c);
432 :
433 : // expected position & direction after reflection
434 : // calculated by hand with the same algorithm as implemented in Boundary.cpp
435 1 : EXPECT_NEAR(90.06356247, c.current.getPosition().x, 1e-7);
436 1 : EXPECT_NEAR(16.09256317, c.current.getPosition().y, 1e-7);
437 1 : EXPECT_NEAR(25.05646296, c.current.getPosition().z, 1e-7);
438 :
439 1 : EXPECT_NEAR(-0.67179589, c.current.getDirection().x, 1e-7);
440 1 : EXPECT_NEAR(-0.42786503, c.current.getDirection().y, 1e-7);
441 1 : EXPECT_NEAR(-0.60466668, c.current.getDirection().z, 1e-7);
442 2 : }
443 :
444 2 : TEST(ReflectiveBox, high) {
445 : // Tests if the reflective boundaries place the particle back inside the box and translate the initial position accordingly.
446 : // Also the initial and final directions are to be reflected
447 : Vector3d origin(10, 10, 10);
448 : Vector3d size(10, 20, 20);
449 1 : ReflectiveBox box(origin, size);
450 :
451 1 : Candidate c;
452 1 : c.source.setPosition(Vector3d(16, 17, 18));
453 1 : c.source.setDirection(Vector3d(1, 1.6, 1.8));
454 1 : c.created.setPosition(Vector3d(15, 15, 15));
455 1 : c.created.setDirection(Vector3d(0, 0.6, 0.8));
456 1 : c.previous.setPosition(Vector3d(15, 15, 29.5));
457 1 : c.previous.setDirection(Vector3d(0, 0.6, 0.8));
458 1 : c.current.setPosition(Vector3d(15, 15, 30.5));
459 1 : c.current.setDirection(Vector3d(0, 0.6, 0.8));
460 :
461 1 : box.process(&c);
462 :
463 1 : EXPECT_DOUBLE_EQ(16, c.source.getPosition().x);
464 1 : EXPECT_DOUBLE_EQ(17, c.source.getPosition().y);
465 1 : EXPECT_DOUBLE_EQ(42, c.source.getPosition().z);
466 :
467 1 : EXPECT_DOUBLE_EQ(15, c.created.getPosition().x);
468 1 : EXPECT_DOUBLE_EQ(15, c.created.getPosition().y);
469 1 : EXPECT_DOUBLE_EQ(45, c.created.getPosition().z);
470 :
471 1 : EXPECT_DOUBLE_EQ(15, c.previous.getPosition().x);
472 1 : EXPECT_DOUBLE_EQ(15, c.previous.getPosition().y);
473 1 : EXPECT_DOUBLE_EQ(30.5, c.previous.getPosition().z);
474 :
475 1 : EXPECT_DOUBLE_EQ(15, c.current.getPosition().x);
476 1 : EXPECT_DOUBLE_EQ(15, c.current.getPosition().y);
477 1 : EXPECT_DOUBLE_EQ(29.5, c.current.getPosition().z);
478 :
479 1 : EXPECT_DOUBLE_EQ(0, c.created.getDirection().x);
480 1 : EXPECT_DOUBLE_EQ(0.6, c.created.getDirection().y);
481 1 : EXPECT_DOUBLE_EQ(-0.8, c.created.getDirection().z);
482 :
483 1 : EXPECT_DOUBLE_EQ(0, c.previous.getDirection().x);
484 1 : EXPECT_DOUBLE_EQ(0.6, c.previous.getDirection().y);
485 1 : EXPECT_DOUBLE_EQ(-0.8, c.previous.getDirection().z);
486 :
487 1 : EXPECT_DOUBLE_EQ(0, c.current.getDirection().x);
488 1 : EXPECT_DOUBLE_EQ(0.6, c.current.getDirection().y);
489 1 : EXPECT_DOUBLE_EQ(-0.8, c.current.getDirection().z);
490 2 : }
491 :
492 2 : TEST(CubicBoundary, inside) {
493 1 : CubicBoundary cube(Vector3d(0, 0, 0), 10);
494 1 : Candidate c;
495 1 : c.current.setPosition(Vector3d(9, 5, 5));
496 1 : cube.process(&c);
497 1 : EXPECT_TRUE(c.isActive());
498 2 : }
499 :
500 2 : TEST(CubicBoundary, outside) {
501 1 : CubicBoundary cube(Vector3d(0, 0, 0), 10);
502 1 : Candidate c;
503 1 : c.current.setPosition(Vector3d(10.1, 5, 5));
504 1 : cube.process(&c);
505 1 : EXPECT_FALSE(c.isActive());
506 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
507 2 : }
508 :
509 2 : TEST(CubicBoundary, limitStepLower) {
510 1 : CubicBoundary cube(Vector3d(10, 10, 10), 10);
511 1 : cube.setLimitStep(true);
512 1 : cube.setMargin(1);
513 1 : Candidate c;
514 1 : c.current.setPosition(Vector3d(15, 15, 10.5));
515 1 : c.setNextStep(100);
516 1 : cube.process(&c);
517 1 : EXPECT_DOUBLE_EQ(1.5, c.getNextStep());
518 2 : }
519 :
520 2 : TEST(CubicBoundary, limitStepUpper) {
521 1 : CubicBoundary cube(Vector3d(-10, -10, -10), 10);
522 1 : cube.setLimitStep(true);
523 1 : cube.setMargin(1);
524 1 : Candidate c;
525 1 : c.current.setPosition(Vector3d(-5, -5, -0.5));
526 1 : c.setNextStep(100);
527 1 : cube.process(&c);
528 1 : EXPECT_DOUBLE_EQ(1.5, c.getNextStep());
529 2 : }
530 :
531 2 : TEST(SphericalBoundary, inside) {
532 1 : SphericalBoundary sphere(Vector3d(0, 0, 0), 10);
533 1 : Candidate c;
534 1 : c.current.setPosition(Vector3d(9, 0, 0));
535 1 : sphere.process(&c);
536 1 : EXPECT_TRUE(c.isActive());
537 1 : EXPECT_FALSE(c.hasProperty("Rejected"));
538 2 : }
539 :
540 2 : TEST(SphericalBoundary, outside) {
541 1 : SphericalBoundary sphere(Vector3d(0, 0, 0), 10);
542 2 : sphere.setRejectFlag("I passed the galactic border", "Nothing happened");
543 1 : Candidate c;
544 1 : c.current.setPosition(Vector3d(0, -10.1, 0));
545 1 : sphere.process(&c);
546 1 : EXPECT_FALSE(c.isActive());
547 2 : EXPECT_TRUE(c.hasProperty("I passed the galactic border"));
548 2 : }
549 :
550 2 : TEST(SphericalBoundary, limitStep) {
551 1 : SphericalBoundary sphere(Vector3d(0, 0, 0), 10);
552 1 : sphere.setLimitStep(true);
553 1 : sphere.setMargin(1);
554 1 : Candidate c;
555 1 : c.setNextStep(100);
556 1 : c.current.setPosition(Vector3d(0, 0, 9.5));
557 1 : sphere.process(&c);
558 1 : EXPECT_DOUBLE_EQ(1.5, c.getNextStep());
559 2 : }
560 :
561 2 : TEST(EllipsoidalBoundary, inside) {
562 1 : EllipsoidalBoundary ellipsoid(Vector3d(-5, 0, 0), Vector3d(5, 0, 0), 15);
563 1 : Candidate c;
564 1 : c.current.setPosition(Vector3d(3, 2, 0));
565 1 : ellipsoid.process(&c);
566 1 : EXPECT_TRUE(c.isActive());
567 1 : EXPECT_FALSE(c.hasProperty("Rejected"));
568 2 : }
569 :
570 2 : TEST(EllipsoidalBoundary, outside) {
571 1 : EllipsoidalBoundary ellipsoid(Vector3d(-5, 0, 0), Vector3d(5, 0, 0), 15);
572 1 : Candidate c;
573 1 : c.current.setPosition(Vector3d(0, 25, 0));
574 1 : ellipsoid.process(&c);
575 1 : EXPECT_FALSE(c.isActive());
576 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
577 2 : }
578 :
579 2 : TEST(EllipsoidalBoundary, limitStep) {
580 1 : EllipsoidalBoundary ellipsoid(Vector3d(-5, 0, 0), Vector3d(5, 0, 0), 15);
581 1 : ellipsoid.setLimitStep(true);
582 1 : ellipsoid.setMargin(0.5);
583 1 : Candidate c;
584 1 : c.setNextStep(2);
585 1 : c.current.setPosition(Vector3d(7, 0, 0));
586 1 : ellipsoid.process(&c);
587 1 : EXPECT_DOUBLE_EQ(c.getNextStep(), 1.5);
588 2 : }
589 :
590 2 : TEST(CylindricalBoundary, inside) {
591 1 : CylindricalBoundary cylinder(Vector3d(0, 0, 0), 2, 15);
592 1 : Candidate c;
593 1 : c.current.setPosition(Vector3d(6, -3, 0.5));
594 1 : cylinder.process(&c);
595 1 : EXPECT_TRUE(c.isActive());
596 1 : EXPECT_FALSE(c.hasProperty("Rejected"));
597 2 : }
598 :
599 2 : TEST(CylindricalBoundary, outside) {
600 1 : CylindricalBoundary cylinder(Vector3d(0, 0, 0), 2, 15);
601 1 : Candidate c;
602 1 : c.current.setPosition(Vector3d(6, -3, 1.5));
603 1 : cylinder.process(&c);
604 1 : EXPECT_FALSE(c.isActive());
605 2 : EXPECT_TRUE(c.hasProperty("Rejected"));
606 2 : }
607 :
608 2 : TEST(CylindricalBoundary, limitStep) {
609 1 : CylindricalBoundary cylinder(Vector3d(0, 0, 0), 2, 15);
610 1 : cylinder.setLimitStep(true);
611 1 : cylinder.setMargin(0.5);
612 1 : Candidate c;
613 1 : c.setNextStep(2);
614 1 : c.current.setPosition(Vector3d(7, 0, 0));
615 1 : cylinder.process(&c);
616 1 : EXPECT_DOUBLE_EQ(c.getNextStep(), 1.5);
617 2 : }
618 :
619 1 : TEST(RestrictToRegion, RestrictToRegion) {
620 :
621 1 : ref_ptr<Observer> obs = new Observer();
622 1 : obs->add(new ObserverDetectAll());
623 1 : RestrictToRegion R(obs, new Sphere(Vector3d(0, 0, 0), 10));
624 :
625 1 : Candidate c;
626 1 : c.previous.setPosition(Vector3d(13,0,0));
627 1 : c.current.setPosition(Vector3d(12,0,0));
628 1 : R.process(&c);
629 1 : EXPECT_TRUE(c.isActive());
630 1 : c.current.setPosition(Vector3d(9,0,0));
631 1 : R.process(&c);
632 1 : EXPECT_FALSE(c.isActive());
633 2 : }
634 :
635 :
636 0 : int main(int argc, char **argv) {
637 0 : ::testing::InitGoogleTest(&argc, argv);
638 0 : return RUN_ALL_TESTS();
639 : }
640 :
641 : } // namespace crpropa
|