Swarm-NG  1.1
trivial.py
Go to the documentation of this file.
1 from common import *
2 ## @file trivial.py Trivial unit tests that basic features work.
3 #
4 # Testing the monitor stop_on_ejection, some planets are put in orbits
5 # very close to one another (1.01 separation) and they are expected to
6 # eject out of the system. Since not all of the eject, in the
7 # verification stage we check that if the body has ejected, the system
8 # should have been deactivated
9 
10 def fill(list_like,initial_value):
11  for i in range(0,len(list_like)):
12  list_like[i] = initial_value
13 
14 
15 def make_test_case(nsys = 16, nbod = 3 , spacing_factor = 1.4, planet_mass = 0.001, ejection_factor = 1, seed = None):
16  random.seed(seed)
17  d = swarmng.DefaultEnsemble.create(nbod,nsys)
18  for i in range(0,d.nsys):
19  s = d[i]
20  s.id = i
21  s.set_active()
22  s.time = 0
23  s[0].pos = [ 0, 0, 0 ]
24  s[0].vel = [ 0, 0, 0 ]
25  s[0].mass = 1
26  fill(s.attributes, 0)
27  for j in range(0,d.nbod):
28  fill(s[j].attributes, 0)
29 
30  for j in range(1,d.nbod):
31  r = spacing_factor ** (j-1)
32  v = sqrt(1/r) * ejection_factor
33  phi = random.uniform(0,2*pi)
34  s[j].pos = [ r*cos(phi), r*sin(phi), 0 ]
35  s[j].vel = [ -v*sin(phi), v*cos(phi), 0 ]
36  s[j].mass = planet_mass
37  return d;
38 
39 
40 def norm(l):
41  return sqrt(sum(x**2 for x in l))
42 
43 
44 class BasicIntegration(abstract.IntegrationTest):
45  cfg = swarmng.config(
46  integrator = 'hermite_cpu',
47  time_step = 1e-4,
48  nogpu = 1
49  )
50  def createEnsemble(self):
51  return make_test_case(nsys = 16, nbod = 3, spacing_factor=1.4)
52  def examine(self):
53  max_deltaE = swarmng.find_max_energy_conservation_error( self.ens, self.ref)
54  self.assertLess(max_deltaE, 1e-13)
55 
56 
57 class InitialConditions(unittest.TestCase):
58  def runTest(self):
59  ref = make_test_case(nsys = 16, nbod = 3, spacing_factor=1.4, seed = 14321)
60  ref.save_to_text("sample.txt")
61  r = system("diff sample.txt '{0}'".format(path.join(TESTDIR,"ref.txt")) )
62  self.assertEqual(r, 0)
63 
64 
65 class EjectionTest(abstract.IntegrationTest):
66  RMAX = 10
67  cfg = swarmng.config(
68  integrator = "hermite_cpu",
69  time_step = 1e-3,
70  nogpu = 1,
71  deactivate_on_ejection = 1,
72  rmax = RMAX
73  )
74  destination_time = 100
75 
76  def createEnsemble(self):
77  return make_test_case(nsys=20, nbod = 6, spacing_factor=1.01);
78 
79  def examine(self):
80  for sys in self.ens:
81  for b in sys:
82  if( b.distance_to_origin() > self.RMAX ):
83  self.assertEqual(sys.state, -1, "a system with an ejected body should be disabled")
84 
85 
86 
87 
88