<jupyter_start><jupyter_text>Loading Arbitrary Curvilinear Data into ytIn this tutorial, we will use yt to perform some simple visualization tasks with curvilinear data. Make sure you have yt installed for this tutorial. If you don't, you can install it by following the instructions in the `installing-yt` notebook.First, let's load the relevant libraries...<jupyter_code>import sys
sys.path.append('.local/lib/python3.5/site-packages')
%matplotlib inline
import h5py
import numpy as np
import yt<jupyter_output><empty_output><jupyter_text>We will use the same accretion disk GRMHD simulation as we did in a previous tutorial. Let's load it in just as we did before.<jupyter_code>grid = {}
with h5py.File('harmdisk2d/grid.h5','r') as f:
    for k,v in f.items():
        grid[k] = v.value
data = {}
with h5py.File('harmdisk2d/data.h5','r') as f:
    for k,v in f.items():
        data[k] = v.value<jupyter_output><empty_output><jupyter_text>If your data is evenly spaced, you can load it via the `load_uniform_grid` method:<jupyter_code>help(yt.load_uniform_grid)<jupyter_output><empty_output><jupyter_text>Unfortunately, our data is logarithmic in radius, so instead we will use the `load_hexahedral_mesh` method, which loads sime-structured grid data. The expectation for this method is that the data is of Cartesian product topology.<jupyter_code>help(yt.load_hexahedral_mesh)<jupyter_output><empty_output><jupyter_text>We'll use the helper function `hexahedral_connectivity` to tell `yt` how the grid points are connected to each other:<jupyter_code>help(yt.hexahedral_connectivity)<jupyter_output><empty_output><jupyter_text>First, we extract the 1d list of cell centers<jupyter_code>r = grid['Xbl'][:,0,0,1]*data['L_unit'][0]
theta = grid['Xbl'][0,:,0,2]
phi = grid['Xbl'][0,0,:,3]<jupyter_output><empty_output><jupyter_text>The hexahedral mesh code actually wants cell faces, though. So we need to extract these. We can do so by finding `dx`.<jupyter_code># r
dr = r[1]-r[0],r[-1]-r[-2]
rf = np.empty(len(r)+1)
rf[:-1] = r - dr[0]/2.
rf[-1] = rf[-2] + dr[1]
# theta
dth = theta[1]-theta[0],theta[-1]-theta[-2]
thf = np.empty(len(theta)+1)
thf[:-1] = theta - dth[0]/2.
thf[-1] = thf[-2] + dth[1]
# phi is special. It's only one zone and we know what the faces are
phif = np.array([0,2*np.pi])<jupyter_output><empty_output><jupyter_text>Now we can generate the `coords` and `conn` data structures which tell us how grid vertices are connected to each other<jupyter_code>coords,conn = yt.hexahedral_connectivity(rf,thf,phif)

rf[-2]<jupyter_output><empty_output><jupyter_text>And now we can load our data into yt<jupyter_code>arr_data = {'pressure':np.abs(data['PRESS'])*data['U_unit'] + 1e12, # to prevent underflow
            'entropy':data['ENT']}
bbox = np.array([[rf.min(),rf.max()],
                 [thf.min(),thf.max()],
                 [phif.min(),phif.max()]])
periodicity=[False,False,True]

ds = yt.load_hexahedral_mesh(arr_data,conn,coords,
                             #length_unit=data['L_unit'][0],
                             bbox=bbox,
                             periodicity=periodicity,
                             geometry='spherical')<jupyter_output><empty_output><jupyter_text>By jumping through these few small hoops, we've enabled the ability to do some cool analysis tricks. For example, we can plot our data in spherical coordinates, which you may recall was an earlier exercise.<jupyter_code>slc = yt.SlicePlot(ds,'phi','pressure')
slc.save('../figures/harmdisk-xz-slice.pdf')
slc.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13052039381153305, 'fraction_numerical': 0.013783403656821378, 'mean_word_length': 4.653418124006359, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '416804', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1228, 'n_words': 326, 'path': 'exercises/curvilinear-data-in-yt.ipynb', 'repo_name': 'naidoom/GR3', 'size': '3555'}