Swarm-NG  1.1
Plotting results from a log file

In this tutorial, we are going to explore how to examine the contents of a log file generated by Swarm-NG library.

For more information on how to generate log files refer to Configuration Files

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

To open and query a log file, we use swarmng.logdb.IndexedLogDB class. You can see the range of all queries that are possible using IndexedLogDB class in the API documentation.

To begin, import the module and create the IndexedLogDB with a filename

1 import swarmng.logdb
2 d = swarmng.logdb.IndexedLogDB("sample_log.db")

In this example, we have already created a sample_log.db file using the following command line:

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 

Now we use two methods from IndexedLogDB, namely initial_conditions and final_conditions. These methods give you list of records for initial conditions and final conditions for the systems.

To specify ranges, you need to use a special class swarmng.range_type.Range. you can specify ranges in 3 ways:

All of the entries in the range, which basically does not restrict the results at all

One system ID (in this example system number 42)

A continuous range of systems (including both ends)

Now we get the initial conditions for all of the systems note that all query methods return iterables and not the actual data.

1 ic = d.initial_conditions(sr1)

To get the data, you should run a for loop over the returned object:

1 from logrecord_tutorial import get_semi_major_axis
2 initial = []
3 for i, l in ic:
4  initial.append(get_semi_major_axis(l, body=1))

In this for loop, i is the index of the system and l is a swarmng.logrecord.LogRecord object that has simple methods such as time, event_id, accessors for the planets, etc. for a tutorial on LogRecord object refer to Examining records from the log file.

In this example we want to extract the semi-major axis for the first planet.

The final_conditions method uses very similar semantics, the code is almost identical. By the way, you do not need to assign the result to an object, you can just call the method from the first line of the for loop

1 final = []
2 for i, l in d.final_conditions(sr1):
3  final.append(get_semi_major_axis(l, body=1))

Just to make the data more meaninful, we only want to know the difference between final and initial semi-major axis. we create the diff array like this:

1 diff = []
2 for i, f in zip(initial,final):
3  diff.append(i - f)

Now we have all the data we need in two arrays: initial and final. We can use the matplotlib package to visualize this data. for more information on how to make customized plots look at matplotlib documentation at http://matplotlib.org/

1 import matplotlib.pyplot as P
2 P.scatter(initial,diff)
3 P.savefig("initial_vs_final_semi-major_axis")
4 P.show()

If you are running this script in an environment where X server is not accessible, just comment out the last line P.show(). The generated plot is saved is initial_vs_final_semi-major_axis.png.

To learn how to make more complicated plots, try reading the API documentation for following classes: