{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Evaluate at\n", "\n", "Some examples of the evaluate_at functionality" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import numpy as np\n", "import xemc3\n", "import matplotlib.pyplot as plt\n", "\n", "# Matplotlib setup\n", "import setup_plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Use local helper function to get some data\n", "from get_data import load_example_data\n", "\n", "ds = load_example_data()\n", "# If you want to use your own data use something like\n", "# ds = xemc3.load.all(\"path/to/mydata/\")\n", "# or if you have converted it already to a netcdf file\n", "# ds = xr.open_dataset(\"path/to/mydata.nc\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Evaluate along a line of sight" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nx = 1000\n", "mapped = ds.emc3.evaluate_at_xyz(\n", " np.linspace(4.8, 6.0, nx),\n", " np.linspace(-0.1, 0.1, nx),\n", " np.linspace(-0.5, 0.5, nx),\n", " \"ne\",\n", " periodicity=5,\n", " updownsym=True,\n", " delta_phi=np.pi / 1800,\n", ")\n", "# Add a coordinate\n", "mapped.coords[\"dim_0\"] = np.linspace(4.8, 6.0, nx)\n", "mapped.dim_0.attrs = dict(units=\"m\", long_name=\"x\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure()\n", "mapped[\"ne\"].plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Evaluate along a 2D array\n", "\n", "Plotting an $R\\times z$ plane can be done with `ds.emc3.plot_rz(key, phi)` this allows to plot abitrary cuts of the domain." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Evaluate an abitrary 2D slice\n", "x = xr.DataArray(np.linspace(0, 7, 100), name=\"x\", dims=\"x\", attrs=dict(units=\"m\"))\n", "y = xr.DataArray(np.linspace(0, 7, 100), name=\"y\", dims=\"y\", attrs=dict(units=\"m\"))\n", "z = 0\n", "# Add coordinates:\n", "x.coords[\"x\"] = x\n", "y.coords[\"y\"] = y\n", "\n", "\n", "mapped = ds.emc3.evaluate_at_xyz(\n", " x, y, z, [\"ne\", \"Te\"], periodicity=5, updownsym=True, delta_phi=np.pi / 180\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plot the pressure\n", "plt.figure()\n", "(mapped.ne * mapped.Te).plot(x=\"x\", y=\"y\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Precalculate the mapping\n", "\n", "Rather then directly evaluating a quantity, we can also calculate the mapping first. This is usefull if the same mapping is needed for different simulations, as long as they use the identical grid." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Evaluate an abitrary 2D slice\n", "x = xr.DataArray(np.linspace(0, 7, 100), name=\"x\", dims=\"x\", attrs=dict(units=\"m\"))\n", "y = xr.DataArray(np.linspace(0, 7, 100), name=\"y\", dims=\"y\", attrs=dict(units=\"m\"))\n", "z = 0\n", "# Add coordinates:\n", "x.coords[\"x\"] = x\n", "y.coords[\"y\"] = y\n", "\n", "# We don't pass in the key we want to evaluate\n", "# In this case we get a dataset with indices\n", "mapped = ds.emc3.evaluate_at_xyz(\n", " x, y, z, key=None, periodicity=5, updownsym=True, delta_phi=np.pi / 180\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "# This time we calculate the pressure\n", "ds[\"Pe\"] = ds.Te * ds.ne\n", "# And then we evaluate with the pre-evaluated indices the pressure in the plane\n", "plt.figure()\n", "ds[\"Pe\"].isel(**mapped).plot(x=\"x\", y=\"y\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Evaluate along a field line\n", "\n", "xemc3 doesn't support field-line tracing\n", "\n", "We can however use the webservices, if we are on the IPP network" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "