Swarm-NG  1.1
ensemble_tutorial.py
1 #!/usr/bin/env python2
2 
3 # @page TutorialPythonEnsemble Tutorial for generating ensembles
4 #
5 # In this tutorial, we will see how to generate test cases and save/load them from files.
6 #
7 # Source code for this tutorial can be found at @ref py/ensemble_tutorial.py
8 #
9 # The first step is always to import swarmng module, make sure that it is in PYTHONPATH.
10 import swarmng
11 #
12 #
13 # For a trivial test case, we create an ensemble of systems with star at the origin and planets orbiting in
14 # in perfect circular orbits.
15 
16 # The parameters are as follows:
17 # * `nsys` : number of systems in the ensemble
18 # * `nbod` : number of bodies in the ensemble
19 # * `spacing_factor` : ratio of the radiuses of orbits of two consecutive planets.
20 # * `planet_mass` : ratio of mass of the planet to the sun.
21 #
22 def make_test_case(nsys = 16, nbod = 3 , spacing_factor = 1.4, planet_mass = 0.001, ejection_factor = 1, seed = None):
23  random.seed(seed)
24  d = swarmng.DefaultEnsemble.create(nbod,nsys)
25  for i in range(0,d.nsys):
26  s = d[i]
27  s.id = i
28  s.set_active()
29  s.time = 0
30  s[0].pos = [ 0, 0, 0 ]
31  s[0].vel = [ 0, 0, 0 ]
32  s[0].mass = 1
33  fill(s.attributes, 0)
34  for j in range(0,d.nbod):
35  fill(s[j].attributes, 0)
36 
37  for j in range(1,d.nbod):
38  r = spacing_factor ** (j-1)
39  v = sqrt(1/r) * ejection_factor
40  phi = random.uniform(0,2*pi)
41  s[j].pos = [ r*cos(phi), r*sin(phi), 0 ]
42  s[j].vel = [ -v*sin(phi), v*cos(phi), 0 ]
43  s[j].mass = planet_mass
44  return d;
45 
46 if __name__ == '__main__':
47  ens = make_test_case()
48 #
49 # You can easily save an ensemble object to a text file:
50  ens.save_to_text("sample.txt")
51 # Alternatively, you can save to a binary format. The binary format is not portable because they
52 # depend not only on the machine but on the flags that are used when compiling the Swarm-NG library.
53 # The only
54 # advantage of binary format is fast loading which makes a big difference when the ensemble
55 # contains thousands of systems. The main purpose of binary files is to save snapshots and resume
56 # integration later. For collaborative work or sharing data, please use text format instead.
57  ens.save_to_bin("sample.bin")