Swarm-NG  1.1
Tutorial for generating ensembles

In this tutorial, we will see how to generate test cases and save/load them from files.

Source code for this tutorial can be found at py/ensemble_tutorial.py

The first step is always to import swarmng module, make sure that it is in PYTHONPATH.

1 import swarmng

For a trivial test case, we create an ensemble of systems with star at the origin and planets orbiting in in perfect circular orbits.

The parameters are as follows:

  • nsys : number of systems in the ensemble
  • nbod : number of bodies in the ensemble
  • spacing_factor : ratio of the radiuses of orbits of two consecutive planets.
  • planet_mass : ratio of mass of the planet to the sun.
1 def make_test_case(nsys = 16, nbod = 3 , spacing_factor = 1.4, planet_mass = 0.001, ejection_factor = 1, seed = None):
2  random.seed(seed)
3  d = swarmng.DefaultEnsemble.create(nbod,nsys)
4  for i in range(0,d.nsys):
5  s = d[i]
6  s.id = i
7  s.set_active()
8  s.time = 0
9  s[0].pos = [ 0, 0, 0 ]
10  s[0].vel = [ 0, 0, 0 ]
11  s[0].mass = 1
12  fill(s.attributes, 0)
13  for j in range(0,d.nbod):
14  fill(s[j].attributes, 0)
15 
16  for j in range(1,d.nbod):
17  r = spacing_factor ** (j-1)
18  v = sqrt(1/r) * ejection_factor
19  phi = random.uniform(0,2*pi)
20  s[j].pos = [ r*cos(phi), r*sin(phi), 0 ]
21  s[j].vel = [ -v*sin(phi), v*cos(phi), 0 ]
22  s[j].mass = planet_mass
23  return d;
24 
25 if __name__ == '__main__':
26  ens = make_test_case()

You can easily save an ensemble object to a text file:

1 ens.save_to_text("sample.txt")

Alternatively, you can save to a binary format. The binary format is not portable because they depend not only on the machine but on the flags that are used when compiling the Swarm-NG library. The only advantage of binary format is fast loading which makes a big difference when the ensemble contains thousands of systems. The main purpose of binary files is to save snapshots and resume integration later. For collaborative work or sharing data, please use text format instead.

1 ens.save_to_bin("sample.bin") *