7 def test_setPositionsAndVelocities_getPositions_getVelocities():
13 nullVector = Vector3DReal(0.0, 0.0, 0.0)
15 pc = ParticleCollection()
16 pc.setPositionsAndVelocities([nullVector], [nullVector])
18 with pytest.raises(TypeError):
19 pc = ParticleCollection()
20 pc.setPositionsAndVelocities((nullVector,), [nullVector])
21 with pytest.raises(TypeError):
22 pc = ParticleCollection()
23 pc.setPositionsAndVelocities([nullVector], (nullVector,))
25 with pytest.raises(TypeError):
26 pc = ParticleCollection()
27 pc.setPositionsAndVelocities([[0.0, 0.0, 0.0]], [nullVector])
28 with pytest.raises(TypeError):
29 pc = ParticleCollection()
30 pc.setPositionsAndVelocities([nullVector], [[0.0, 0.0, 0.0]])
33 with pytest.raises(ValueError):
34 pc = ParticleCollection()
35 pc.setPositionsAndVelocities([nullVector, nullVector], [nullVector])
36 with pytest.raises(ValueError):
37 pc = ParticleCollection()
38 pc.setPositionsAndVelocities([nullVector], [nullVector, nullVector])
41 pc = ParticleCollection()
45 positions.append(Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0))
46 velocities.append(Vector3DReal(1.23 / (x + 1), x, -x))
48 pc.setPositionsAndVelocities(positions, velocities)
50 assert isinstance(pc.getPositions(), list)
51 assert isinstance(pc.getVelocities(), list)
53 assert isinstance(pc.getPositions()[x], Vector3DReal)
54 assert isinstance(pc.getVelocities()[x], Vector3DReal)
57 pc.getPositions()[x] == Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0)
59 pc.getVelocities()[x] == Vector3DReal(1.23 / (x + 1), x, -x)
62 positions[0].normalize()
63 velocities[1].normalize()
64 assert isinstance(pc.getPositions(), list)
65 assert isinstance(pc.getVelocities(), list)
67 assert isinstance(pc.getPositions()[x], Vector3DReal)
68 assert isinstance(pc.getVelocities()[x], Vector3DReal)
71 pc.getPositions()[x] == Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0)
73 pc.getVelocities()[x] == Vector3DReal(1.23 / (x + 1), x, -x)
76 def test_setUniformMass():
79 pc = ParticleCollection()
80 with pytest.raises(TypeError):
81 pc.setUniformMass([1.0])
83 pc = ParticleCollection()
84 with pytest.raises(ValueError):
85 pc.setUniformMass(-0.5)
87 pc = ParticleCollection()
88 pc.setUniformMass(0.0)
89 pc.setUniformMass(1.0)
90 pc.setUniformMass(1.5)
95 nullVector = Vector3DReal(0, 0, 0)
97 pc.setPositionsAndVelocities([nullVector], [nullVector])
98 pc.setUniformMass(0.0)
99 pc.setUniformMass(1.0)
100 pc.setUniformMass(1.5)
106 pc = ParticleCollection()
111 nullVector = Vector3DReal(0, 0, 0)
113 pc.setPositionsAndVelocities([nullVector], [nullVector])
115 assert not pc.isEmpty()
116 assert not pc.isEmpty()
118 twice = [nullVector, nullVector]
119 pc.setPositionsAndVelocities(twice, twice)
121 assert not pc.isEmpty()
122 assert not pc.isEmpty()
125 def test_getParticleCount():
126 pc = ParticleCollection()
127 assert pc.getParticleCount() == 0
128 assert pc.getParticleCount() == 0
129 assert isinstance(pc.getParticleCount(), int)
132 nullVector = Vector3DReal(0, 0, 0)
134 pc.setPositionsAndVelocities([nullVector], [nullVector])
136 assert pc.getParticleCount() == 1
137 assert pc.getParticleCount() == 1
138 assert isinstance(pc.getParticleCount(), int)
140 twice = [nullVector, nullVector]
141 pc.setPositionsAndVelocities(twice, twice)
143 assert pc.getParticleCount() == 2
144 assert pc.getParticleCount() == 2
145 assert isinstance(pc.getParticleCount(), int)
148 def test_getPosition_getVelocity():
154 pc = ParticleCollection()
155 with pytest.raises(IndexError):
157 with pytest.raises(IndexError):
159 with pytest.raises(IndexError):
161 with pytest.raises(IndexError):
165 pc = ParticleCollection()
168 for x
in range(0, 5):
169 positions.append(Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0))
170 velocities.append(Vector3DReal(1.23 / (x + 1), x, -x))
172 pc.setPositionsAndVelocities(positions, velocities)
175 with pytest.raises(TypeError):
177 with pytest.raises(TypeError):
180 with pytest.raises(IndexError):
182 with pytest.raises(IndexError):
183 pc.getPosition(pc.getParticleCount())
184 with pytest.raises(IndexError):
185 pc.getPosition(pc.getParticleCount() + 1)
187 with pytest.raises(IndexError):
189 with pytest.raises(IndexError):
190 pc.getVelocity(pc.getParticleCount())
191 with pytest.raises(IndexError):
192 pc.getVelocity(pc.getParticleCount() + 1)
195 for x
in range(0, 5):
196 assert isinstance(pc.getPosition(x), Vector3DReal)
197 assert isinstance(pc.getVelocity(x), Vector3DReal)
200 pc.getPosition(x) == Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0)
202 pc.getVelocity(x) == Vector3DReal(1.23 / (x + 1), x, -x)
205 positions[0].normalize()
206 velocities[1].normalize()
207 for x
in range(0, 5):
208 assert isinstance(pc.getPosition(x), Vector3DReal)
209 assert isinstance(pc.getVelocity(x), Vector3DReal)
212 pc.getPosition(x) == Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0)
214 pc.getVelocity(x) == Vector3DReal(1.23 / (x + 1), x, -x)
223 pc = ParticleCollection()
224 with pytest.raises(IndexError):
226 with pytest.raises(IndexError):
230 pc = ParticleCollection()
233 for x
in range(0, 5):
234 positions.append(Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0))
235 velocities.append(Vector3DReal(1.23 / (x + 1), x, -x))
237 pc.setPositionsAndVelocities(positions, velocities)
239 with pytest.raises(RuntimeError):
243 with pytest.raises(TypeError):
245 with pytest.raises(TypeError):
248 with pytest.raises(IndexError):
250 with pytest.raises(IndexError):
251 pc.getMass(pc.getParticleCount())
252 with pytest.raises(IndexError):
253 pc.getMass(pc.getParticleCount() + 1)
256 pc.setUniformMass(1.23)
258 for x
in range(0, pc.getParticleCount()):
259 assert isinstance(pc.getMass(x), float)
260 assert pc.getMass(x) == 1.23
262 pc.setPositionsAndVelocities(positions[:3], velocities[:3])
263 for x
in range(0, pc.getParticleCount()):
264 assert isinstance(pc.getMass(x), float)
265 assert pc.getMass(x) == 1.23
267 pc.setPositionsAndVelocities(positions + velocities, velocities + positions)
268 for x
in range(0, pc.getParticleCount()):
269 assert isinstance(pc.getMass(x), float)
270 assert pc.getMass(x) == 1.23
273 for x
in range(0, pc.getParticleCount()):
274 assert isinstance(pc.getMass(x), float)
275 assert pc.getMass(x) == 4
278 def test_getMomentum():
284 pc = ParticleCollection()
285 with pytest.raises(IndexError):
287 with pytest.raises(IndexError):
291 pc = ParticleCollection()
294 for x
in range(0, 5):
295 positions.append(Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0))
296 velocities.append(Vector3DReal(1.23 / (x + 1), x, -x))
298 pc.setPositionsAndVelocities(positions, velocities)
300 with pytest.raises(RuntimeError):
304 with pytest.raises(TypeError):
306 with pytest.raises(TypeError):
309 with pytest.raises(IndexError):
311 with pytest.raises(IndexError):
312 pc.getMomentum(pc.getParticleCount())
313 with pytest.raises(IndexError):
314 pc.getMomentum(pc.getParticleCount() + 1)
317 pc.setUniformMass(1.23)
319 for x
in range(0, pc.getParticleCount()):
320 assert isinstance(pc.getMomentum(x), Vector3DReal)
321 assert pc.getMomentum(x) == pc.getVelocity(x) * pc.getMass(x)
322 assert pc.getMomentum(x) == pc.getVelocity(x) * pc.getMass(x)
324 pc.setPositionsAndVelocities(positions[:3], velocities[:3])
325 for x
in range(0, pc.getParticleCount()):
326 assert isinstance(pc.getMomentum(x), Vector3DReal)
327 assert pc.getMomentum(x) == pc.getVelocity(x) * pc.getMass(x)
328 assert pc.getMomentum(x) == pc.getVelocity(x) * pc.getMass(x)
330 pc.setPositionsAndVelocities(positions + velocities, velocities + positions)
331 for x
in range(0, pc.getParticleCount()):
332 assert isinstance(pc.getMomentum(x), Vector3DReal)
333 assert pc.getMomentum(x) == pc.getVelocity(x) * pc.getMass(x)
334 assert pc.getMomentum(x) == pc.getVelocity(x) * pc.getMass(x)
336 pc.setUniformMass(4.56)
337 for x
in range(0, pc.getParticleCount()):
338 assert isinstance(pc.getMomentum(x), Vector3DReal)
339 assert pc.getMomentum(x) == pc.getVelocity(x) * pc.getMass(x)
340 assert pc.getMomentum(x) == pc.getVelocity(x) * pc.getMass(x)
343 def test_getCenterOfMass():
349 pc = ParticleCollection()
350 with pytest.raises(RuntimeError):
355 for x
in range(0, 5):
356 positions.append(Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0))
357 velocities.append(Vector3DReal(1.23 / (x + 1), x, -x))
359 pc.setPositionsAndVelocities(positions, velocities)
361 with pytest.raises(RuntimeError):
365 pc.setUniformMass(mass)
367 expected = Vector3DReal(0.0, 0.0, 0.0)
369 for position
in positions:
370 expected += position * mass
372 expected /= totalMass
374 assert isinstance(pc.getCenterOfMass(), Vector3DReal)
375 assert pc.getCenterOfMass() == expected
376 assert pc.getCenterOfMass() == expected
379 def test_getCenterOfPositions():
385 pc = ParticleCollection()
386 with pytest.raises(RuntimeError):
387 pc.getCenterOfPositions()
391 for x
in range(0, 5):
392 positions.append(Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0))
393 velocities.append(Vector3DReal(1.23 / (x + 1), x, -x))
395 pc.setPositionsAndVelocities(positions, velocities)
397 expected = Vector3DReal(0.0, 0.0, 0.0)
398 for position
in positions:
400 expected /= len(positions)
402 assert isinstance(pc.getCenterOfPositions(), Vector3DReal)
403 assert pc.getCenterOfPositions() == expected
404 assert pc.getCenterOfPositions() == expected
407 def test_getCenterOfMassVelocity():
413 pc = ParticleCollection()
414 with pytest.raises(RuntimeError):
415 pc.getCenterOfMassVelocity()
419 for x
in range(0, 5):
420 positions.append(Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0))
421 velocities.append(Vector3DReal(1.23 / (x + 1), x, -x))
423 pc.setPositionsAndVelocities(positions, velocities)
425 with pytest.raises(RuntimeError):
426 pc.getCenterOfMassVelocity()
429 pc.setUniformMass(mass)
431 expected = Vector3DReal(0.0, 0.0, 0.0)
433 for velocity
in velocities:
434 expected += velocity * mass
436 expected /= totalMass
438 assert isinstance(pc.getCenterOfMassVelocity(), Vector3DReal)
439 assert pc.getCenterOfMassVelocity() == expected
440 assert pc.getCenterOfMassVelocity() == expected
443 def test_rotateAroundNormalizedAxis():
448 axis = Vector3DReal(1, 2, 3)
452 pc = ParticleCollection()
453 pc.rotateAroundNormalizedAxis(axis, angle)
455 with pytest.raises(TypeError):
456 pc.rotateAroundNormalizedAxis([1.0, 0.0, 0.0], angle)
457 with pytest.raises(TypeError):
458 pc.rotateAroundNormalizedAxis(axis, 1)
459 with pytest.raises(ValueError):
460 pc.rotateAroundNormalizedAxis(Vector3DReal(0, 0, 0), angle)
464 for x
in range(0, 5):
465 positions.append(Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0))
466 velocities.append(Vector3DReal(1.23 / (x + 1), x, -x))
468 pc.setPositionsAndVelocities(positions, velocities)
470 pc.rotateAroundNormalizedAxis(axis, angle)
472 for x
in range(0, pc.getParticleCount()):
473 expectedPos = positions[x].getRotatedAroundNormalizedAxis(axis, angle)
474 expectedVel = velocities[x].getRotatedAroundNormalizedAxis(axis, angle)
476 assert pc.getPosition(x) == expectedPos
477 assert pc.getPosition(x) == expectedPos
479 assert pc.getVelocity(x) == expectedVel
480 assert pc.getVelocity(x) == expectedVel
483 def test_getGyrationTensor():
490 pc = ParticleCollection()
491 with pytest.raises(ValueError):
492 pc.getGyrationTensor()
493 with pytest.raises(ValueError):
494 pc.getGyrationTensor()
499 for x
in range(0, particleCount):
500 positions.append(Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0))
501 velocities.append(Vector3DReal(1.23 / (x + 1), x, -x))
503 pc.setPositionsAndVelocities(positions, velocities)
505 expected = numpy.zeros((3, 3))
506 for m
in range(0, 3):
507 for n
in range(0, 3):
508 for i
in range(0, len(positions)):
509 for j
in range(0, len(positions)):
512 expected[m][n] += (r1[m] - r2[m]) * (r1[n] - r2[n])
514 for i
in range(0, 3):
515 for j
in range(0, 3):
516 expected[i][j] /= (2 * particleCount * particleCount)
518 S = pc.getGyrationTensor()
519 S2 = pc.getGyrationTensor()
521 assert numpy.array_equal(S, S2)
523 assert isinstance(S, numpy.ndarray)
524 assert S.shape == (3, 3)
525 for i
in range(0, 3):
526 for j
in range(0, 3):
527 assert S[i][j] == S[j][i]
529 for i
in range(0, 3):
530 for j
in range(0, 3):
531 assert S[i][j] == pytest.approx(expected[i][j])
534 def test_getGyrationTensorPrincipalMoments():
541 pc = ParticleCollection()
542 with pytest.raises(ValueError):
543 pc.getGyrationTensor()
544 with pytest.raises(ValueError):
545 pc.getGyrationTensor()
550 for x
in range(0, particleCount):
551 positions.append(Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0))
552 velocities.append(Vector3DReal(1.23 / (x + 1), x, -x))
554 pc.setPositionsAndVelocities(positions, velocities)
556 eigensystem = pc.getGyrationTensorEigensystem()
558 eigenvalues1 = pc.getGyrationTensorPrincipalMoments()
559 eigenvalues2 = pc.getGyrationTensorPrincipalMoments()
561 for eigenvalues
in [eigenvalues1, eigenvalues2]:
562 assert isinstance(eigenvalues, list)
563 assert len(eigenvalues) == 3
565 for i
in range(0, 3):
566 assert isinstance(eigenvalues[i], numpy.float64)
567 assert numpy.isreal(eigenvalues[i])
569 assert eigenvalues[i] == eigensystem[i][0]
572 def test_getGyrationTensorEigensystem():
579 pc = ParticleCollection()
580 with pytest.raises(ValueError):
581 pc.getGyrationTensor()
582 with pytest.raises(ValueError):
583 pc.getGyrationTensor()
588 for x
in range(0, particleCount):
589 positions.append(Vector3DReal(0.5 * x, -0.5 + x, x * x / 2.0))
590 velocities.append(Vector3DReal(1.23 / (x + 1), x, -x))
592 pc.setPositionsAndVelocities(positions, velocities)
594 eigensystem1 = pc.getGyrationTensorEigensystem()
595 eigensystem2 = pc.getGyrationTensorEigensystem()
597 gyrationTensor = pc.getGyrationTensor()
599 for eigensystem
in [eigensystem1, eigensystem2]:
600 assert isinstance(eigensystem, list)
601 assert len(eigensystem) == 3
604 for eigenpair
in eigensystem:
605 assert isinstance(eigenpair, list)
606 assert len(eigenpair) == 2
608 eigenvalue = eigenpair[0]
609 eigenvector = eigenpair[1]
611 assert numpy.isreal(eigenvalue)
612 assert isinstance(eigenvalue, numpy.float64)
615 previous = eigenvalue
617 assert previous < eigenvalue
618 previous = eigenvalue
620 assert eigenvector.shape == (3,)
621 for i
in range(0, 3):
622 assert isinstance(eigenvector[i], numpy.float64)
625 Sv = gyrationTensor.dot(eigenvector)
626 lambda_v = eigenvalue * eigenvector
628 for i
in range(0, 3):
629 assert Sv[i] == pytest.approx(lambda_v[i])
633 pc1 = ParticleCollection()
634 pc2 = ParticleCollection()
642 pc1.setUniformMass(1.0)
645 assert not pc1 == pc2
646 assert not pc2 == pc1
649 pc2.setUniformMass(1.0)
658 v1 = Vector3DReal(0, 1, 2)
659 v2 = Vector3DReal(0.1, -1, 2.4)
661 pc1.setPositionsAndVelocities([v1], [v2])
663 assert not pc1 == pc2
664 assert not pc2 == pc1
667 pc2.setPositionsAndVelocities([v1], [v2])
674 pc1.setPositionsAndVelocities([v1, v2], [v1, v2])
676 assert not pc1 == pc2
677 assert not pc2 == pc1
680 pc2.setPositionsAndVelocities([v1], [v1])
682 assert not pc1 == pc2
683 assert not pc2 == pc1
686 pc2.setPositionsAndVelocities([v1, v2], [v1, v2])
693 pc2.setPositionsAndVelocities([v1, v2], [v1, v1])
695 assert not pc1 == pc2
696 assert not pc2 == pc1
700 pc2.setPositionsAndVelocities([v1, v1], [v1, v2])
702 assert not pc1 == pc2
703 assert not pc2 == pc1
707 pc1.setUniformMass(0.1)
709 assert not pc1 == pc2
710 assert not pc2 == pc1
715 pc1 = ParticleCollection()
716 pc2 = ParticleCollection()
718 assert not pc1 != pc1
719 assert not pc1 != pc2
720 assert not pc2 != pc2
721 assert not pc2 != pc1
724 pc1.setUniformMass(1.0)
726 assert not pc1 != pc1
729 assert not pc2 != pc2
731 pc2.setUniformMass(1.0)
733 assert not pc1 != pc1
734 assert not pc1 != pc2
735 assert not pc2 != pc2
736 assert not pc2 != pc1
740 v1 = Vector3DReal(0, 1, 2)
741 v2 = Vector3DReal(0.1, -1, 2.4)
743 pc1.setPositionsAndVelocities([v1], [v2])
744 assert not pc1 != pc1
747 assert not pc2 != pc2
749 pc2.setPositionsAndVelocities([v1], [v2])
750 assert not pc1 != pc1
751 assert not pc1 != pc2
752 assert not pc2 != pc2
753 assert not pc2 != pc1
756 pc1.setPositionsAndVelocities([v1, v2], [v1, v2])
758 assert not pc1 == pc2
759 assert not pc2 == pc1
762 pc2.setPositionsAndVelocities([v1], [v1])
764 assert not pc1 == pc2
765 assert not pc2 == pc1
768 pc2.setPositionsAndVelocities([v1, v2], [v1, v2])
775 pc2.setPositionsAndVelocities([v1, v2], [v1, v1])
776 assert not pc1 != pc1
779 assert not pc2 != pc2
782 pc2.setPositionsAndVelocities([v1, v1], [v1, v2])
783 assert not pc1 != pc1
786 assert not pc2 != pc2
789 pc1.setUniformMass(0.1)
791 assert not pc1 == pc2
792 assert not pc2 == pc1