Skip to content

Commit

Permalink
Merge pull request #62 from osyris-project/repair_loaders
Browse files Browse the repository at this point in the history
Better select behaviour in loaders
  • Loading branch information
nvaytet authored Feb 7, 2022
2 parents f05ae76 + 02649ee commit 1551d79
Show file tree
Hide file tree
Showing 18 changed files with 307 additions and 219 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data = osyris.Dataset(8, scale="au", path="data").load()
osyris.histogram2d(data["hydro"]["density"], data["hydro"]["B_field"],
norm="log", loglog=True)
```
![hist2d](https://osyris.readthedocs.io/en/stable/_images/plotting_3_1.png)
![hist2d](https://osyris.readthedocs.io/en/stable/_images/plotting_histograms_13_1.png)

Create a 2D gas density slice 2000 au wide through the plane normal to ``z``,
with velocity vectors overlayed as arrows, once again using ``layers``:
Expand All @@ -45,7 +45,7 @@ osyris.plane({"data": data["hydro"]["density"], "norm": "log"}, # layer 1
origin=center,
direction="z")
```
![map2d](https://osyris.readthedocs.io/en/stable/_images/plotting_7_1.png)
![map2d](https://osyris.readthedocs.io/en/stable/_images/plotting_maps_23_1.png)

## Have a problem or need a new feature?

Expand Down
22 changes: 11 additions & 11 deletions docs/basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simple cut plane\n",
"## Simple 2D map\n",
"\n",
"The goal here is to create a 2D gas density slice, 2000 au wide, through the plane normal to `z`,\n",
"that passes through the center of the young star forming system.\n",
Expand All @@ -172,7 +172,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Once we have this center coordinate, we can use it as the origin for our cut plane:"
"Once we have this center coordinate, we can use it as the origin for our cut plane and create a map:"
]
},
{
Expand All @@ -181,24 +181,24 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.plane({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
" dx=2000 * osyris.units('au'),\n",
" origin=center,\n",
" direction=\"z\")"
"osyris.map({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
" dx=2000 * osyris.units('au'),\n",
" origin=center,\n",
" direction=\"z\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where the first argument is the variable to display, `dx` is the extent of the vieweing region,\n",
"and `direction` is the normal to the slice plane."
"where the first argument is the variable to display, `dx` is the extent of the viewing region,\n",
"and `direction` is the normal to the slicing plane."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -212,9 +212,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
38 changes: 36 additions & 2 deletions docs/installation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,40 @@
"$ pip install osyris"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you have already installed `osyris` in the past, but would like to upgrade, use"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"$ pip install osyris --upgrade"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-warning\">\n",
"\n",
"**Warning**\n",
"\n",
"If you are upgrading from an old version to a version >= 2.6.0,\n",
"and you are getting some strange errors,\n",
"you may need to update your `config_osyris.py` configuration file in `/home/user/.osyris`.\n",
"\n",
"- If you had never touched the configuration file, it is safe to simply delete it (a new one will be created when importing `osyris`).\n",
"- If you had made changes to the configuration, the easiest is probably to move it to a new location/filename. Then import `osyris` and update the newly created file to incorporate the changes you had made previously.\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -73,7 +107,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -87,7 +121,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
"version": "3.9.5"
},
"nbsphinx": {
"execute": "never"
Expand Down
142 changes: 121 additions & 21 deletions docs/loading_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,21 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.plane({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
" dx=1000 * osyris.units('au'),\n",
" origin=data[\"amr\"][\"xyz\"][np.argmax(data[\"hydro\"][\"density\"]).values],\n",
" direction='z')"
"osyris.map({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
" dx=1000 * osyris.units('au'),\n",
" origin=data[\"amr\"][\"xyz\"][np.argmax(data[\"hydro\"][\"density\"]).values],\n",
" direction='z')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Only loading certain variables\n",
"### Only loading certain groups or variables\n",
"\n",
"It is also possible to skip a variable entirely by setting the key to `False` in the `select` dict:"
"It is also possible to select which groups to load.\n",
"The different groups are `'amr'`, `'hydro'`, `'grav'`, `'rt'`, and `'part'`.\n",
"For example, to load only the `amr` and `hydro` groups, use"
]
},
{
Expand All @@ -138,18 +140,21 @@
"metadata": {},
"outputs": [],
"source": [
"data = osyris.Dataset(8, scale=\"au\", path=path).load(\n",
" select={\"hydro\": {\"density\": False}})\n",
"\"density\" in data[\"hydro\"]"
"data = osyris.Dataset(8, scale=\"au\", path=path).load(['amr', 'hydro'])\n",
"data.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Entire groups of variables can also be skipped.\n",
"The different groups are `'amr'`, `'hydro'`, `'grav'` (or `'gravity'`), `'rt'`, and `'part'`.\n",
"Loading all but the hydro variables can be achieved via"
"It is also possible to load or skip only a single variable in a group,\n",
"by setting the key to `True` or `False` in the `select` dict.\n",
"\n",
"The convention is:\n",
"\n",
"- If at least one key in a group is set to `True`, all unspecified variables in the group are skipped\n",
"- If a key is set to `False` in a group, all other variables are loaded"
]
},
{
Expand All @@ -159,8 +164,19 @@
"outputs": [],
"source": [
"data = osyris.Dataset(8, scale=\"au\", path=path).load(\n",
" select={\"hydro\": False})\n",
"data"
" select={\"hydro\": {\"density\": False}})\n",
"\"density\" in data[\"hydro\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = osyris.Dataset(8, scale=\"au\", path=path).load(\n",
" select={\"hydro\": {\"temperature\": True, \"thermal_pressure\": True}})\n",
"data[\"hydro\"].keys()"
]
},
{
Expand Down Expand Up @@ -234,10 +250,10 @@
"metadata": {},
"outputs": [],
"source": [
"osyris.plane({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
" dx=2000 * osyris.units(\"au\"),\n",
" origin=data[\"amr\"][\"xyz\"][np.argmax(data[\"hydro\"][\"density\"]).values],\n",
" direction='z')"
"osyris.map({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
" dx=2000 * osyris.units(\"au\"),\n",
" origin=data[\"amr\"][\"xyz\"][np.argmax(data[\"hydro\"][\"density\"]).values],\n",
" direction='z')"
]
},
{
Expand All @@ -254,11 +270,95 @@
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example: loading a region around a sink particle\n",
"\n",
"Combining some of the methods illustrated above,\n",
"we show here how to load only a small region 400 AU wide around a sink particle.\n",
"\n",
"We begin by loading only the sink particle data, by using `select='sink'` in the `load` method:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = osyris.Dataset(8, scale=\"au\", path=path).load(select='sink')\n",
"data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `sink` data group contains the positions of the sink particles.\n",
"We wish to load the region around the first sink particle, which will be the center of our domain."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"center = data['sink']['xyz'][0:1]\n",
"dx = 200 * osyris.units('au')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are now in a position to load the rest of the data (`amr`, `hydro`, etc.)\n",
"and perform a spatial selection based on the sink's position\n",
"(note how the loading below is only looking through 6 files, as opposed to 12 at the top of the notebook,\n",
"because it uses the knowledge from the Hilbert space-filling curve to skip CPUs that are not connected\n",
"to the domain of interest)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data.load(\n",
" select={\"amr\": {\"x\": lambda x : np.logical_and(x > center.x - dx, x < center.x + dx),\n",
" \"y\": lambda y : np.logical_and(y > center.y - dx, y < center.y + dx),\n",
" \"z\": lambda z : np.logical_and(z > center.z - dx, z < center.z + dx)}\n",
" })"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, we can make a map of the gas density around our sink particle\n",
"(note that no spatial limits are specified in the `map` call,\n",
"it is making a map using all the cells that have been loaded)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"osyris.map(data[\"hydro\"][\"density\"],\n",
" {\"data\": data[\"sink\"][\"xyz\"], \"mode\": \"scatter\", \"c\": \"white\"},\n",
" norm='log', direction=\"z\", origin=center[0])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -272,9 +372,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = osyris
version = 2.5.1
version = 2.6.0
author = Neil Vaytet
author_email = [email protected]
description = A package to visualize AMR data from the RAMSES code
Expand Down
21 changes: 16 additions & 5 deletions src/osyris/config/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}


def get_unit(string, ud, ul, ut):
def get_unit(string, ud, ul, ut, scale):

density = ud * (ureg.g / (ureg.cm**3))
velocity = (ul / ut) * (ureg.cm / ureg.s)
Expand All @@ -29,6 +29,11 @@ def get_unit(string, ud, ul, ut):
length = ul * ureg.cm
mass = density * (length**3)

scaling = length
if scale is not None:
scale = ureg(scale)
scaling = (length.to(scale) / scale).magnitude * scale

ramses_units = {
'density': density,
'velocity': velocity,
Expand Down Expand Up @@ -67,10 +72,16 @@ def get_unit(string, ud, ul, ut):
'radiative_energy_1': energy,
'temperature': 1.0 * ureg.K,
'time': time,
'x': length,
'y': length,
'z': length,
'dx': length,
'x': scaling,
'y': scaling,
'z': scaling,
'xyz_x': scaling,
'xyz_y': scaling,
'xyz_z': scaling,
'position_x': scaling,
'position_y': scaling,
'position_z': scaling,
'dx': scaling,
'mass': mass
}

Expand Down
Loading

0 comments on commit 1551d79

Please sign in to comment.