Swarm-NG  1.1
tutorial.py
Go to the documentation of this file.
1 ## @file tutorial.py Beginner tutorial on how to interface with the integrators.
2 # refer to @ref TutorialPython for the formatted version
3 
4 
5 # @page TutorialPython Beginner Python Tutorial
6 # In this tutorial, we explore the basic functionality of Swarm-NG, note that this tutorial
7 # is too short and only covers commonly used API. For more examples look at other Python tutorials.
8 #
9 # Source code for this tutorial can be found at @ref py/tutorial.py
10 #
11 # First thing is to import swarmng package. It is located in `<SOURCE DIRECTORY>/py/swarmng/`
12 # It takes care of loading the libswarmng_ext.so; it also contains some Pythonified
13 # API on top of libswarmng_ext. But please run the scripts
14 # from your build directory so swarmng can find your libraries.
15 import swarmng
16 import os
17 
18 # swarm functions cannot take hashes for config. So
19 # we have to create the special config object (a C++ std::map)
20 # using swarmng.config function
21 # This first line initializes Swarm-NG. 'nogpu' option
22 # allows us to run this script on a machine without GPU
24 
25 # Now we can load an ensemble from a text file. The ensemble file is in the swarm source directory:
26 ensemble_filename = os.path.join(swarmng.SWARMDIR , 'test/integrators/test.3.in.txt')
27 # We use a simple call that loads the file and returns the data structure
28 ref = swarmng.DefaultEnsemble.load_from_text( ensemble_filename )
29 
30 
31 # We can also look into the data structure and view the values
32 for s in ref:
33  print("System {0}, time: {1}, state: {2} ".format(s.id, s.time, s.state))
34  for i,b in enumerate(s):
35  print("\tBody {0} pos: {1}\tvel:{2}".format(i,b.pos,b.vel))
36 
37 # We can treat the data structure as an array. e.g to set the position
38 # of body 0 of system 3 you can write:
39 ref[3][0].pos = [ 1.0, 2.0 , 5.0 ]
40 
41 # We can easily make a copy of the whole data structure in this example
42 # we need to have two ensembles: reference (ref) and a working copy (ens)
43 ens = ref.clone()
44 
45 # Now we have to select our integrator, swarmng.Integrator.create function
46 # instantiates an integrator for us.
47 # In this case, we use a CPU based implementation of Hermite PEC2 integrator(hermite_cpu)
48 # To see what integrators are available, 'run bin/swarm -p' from command line.
49 integrator_cfg = swarmng.config(
50  integrator='hermite_cpu',
51  time_step=0.001
52 )
53 integ = swarmng.Integrator.create(integrator_cfg)
54 
55 # we set up the parameters to the integrator
56 integ.ensemble = ens
57 integ.destination_time = 1.0
58 
59 # Now do the integration
60 integ.integrate()
61 
62 
63 # A very common task is to check the energy conservation
64 # the function swarmng.find_max_energy_conservation_error does this
65 # task for us, given two ensembles.
66 max_deltaE = swarmng.find_max_energy_conservation_error( ens, ref)
67 print("Max energy conservation error %g" % max_deltaE)
68