Skip to content

Commit

Permalink
Moving examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hannorein committed Jul 30, 2015
1 parent 36feb4a commit 612dc16
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 338 deletions.
1 change: 1 addition & 0 deletions doc/index.rst
Expand Up @@ -103,6 +103,7 @@ Table of Contents
:numbered:

self
pythoninstall
modules
rebound
librebound
Expand Down
2 changes: 1 addition & 1 deletion doc/librebound.rst
@@ -1,7 +1,7 @@
The C version of REBOUND
========================

This section describes the C version of REBOUND. To learn how to install REBOUND for Python have a look at the iPython/Jupiter notebooks at https://github.com/hannorein/rebound/blob/master/ipython_examples/index.ipynb. Hint: It's super easy!
This section describes the C version of REBOUND.

Installation
------------
Expand Down
87 changes: 87 additions & 0 deletions doc/pythoninstall.rst
@@ -0,0 +1,87 @@
Installing REBOUND (Python)
===========================

Installing REBOUND is very easy. Rebound does not depend on any libraries. However, you need to have python (version 2 or 3) and a C compiler installed. Most likely, you already have those on your system.

If you don't, and aren't sure how to go about getting them, it is probably easiest to install either the Enthought or Anaconda python distributions (which are free and come with typically used libraries and an easy-to-use installer). For the C compiler on Mac, it's probably easiest to install Xcode from the App store.

Create a virtual environment
----------------------------

Before you start, you can create a virtual environment to keep your python installation clean. This is recommended, but not necessary. You need to have [virtualenv](https://virtualenv.pypa.io/en/latest/) installed (if you use the Anaconda python distribution, you'll need to instead create a conda environment - see below).

To create a virtual environment, simply open a terminal window, go to the folder where you want REBOUND to be installed (e.g. `/home/username/rebound/`) and enter::

virtualenv venv

This creates a virtual environment named `venv`. To activate it, type::

source venv/bin/activate

If you log out of your terminal or open a new one, you'll need to reactivate the virtual environment with the above command (if the environment is active you'll see its name in parentheses before the command prompt).

If you use the Anaconda distribution, the above likely will not work. To create a conda environment run the following command::

conda create -n venv pip
source activate venv


Standard python installation using pip
--------------------------------------

Now you can install REBOUND using pip. All you have to do is type the following into a terminal window (if you created a virtual environment above you should first run the appropriate activate command depending on whether you used virtualenv or conda):

pip install rebound

