Skip to content

Commit

Permalink
Added more profile options, CLI. Improved documentation [build docs]
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHls committed Jan 20, 2022
1 parent 06cfd4b commit 4e3290d
Show file tree
Hide file tree
Showing 3 changed files with 676 additions and 42 deletions.
149 changes: 122 additions & 27 deletions docs/io/configuration/components/models/converters/arepo_to_tardis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
"# Converting Arepo snapshots to be usable by TARDIS"
]
},
{
"cell_type": "markdown",
"id": "84aedaa4",
"metadata": {},
"source": [
"### What is [Arepo](https://arepo-code.org/)?\n",
"> Arepo is a massively parallel gravity and magnetohydrodynamics code for astrophysics, designed for problems of large dynamic range. It employs a finite-volume approach to discretize the equations of hydrodynamics on a moving Voronoi mesh, and a tree-particle-mesh method for gravitational interactions. Arepo is originally optimized for cosmological simulations of structure formation, but has also been used in many other applications in astrophysics.\n",
"\n",
"{cite:ps}`Weinberger2020`\n",
"\n",
"This parser is intended for loading Arepo output files ('snapshots'), extracting the relevant (line-of-sight dependent) data and exporting it to `csvy` files, which can in turn be used in TARDIS models ([How to run TARDIS with a custom model](../Custom_TARDIS_Model_Tutorial))\n",
"\n",
"*Note: This parser has been developed for the (not publically available) development version of Arepo, not the public version. Althought it should also work with snapshots from the public version, this has not been tested. If you run into trouble loading the snapshot using the built-in functions, try providing the data manually*"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -16,8 +31,18 @@
"outputs": [],
"source": [
"import numpy as np\n",
"from tardis.io.parsers import arepo\n",
"import json"
"import matplotlib.pyplot as plt\n",
"import arepo\n",
"import json\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"id": "60695b40",
"metadata": {},
"source": [
"### Loading the simulation data"
]
},
{
Expand All @@ -27,31 +52,48 @@
"source": [
"As a first step, the relevant data has to be loaded from an Arepo snapshot. In case you have the arepo-snap-util package installed, you can use the built in wrapper (as described below) to load the relevant data. In case you do not have this package installed or want to load the snapshot in a different way, you can manually provide the relevant data and continue with the next step.\n",
"\n",
"If you're using the built-in tool, you will need to provide the path to the snapshot file, a list with the elements you want to include in your Tardis model, as well as the species file with which the Arepo simulation was run."
"If you're using the built-in tool, you will need to provide the path to the snapshot file, a list with the elements you want to include in your TARDIS model, as well as the species file with which the Arepo simulation was run. *(The species file should contain one header line, followed by two colums, where the first contains the names of all the species and is used to find the indices of the individual species within the snapshot. The second column is not used by the loader.)*"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d97d9d3",
"cell_type": "markdown",
"id": "27d90938",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" snapshot = arepo.ArepoSnapshot(\"arepo_snapshot.hdf5\", [\"ni56\", \"si28\"], \"species55.txt\", resolution=32)\n",
" pos, vel, rho, xnuc, time = snapshot.get_grids()\n",
"except ImportError:\n",
" print(\"Looks like arepo-snap-util is missing!\")"
"In case you have the arepo-snap-util package installed, you can load the data directly from a snapshot:\n",
"```python\n",
"snapshot = arepo.ArepoSnapshot(\n",
" \"arepo_snapshot.hdf5\", [\"ni56\", \"si28\"], \"species55.txt\", resolution=32\n",
")\n",
"pos, vel, rho, xnuc, time = snapshot.get_grids()\n",
"```\n",
"This will load the necessary from the snapshot. See the API documentation for more options on how to load snapshots."
]
},
{
"cell_type": "markdown",
"id": "49203e1f",
"metadata": {},
"source": [
"This will load the necessary from the snapshot. See the API documentation for more options on how to load snapshots.\n",
"This will fail with an error if you do not have the arepo-snap-util package installed. Since this is not a default dependency of TARDIS, lets manually load the data. *(This manual load can effectively be used to load all kinds of models unrelated to Arepo, as long as the data comes in the correct format.)*\n",
"\n",
"As you can see, it will fail if you do not have the arepo-snap-util package installed. So lets manually load the data. In this case from a `json` file, where the data which would have been loaded with the function above has been manually saved to."
"In this case the data is loaded from a `json` file. This file has been created dumping the data which would have been loaded from the snapshot to a `json` file.\n",
"<details> \n",
" <summary>Code: (<i>click to expand</i>) </summary>\n",
" \n",
"```python\n",
"data = {\n",
" \"pos\" : pos.tolist(),\n",
" \"vel\" : vel.tolist(),\n",
" \"rho\" : rho.tolist(),\n",
" \"xnuc\": [xnuc[x].tolist() for x in list(xnuc.keys())],\n",
" \"time\": time,\n",
"}\n",
"json_string = json.dumps(data)\n",
"with open('arepo_snapshot.json', 'w') as outfile:\n",
" json.dump(json_string, outfile)\n",
"```\n",
"</details>"
]
},
{
Expand Down Expand Up @@ -103,10 +145,11 @@
"id": "bf82ae2e",
"metadata": {},
"source": [
"### Extracting a profile and converting it to a csvy file\n",
"Now You can create a TARDIS model. There are three possibilities on how to extract the profiles from the snapshot:\n",
"- **Line profile**: This extracts the data along a straight line (the x-axis) without any averaging\n",
"- **Cone profile**: This extracts the data within a specified cone *(TBD)*\n",
"- **Full profile**: This averages over the whole simulation *(TBD)*"
"- **Line profile**: This extracts the data along a straight line (the x-axis) \n",
"- **Cone profile**: This extracts the data within a specified cone\n",
"- **Full profile**: This averages over the whole simulation"
]
},
{
Expand All @@ -116,7 +159,7 @@
"metadata": {},
"outputs": [],
"source": [
"profile = arepo.LineProfile(pos, vel, rho, xnuc, time)"
"profile = arepo.ConeProfile(pos, vel, rho, xnuc, time)"
]
},
{
Expand All @@ -125,7 +168,7 @@
"metadata": {},
"source": [
"This loads the data (here for the line profile), which can then be cut to the ranges which you want to include in your TARDIS model. The syntax for the other profiles is similar:\n",
"- `arepo.ConeProfile(<args>)`\n",
"- `arepo.LineProfile(<args>)`\n",
"- `arepo.FullProfile(<args>)`"
]
},
Expand All @@ -134,7 +177,8 @@
"id": "af9a76e0",
"metadata": {},
"source": [
"Next you can create the profiles acccording to the model option you selected. A diagnostic plot will be shown per default, but this behaviour can be turned off with the option `show_plot=False`. The plot will always show both the positve and negative axis."
"Next you can create the profiles acccording to the model option you selected. A diagnostic plot will be shown per default, but this behaviour can be turned off with the option `show_plot=False`. The plot will always show both the positve and negative axis.\n",
"> **_NOTE:_** The keyword `opening_angle=40` is only needed for the cone profile. The other modes do not accept this keyword! The angle itself is the opening angle of the full cone and NOT the angle between the central x-axis and the cone!"
]
},
{
Expand All @@ -144,7 +188,7 @@
"metadata": {},
"outputs": [],
"source": [
"profile.create_profile()"
"profile.create_profile(opening_angle=40)"
]
},
{
Expand All @@ -162,15 +206,15 @@
"metadata": {},
"outputs": [],
"source": [
"profile.create_profile(inner_radius=1e11, outer_radius=2e11)"
"profile.create_profile(opening_angle=40, inner_radius=1e11, outer_radius=2e11)"
]
},
{
"cell_type": "markdown",
"id": "dd6310ec",
"metadata": {},
"source": [
"Once you have created a profile of the desired region, you can export the profile to a `csvy` file, which in turn can be used in a TARDIS model. Here you have to specify how many shells you want to export. Note that the number of exported shells cannot be larger than the resolution of the given grid since no interpolation on the profile is done. If you want more shells, you either need to map your snapshot to a higher resolution Carthesian grid or interpolate the initial data yourself."
"Once you have created a profile of the desired region, you can export the profile to a `csvy` file, which in turn can be used in a TARDIS model. Here you have to specify how many shells you want to export. The profiles are rebinned using [Scipys binned_statistic function](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binned_statistic.html), using the mean value of the data in each bin."
]
},
{
Expand All @@ -180,7 +224,7 @@
"metadata": {},
"outputs": [],
"source": [
"profile.export(5, \"snapshot_converted_to_tardis.csvy\")"
"profile.export(50, \"snapshot_converted_to_tardis.csvy\")"
]
},
{
Expand All @@ -192,13 +236,64 @@
"\n",
"All abundences will normalised such that roughly sum to 1, but slight deviations are expected to occur.\n",
"\n",
"Note that the export function will not overwrite existing files, but rather add an incrementing number to the filename."
"> **_NOTE:_** The export function will not overwrite existing files, but rather add an incrementing number to the filename."
]
},
{
"cell_type": "markdown",
"id": "d0347042",
"metadata": {},
"source": [
"### Manually rebinning the data\n",
"Using `profile.rebin(<nshells>, statistic=<statistic>)`, you can manually rebin the data and use all `<statistic>` keywords accepted by the `scipy.stats.binned_statistic` function. In this case you should pass the `statistic=None` keyword to the `export` function, so the data does not get rebinned twice."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e9bcc38",
"metadata": {},
"outputs": [],
"source": [
"profile.create_profile(opening_angle=40, inner_radius=1e11, outer_radius=2e11)\n",
"profile.rebin(50)\n",
"profile.plot_profile()\n",
"plt.show()\n",
"profile.export(50, \"snapshot_converted_to_tardis.csvy\", statistic=None)"
]
},
{
"cell_type": "markdown",
"id": "8a4a41a8",
"metadata": {},
"source": [
"## Using the parser as a command line tool"
]
},
{
"cell_type": "markdown",
"id": "97bd4fa6",
"metadata": {},
"source": [
"You can also use the `tardis.io.arepo` package as a command line utility, e.g. if you are running batch jobs and want to automatically convert your snapshots from within you job-script.\n",
"\n",
"To export the same profile as in the example above you can run\n",
"```bash\n",
"python ./<location_of_arepo_parser>/arepo.py snapshot.hdf5 snapshot_converted.csvy -o 40 --inner_radius 1e11 --outer_radius 2e11 -e ni56 si28 --save_plot plot.png --resolution 32 --plot_rebinned plot_binned.png\n",
"```\n",
"\n",
"This will also save diagnostic plots of both the raw and rebinned profiles. For more information on how to use the command line tool run\n",
"```bash\n",
"python ./<location_of_arepo_parser>/arepo.py --help\n",
"```\n",
"\n",
"> **_NOTE:_** The command line tool does only work with snapshot files, not with e.g. json files. It is in any case not advised to "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -212,7 +307,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
"version": "3.7.12"
}
},
"nbformat": 4,
Expand Down
15 changes: 15 additions & 0 deletions docs/tardis.bib
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,18 @@ @Article{Barna2017
doi = {10.1093/mnras/stx1894},
eprint = {1707.07848},
}

@article{Weinberger2020,
title = {The AREPO Public Code Release},
volume = {248},
ISSN = {1538-4365},
url = {http://dx.doi.org/10.3847/1538-4365/ab908c},
DOI = {10.3847/1538-4365/ab908c},
number = {2},
journal = {The Astrophysical Journal Supplement Series},
publisher = {American Astronomical Society},
author = {Weinberger, Rainer and Springel, Volker and Pakmor, Rüdiger},
year = {2020},
month = {Jun},
pages = {32}
}
Loading

0 comments on commit 4e3290d

Please sign in to comment.