Swarm-NG  1.1
plot_tutorial.py
Go to the documentation of this file.
1 ## @file plot_tutorial.py Tutorial for extracting and plotting information from the BDB log files.
2 # refer to @ref TutorialPythonQueryPlot for the formatted version
3 
4 # @page TutorialPythonQueryPlot Plotting results from a log file
5 #
6 # In this tutorial, we are going to explore how to examine the
7 # contents of a log file generated by Swarm-NG library. For
8 # more information on how to generate log files refer to @ref Configuration
9 #
10 # Source code for this tutorial can be found at @ref py/plot_tutorial.py
11 #
12 # To open and query a log file, we use \ref swarmng.logdb.IndexedLogDB
13 # class. You can see the range of all queries that are possible using
14 # IndexedLogDB class in the API documentation.
15 #
16 # To begin, import the module and create the IndexedLogDB with a filename
17 #
18 import swarmng.logdb
19 d = swarmng.logdb.IndexedLogDB("sample_log.db")
20 #
21 # In this example, we have already created a `sample_log.db` file
22 # using the following command line:
23 # @verbatim bin/swarm integrate nsys=16 nbod=3 integrator=hermite_cpu_log time_step=0.001 log_writer=bdb log_output_db=sample_log.db log_time_interval=1 destination_time=100 @endverbatim
24 #
25 # Now we use two methods from IndexedLogDB, namely \ref swarmng.logdb.IndexedLogDB.initial_conditions "initial_conditions" and \ref swarmng.logdb.IndexedLogDB.final_conditions "final_conditions".
26 # These methods give you list of records for initial conditions and final conditions
27 # for the systems.
28 #
29 # To specify ranges, you need to use a special class swarmng.range_type.Range.
30 # you can specify ranges in 3 ways:
31 #
32 # All of the entries in the range, which basically does not restrict the results at all
33 import swarmng.range_type
35 # One system ID (in this example system number 42)
37 # A continuous range of systems (including both ends)
39 #
40 # Now we get the initial conditions for all of the systems
41 # note that all query methods return iterables and not
42 # the actual data.
43 ic = d.initial_conditions(sr1)
44 # To get the data, you should run a for loop over the returned
45 # object:
46 from logrecord_tutorial import get_semi_major_axis
47 initial = []
48 for i, l in ic:
49  initial.append(get_semi_major_axis(l, body=1))
50 
51 # In this for loop, i is the index of the system and l is a
52 # swarmng.logrecord.LogRecord object that has simple methods such
53 # as time, event_id, accessors for the planets, etc.
54 # for a tutorial on LogRecord object refer to @ref TutorialPythonLogRecord
55 #
56 # In this example we want to extract the semi-major axis for the
57 # first planet.
58 #
59 # The final_conditions method uses very similar semantics, the code
60 # is almost identical. By the way, you do not need to assign the result
61 # to an object, you can just call the method from the first line of
62 # the for loop
63 final = []
64 for i, l in d.final_conditions(sr1):
65  final.append(get_semi_major_axis(l, body=1))
66 
67 # Just to make the data more meaninful, we only want to
68 # know the difference between final and initial semi-major axis.
69 # we create the diff array like this:
70 diff = []
71 for i, f in zip(initial,final):
72  diff.append(i - f)
73 
74 # Now we have all the data we need in two arrays: initial and final.
75 # We can use the matplotlib package to visualize this data. for more
76 # information on how to make customized plots look at matplotlib
77 # documentation at http://matplotlib.org/
78 #
79 
80 import matplotlib.pyplot as P
81 P.scatter(initial,diff)
82 P.savefig("initial_vs_final_semi-major_axis")
83 P.show()
84 
85 # If you are running this script in an environment where X server
86 # is not accessible, just comment out the last line `P.show()`. The
87 # generated plot is saved is `initial_vs_final_semi-major_axis.png`.
88 #
89 # To learn how to make more complicated plots, try reading the
90 # API documentation for following classes:
91 # * swarmng.logdb.IndexedLogDB : for running other types of queries
92 # * swarmng.logrecord.LogRecord : to extract other information about planetary systems, e.g. time, eccentricity of planets, etc.
93 #