The setup script will download the latest version of REBOUND from [PyPI](https://pypi.python.org/pypi) (the Python Package Index), compile the C code in the background and place all the files in their correct location. No other libraries are needed to start working with WHFast and REBOUND, but you might want to install numpy and matplotlib to be able to post-process your data and make plots. For analysis tools (and to run FourierSpectrum.ipynb) you might also want scipy. Installing those libraries is also very easy (but may need a few minutes). Depending on whether you use virtualenv or conda, use

pip install numpy matplotlib scipy

or

conda install numpy matplotlib scipy


That's all there is to do!


Installing the development version directly from github
-------------------------------------------------------

Instead of using pip to install the latest version of REBOUND from PyPI, you can also install REBOUND directly from github. This is useful if you want to make changes to REBOUND.

First clone the repository (you might need to create a github account)::

git clone git@github.com:hannorein/rebound.git

If you already know that you might be contributing something to REBOUND, you can also first fork the repository on github and then clone your own repository.

Next, create a virtual environment with the same commands as above::

virtualenv venv && source venv/bin/activate

Now you can install rebound from source using::

pip install -e .

You can modify the python module files in the directory `rebound/` and you'll see the changed the next time you run a python script (no need to reinstall the REBOUND package every time).

If you install REBOUND directly from github, you can also run it without python. Have a look at the README file in the main directory and at the examples in the `examples/` directory for examples in C. These are much more diverse than the python examples (e.g. allow you to use a tree code for gravity calculations, use other boundary conditions, etc).

Python Notebook
---------------


The tutorials in python_tutorials were written using iPython/Jupyter notebooks. You can view them directly on GitHub. If you want to edit them or create your own notebook, you'll need to install iPython (make sure to activate the virtual environment first if you created one)::

pip install "ipython[notebook]"

or::

conda install ipython-notebook
You can then open iPython notebooks in your browser by typing::

ipython notebook
To create a new notebook select from the dropdown menu on the top right the item that says 'New'. Now you can interactively follow the commands in the tutorials or run your own!
25 changes: 13 additions & 12 deletions ipython_examples/AdvWHFast.ipynb
Expand Up @@ -8,7 +8,7 @@
"\n",
"There are several performance enhancements one can make to WHFast. However, each one has pitfalls that an inexperienced user can unwittingly fall into. We therefore chose safe default settings that make the integrator difficult to misuse. **This makes the default WHFast substantially slower and less accurate than it can be**. Here we describe how to alter the integrator settings to improve WHFast's performance.\n",
"\n",
"## TL;DR\n",
"**TL;DR**\n",
"\n",
"As long as \n",
"\n",
Expand All @@ -24,7 +24,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## The Wisdom-Holman algorithm\n",
"**The Wisdom-Holman algorithm**\n",
"\n",
"In order to understand and apply the various integrator flags, we need to first understand the Wisdom-Holman scheme (see, e.g., Wisdom & Holman 1991, or Rein & Tamayo 2015 for more details).\n",
"\n",
Expand All @@ -41,7 +41,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Combining Kepler steps and synchronizing \n",
"**Combining Kepler steps and synchronizing**\n",
"\n",
"It turns out that Kepler steps take longer than interaction steps as long as you don't have many planets, so an obvious and important performance boost would be to combine adjacent Kepler half-steps into full ones, i.e.:\n",
"\n",
Expand All @@ -53,15 +53,15 @@
"\n",
"We call this step of reinserting half-Kepler steps to obtain the physical state of the particles *synchronizing*. This must be done whenever the **actual** states of the particles are required, e.g., before every output, or if one wanted to use the particles' states to compute additional changes to the particle orbits between timesteps. It is also necessary to synchronize each timestep whenever the MEGNO chaos indicator is being computed.\n",
"\n",
"## Conversions between Jacobi and Inertial Coordinates\n",
"**Conversions between Jacobi and Inertial Coordinates**\n",
"\n",
"It turns out that the most convenient coordinate system to work in for performing the Kepler steps is Jacobi coordinates (see, e.g., 9.5.4 of Murray & Dermott). WHFast therefore works in Jacobi coordinates, converting to inertial coordinates when it needs to (e.g. for output, and for doing the direct gravity calculation in the interaction step, which is most easily done in inertial coordinates).\n",
"\n",
"One feature of WHFast is that it works in whatever inertial coordinate system you choose for your initial conditions. This means that whatever happens behind the scenes, the user always gets the particles' inertial coordinates at the front end. At the beginning of every timestep, WHFast therefore has to somehow obtain the Jacobi coordinates. The straightforward thing would be to convert from the inertial coordinates to Jacobi coordinates every timestep, but these conversions slow things down, and they represent extra operations that grow the round-off error.\n",
"\n",
"WHFast therefore stores the Jacobi coordinates internally throughout the time it is running, and only recalculates Jacobi coordinates from the inertial ones if told to do so. Since Jacobi coordinates reference particles to the center of mass of all the particles with indices lower than their own (typically all the particles interior to them), the main reason you would have to recalculate Jacobi coordinates is if between timesteps you choose to somehow change the particles' positions or velocities (give them kicks in addition to their mutual gravity), or change the particles' masses. \n",
"\n",
"## Overriding the defaults\n",
"**Overriding the defaults**\n",
"\n",
"Let's begin by importing rebound, and defining a simple function to reset rebound and initialize a new simulation with a test case,"
]
Expand Down Expand Up @@ -205,7 +205,7 @@
"collapsed": true
},
"source": [
"## Modifying particles/forces\n",
"**Modifying particles/forces**\n",
"\n",
"Again, if performance is a factor in your simulations, you would not want to write a custom stepper in python that modifies the particles, since this will be very slow. You could either write a modified C version of `reb_integrate` in `src/librebound.c` (the flags are defined in `librebound.h`, and have the same name as the python ones, just without `sim.` in front), or you can use the REBOUNDXF library, which takes care of this for you and supports many typically used modifications. We again illustrate a simple scheme with python code:"
]
Expand Down Expand Up @@ -266,7 +266,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Symplectic correctors\n",
"**Symplectic correctors**\n",
"\n",
"Symplectic correctors make the Wisdom-Holman scheme higher order (without symplectic correctors it's second order). The great thing about them is that they only need to get applied when you synchronize. So if you just need to synchronize to output, and there are many timesteps between outputs, they represent a very small performance loss for a huge boost in accuracy (compare for example the green line (11th order corrector) to the red line (no corrector) in Fig. 4 of Rein & Tamayo 2015--beyond the right of the plot, where the round-off errors dominate, the two lines would rise in unison). We have implemented symplectic correctors up to order 11. You can set the order with (must be an odd number), e.g.,"
]
},
Expand All @@ -291,21 +292,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 2",
"language": "python",
"name": "python3"
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
Expand Down
8 changes: 5 additions & 3 deletions ipython_examples/Churyumov-Gerasimenko.ipynb
Expand Up @@ -15,7 +15,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## NASA Horizons\n",
"**NASA Horizons**\n",
"\n",
"If you're interested in Solar System dynamics, you have probably heard of NASA Horizons. It's a large database of Solar System objects, their orbits and physical properties. It includes planets, moons, satellites, asteroids, comets and spacecrafts. With REBOUND, you can easily import data from NASA Horizons. As an example, let's pull in the present day positions of Jupiter, Saturn and the Sun:"
]
Expand Down Expand Up @@ -119,7 +119,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Integration with IAS15\n",
"**Integration with IAS15**\n",
"\n",
"We will integrate backwards in time for 70 years. Because we don't know what will happen yet (hint: a close encounter) we will use the IAS15 integrator. It is fast, accurate and has adaptive timesteps to capture any potential close encounters. \n",
"\n",
"To integrate backwards, we could set a negative timestep or multiply all velocities with $-1$. We'll choose the first option:"
Expand Down Expand Up @@ -190,7 +191,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualization with matplotlib\n",
"**Visualization with matplotlib**\n",
"\n",
"Let's plot the orbits of Jupiter (blue) and the comet (green) to get an idea of what was going on during our integration."
]
},
Expand Down
6 changes: 4 additions & 2 deletions ipython_examples/Forces.ipynb
Expand Up @@ -9,7 +9,8 @@
"\n",
"This tutorial gives you a very quick overview of how that works. \n",
"\n",
"## Stark problem\n",
"**Stark problem**\n",
"\n",
"We'll start be adding two particles, the Sun and an Earth-like planet to REBOUND."
]
},
Expand Down Expand Up @@ -131,7 +132,8 @@
"source": [
"You can see that the eccentricity is oscillating between 0 and almost 1. \n",
"\n",
"## Non-conservative forces\n",
"**Non-conservative forces**\n",
"\n",
"The previous example assumed a conservative force, i.e. we could describe it as a potential as it is velocity independent. Now, let's assume we have a velocity dependent force. This could be a migration force in a protoplanetary disk or PR drag. We'll start from scratch and add the same two particles as before."
]
},
Expand Down
17 changes: 9 additions & 8 deletions ipython_examples/Horizons.ipynb
Expand Up @@ -8,7 +8,8 @@
"\n",
"REBOUND can add particles to simulations by obtaining ephemerides from NASA's powerful HORIZONS database. HORIZONS supports many different options, and we will certainly not try to cover everything here. This is meant to serve as an introduction to the basics, beyond what's in [Churyumov-Gerasimenko.ipynb](Churyumov-Gerasimenko.ipynb). If you catch any errors, or would either like to expand on this documentation or improve REBOUND's HORIZONS interface (`rebound/horizons.py`), please do fork the repository and send us a pull request.\n",
"\n",
"## Adding particles\n",
"**Adding particles**\n",
"\n",
"When we add particles by passing a string, REBOUND queries the HORIZONS database and takes the first dataset HORIZONS offers. For the Sun, moons, and small bodies, this will typically return the body itself. For planets, it will return the barycenter of the system (for moonless planets like Venus it will say barycenter but there is no distinction). In all cases, REBOUND will print out the name of the HORIZONS entry it's using.\n",
"\n",
"You can also add bodies using their integer NAIF IDs: [NAIF IDs](http://naif.jpl.nasa.gov/pub/naif/toolkit_docs/MATLAB/req/naif_ids.html). Note that because of the number of small bodies (asteroids etc.) we have discovered, this convention only works for large objetcts. For small bodies, instead use \"NAME=name\" (see the SMALL BODIES section in the [HORIZONS Documentation](http://ssd.jpl.nasa.gov/?horizons_doc)):"
Expand Down Expand Up @@ -78,7 +79,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Time\n",
"**Time**\n",
"\n",
"By default, REBOUND queries HORIZONS for objects' current positions. Specifically, it caches the current time the first time you call `rebound.add`, and gets the corresponding ephemeris. All subsequent calls to `rebound.add` will then use that initial cached time to make sure you get a synchronized set of ephemerides.\n",
"\n",
Expand Down Expand Up @@ -174,7 +175,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reference Frame\n",
"**Reference Frame**\n",
"\n",
"REBOUND queries for particles' positions and velocities relative to the Sun:"
]
Expand Down Expand Up @@ -218,21 +219,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 2",
"language": "python",
"name": "python3"
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 612dc16

Please sign in to comment.