Swarm-NG  1.1
__init__.py
Go to the documentation of this file.
1 ## @file __init__.py Loader of libswarmng_ext and root of the @ref swarmng package.
2 #
3 # To read the documentation generated from this file refer to @ref swarmng
4 
5 ## @package swarmng
6 # Python interface to the Swarm-NG library
7 #
8 # This package loads and verifies the libswarmng_ext.so and
9 # adds some API that is implemented in Python.
10 #
11 # The extensive module for opening log files is also included in \ref swarmng.logdb
12 #
13 
14 import sys
15 import os
16 import imp
17 from ctypes import CDLL, c_char_p
18 import platform
19 
20 # First find the source directory of Swarm
21 SWARMDIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
22 
23 # We have some guesses as where to find the library
24 DEFAULT_PLACES = [ 'lib', os.path.join(SWARMDIR,'build/lib'), os.path.join(SWARMDIR,'lib') ]
25 
26 # Find and load the so library
27 _, fn, _ = imp.find_module('libswarmng_ext', sys.path + DEFAULT_PLACES)
28 so_dll = CDLL(fn)
29 
30 # Get the functions that allow you to read the Python versions
31 so_hexversion = so_dll.python_hexversion
32 so_version = so_dll.python_version
33 so_version.restype = c_char_p
34 
35 # Test the Python version
36 if so_hexversion() != sys.hexversion :
37  print("The swarmng library was build for Python version %s, but you are running %s" % (so_version(), platform.python_version()))
38  sys.exit(1)
39 else:
40  imp.load_dynamic('libswarmng_ext', fn)
41 
42 from libswarmng_ext import *
43 from collections import namedtuple
44 
45 
46 
47 
48 ## Convert hashes and keyword arguments to a @ref swarmng.Config object
49 #
50 #
51 # There are three ways to use this function:
52 # # Combining one or more hashes into a @ref swarmng.Config object
53 # @code{.py}
54 # >>> cc = { 'nsys' : 16, 'nbod' : 3 }
55 # >>> c1 = { 'integrator' : 'hermite_cpu' , 'time_step' : 0.001, 'log_writer' : 'bdb' }
56 # >>> swarmng.config(c1)
57 # >>> swarmng.config(c1, c2)
58 # @endcode
59 # # Create a config object for inline use using keyword arguments. Following snippet initializes Swarm with `nogpu = 1`
60 # @code{.py}
61 # >>> c = swarmng.config(nogpu = 1)
62 # >>> swarmng.init(c)
63 # @endcode
64 # # Combine hashes and keyword arguments.
65 # @code{.py}
66 # >>> c1 = { 'integrator' : 'hermite_cpu' , 'time_step' : 0.001, 'log_writer' : 'bdb' }
67 # >>> swarmng.config(c1, { 'nsys' : 16, 'nbod' : 3 } , nogpu = 1)
68 # @endcode
69 #
70 # Arguments and return type:
71 # @arg arbitrary number of Hash objects and keyword arguments
72 #
73 # Returns: a @ref swarmng.Config object that combines configuration options
74 # from the hashes and keyword arguments.
75 def config(*l,**kw):
76  c = Config()
77  def addHash(h):
78  for k,v in h.items():
79  c[k] = str(v)
80 
81  for h in l:
82  addHash(h)
83  addHash(kw)
84  return c
85 
86 KeplerianCoordinates = namedtuple('KeplerianCoordinates',
87  ['a', 'e', 'i', 'O', 'w', 'M' ])
88 
89 
90 ## Caluclate Keplerian coordinates for a planet with respect to a predefined center
91 #
92 # \arg \c planet : a tuple of structure ((x,y,z),(vx,vy,vz),mass)
93 # \arg \c center : the supposed center of the orbit that planet is orbiting ( around of structure ((x,y,z),(vx,vy,vz),mass) )
94 #
95 # \ret Return type: a named tuple that has the following attributes
96 # * a : semi-major axis
97 # * e : eccentricity
98 # * i : inclination
99 # * O : Longitude of the ascending node
100 # * w : Argument of periapsis
101 # * M : mean anomaly.
102 #
103 # Refer to <a href="https://en.wikipedia.org/wiki/Orbital_elements">Wikipedia:Orbital elements</a> for meaning of these.
104 #
105 def keplerian_for_cartesian(planet,center):
106  ((x,y,z),(vx,vy,vz),mass) = planet
107  ((cx,cy,cz),(cvx,cvy,cvz),cmass) = center
108  (a,e,i,O,w,M) = calc_keplerian_for_cartesian(x-cx,y-cy,z-cz, vx-cvx,vy-cvy,vz-cvz, mass + cmass)
109  return KeplerianCoordinates(a,e,i,O,w,M)
110 
111 
112 
113 def mkConfig(h):
114  return config(h)